codeforces545C
Woodcutters
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.
Examples
5
1 2
2 1
5 10
10 9
19 1
3
5
1 2
2 1
5 10
10 9
20 1
4
Note
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].
sol:dp还是挺明显的吧,同一个点有3种状态[0/1/2] 向左倒,向右倒,不动,O(1)转移即可
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,dp[N][];
struct Shu
{
int Weiz,Gao;
}Tree[N];
int main()
{
int i;
R(n);
for(i=;i<=n;i++)
{
R(Tree[i].Weiz); R(Tree[i].Gao);
}
dp[][]=dp[][]=; dp[][]=;
for(i=;i<=n;i++)
{
dp[i][]=max(max(dp[i-][],dp[i-][]),dp[i-][]);
if(Tree[i].Weiz-Tree[i].Gao>Tree[i-].Weiz) dp[i][]=max(dp[i-][],dp[i-][])+;
if(Tree[i].Weiz-Tree[i].Gao>Tree[i-].Weiz+Tree[i-].Gao) dp[i][]=max(dp[i][],dp[i-][]+);
if((i==n)||(Tree[i].Weiz+Tree[i].Gao<Tree[i+].Weiz)) dp[i][]=dp[i][]+;
}
Wl(max(max(dp[n][],dp[n][]),dp[n][]));
return ;
}
/*
Input
5
1 2
2 1
5 10
10 9
19 1
Output
3 Input
5
1 2
2 1
5 10
10 9
20 1
Output
4
*/
codeforces545C的更多相关文章
- Codeforces 刷水记录
Codeforces-566F 题目大意:给出一个有序数列a,这个数列中每两个数,如果满足一个数能整除另一个数,则这两个数中间是有一条边的,现在有这样的图,求最大联通子图. 题解:并不需要把图搞出来, ...
随机推荐
- C# Oracle 时间字符串转时间类型
C# 字符串转时间类型 yyyy-MM-dd HH:mm:ss yyyy-MM-dd hh:mm:ss d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. d ...
- docker容器中安装vi命令
在使用docker容器时,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要安装vim,可是当你敲apt-get install vim命令时 ...
- Maven学习第3期---m2eclipse使用
一.m2eclipse简介 和Nexus一样,m2eclipse也是Sonatype出品的一款开源工具,它基于Eclipse Public License-v.10开源许可证发布,用户可以免费下载并使 ...
- MyBatis-你所不了解的sql和include
目录 <sql> 节点的基础 <include> 节点 <sql> 节点包含的节点 一起来学习 mybatis @ 在前一篇[MyBatis动态SQL(认真看看, ...
- mybatis源码-原来resultMap解析完是这样
目录 1 两个基础类 1.1 列映射类ResultMapping 1.2 结果集映射类ResultMap 2. 解析 2.1 入口函数 2.2 解析流程 2.3 获取 id 2.4 解析结果集的类型 ...
- 面试 15:顺时针从外往里打印数字(剑指 Offer 第 20 题)
面试 15:顺时针从外往里打印数字 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印每一个数字.例如输入: {{1,2,3}, {4,5,6}, {7,8,9}} 则依次打印数字为 1.2.3. ...
- Linux 命令(二)
man help:线上查询及帮助命令 命令 --help:简单帮助 help cd:查看一些Linux命令行的一些内置命令 文件和目操作命令(19个) ls cd cp find mkdi ...
- H5 61-浮动元素贴靠现象
61-浮动元素贴靠现象 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- 埋锅。。。BZOJ1004-置换群+burnside定理+
看这道题时当时觉得懵逼...这玩意完全看不懂啊...什么burnside...难受... 于是去看了点视频和资料,大概懂了置换群和burnside定理,亦步亦趋的懂了别人的代码,然后慢慢的打了出来.. ...
- R语言绘制直方图,
直方图: 核密度函数: 练习题目1: 绘制出15位同学体重的直方图和核密度估计图,并与正态分布的概率密度函数作对比 代码如下: > w <- c(75.0, 64.0, 47.4, 66. ...