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!
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.
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.
3
1 2 -3
YES
2
1 2
3 3
8
9 -12 3 4 -4 -10 7 3
YES
2
1 2
3 8
1
0
NO
4
1 2 3 -5
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
#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
const int maxn=1e5;
int a[maxn];
pair<int,int>Pa[maxn];
int main(){
int n;
int sum=;
int flag=;
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i];
sum+=a[i];
}
if(sum){
cout<<"YES"<<endl;
cout<<""<<endl;
cout<<""<<" "<<n<<endl;
}else{
sum=;
for(int i=;i<=n;i++){
sum+=a[i];
if(sum){
cout<<"YES"<<endl;
cout<<""<<endl;
cout<<""<<" "<<i<<endl;
cout<<i+<<" "<<n<<endl;
return ;
}
}
cout<<"NO"<<endl;
}
return ;
}
#include<bits/stdc++.h> using namespace std; int n, a[], nule = , poc = ;
vector <pair<int, int> > p; int main(){
cin >> n;
for (int i = ; i <= n; i++){
cin >> a[i];
if (a[i] == ) nule++;
}
if (nule == n){
cout << "NO" << endl;
return ;
}
cout << "YES" << endl;
for (int i = ; i <= n; i++){
if (a[i] != ){
while(i < n && a[i + ] == ) i++;
p.push_back(make_pair(poc, i));
poc = i + ;
}
}
if (poc != n + ){
p.push_back(make_pair(poc, n));
}
cout << p.size() << endl;
for (int i = ; i < p.size(); i++){
pair <int, int> x = p[i];
cout << x.first << ' ' << x.second << endl;
}
return ;
}
最新文章
- centos 6 cglib
- 使用ionic framework创建一个简单的APP
- Mac下命令行中用sublime打开指定文件 设置方法
- android 学习随笔十四(页面跳转与数据传递)
- Java DES 加解密文件
- jQuery选择器总结 转
- 版本控制-cvs
- poj 1066 Treasure Hunt
- [c#]asp.net开发微信公众平台(1)数据库设计
- cocos2d-x游戏开发系列教程-搭建cocos2d-x的windows开发环境
- Wamp环境下配置--Apache虚拟主机
- Oracle笔试题库之问答题篇-总共60道
- 229. Majority Element II求众数II
- LeetCode算法题-Factorial Trailing Zeroes(Java实现)
- PHP 依赖注入,依赖反转 (IOC-DI)
- 小程序和H5互调
- jquery 中多选和全选
- 继续JS之DOM对象二
- 目标检测(一)RCNN--Rich feature hierarchies for accurate object detection and semantic segmentation(v5)
- 【react】---context的基本使用新版---【巷子】