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 ... 
随机推荐
- 自定义构造、description方法、SEL
			[Objective-C]07-自定义构造方法和description方法 // 构造方法:用来初始化对象的方法,是个对象方法,”-"开头// 重写构造方法的目的:为了让对象创建出来,成 ... 
- 分享一个自用的 Inno Setup 软件打包脚本
			此脚本支持打包mysql.安装mysql服务.安装windows服务.操作ini文件.操作注册表.高效压缩文件等功能,基本能满足常用的软件打包需求. ;定义各种常量 #define MyAppName ... 
- C语言-字符编码转换:UTF与GB2312
			依赖库libiconv,libiconv库的交叉编译不做描述,网上很多 #include <stdio.h> #include <stdlib.h> #include < ... 
- Delphi 的接口机制——接口操作的编译器实现过程(2)
			接口对象的内存空间 假设我们定义了如下两个接口 IIntfA 和 IIntfB,其中 ProcA 和 ProcB 将实现为静态方法,而 VirtA 和 VirtB 将以虚方法实现: IIntfA = ... 
- mysql 权限控制具体解释
			概述 mysql权限控制在不同的上下文和不同的操作水平上都能够进行控制,他们包括例如以下几个 ** 管理权限能够同意用户管理mysql server的操作. 这些权限控制是全局的,不是针对某个特定的数 ... 
- Windows内核之进程的终止和子进程
			1 进程终止的方法: <1>主线程的进入点函数返回(最好使用这种方法) <2>进程中的一个线程调用ExitProcesss函数(应该避免使用这样的方法). <3>还 ... 
- 数据结构中La表的数据合并到Lb表中
			实验描述:La表中的数据为(3,5,8,11) Lb 表中的数据为(2,6,8,9,11,15,20) 将La表中的数据而不存在Lb表的数据插入到Lb表中,从而实现并集操作. 出现的问题:最后实现的 ... 
- 2014年同年CFA考试中哪些CFA资料没有变化?
			从2014年起,美国CFA协会将官方教材.题库.模拟题等CFA资料捆绑在报名费用之中,而以往可以单独选购的纸质版教材也变成了额外购买.这让非常多參加12月的CFA考生产生了借阅6月考生CFA资料的想法 ... 
- Android - 不管在应用的哪个activity按Home键整个App就结束了
			最开始,客户反映说在用app的时候,来个电话,接完再点app,不是原来的界面,而是重启了.数据都没了,所以就在activity重写onSaveInstanceState方法,将数据保存起来.后经测试发 ... 
- 【原创】重绘winform的GroupBox
			功能:重绘winform的GroupBox,以便调整边框颜色和边框宽度 using System; using System.Collections.Generic; using System.Com ... 
