ACM:https://blog.csdn.net/weixin_39778570/article/details/83187443
题目:https://vjudge.net/contest/294289#problem/J
DreamGrid has classmates numbered from to . Some of them are boys and the others are girls. Each classmate has some gems, and more specifically, the -th classmate has gems.
DreamGrid would like to divide the classmates into four groups G1 ,G2 , G3 and G4 such that:
Each classmate belongs to exactly one group.
Both G1 and G2 consist only of girls. Both G3 and G4 consist only of boys.
The total number of gems in G1 and G3 is equal to the total number of gems in G2 and G4.
Your task is to help DreamGrid group his classmates so that the above conditions are satisfied. Note that you are allowed to leave some groups empty.
输入T组数据
n,长度为n的01字符串,0女1男
有解输出任意一组,无解输出-1
#include<bits/stdc++.h>
#define ll long long
#define fo(i,j,n) for(register int i=j; i<=n; i++)
using namespace std;
int n,T,ans[100005];
char s[100005];
void solve(int t){
int x=t/2;
bool f=1;
fo(i,1,x){
if(f){ // 24
f=0;
ans[i]=(s[i]=='0')?2:4;
ans[t-i+1]=(s[t-i+1]=='0')?2:4;
}else{ // 13
f=1;
ans[i]=(s[i]=='0')?1:3;
ans[t-i+1]=(s[t-i+1]=='0')?1:3;
}
}
fo(i,1,n)printf("%d",ans[i]);
putchar(10);
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
getchar();
gets(s+1);
if(n%2==0){
if(n%4!=0)puts("-1"); // 有奇数对子不行
else solve(n);
}else{
if(((n+1)/2)%2!=0)puts("-1"); // +1后,有奇数对子不行
else{
ans[n] = (s[n]=='0')?1:3;
solve(n-1);
}
}
}
return 0;
}