CodeForces - 1382B
博弈论,先遇到大于1的可以决定自己多走还是少走
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100010
int a[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int cnt=0,f=0;
for(int i=1;i<=n;i++)
{
if(a[i]==1)cnt++;
else
{
if(cnt%2)printf("Second\n");
else printf("First\n");
f=1;
break;
}
}
if(f)continue;
if(cnt%2==0)printf("Second\n");
else printf("First\n");
}
return 0;
}
从后往前暴力,如果跟当前不一样,就判断第一个,然后再翻转就好了
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define N 1010
int a[N],b[N],n;
char stra[N],strb[N];
int ans[3*N];
void init()
{
for(int i=0;i<n;i++)
{
a[i+1]=stra[i]-'0';
}
for(int i=0;i<n;i++)
{
b[i+1]=strb[i]-'0';
}
}
void filp(int x)
{
int n1=x/2;
for(int i=1;i<=n1;i++)
{
swap(a[i],a[x-i+1]);
}
for(int i=1;i<=x;i++)
{
a[i]^=1;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%s",stra);
scanf("%s",strb);
init();
int flag=1,cnt=0;
for(int i=n;i>1;i--)
{
if(a[i]!=b[i])
{
flag=0;
if(a[1]==b[i])
{
a[1]^=1;
ans[++cnt]=1;//printf("1 ");
}
ans[++cnt]=i;//printf("%d ",i);
filp(i);
}
}
if(a[1]!=b[1])
{
flag=0;
ans[++cnt]=1;//printf("1 ");
}
if(flag)printf("0");
else
{
printf("%d ",cnt);
for(int i=1;i<=cnt;i++)printf("%d ",ans[i]);
}
cout<<endl;
}
return 0;
}
把a串先变为0,只要把n+1位置赋值为0就好;
b串也变为0,然后用a串执行b串的逆执行就好了
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
#define N 101000
char s1[N],s2[N];
int ans1[N],ans2[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int cnt1=0,cnt2=0;
scanf("%d",&n);
scanf("%s%s",s1+1,s2+1);
s1[n+1]='0',s2[n+1]='0';
for(int i=1;i<=n;i++)
{
if(s1[i]!=s1[i+1]) ans1[++cnt1]=i;
}
for(int i=1;i<=n;i++)
{
if(s2[i]!=s2[i+1]) ans2[++cnt2]=i;
}
cout<<cnt1+cnt2<<" ";
for(int i=1;i<=cnt1;i++)
{
printf("%d ",ans1[i]);
}
for(int i=cnt2;i>=1;i--)
{
printf("%d ",ans2[i]);
}
cout<<endl;
}
return 0;
}
把数组分成n个递减的串,如果最大的串大于n自然就不行;
后面的用完全背包做就好了,判断容量为n的背包能不能装满。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int inf = 0x3f3f3f3f;
#define LL long long
#define N 10010
int a[N];
int dp[N];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
int m = n * 2;
int x, cnt, temp;
int num = 0;
for (int i = 1; i <= m; i++)
{
scanf("%d", &x);
if (i == 1)cnt = 1, temp = x;
else
{
if (x < temp)cnt++;
else
{
a[++num] = cnt;
temp = x;
cnt = 1;
}
}
}
a[++num] = cnt;
int maxn = 0;
for (int i = 1; i <= num; i++)
{
maxn = max(a[i], maxn);
}
if (maxn > n)
{
printf("NO\n");
continue;
}
memset(dp, inf, sizeof(dp));
dp[0] = 0;
for (int i = 1; i <= num; i++)
{
for (int j = n; j >= a[i]; j--)
{
dp[j] = min(dp[j], dp[j - a[i]]);
}
}
if (dp[n] == 0)printf("YES\n");
else printf("NO\n");
}
return 0;
}