Codeforces Round #390 (Div. 2) A

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 liri 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

题意:求区间和不是0有多少个
解法:
1 看样列貌似不是0就算区间本身,是0就右展开,不过这样代码就有点复杂(第二个)
2 全为0的情况没有区间,数组和不为0,那么就算整个区间
3 如果发现整个和为0,则考虑一个个加,发现和不为0就输出区间1-i,另外一个就算i+1~n
 1 #include<bits/stdc++.h>
 2 typedef long long LL;
 3 typedef unsigned long long ULL;
 4 using namespace std;
 5 const int maxn=1e5;
 6 int a[maxn];
 7 pair<int,int>Pa[maxn];
 8 int main(){
 9     int n;
10     int sum=0;
11     int flag=0;
12     cin>>n;
13     for(int i=1;i<=n;i++){
14        cin>>a[i];
15        sum+=a[i];
16     }
17     if(sum){
18         cout<<"YES"<<endl;
19         cout<<"1"<<endl;
20         cout<<"1"<<" "<<n<<endl;
21     }else{
22         sum=0;
23         for(int i=1;i<=n;i++){
24             sum+=a[i];
25             if(sum){
26                 cout<<"YES"<<endl;
27                 cout<<"2"<<endl;
28                 cout<<"1"<<" "<<i<<endl;
29                 cout<<i+1<<" "<<n<<endl;
30                 return 0;
31             }
32         }
33         cout<<"NO"<<endl;
34     }
35     return 0;
36 }
#include<bits/stdc++.h>

using namespace std;

int n, a[105], nule = 0, poc = 1;
vector <pair<int, int> > p;

int main(){
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
        if (a[i] == 0) nule++;
    }
    if (nule == n){
        cout << "NO" << endl;
        return 0;
    }
    cout << "YES" << endl;
    for (int i = 1; i <= n; i++){
        if (a[i] != 0){ 
            while(i < n && a[i + 1] == 0) i++;
            p.push_back(make_pair(poc, i));
            poc = i + 1;
        }    
    }
    if (poc != n + 1){
        p.push_back(make_pair(poc, n));
    }
    cout << p.size() << endl;
    for (int i = 0; i < p.size(); i++){
        pair <int, int> x = p[i];
        cout << x.first << ' ' << x.second << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yinghualuowu/p/7226811.html