Codeforces 754A Lesha and array splitting(简单贪心)
A. Lesha and array splitting
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
题目链接:http://codeforces.com/contest/754/problem/A
分析:求前缀和; 看看pre[n]等于多少; pre[n]!=0; 则直接整个数组全部输出; 如果pre[n]==0 则在前面找一个i pre[i]!=0 如果找到了 则 输出a[1..i]和a[i+1..n]; 可以看成是pre[0]=0,pre[i]!=0,pre[n]=0 则可知这两段都是符合要求的不为0; 但是如果没有找到pre[i]!=0 那么就意味着pre[1..n-1]都为0;则数字全为0;则不可能了; 贪心吧。
下面给出AC代码:
#include<iostream>
using namespace std;
int n,a[],i,s,b;
int main()
{
cin>>n;
for(i=;i<=n;i++)
{
cin>>a[i];
s+=a[i];
if(a[i])
b=i;
}
if(b==)
cout<<"NO\n";
else if(s)
cout<<"YES\n1\n"<<<<" "<<n<<"\n";
else
cout<<"YES\n2\n"<<<<" "<<b-<<"\n"<<b<<" "<<n<<"\n";
return ;
}
Codeforces 754A Lesha and array splitting(简单贪心)的更多相关文章
- Codeforces 754A Lesha and array splitting (搜索)
题目链接 Lesha and array splitting 设s[i][j]为序列i到j的和,当s[i][j]≠0时,即可从i跳到j+1.目标为从1跳到n+1,所以按照题意暴力即可. #includ ...
- 【codeforces 754A】Lesha and array splitting
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #390 (Div. 2) A. Lesha and array splitting
http://codeforces.com/contest/754/problem/A 题意: 给出一串序列,现在要把这串序列分成多个序列,使得每一个序列的sum都不为0. 思路: 先统计一下不为0的 ...
- CodeForces - 794C:Naming Company(博弈&简单贪心)
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...
- Codeforces 845C. Two TVs 思路:简单贪心算法
题目: 题目原文链接:http://codeforces.com/contest/845/problem/C 题意:现在我们有一个电视清单,有两个电视,电视清单上有每一个节目的开始时间和结束时间. 电 ...
- codeforces 402 D. Upgrading Array(数论+贪心)
题目链接:http://codeforces.com/contest/402/problem/D 题意:给出一个a串和素数串b .f(1) = 0; p为s的最小素因子如果p不属于b , 否则 . a ...
- Codeforces F. Maxim and Array(构造贪心)
题目描述: Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- cf754 A. Lesha and array splitting
应该是做麻烦了,一开始还没A(幸好上一次比赛水惨了) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL lon ...
- Educational Codeforces Round 69 (Rated for Div. 2) C. Array Splitting 水题
C. Array Splitting You are given a sorted array
随机推荐
- iOS 数据储存--SQLite 操作数据库-FMDB,sqlite数据类型,保存图片,demo
1.SQLite 语句中 数据类型的储存 /* 不区分大小写 char(长度).字符串 NULL. 空值 INTEGER. 整型 REAL.浮点型 TEXT.文本类型 BLOB. 二进制类型,用来存储 ...
- SQL Server AlwaysOn添加监听器失败
标签:MSSQL/ 一.错误描述 1.群集服务未能使群集服务或应用程序“Alwayson22”完全联机或脱机.一个或多个资源可能处于失败状态.这可能会影响群集服务或应用程序的可用性 2.群集服务中的群 ...
- COM_第四讲_保存GUID_优化使用代码
优化以前的代码,让使用者更方便 一丶 优化思路 1.我们可以将我们写的GUID(类工厂的ID)保存到注册表中,并且保存一下DLL的文件路径,遍历注册表去DLL路径即可. 2.每个类工厂我们就要使用一个 ...
- ArcGIS 网络分析[8.4] 资料4 聚合——创建及打开网络数据集的类实现
这篇是对前三篇的总结,因为网络数据集涉及的"点"太多了,我只能挑重点来设置,大家明白框架后可以自行寻求帮助文档添加功能. 我以C#类的形式给出,这个类包含很多种方法,因为本人的C# ...
- 与apk签名有关的那些概念与命令
一.概念篇 1.消息摘要-Message Digest 消息摘要:在消息数据上,执行一个单向的hash函数,生成一个固定长度的hash值,这个Hash值就是消息摘要,也成为数字指纹. 消息摘要特点: ...
- Web API系列之三 基本功能实现
Web API系列之二讲解了如何搭建一个WebApi的基架,本文主要在其基础之上实现基本的功能.下面开始逐步操作: 一.配置WebApi的路由-用于配置外部如何访问内部资源的url的规则 1.添加Gl ...
- F和弦大横按
用食指手掌关节顶住. 靠近品柱往上压 右手压住琴尾,这样就很轻松试下C跟F不停转换就快找到感觉 等熟练了,食指自然不会按太上 练得差不多了,手指向琴头倾压,有两个好处 1.手指后面的肉不会碰到弦 2. ...
- javascript计算对象的长度
计算对象的长度,即获取对象属性的个数 方法一:通过for in 遍历对象,并通过hasOwnProperty判断是否是对象自身可枚举的属性 var obj = {"c1":1,&q ...
- python模块:shelve
shelve 1)模块功能:以 key - value 的方式存储数据. 2)写数据 >>> import shelve >>> db = shelve.open( ...
- TPYBoard v102 驱动28BYJ-48步进电机
TPYBoard v102 驱动28BYJ-48步进电机 实验目的 了解步进电机的工作原理 学习步进电机的驱动方法 实验器材 TPYBoard v102 1块 微型步进电机(28BYJ-48) 1个 ...