您的位置:首页 > 其它

codeforces 754 A Lesha and array splitting

2017-01-08 15:42 337 查看
题目描述:

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


题目大意:
将一串数组分成相连的任意份,问是否有一种分法将其分成每一份的和都不为0

题目分析:
显而易见只有全0的时候不存在分法,其他的肯定都存在。现在我们来看,如果这个串首位不为0,那么我们将其作为开头,看他的下一位是否是0,如果是0,则一直包含,直到遇到下一个不为0的数;如果这个串的首位是0,那么我们要看这个0的位置是不是总串的首位,如果是,那么找到后面第一个不为0的数作为这个字串的末尾,如果这个0不是总串的首位,则将其包含到前一个字串的末尾(以 0 0 1 0 0这个例子做理解)

AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=105;

struct node
{
int l,r;
}p[maxn];

int x[maxn];
int t;

int main()
{
while(~scanf("%d",&t))
{
memset(p,0,sizeof(p));
memset(x,0,sizeof(x));
bool sign=false;
for(int i=1;i<=t;i++)
{
scanf("%d",&x[i]);
if(x[i]!=0)
sign=true;
}
if(!sign)
cout<<"NO"<<endl;
else
{
int k=1;
for(int i=1;i<=t;i++)
{
p[k].l=i;
if(x[i]!=0)
{
while(x[i+1]==0&&i<t)
{
i++;
}
p[k++].r=i;
}
else
{
if(i==1)
{
while(i<t)
{
i++;
if(x[i]!=0)
{
p[k++].r=i;
break;
}
}
}
else
{
p[k-1].r++;
}
}
}
cout<<"YES"<<endl;
cout<<k-1<<endl;
for(int i=1;i<k;i++)
cout<<p[i].l<<" "<<p[i].r<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: