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

    【题意】:https://www.cnblogs.com/bluefly-hrbust/p/8971769.html

    本题题意就是把1到n数表示为,两组数之和相等,即能不能1->n划分成两部分(男女分组是干扰的)

    1 2 3 4 5 6 7 8
    偶数-->1+8+2+7 = 3+6+4+5

    1 2 3 4 5 6 7
    奇数-->1+3+4+6 = 2+5+7

    偶数: n只需要前后匹配即可

    奇数: n/2之前的奇数位和n/2之后的偶数位相加,等于n/2之前的偶数位加n/2的奇数位

    【分析】:ACZone+

    【出处】:CodeForces - 899C Dividing the numbers

    【代码】:

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    int main()
    {
        int t,n,len;
        char a[100050];
        int  b[100050];
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            getchar();
            scanf("%s",&a);
            len=strlen(a);
            if ((n%4)<=2 && n%4!=0)
            {
                printf("-1
    ");
            }
            else
            {
                if(n%2==0) //偶数
                {
                    for(int i=0; i<n/4; i++)
                    {
                        if (a[i]=='1')printf("3");
                        else printf("1");
                    }
                    for (int i=n/4; i<n-n/4; i++)
                    {
                        if (a[i]=='1')printf("4");
                        else printf("2");
                    }
                    for (int i=n-n/4; i<n; i++)
                    {
                        if (a[i]=='1')printf("3");
                        else printf("1");
                    }
                    printf("
    ");
                }
                else //奇数
                {
                    for(int i=0; i<n/2; i++)
                    {
                        if (a[i]=='1')
                        {
                            if ((i+1)%2==1)printf("3");
                            else printf("4");
                        }
                        else
                        {
                            if ((i+1)%2==1)printf("1");
                            else printf("2");
                        }
                    }
                    for(int i=n/2; i<len; i++)
                    {
                        if (a[i]=='1')
                        {
                            if ((i+1)%2==1)printf("4");
                            else printf("3");
                        }
                        else
                        {
                            if ((i+1)%2==1)printf("2");
                            else printf("1");
                        }
                    }
                    printf("
    ");
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Linux内核调试方法
    linux查看系统的日志------健康检查特性
    检测磁盘驱动的健康程度SMART
    用十条命令在一分钟内检查Linux服务器性能
    Nginx安装及配置
    getopts的使用
    grub rescue 主引导修复
    linux C中调用shell命令和运行shell脚本
    Makefile基础---编译
    OVMF基础
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8971993.html
Copyright © 2011-2022 走看看