zoukankan      html  css  js  c++  java
  • [模拟]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
    
    题意:编号为<1..n>的n个同学的gems数量分别为<1..n>,现要将这n个同学中的女生分配到一、二大组中去,男生分配到三、四中去(允许有的组为空),使得一、三组中学生的gems总数等于二、四组中学生的gems总数(答案不唯一)。

    思路:按规则模拟即可。首先所有学生的gems总数为sum=n*(n+1)/2;因为一三组的gems数量=二四组的gems数量=sum/2,所以若sum%2!=0,输出-1。
    若sum%2==0(即n==4k或n==4k+1(k=1,2...)),则一定可以从gems[1..n]:<1..n>中找到一些数,使它们的和等于sum的一半(从后面的分析可以看出),找到这些数后,即得到了对应学生的编号(宝石数为gems[i]的学生编号也为gems[i]),再将其中女生分配到一组,男生分配到三组;此外剩余学生中女生分配到二组,男生分配到四组。不难发现,当n==4k时,Σ(i+(n+1-i))(1<=i<=n/4)为sum的一半;
    当n==(4k+1)时,Σ(i+(n-i))(1<=i<=(n-1)/4+1)为sum的一半。

    AC代码:
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    char sex[100010];
    int gem[100010];
    int group[100010];
    int vis[100010];
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--){
            memset(vis,0,sizeof(vis));
            int n;
            scanf("%d",&n);
            getchar();
            int sum=0;
            for(int i=1;i<=n;i++){
                scanf("%c",&sex[i]);
                gem[i]=i;
                sum+=gem[i];
            }
            if(sum%2!=0) {
                printf("-1
    ");
            }
            else{
                if(n%4==0){
                    for(int i=1;i<=n/4;i++) {vis[i]=1; vis[n+1-i]=1;}
                }
                else{
                    for(int i=1;i<=(n-1)/4+1;i++) {vis[i]=1; vis[n-i]=1;}
                }
                for(int i=1;i<=n;i++){
                    if(vis[i]){
                        if(sex[i]=='1') group[i]=3;
                        else group[i]=1;
                    }
                    else{
                        if(sex[i]=='1') group[i]=4;
                        else group[i]=2;
                    }
                }
                for(int i=1;i<=n;i++){
                    printf("%d",group[i]);
                }
                printf("
    ");
            }
        }
        return 0;
    }

    一开始的模拟思路是,在所有女生中选一些分给一组,在所有男生中选一些分给三组,使得一三组gems总数为sum的一半,这样很难操作。其实一开始只要在所有学生中选一些属于一三组的,使得他们的gems总数为sum的一半,再将这些学生按性别分别分给一、三两个组就好了。模拟的方式很重要。

    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    Android fill_parent和wrap_content分析
    美亚退保
    房子
    回家看
    Interface小例子
    做网页 推荐
    转发;Dota英文名
    【转】meta标签中的http-equiv属性使用介绍
    【转】php 操作数组(合并,拆分,追加,查找,删除等)
    【转】服务器.htaccess 详解以及 .htaccess 参数说明
  • 原文地址:https://www.cnblogs.com/lllxq/p/8973061.html
Copyright © 2011-2022 走看看