题目

链接:https://codeforces.com/problemset/problem/545/C

time limit per test:1 second;memory limit per test:256 megabytes

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].

思路

        如果只有1棵树,答案就是1
        如果有大于两棵树,第1棵树向左倒,最后一棵树向右倒,答案至少为2,到底有多少,还要分析分析。
        当n>=2时,对于中间的n-2棵树,能向左倒则向左倒,不能向左倒则向右倒(不这样做就会浪费可倒的空间)——这就是这一题的贪心策略。实施这一策略时,要注意if条件语句的顺序。树什么时候能倒、什么时候不能倒?如果该树倒下后占据的区间覆盖到了该树左侧或右侧(取决于向左倒还是向右倒)的树,那肯定不能倒。同时,如果该树倒下后占据的区间的最左端或最右端(也就是倒下的树的树顶)恰好在已经倒下的树所覆盖的区间内,这棵树也不能倒。反之,这棵树就可以倒下。至于已经倒下的树所覆盖的区间,有新的树倒下,就用新的倒下的树覆盖的区间更新已经倒下的树所覆盖的区间,只要维护该区间的两个端点值就可以了。

代码

n=int(input())
xs=[]
hs=[]
for _ in range(n):
    x,h=map(int,input().split())
    xs.append(x)
    hs.append(h)
t=1
if n>1:
    t=2
occupies=[xs[0]-hs[0],xs[0]]
for i in range(1,n-1):
    if xs[i]-hs[i]>xs[i-1] and (not (occupies[0]<=xs[i]-hs[i]<=occupies[1])):
        t+=1
        occupies=[xs[i]-hs[i],xs[i]]
    elif xs[i]+hs[i]<xs[i+1] and (not (occupies[0]<=xs[i]+hs[i]<=occupies[1])):
        t += 1
        occupies=[xs[i],xs[i]+hs[i]]

print(t)
n=int(input())
xs=[]
hs=[]
for _ in range(n):
    x,h=map(int,input().split())
    xs.append(x)
    hs.append(h)
t=1
if n>1:
    t=2
# occupies=[xs[0]-hs[0],xs[0]]
for i in range(1,n-1):
    if xs[i]-hs[i]>xs[i-1]:
        t+=1
        # occupies=[xs[i]-hs[i],xs[i]]
    elif xs[i]+hs[i]<xs[i+1]:
        t += 1
        xs[i]=xs[i]+hs[i]   # 更新xi的值,只有在树向右倒时才需要这样做
        # occupies=[xs[i],xs[i]+hs[i]]

print(t)

Logo

集算法之大成!助力oier实现梦想!

更多推荐