Codeforces 754A Lesha and array splitting(简单贪心)
2017-05-08 23:19
399 查看
A. Lesha and array splitting
time limit per test:2 secondsmemory limit per test:256 megabytes
input:standard input
output:standard output
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.
Examples
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 题目链接:http://codeforces.com/contest/754/problem/A 分析:求前缀和; 看看pre 等于多少; pre !=0; 则直接整个数组全部输出; 如果pre ==0 则在前面找一个i pre[i]!=0 如果找到了 则 输出a[1..i]和a[i+1..n]; 可以看成是pre[0]=0,pre[i]!=0,pre =0 则可知这两段都是符合要求的不为0; 但是如果没有找到pre[i]!=0 那么就意味着pre[1..n-1]都为0;则数字全为0;则不可能了; 贪心吧。 下面给出AC代码:
#include<iostream> using namespace std; int n,a[101],i,s,b; int main() { cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; s+=a[i]; if(a[i]) b=i; } if(b==0) cout<<"NO\n"; else if(s) cout<<"YES\n1\n"<<1<<" "<<n<<"\n"; else cout<<"YES\n2\n"<<1<<" "<<b-1<<"\n"<<b<<" "<<n<<"\n"; return 0; }
相关文章推荐
- codeforces 525C Ilya and Sticks(简单贪心)
- Codeforces 629B Far Relative’s Problem(简单区间贪心)
- codeforces 74B Train 贪心 简单模拟
- codeforces 285C Building Permutation (简单贪心)
- CodeForces - 794C:Naming Company(博弈&简单贪心)
- CodeForces - 609A USB Flash Drives ( 排序 简单贪心 )
- codeforces 472BDesign Tutorial: Learn from Life(简单贪心)
- Codeforces 755 F. PolandBall and Gifts (简单贪心+二进制优化多重背包)
- codeforces-392D->(简单贪心)
- Codeforces #277.5 (Div. 2) C. Given Length and Sum of Digits...(简单贪心)
- Codeforces 712B Memory and Trident 简单贪心
- HDU 1800 简单贪心 13.1.10用trie重做 ---其实是统计出现次数最多的单词
- POJ 3183 Stump Removal(简单的贪心)
- Codeforces Round #111 (Div. 2) 160A Twins(简单贪心)
- 简单DP 01 背包 饭卡HDU 2546 ( 饭卡 ) 不知道算不算是稍稍加了点贪心的思想~
- Codeforces 226D The table 贪心
- hdu 4070福州网络赛 简单贪心
- POJ 2940 Wine Trading in Gergovia(简单贪心)
- poj 1065Wooden Sticks (简单的贪心)
- 【HDU4313】Matrix 多校 解题报告+AC代码+思路+算法正确性证明,此为Kruskal贪心简单版本,恶心版本稍后放出【目标达成 0.2%】