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].
题意:n棵树位于一排 给你n棵树的坐标以及树高 砍树可以将树向左推倒或者向右推倒
有一个区间范围[xi - hi, xi] or [xi;xi + hi]. 但是不能有别的树在这个区间内(闭区间) 问最多能砍多少棵树
题解:dp 设置状态
dp[i][0] 当前位置的树不砍,前i棵树最多能砍多少棵
dp[i][1] 当前位置的树向左推倒,前i棵树最多能砍多少棵
dp[i][2] 当前位置的树向右推倒,前i棵树最多能砍多少棵
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 //#include<bits/stdc++.h> 8 #include<iostream> 9 #include<cstring> 10 #include<cstdio> 11 #include<map> 12 #include<algorithm> 13 #include<queue> 14 #include<cmath> 15 #define ll __int64 16 #define PI acos(-1.0) 17 #define mod 1000000007 18 using namespace std; 19 int n; 20 struct node 21 { 22 int x,h; 23 } N[100005]; 24 int dp[100005][3]; 25 int main() 26 { 27 scanf("%d",&n); 28 for(int i=1; i<=n; i++) 29 scanf("%d %d",&N[i].x,&N[i].h); 30 dp[1][1]=1; 31 if((N[1].x+N[1].h)<N[2].x) 32 dp[1][2]=1; 33 else 34 dp[1][2]=0; 35 dp[1][0]=0; 36 for(int i=2; i<n; i++) 37 { 38 if((N[i-1].x+N[i-1].h)<(N[i].x-N[i].h)) 39 dp[i][1]=max(dp[i-1][1],max(dp[i-1][0],dp[i-1][2]))+1; 40 else if((N[i].x-N[i].h)>N[i-1].x) 41 dp[i][1]=max(dp[i-1][1],dp[i-1][0])+1; 42 else 43 dp[i][1]=max(dp[i-1][1],max(dp[i-1][0],dp[i-1][2])); 44 if((N[i].x+N[i].h)<N[i+1].x) 45 dp[i][2]=max(dp[i-1][1],max(dp[i-1][0],dp[i-1][2]))+1; 46 else 47 dp[i][2]=max(dp[i-1][1],max(dp[i-1][0],dp[i-1][2])); 48 dp[i][0]=max(dp[i-1][1],max(dp[i-1][0],dp[i-1][2])); 49 } 50 cout<<max(dp[n-1][1],max(dp[n-1][0],dp[n-1][2]))+1<<endl; 51 }