CF R303 div2 C. Woodcutters
1 second
256 megabytes
standard input
standard output
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.
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.
Print a single number — the maximum number of trees that you can cut down by the given rules.
5
1 2
2 1
5 10
10 9
19 1
3
5
1 2
2 1
5 10
10 9
20 1
4
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].
题目大意是给出树的点和他们的高度,伐木工砍伐时可以让他们向左倒或向右倒,倒下后占据覆盖的坐标,但不能倒到已经被占据的点上。
是一道简单的dp和贪心题,可以看到,第一棵树和最后一棵树都是必定可以砍倒的,而从左到右遍历中间的树尽量让他们向左倒,这样倒下后占据的点不会影响到后面的点,每棵树给它一个occupy属性表明它占据的坐标的最大值。dp[i]=max(dp[i-1],i向左倒,i向右倒)。
AC代码:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int dp[];
struct tree{
int h,x,occupy;
}t[];
int main(){
int n,i;
while(cin>>n){
memset(dp,,sizeof(dp));
for(i=;i<n;i++){
cin>>t[i].x>>t[i].h;
t[i].occupy=t[i].x;
}
dp[]=;
if(n==){
cout<<<<endl;
continue;
}
if(n==){
cout<<<<endl;
continue;
}
for(i=;i<n-;i++){
int th=t[i].h,tx=t[i].x,toc=t[i-].occupy;
int l=dp[i-],r=dp[i-];
if((tx+th)<t[i+].x){
r++;
t[i].occupy=tx+th;
}
if(tx-th>toc){
l++;
t[i].occupy=tx;
}
dp[i]=max(max(dp[i-],l),r);
}
dp[n-]=dp[n-]+;
cout<<dp[n-]<<endl;
}
return ;
}
CF R303 div2 C. Woodcutters的更多相关文章
- cf 442 div2 F. Ann and Books(莫队算法)
cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...
- CF#603 Div2
差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...
- CF R631 div2 1330 E Drazil Likes Heap
LINK:Drazil Likes Heap 那天打CF的时候 开场A读不懂题 B码了30min才过(当时我怀疑B我写的过于繁琐了. C比B简单多了 随便yy了一个构造发现是对的.D也超级简单 dp了 ...
- CF#581 (div2)题解
CF#581 题解 A BowWow and the Timetable 如果不是4幂次方直接看位数除以二向上取整,否则再减一 #include<iostream> #include< ...
- [CF#286 Div2 D]Mr. Kitayuta's Technology(结论题)
题目:http://codeforces.com/contest/505/problem/D 题目大意:就是给你一个n个点的图,然后你要在图中加入尽量少的有向边,满足所有要求(x,y),即从x可以走到 ...
- CF 197 DIV2 Xenia and Bit Operations 线段树
线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...
- CF#345 div2 A\B\C题
A题: 贪心水题,注意1,1这组数据,坑了不少人 #include <iostream> #include <cstring> using namespace std; int ...
- CF 192 Div2
A.Cakeminator 暴搞之,从没有草莓覆盖的行.列遍历 char map[30][30]; int vis[30][30]; int hang[30],lie[30]; int main() ...
- CF 191 div2
A.数据量很小,直接爆搞. #include <iostream> #include <cstdio> #include <algorithm> #include ...
随机推荐
- OC语法3——点语法,self关键字
点语法: 为了给程序员提供便捷,OC中也引入了点语法.不过它和Java中点语法的意义是完全不同的. 在Java中无论调用任何方法,还是访问public类型的成员变量都是用点语法(.号). 而在OC ...
- 解决ie6支持最大高度最小高度的方法
1.IE6支持max-height解决方法 IE6支持最大高度解决CSS代码:.yangshi{max-height:1000px;_height:expression((document.docum ...
- CSS3 Pie工具可以让IE6至IE8版本实现大多数的CSS3修饰特性,如圆角、阴影、渐变等
css3 pie使用方法: <!doctype html> <html lang="en"> <head> <meta charset=& ...
- 安装solaris_11.2与windows双系统(VM10模拟实现)(一)
感慨:这周刚接触solaris,装solaris很蛋疼,一个字:慢! 在上面安装软件包依然很慢,无线网也很不稳定. 在上面搭建环境更蛋疼,一个字:惨! 什么环境之类的废话就不多说了,一般的电脑都可以. ...
- 移动跨平台开发框架Ionic开发一个新闻阅读APP
移动跨平台开发框架Ionic开发一个新闻阅读APP 前言 这是一个系列文章,从环境搭建开始讲解,包括网络数据请求,将持续更新到项目完结.实战开发中遇到的各种问题的解决方案,也都将毫无保留的分享给大家. ...
- MYSQL 数据表备份
方法: mysqldump -uuser_name -p db_name table_nameList >save_file_path 例子: 备份分studio数据库的student 表 my ...
- WEB Application Development Integrator : 应用设置
1.1. 系统安装 应用 Oracle EBS WEB Application Development Integrator WEB ADI在Oracle EBS 11.5.10.* 版本 ...
- 供应类型与计划分类一致性检查(PO)
应用 Oracle Purchasing 层 Level Function 函数名 Funcgtion Name CUXPOIMM 表单名 Form Name CUXPOIMM 说明 Descri ...
- 解密电子书之四:MCU(freescale)
谈完国产的君正,让我们再看看呛了君正财路的freescale iMX51. 这是freescale近期的主打产品,用的是ARM Cortex A8架构,主频在消费电子领域最高可达800MHz,在工业领 ...
- 新iPhone开售现场:土豪金火了
今日苹果在全球范围内开始正式发售iPhone 5s与5c手机.在中国王府井苹果零售店,首位购机者李先生在今早4点50分便在此等候. 现场未发现黄牛 本次苹果在中国大陆的自有零售店渠道首批iPhone ...