每一步都保证 a 和 b 的数量不会变成负数,如果做不到,就说明无法生成。100pts
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
string s;
void solve()
{
cin>>n>>s;s='#'+s;
int x,y,z=0;
if(n%2==0)y=1,x=1;
else y=0,x=2;
if(n==1 && s[1]!='b')
{
cout<<"Yes\n";
return ;
}
for(int i=1;i<=n;i++)
{
if(s[i]=='a')
{
if(z%2)
{
if(x==1)
{
x++,y--,z--;
}
}
x--,y++;
}
else if(s[i]=='b')
{
if(z%2)
{
if(y==1)
{
x--,y++,z--;
}
}
x++,y--;
}
else if(x==2)x--,y++;
else if(y==2)x++,y--;
else z++;
while(x<0 || y<0)
{
if(z>0)
{
if(z%2)
{
if(x<0)x++,y--;
else y++,x--;
z=0;
}
else
{
cout<<"No\n";
return ;
}
}
else
{
cout<<"No\n";
return ;
}
}
}
cout<<"Yes\n";
}
signed main()
{
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int T;
cin>>T;
while(T--)solve();
return 0;
}
F - Absolute Cinema
我们先算出一个关键的 “和数” sum,用 f (1) 加 f (n) 再除以 (n-1)。然后从左到右,用后一个 f 值减去前一个 f 值,加上 sum 再除以 2,再减去前面所有 a 的和,就能得到当前的 a。最后一个 a 用 sum 减去前面所有 a 的和就可以算出来了。
100pts
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+1;
long long f[maxn];
int a[maxn];
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int cnt = 0;
long long sum=0;
long long pre=0;
for(int i=1;i<=n;i++)cin>>f[i];
sum=(f[1]+f[n])/(n-1);
for(int i=1;i<n;i++){
long long tmp=f[i+1]-f[i];
tmp+=sum;
tmp/=2;
a[i]=tmp-pre;
pre+=a[i];
cout<<a[i]<<" ";
}
cout<<sum-pre<<endl;
}
}
G - Lost Civilization (Easy Version)
100pts
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e5+10;
int n,a[N];
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
int lst=a[1];
int res=1;
for(int i=2;i<=n;i++)
{
if(lst<a[i]&&(a[i]==a[i-1]+1||a[i]<=a[i-1]))continue;
lst=a[i];
res++;
}
cout<<res<<"\n";
}
signed main()
{
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int T;
cin>>T;
while(T--)solve();
return 0;
}