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
随机推荐
- 【java】HashMap、Map、Set、HashMap.put()、HashMap.keySet()、HashMap.entrySet()、Map.Entry内部类
package com.tn.hashMap; public class Student { private String id; private String name; public Studen ...
- 在IntelliJ IDEA里创建简单的基于Maven的SpringMVC项目
后来发现了一种更加方便的创建方式,即第一步不选择Create from archetype,创建完毕后打开Project Structure-Modules,然后添加Web,但是注意添加的Web里面的 ...
- slowhttptest慢攻击工具介绍
slowhttptest介绍 Slowhttptest是依赖HTTP协议的慢速攻击DoS攻击工具,设计的基本原理是服务器在请求完全接收后才会进行处理,如果客户端的发送速度缓慢或者发送不完整,服务端为其 ...
- weex Mac开发环境
安装: 1.java的jdk下载和安装 1-1.下载.安装:省略 1-2.配置 第一步:命令行内输入touch .bash_profile命令,生成.bash_profile的隐藏配置文件,用于配置j ...
- css3特效样式库
直接调用样式类即可: /* animation */ .a-bounce,.a-flip,.a-flash,.a-shake,.a-swing,.a-wobble,.a-ring{-webkit-an ...
- detach() 与remove()
detach() 与remove()区别,在于remove()掉后,就没有啦,添加的事件也没有啦,后者还有啊,可以保留的哦,虽然 $("div").click(function() ...
- 一、JavaSE语言概述
1.软件:系统软件 VS 应用软件 2.人与计算交互:使用计算机语言.图形化界面VS命令行. 3.语言的分类:第一代:机器语言 第二代:汇编语言 第三代语言:高级语言(面向过程-面向对象) 4.jav ...
- 非常好用的弹出层 layer,常用功能demo,快速上手!
功能强大,实用,操作方便,文档齐全. 参数灵活,丰富.可以作为开发项目的公共模块,多处使用.老文档地址:http://layer.layui.com/api.html 已经停止维护 新文档地址:htt ...
- Linux入门篇(四)——Vim的使用与Bash
这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...
- Java实现贪吃蛇游戏【代码】
花了两个下午写了一个贪吃蛇小游戏,本人想写这游戏很长时间了.作为以前诺基亚手机上的经典游戏,贪吃蛇和俄罗斯方块一样,都曾经在我们的童年给我们带来了很多乐趣.世间万物斗转星移,诺基亚曾经作为手机业的龙头 ...