J - CONTINUE...?(思维模拟)

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值