C - Woodcutters
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below.
There are n trees located along the road at points with coordinates x1, x2, ..., xn. Each tree has its height hi. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [xi - hi, xi] or [xi;xi + hi]. The tree that is not cut down occupies a single point with coordinate xi. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the number of trees.
Next n lines contain pairs of integers xi, hi (1 ≤ xi, hi ≤ 109) — the coordinate and the height of the і-th tree.
The pairs are given in the order of ascending xi. No two trees are located at the point with the same coordinate.
Output
Print a single number — the maximum number of trees that you can cut down by the given rules.
Sample Input
Input5
1 2
2 1
5 10
10 9
19 1Output3Input5
1 2
2 1
5 10
10 9
20 1Output4Hint
In the first sample you can fell the trees like that:
- fell the 1-st tree to the left — now it occupies segment [ - 1;1]
- fell the 2-nd tree to the right — now it occupies segment [2;3]
- leave the 3-rd tree — it occupies point 5
- leave the 4-th tree — it occupies point 10
- fell the 5-th tree to the right — now it occupies segment [19;20]
In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19].
题意:一条路上有树,树有高度h,我们可以将树向左或向右砍倒只要它倒下去不会压到其他树(不论这些树是站着还是倒下了)。求最多可以砍倒多少树。
这一场cf真是水的一匹,我当时怎么没打!!!简直上分福利局。
我们只要按照题目给定的操作走一遍就可以了。优先向左倒,就连数据都已经给你排好序了。
附AC代码:
#include<iostream>
using namespace std; long long x[];
long long h[]; int main(){
int n;
cin>>n;
for(int i=;i<n;i++){
cin>>x[i]>>h[i];
}
if(n>){
int ans=;
for(int i=;i<n-;i++){
if(x[i]-h[i]>x[i-]){
ans++;
}
else if(x[i]+h[i]<x[i+]){
ans++;
x[i]+=h[i];
}
}
cout<<ans<<endl;
}
else
cout<<n<<endl;
return ;
}
C - Woodcutters的更多相关文章
- CF R303 div2 C. Woodcutters
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- (贪心 or DP)Woodcutters -- Codefor 545C
http://codeforces.com/contest/545/problem/C Woodcutters time limit per test 1 second memory limit p ...
- Codeforces Round #303 (Div. 2) C. Woodcutters 贪心
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- 545C. Woodcutters
题目链接 题意: n个树,在x1,x2,...,xn的位置,树的高度依次是h1,h2,...,hn 求的是当把树砍倒时候,不占用相邻树的位置,最大砍树个数 可向左 向右砍,即树向左向右倒,很显然 当树 ...
- Codeforces 545C Woodcutters
http://codeforces.com/contest/545/problem/C 题目大意: 给n棵树的在一维数轴上的坐标,以及它们的高度.现在要你砍倒这些树,树可以向左倒也可以向右倒,砍倒的树 ...
- 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)
这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...
- Codeforces Round #303 (Div. 2) C dp 贪心
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- python瓦登尔湖词频统计
#瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...
随机推荐
- UNIDAC如何驱动MSSQL2000
UNIDAC如何驱动MSSQL2000 如下图,PROVIDER必须设置为PRSQL.默认的PRAUTO,如果操作系统是XP可以,如果是WIN7以上的,不可以. 原因是MSSQL NATIVE 11已 ...
- 实时获取键盘高度 CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
注意:要想实时获取键盘的高度,比如当前如果是中文那么就会增高的.那么需要使用 UIKeyboardFrameEndUserInfoKey 而不是 UIKeyboardFrameBeginUserIn ...
- BUPT复试专题—找K小数(2011)
https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd?tpId=67&tqId=29637&rp=0&a ...
- 手机加载优化 - 2x、3x图
人们常说,现在的流量不值钱了,图越大越好咯! 我想说的是,浏览器虽然不值钱了,但速度还是略快吧! 文章来自:UI妹儿 icon_alipay.png→iPhone 1-3代的手机(已经不考虑了) ic ...
- JQUERY多选框,单选框,检查选中的值
var str=""; $(":checkbox:checked").each(function(){ if($(this).attr("checke ...
- The user's guide what comes in the kernel Documentation directory
The Linux IPMI Driver --------------------- Corey Minyard <minyard@mvista.com> <minyard@acm ...
- long long , __int64 范围
VC的64位整数 分别叫做__int64与unsigned __int64,其范 围分别是[-2^63, 2^63)与[0,2^64),即-9223372036854775808~9223372036 ...
- 5. TCP客户/服务器程序示例
signal 信号是一种软件中断,异步发生,在进程运行的时候随时可能发生.信号可以: 由一个进程发给另一个进程,或发给自身 由内核发给某个进程 信号的action: signal handler,在信 ...
- Android安装应用后点击"打开"(Open)带来的问题及解决方案
MainActivity例如以下: package cc.cc; import android.app.Activity; import android.content.Intent; import ...
- JSP与HTML的差别
HTML(Hypertext Markup Language)文本标记语言,它是静态页面,和JavaScript一样解释性语言,为什么说是解释性 语言呢?由于.仅仅要你有一个浏览器那么它就能够正常显示 ...