A
先考虑用填2然后用1填补,若可填补则成功
# include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int t;
cin>>t;
while(t--)
{
int a,b;
cin>>a>>b;
int x=0;
bool st=0;
if(b%2==0)
x=0;
else x=2;
if(x==2&&a-2>=0&&(a-2)%2==0)
st=1;
if(x==0&&a%2==0)
st=1;
if(st)
cout<<"YES";
else cout<<"NO";
cout<<endl;
}
}
B
题意模拟即可
# include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int t;
cin>>t;
//cout<<(5%3==0)||(5%3==1);
while(t--)
{
int n;
string s=" ",s1;
cin>>n>>s1;
s+=s1;
bool st=0;
int x=sqrt(n);
if(x*x!=n)
{
cout<<"NO"<<endl;
continue;
}
for(int i=1;i<=n;i++)
{
if((i>=1&&i<=x))
{
if(s[i]=='0')
st=1;
}
if((i>=n-x+1&&i<=n))
{
if(s[i]=='0')
st=1;
}
else if(i>x&&i<n-x+1)
{
if(i%x==0||i%x==1)
{
//cout<<i<<" ";
if(s[i]=='0')
{
st=1;
//cout<<i<<endl;
}
}
else
{
//cout<<i<<" ";
if(s[i]=='1')
st=1;
}
}
}
if(st)
cout<<"NO";
else cout<<"YES";
cout<<endl;
}
return 0;
}
C
暴力模拟即可
# include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int T;
cin>>T;
while(T--)
{
int l,r;
cin>>l>>r;
l++;
int x=2,a=0;
while(l<=r)
{
l+=x;
x++;
a++;
}
cout<<a+1;
cout<<endl;
}
return 0;
}
D
模拟样例会发现形成环 同一个环上的点可以相互到达 dfs环上的点
存在重复计算 所以递归到底层时不断返回记录答案即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=200010;
int a[N],ans[N];
int n;
string s;
bool vis[N];
int cnt;
int dfs(int p)
{
if(vis[p])
{
return cnt;
}
vis[p]=1;
if(s[p]=='0')
cnt++;
ans[p]=dfs(a[p]);
return cnt;
}
signed main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
s="";
cin>>s;
s=" "+s;
memset(ans,-1,sizeof(ans));
for(int i=1;i<=n;i++)
{
if(ans[i]==-1)
{
cnt=0;
memset(vis,0,sizeof(vis));
ans[i] = dfs(i);
}
}
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
cout<<endl;
}
}
E
分类讨论
对于偶数只执行操作二,奇数执行一次操作一。用前缀和的思想,记 odd[i][j] 为前 i个数,奇数位字符为 j的个数,even[i][j] 为偶数位字符为 j的个数,再枚举删去哪个点,最终序列的奇数位是这个点前面的奇数位和后面的偶数位,再分别枚举奇数位和偶数位的最终字符,这样就能得到正确答案。
偶数直接计算,奇数因为要删去一位所以对于奇数位考虑前面奇数后面的偶数,偶数位同理。
# include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 5;
int odd[N][26], even[N][26];
signed main()
{
int T;
cin>>T;
while(T--)
{
int n;
string s;
cin>>n>>s;
s=' '+s;
int ans=0x3f3f3f3f;
for(int i = 1; i <= n; ++i)
{
for(int j = 0; j <= 25; ++j) odd[i][j] = even[i][j] = 0;
if(i%2==1) odd[i][s[i] - 'a'] = 1;
else even[i][s[i] - 'a'] = 1;
for(int j = 0; j <= 25; ++j)
odd[i][j] += odd[i - 1][j], even[i][j] += even[i - 1][j];
}
if(n%2==1)
{
for(int i = 1; i <= n; ++i)
{
for(int j = 0; j <= 25; ++j)
for(int k = 0; k <= 25; ++k)
{
int x = odd[i - 1][j] + even[n][j] - even[i][j];
int y = even[i - 1][k] + odd[n][k] - odd[i][k];
ans = min(ans, n - 1 - x - y);
}
}
cout<<ans+1;
}
else
{
for(int i = 0; i <= 25; ++i)
for(int j = 0; j <= 25; ++j)
ans = min(ans, n - (odd[n][i] + even[n][j]));
cout<<ans;
}
cout<<endl;
}
}