Woodcutters

CodeForces - 545C

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

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

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的更多相关文章

  1. Codeforces 刷水记录

    Codeforces-566F 题目大意:给出一个有序数列a,这个数列中每两个数,如果满足一个数能整除另一个数,则这两个数中间是有一条边的,现在有这样的图,求最大联通子图. 题解:并不需要把图搞出来, ...

随机推荐

  1. [MicroPython]TurniBit开发板DIY自动窗帘模拟系统

    一.准备工作 üTurnipBit 开发板 一块 ü下载数据线 一条 ü微型步进电机(28BYJ-48) 一个 ü步进电机驱动板(ULN2003APG) 一块 ü光敏传感器 一个 üTurnipBit ...

  2. python之函数、参数、作用域、递归

    函数的定义 函数也就是带名字的代码块.使用关键字def来定义,指出函数名及参数,以冒号结尾. def fibs(num): result =[0,1] for i in range(num-2): r ...

  3. buildroot使用介绍

    buildroot是Linux平台上一个构建嵌入式Linux系统的框架.整个Buildroot是由Makefile脚本和Kconfig配置文件构成的.你可以和编译Linux内核一样,通过buildro ...

  4. input表单提交完毕,返回重新填入有黄色背景,和 历史记录 清除

    <input autocomplete="value"> // 添加这个属性,可以解决然后添加一个css input:-webkit-autofill {box-sha ...

  5. F#.NET周报 2018第34周-Ionide下载量100万

    回顾一下过去一周F#和.NET最新相关信息   原文   新闻 Ionide 你在VS Code 上写F# 是离不开他的. ^^ 下载100万了 .NET Core 2.1.3发布,支持LTS版本(L ...

  6. React组件重构:嵌套+继承 与 高阶组件

    前言 在最近做的一个react项目中,遇到了一个比较典型的需要重构的场景:提取两个组件中共同的部分. 最开始通过使用嵌套组件和继承的方式完成了这次重构. 但是后来又用高阶组件重新写了一遍,发现更好一点 ...

  7. NLP基础——词集模型(SOW)和词袋模型(BOW)

    (1)词集模型(Set Of Words): 单词构成的集合,集合自然每个元素都只有一个,也即词集中的每个单词都只有一个. (2)词袋模型(Bag Of Words): 如果一个单词在文档中出现不止一 ...

  8. OSGI基础环境搭建

    1.安装jdk,步骤自行搜索 2.下载eclipse,用luna版本,下载地址: https://pan.baidu.com/s/1gdfmW5znU4fltsLCAo8tkg 提取码: nrg7 3 ...

  9. JS 有趣的JS

    一. var arr = []; for (var i = 0; i < 3; i++) { arr[i] = function() { console.log(i+'__') // 3 3 3 ...

  10. HDU 3478 Catch (连通性&&二分图判断)

    链接 [https://vjudge.net/contest/281085#problem/C] 题意 一个n个点,m条边的图,开始的点是s 每次必须移动到相邻的位置,问你是否存在某个时刻所有点都可能 ...