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
题意:
就是把一个数组分成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);
}
}
}
}