Lesha and array splitting
Onespringday on his way to university Lesha found an arrayA. Lesha likes to split arrays into several parts. This time Lesha decided to split the arrayAinto 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 arrayA.
Lesha is tired now so he asked you to split the array. Help Lesha!
Input
The first line contains single integern(1?≤n≤?100) — the number of elements in the arrayA.
The next line containsnintegersa1,a2,?...,an(-?103≤ai≤?103) — the elements of the arrayA.
Output
If it is not possible to split the arrayAand 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 integerk— the number of new arrays. In each of the nextklines print two integersliandriwhich denote the subarrayA[li...ri]of the initial arrayAbeing thei-th new array. Integersli,rishould satisfy the following conditions:
l1=?1
rk=n
ri+?1?=li+?1for each1?≤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
題意:
就是把一個(gè)數(shù)組分成N個(gè);每個(gè)數(shù)組的和不能為0;If there are multiple answers, print any of them.注意這一句話
思路就是不為0的就一個(gè)就可以了,為0的話,分兩種,全為0特判一下,其他的分成兩個(gè),從不為0到不為0(考慮第一個(gè)為0之后也是的情況),然后剩下的為一個(gè)。
還有一種是有多少個(gè)就多少個(gè)數(shù)組,然后0和他前面的數(shù)一個(gè)數(shù)組,要考慮前面為0的情況,把他歸到第一個(gè)數(shù)組里面。
這是第一個(gè)思路:
#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);
}
}
}
}