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
Output
Input
Output
Input
Output
Input
Output
题目大意:
将一串数组分成相连的任意份,问是否有一种分法将其分成每一份的和都不为0
题目分析:
显而易见只有全0的时候不存在分法,其他的肯定都存在。现在我们来看,如果这个串首位不为0,那么我们将其作为开头,看他的下一位是否是0,如果是0,则一直包含,直到遇到下一个不为0的数;如果这个串的首位是0,那么我们要看这个0的位置是不是总串的首位,如果是,那么找到后面第一个不为0的数作为这个字串的末尾,如果这个0不是总串的首位,则将其包含到前一个字串的末尾(以 0 0 1 0 0这个例子做理解)
AC代码:
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; }
相关文章推荐
- 【codeforces 754A】Lesha and array splitting
- codeforces-754A-Lesha and array splitting(简单分类处理)
- Codeforces 754A - Lesha and array splitting
- Lesha and array splitting CodeForces - 754A
- Codeforces Round #390(Div. 2)A. Lesha and array splitting【思维】
- A. Lesha and array splitting----贪心
- A. Lesha and array splitting
- Codeforces Round #390 (Div. 2) A. Lesha and array splitting
- A - Lesha and array splitting
- Codeforces Round #390 (Div. 2)A Lesha and array splitting
- CodeForces 360 B.Levko and Array(二分+dp)
- Codeforces 220B Little Elephant and Array
- codeforces 295A - Greg and Array
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery && Codeforces 360 A 详解(思维+维护理论值)
- codeforces 221D Little Elephant and Array
- Codeforces 295A Greg and Array 线段树区间累加,单点询问
- Codeforces 754 D Fedor and coupons【优先队列】
- codeforces 442C C. Artem and Array(有深度的模拟)
- CodeForces 295A Greg and Array
- codeforces 718 C.Sasha and Array