zoukankan      html  css  js  c++  java
  • 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;
    }
    


  • 相关阅读:
    高手详解:sscanf函数的高级用法
    堆排序——BuildHeap和Heapify函数的实现
    递归与动态规划求解最长公共子序列
    分享:crpcut 1.8.4 发布,C++ 的单元测试框架
    团队展示 京拍档 电商运营服务、电子商务服务外包 首家京东代运营电子商务服务平台
    Linux中link,unlink,close,fclose详解
    常用排序算法的c++实现(冒泡,选择,插入,堆,shell,快速,归并 )与sort()对比 coder_xia的专栏 博客频道 CSDN.NET
    CAJ文件转PDF文件方法
    递归与动态规划求解最长公共子序列
    NLP Job 专注自然语言处理&机器学习等领域的求职招聘 | 关注自然语言处理|机器学习|数据挖掘|搜索引擎|计算广告|推荐算法等相关领域的工作机会
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490339.html
Copyright © 2011-2022 走看看