CONTINUE...? (Thinking)

  • CONTINUE…?
  • Question meaning
    There are N students in the class, numbered 1, 2, 3...N, and the i-th student has i gems. Let us divide these N students into four groups: G1, G2, G3, G4, and meet the following rules
  1. Each student can only be assigned to one group
  2. Female students can only be assigned to G1 or G2 group, male students can only be assigned to G3 or G4 group (the gender of these N students will be given in the form of strings, '1' means male students, and '0' means female students )
  3. The number of gems in G1+G3 is equal to the number of gems in G2+G4
  4. Allow one empty group

Ask which group the N students are assigned to. If there are multiple answers, just output one at will.

  • Ideas
  1. Let's consider the impossible situation first, suppose:
    The number of gems in G1 is sum 1 sum_1sum1
    The number of gems in G2 is sum 2 sum_2sum2
    The number of gems in G3 is sum 3 sum_3sum3
    The number of gems in G4 is sum 4 sum_4sum4
    s u m 1 + s u m 3 = = s u m 2 + s u m 4 sum_1+sum_3==sum_2+sum_4 sum1+sum3==sum2+sum4
    s u m 1 + s u m 2 + s u m 3 + s u m 4 = = N ∗ ( N + 1 ) / 2 sum_1+sum_2+sum_3+sum_4==N*(N+1)/2 sum1+sum2+sum3+sum4==N(N+1)/2
    2 ∗ ( s u m 1 + s u m 3 ) = = N ∗ ( N + 1 ) / 2 2*(sum_1+sum_3)==N*(N+1)/2 2(sum1+sum3)==N(N+1 ) / 2
    So if N*(N+1)/2 is not an even number, directly output -1

  2. If possible, how to allocate it?
    First consider how to satisfy sum 1 + sum 3 == sum 2 + sum 4 sum_1+sum_3==sum_2+sum_4sum1+sum3==sum2+sum4
    Because the number of gems is consecutive 1~N, we can allocate it like this, for example, N=7, the number of gems is 1 2 3 4 5 6 7, grouping in pairs (6,7),(4,5),( 2,3),(1)

    Let 7 points to 1, 3 (which one depends on gender), 6 points to 2, 4 (which one depends on gender),
    4 points to 1, 3 (which one depends on gender), and 5 points to 2, 4 (which one depends on specific) Gender)
    Let 3 points to 1, 3 (which one depends on gender), 2 points to 2, 4 (which one depends on gender)

    This can ensure that the maximum difference between 1, 3 and 2, 4 is 1 (only when N%4 is not 0, the difference is 1, at this time, the previous 1 is useless, just to make up the difference)

  • Code
#pragma GCC optimize(2)
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
#define pi acos(-1.0)
#define e exp(1.0)
#define pb push_back
#define mk make_pair
#define fir first
#define sec second
#define scf scanf
#define prf printf
typedef pair<ll,ll> pa;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int MAX_N=1e5+7;
int N,T;
char s[MAX_N];
int res[MAX_N];//记录答案 
int main()
{
    
    
//  freopen(".../.txt","w",stdout);
//  freopen(".../.txt","r",stdin);
	ios::sync_with_stdio(false);
	cin>>T;
	int i,j;
	while(T--){
    
    
		cin>>N;
		cin>>s+1;
		int sum=N*(N+1)/2;
		if(sum&1){
    
    
			cout<<-1<<endl;
			continue;
		}
		bool flag=1;
		for(i=N;i>=1;i-=2){
    
    
			if(flag){
    
    //大的放到1或者3里面 
				res[i]=(s[i]=='1'?3:1);
				res[i-1]=(s[i-1]=='1'?4:2);
			}
			else{
    
    //大的放到2或者4里面 
				res[i-1]=(s[i-1]=='1'?3:1);
				res[i]=(s[i]=='1'?4:2);
			}
			flag=!flag;//1、3和2、4交替着放大的,这样才能保证1、3的和与2、4的和最大相差1 
		}
		if(N%4){
    
    //没有分均衡,将最后一个1分配给2、4(因为我们先给1、3分配的,如果不够的话也是2、4不够) 
			if(s[1]=='1')
			res[1]=4;
			else
			res[1]=2;
		}
		for(i=1;i<=N;i++){
    
    
			cout<<res[i];
		}
		cout<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43311695/article/details/108912582