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 ...
随机推荐
- Material Theme
Material Theme提供了一下功能: 1.系统widgets可以设置调色板 2.系统widgets的触摸反馈 3.Activity过渡动画 你可以根据你品牌的色彩来定义Material The ...
- Apdex——衡量服务器性能的标准
Apdex 全称是 Application Performance Index,是由 Apdex 联盟开放的用于评估应用性能的工业标准.Apdex 联盟起源于 2004 年,由 Peter Sevci ...
- 【Todo】C++类 & 通用面试题分析记录 & 最难的bug
1. the most difficult bug u fixed and how u solved this problem.. 解决过很多疑难bug.最困难的分为两类.一类是并发.多线程类的,因为 ...
- 线程特定数据TSD总结
一线程的本质 二线程模型的引入 三线程特定数据 四关键函数说明 五刨根问底啥原理 六私有数据使用演示样例 七參考文档 一.线程的本质 Linux线程又称轻量进程(LWP),也就说线程本质是用进程之间共 ...
- Redis经常使用命令
1 创建-是否存储-查看-删除 set name maojun;exists name;get name;del name; 2 序列化记录 set name maojun;exists name;d ...
- HDU 4791 Alice's Print Service 水二分
点击打开链接 Alice's Print Service Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- Spring Boot与Micronaut性能比较
文章转载出处:微信公众号——锅外的大佬 链接:https://mp.weixin.qq.com/s/MdBByJ0ju-rROKg7jsWygA 今天我们将比较两个在JVM上构建微服务的框架:Spri ...
- Dell服务器相关操作
1. Raid相关: # 删除已有的Raid配置 MegaCli64 -CfgLdDel -LALL -aALL # 获取设备ID和槽号 MegaCli64 -PDList -aALL|egrep ' ...
- hdoj 4932 Miaomiao's Geometry 【暴力枚举】
题意:在一条直线上有n个点.取一长度差为x的区间. 规定点必须是区间的端点. 让你找出来最大的x 策略:rt 分析可得:两个相邻点之间的区间要么是两个点的差,要么就是两个点的差的一半,那我们就简单枚举 ...