• ZOJ4033 CONTINUE...?


    CONTINUE...?

    Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge

    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
    

    Author: LIN, Xi

    Source: The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple

    题意:给n个人,0表示女生,1表示男生。女生分两组,男生分两组。使其中女生中的一组与男生中的一组中权值的和等于总权值的一半。

    思路:易发现第i个权值与第n-i个权值的和为n,所以依次找出规律:

    把选中的男生和女生 分组,选中的标记为0,没选的标记为1;

    以样例四为例:

    权值: 1 2 3 4 5 6 7

    原始: 1 1 0 1 0 0 1

    分组 :  3 4 1 3 2 1 4

    标记: 0 1 0 1 0 1 0


    当 0<=i<n/2 时,将标记为0的男生分到3组,女生分到1组;

    当  i >= n/2  时,将标记为1的男生分到3组,女生分到一组;

    分别对1、3组和2、4组的权值求和,若不相等输出-1,否则输出位置。


    附蒟蒻AC代码:

    #include <cstdio>
    #include <algorithm>
    #include <functional>
    #include <cstring>
    #include <iostream>
    #include <cmath>
    using namespace std;
    const int maxn=1e6+10,inf=(1<<30),mod=1e9+7;
    char que[maxn];
    int place[maxn];
    int main(){
        int t;
        scanf("%d",&t);
        while(t--){
            int n;
            scanf("%d",&n);
            scanf("%s",que);
            int sum1=0,sum2=0;
            for (int i=0;i<n/2;i++){
                if(i%2==0) place[i]=0,sum1+=i+1;
                else place[i]=1,sum2+=i+1;
            }
            for (int i=n/2;i<n;i++){
                if(i%2==0) place[i]=1,sum2+=i+1;
                else place[i]=0,sum1+=i+1;
            }
            if(sum1!=sum2){
                printf("-1
    ");
                continue;
            }
            for (int i=0;i<n;i++){
                if(que[i]=='1'){
                    if(place[i]) printf("4");
                    else printf("3");
                }
                else {
                    if(place[i]) printf("2");
                    else printf("1");
                }
            }
            printf("
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    新浪微博爬虫项目
    time
    黑客增长
    python2 3 区别
    爬虫高性能相关
    登录_爬取并筛选拉钩网职位信息_自动提交简历
    破解极验验证码
    tesseract-ocr 传统验证码识别
    刻意练习
    计算学员的考试总成绩以及平均成绩
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490339.html
Copyright © 2020-2023  润新知