美文网首页
CodeForces 754A

CodeForces 754A

作者: 与卿__歌 | 来源:发表于2017-01-19 20:50 被阅读0次

    Lesha and array splitting
    One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into several parts. This time Lesha decided to split the array A into several, possibly one, new arrays so that the sum of elements in each of the new arrays is not zero. One more condition is that if we place the new arrays one after another they will form the old array A.

    Lesha is tired now so he asked you to split the array. Help Lesha!

    Input
    The first line contains single integer n (1 ≤ n ≤ 100) — the number of elements in the array A.

    The next line contains n integers a1, a2, ..., an ( - 103 ≤ ai ≤ 103) — the elements of the array A.

    Output
    If it is not possible to split the array A and satisfy all the constraints, print single line containing "NO" (without quotes).

    Otherwise in the first line print "YES" (without quotes). In the next line print single integer k — the number of new arrays. In each of the next k lines print two integers li and ri which denote the subarray A[li... ri] of the initial array A being the i-th new array. Integers li, ri should satisfy the following conditions:

    l1 = 1
    rk = n
    ri + 1 = li + 1 for each 1 ≤ i < k.
    If there are multiple answers, print any of them.

    Example
    Input
    3
    1 2 -3
    Output
    YES
    2
    1 2
    3 3
    Input
    8
    9 -12 3 4 -4 -10 7 3
    Output
    YES
    2
    1 2
    3 8
    Input
    1
    0
    Output
    NO
    Input
    4
    1 2 3 -5
    Output
    YES
    4
    1 1
    2 2
    3 3
    4 4
    题意:
    就是把一个数组分成N个;每个数组的和不能为0;If there are multiple answers, print any of them.注意这一句话
    思路就是不为0的就一个就可以了,为0的话,分两种,全为0特判一下,其他的分成两个,从不为0到不为0(考虑第一个为0之后也是的情况),然后剩下的为一个。
    还有一种是有多少个就多少个数组,然后0和他前面的数一个数组,要考虑前面为0的情况,把他归到第一个数组里面。
    这是第一个思路:

    #include<stdio.h>
    int main()
    {
        int n,i,j,sum,s[105],flag,x;
        while(scanf("%d",&n)!=EOF)
        {
            flag=0;
            sum=0;
            for(i=0;i<n;i++)
            {
                scanf("%d",&s[i]);
                sum=sum+s[i];
                if(s[i]!=0)
                {
                    x=i;
                    flag=1;
                }
            }
            if(sum==0&&flag==0)
            {
                printf("NO\n");
                continue;
            }
            if(sum!=0)
            {
            printf("YES\n1\n1 %d\n",n);
             }
             if(sum==0)
             {
                 printf("YES\n2\n");
                 if(x==0)
                 {
                     printf("1 1\n");
                     printf("2 %d\n",n);
                 }
                 if(x!=0)
                 {
                     printf("1 %d\n",x);
                     printf("%d %d\n",x+1,n);
                 }
             }
        }
    
    }
    

    相关文章

      网友评论

          本文标题:CodeForces 754A

          本文链接:https://www.haomeiwen.com/subject/lhaibttx.html