原题链接
- 题意:一棵树,有高度,在一维平面上,可以向左向右砍到,不允许重叠,然后求最大砍倒树的数量。
- 题解:从左往右的话,就现往左倒,如果不可以往左倒,就往右倒。
- 代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
struct node {
ll pos, h, l, r;
}p[N];
int dp[N][4];
int main() {
int n;cin >> n;
for (int i = 1; i <= n ;i ++) {
cin >> p[i].pos >> p[i].h;
p[i].l = p[i].pos - p[i].h;
p[i].r = p[i].pos + p[i].h;
}
int now = 0;
for (int i = 1; i <= n; i ++) {
if (i == 1 || i == n){
now++;
continue;
}
if (p[i].l > p[i-1].pos) {
now++;
} else {
if (p[i].r < p[i + 1].pos) {
now++;
p[i].pos = p[i].r;
}
}
}
cout << now << endl;
return 0;
}