T738342 完美旋律
题意
有一个数列,要求保证数列每个相邻的元素之间只能相差或
思路
只要输入完数组后,挨个判断相邻的元素之间是否相差或就行了
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 55;
int a[N];
int t;
void solve(){
int n;
cin >> n;
bool b = true;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
for(int i = 2;i <= n;i++){
if(abs(a[i] - a[i - 1]) != 5 and abs(a[i] - a[i - 1]) != 7){
b = false;
}
}
if(b == true){
cout << "YES"<< '\n';
}else{
cout << "NO"<< '\n';
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}T738345 自动售货机
题意
有一台层售货机,每一层只能放一种类型饮料,有瓶饮料,类型为,售价为,所有放到售货机上的饮料都会被卖出,请问可以做到的最大收入是多少?
思路
累加每一种饮料的总价格,最后把总价值最高的瓶饮料放上去就好了
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
struct P{
int b, c;
}a[N];
int t;
int tt[N];
bool cmp(int a1, int a2){
return a1 > a2;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
int n, k;
cin >> n>> k;
for(int i = 1;i <= k;i++){
cin >> a[i].b>> a[i].c;
tt[a[i].b] += a[i].c;
}
sort(tt + 1, tt + 1 + k, cmp);
int sum = 0;
for(int i = 1;i <= n;i++){
sum += tt[i];
}
cout << sum<< '\n';
memset(tt, 0, sizeof tt);
}
return 0;
}T738351 1100子串
题意
有一个只包含和的字符串,可以把它进行次累加的更改,把位置改成,判断它是否包含一个连续的字串
思路
查找子串可以使用,如果这个字符串长度小于,就可以直接输出
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t;
void solve(){
string s;
cin >> s;
int n = s.size();
s = '#' + s;
int q;
cin >> q;
while(q--){
int i;
char v;
cin >> i>> v;
s[i] = v;
if(n < 4){
cout << "NO"<< '\n';
}else{
if(s.find("1100") != string::npos){
cout << "YES"<< '\n';
}else{
cout << "NO"<< '\n';
}
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}T738401 我爱1543
题意
有一个数字矩阵,判断按从外到内的顺序,顺时针遍历地毯的每层时连续的
思路
上⾯横着的边是
右⾯横着的竖是
下⾯横着的边是
左⾯横着的竖是
每层包含这些。可以把他们串成一个字符串来寻找有没有,注意最前面的三个字符要复制到最后,因为可能最后面一个字符和最前面三个字符刚好构成一个
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
char a[N][N];
int t;
void solve(){
int n, m;
cin >> n>> m;
int cnt = n * m;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
cin >> a[i][j];
}
}
int ans = 0;
for(int k = 1;cnt;k++){
int num = 0;
string s = "";
for(int i = k;i <= m - k + 1;i++){
s += a[k][i];
num++;
}
for(int i = k + 1;i <= n - k;i++){
s += a[i][m - k + 1];
num++;
}
for(int i = m - k + 1;i >= k;i--){
s += a[n - k + 1][i];
num++;
}
for(int i = n - k;i >= k + 1;i--){
s += a[i][k];
num++;
}
s += s.substr(0, 3);
int pos = s.find("1543");
while(pos != -1){
ans++;
pos = s.find("1543", pos + 3);
}
cnt -= num;
}
cout << ans<< '\n';
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> t;
while(t--){
solve();
}
return 0;
}T738432 河流改道
题意
有个国家,每个国家个地区,第个国家的第个地区的水量为,要在国区和国区,修建河道,因为国际们在斜坡上,水流会像编号大的国家流,建好后国区的新水量为,表示按位或
有个查询,判断所选国家地区的水是否大于或小于,请问每次改选哪个国家,输出编号最小的
思路
因为按位或运算,会让水量只升不降,我们可以通过这个点使用⼆分来找到能满⾜每个约束条件的位置(因为这样会使数组是单调递增的),如果是号,可以限制答案区间的起点位置;如果是号,可以限制答案区间的终点位置
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, k, q;
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> k>> q;
vector<vector <int> > a(k, vector <int> (n));//防止越界
for(int i = 0;i < n;i++){
for(int j = 0;j < k;j++){
cin >> a[j][i];//要以区域来计数
if(i >= 1){
a[j][i] |= a[j][i - 1];
}
}
}
while(q--){
int st = 0, en = 2e9 + 100, ci;
cin >> ci;
while(ci--){
int r, c;
char op;
cin >> r>> op>> c;
r--;
if(op == '>'){//锁定起点位置
int x = upper_bound(a[r].begin(), a[r].end(), c) - a[r].begin();
st = max(st, x);
}else{//锁定起点终点
int x = lower_bound(a[r].begin(), a[r].end(), c) - a[r].begin();
en = min(en, x - 1);
}
}
if(st <= en and st < n){
cout << st + 1<< '\n';
}else{
cout << -1<< '\n';
}
}
return 0;
}