日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【ZOJ - 4033】CONTINUE...? (思维,整体思想,分组思想)

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ZOJ - 4033】CONTINUE...? (思维,整体思想,分组思想) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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?,?,??and?such that:

  • Each classmate belongs to exactly one group.

  • Both??and??consist only of girls. Both??and??consist only of boys.

  • The total number of gems in??and??is equal to the total number of gems in??and?.

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.

Input

There are multiple test cases. The first line of input is an integer??indicating the number of test cases. For each test case:

The first line contains an integer??() -- the number of classmates.

The second line contains a string??() consisting of 0 and 1. Let??be the?-th character in the string?. If?, the?-th classmate is a boy; If?, the?-th classmate is a girl.

It is guaranteed that the sum of all??does not exceed?.

Output

For each test case, output a string consists only of {1, 2, 3, 4}. The?-th character in the string denotes the group which the?-th classmate belongs to. If there are multiple valid answers, you can print any of them; If there is no valid answer, output "-1" (without quotes) instead.

Sample Input

5 1 1 2 10 3 101 4 0000 7 1101001

Sample Output

-1 -1 314 1221 3413214

題目大意:給n個人,0表示女生,1表示男生,第i個人的權值是i。女生分兩組G1,G2,男生分兩組G3,,G4。現在要構造這n個人的分組,使得G1+G3=G2+G4。

解題報告:

換句話說,使其中女生中的一組與男生中的一組中權值的和等于總權值的一半。

不難發現,其實與男女生無關,我們只要將權值分成一半和一半,然后對于前一半這個集合,男生就塞進G3,女生就塞進G1就行了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; char s[MAX]; int bk[MAX]; int main() {int t;cin>>t;while(t--) {ll n;scanf("%lld",&n);ll ans = (n+1)*n/2;scanf("%s",s+1);if(ans%2 == 1) {printf("-1\n");continue;}ll res = ans/2;//bk=1 代表放入 女1 男3 for(ll i = n; i>=1; i--) {if(res-i >= 0) {bk[i] = 1;res -= i;}else bk[i] = 0;}for(int i = 1; i<=n; i++) {if(s[i] == '1') {//男 if(bk[i] == 1) printf("3");else printf("4");}else {if(bk[i] == 1) printf("1");else printf("2");}}printf("\n"); }return 0 ; }

?

總結

以上是生活随笔為你收集整理的【ZOJ - 4033】CONTINUE...? (思维,整体思想,分组思想)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。