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 ...
随机推荐
- 【postgresql】postgresql中的between and以及日期的使用
在postgresql中的between and操作符作用类似于,是包含边界的 a BETWEEN x AND y 等效于 a >= x AND a <= y 在postgresql中比较 ...
- python实现网速控制,限制上传下载速度
对于python的web,比如flask使用的werkzeug,首先找到wsgi的请求和响应的代码,使用算法实现大文件的小速率上传和下载 考虑python实现socket限流 关于限速的讨论:http ...
- 自己动手实现浏览器,21天自制chromium:起手篇
转:https://zhuanlan.zhihu.com/p/29101613?utm_medium=social&utm_source=qq 大家好,我又来了.这篇是21天自制原子弹的姐妹篇 ...
- 线性表的顺序存储和链式存储的实现(C)
//线性表的顺序存储 #include <stdio.h>typedef int DataType;#define MaxSize 15//定义顺序表typedef struct { Da ...
- Effective C++ 条款11,12 在operator= 中处理“自我赋值” || 复制对象时不要忘记每一个成分
1.潜在的自我赋值 a[i] = a[j]; *px = *py; 当两个对象来自同一个继承体系时,他们甚至不需要声明为相同类型就可能造成别名. 现在担心的问题是:假如指向同一个对象, ...
- TagCanvas - HTML5 Canvas Tag Cloud
http://www.goat1000.com/tagcanvas.php watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbmlja3JvcHJhaw==/ ...
- Java的编程逻辑--15章 并发基础
1.run()和start()的区别 2.线程的基本属性和方法 id:一个递增的整数,每创建一个线程就加一 name 优先级:从1到10,默认为5,会映射到系统中的优先级.数字越大,要优先级越高 状态 ...
- MFC中CAsyncSocket和CSocket
原文链接:https://blog.csdn.net/libaineu2004/article/details/40395917 摘要部分重点: 1.CAsyncSocket类逐个封装了WinSock ...
- Struts错误信息回传
<td height="20" align="center" class="loginMiddleDiv_loginInfo_window_wa ...
- Java 快速失败( fail-fast ) 安全失败( fail-safe )
原文:http://www.cnblogs.com/ygj0930/p/6543350.html 快速失败( fail-fast ):当你在迭代一个集合的时候,如果有另一个线程正在修改你正在访问的那个 ...