A
# include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
const int N=60;
int a[N];
signed main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
int st=0;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<n;i++)
{
if(abs(a[i]-a[i+1])==5)
st=1;
else if(abs(a[i]-a[i+1])==7)
st=2;
else
{
st=3;
break;
}
}
if(st==3)
cout<<"NO";
else cout<<"YES";
cout<<endl;
}
}
B
# include <bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
map<int,int>ma;
signed main()
{
int T;
cin>>T;
while(T--)
{
ma.clear();
cin>>n>>k;
for(int i=1;i<=k;i++)
{
int a,b;
cin>>a>>b;
ma[a]+=b;
}
priority_queue<int> q;
for (auto [x,y] : ma)
{
q.push(y);
}
int ans = 0;
int cnt = 0;
while (!q.empty() && cnt < k && cnt < n)
{
ans += q.top();
q.pop();
cnt++;
}
cout<<ans<<endl;
}
}
C
先统计字符串中”1100”的个数,对于q次询问,改变的这一个字符只影响它前面3个字符和后面3个字符,每次判断即可
# include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
string s;
int q,sum,m;
void check1(string s)
{
for(int i=1;i<=m;i++)
{
if(i+3<=m)
{
if(s[i+1]=='1'&&s[i]=='1'&&s[i+2]=='0'&&s[i+3]=='0')
sum++;
}
}
}
bool check2(int x,int a)
{
//int cnt=0;
int start=max(x-3,(ll)1),end=min(x+3,m);
for(int i=start;i<=end;i++)
{
if(i+3<=end)
if(s[i+1]=='1'&&s[i]=='1'&&s[i+2]=='0'&&s[i+3]=='0')
sum--;
}
if(a==1)
s[x]='1';
else s[x]='0';
//cout<<s<<endl;
//cout<<start<<" "<<end<<endl;
for(int i=start;i<=end;i++)
{
if(i+3<=end)
if(s[i+1]=='1'&&s[i]=='1'&&s[i+2]=='0'&&s[i+3]=='0')
sum++;
}
//cout<<cnt<<endl;
if(sum>0)
return true;
return false ;
}
signed main()
{
int T;
cin>>T;
while(T--)
{
cin>>s>>q;
m=s.size();
s=" "+s;
//cout<<m<<endl;
sum=0;
check1(s);
//cout<<sum<<endl;
while(q--)
{
int a,b;
cin>>a>>b;
if(check2(a,b))
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
}
D
每次考虑边界将一个顺时针环所有点加入,每次都计算一遍累加
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n,m;
cin>>n>>m;
string s[n+10];
for(int i=1;i<=n;i++)
{
cin>>s[i];
s[i]=" "+s[i];
}
int t=1,x=n,y=m;
int ans=0;
while(t<=x&&t<=y)
{
vector<char>v;
for(int i=t;i<=y;i++)
{
v.push_back(s[t][i]);
}
for(int i=t+1;i<=x;i++)
{
v.push_back(s[i][y]);
}
for(int i=y-1;i>=t;i--)
{
v.push_back(s[x][i]);
}
for(int i=x-1;i>=t+1;i--)
{
v.push_back(s[i][t]);
}
int len=v.size();
for(int i=0;i<len;i++)
{
if(v[i]=='1'&&v[(i+1)%len]=='5'&&v[(i+2)%len]=='4'&&v[(i+3)%len]=='3'){
ans++;
}
}
t++,x--,y--;
}
cout<<ans<<"\n";
}
signed main()
{
int _=1;
cin>>_;
while(_--) {
solve();
}
return 0;
}