CF545C Woodcutters

原题链接

  • 题意:一棵树,有高度,在一维平面上,可以向左向右砍到,不允许重叠,然后求最大砍倒树的数量。
  • 题解:从左往右的话,就现往左倒,如果不可以往左倒,就往右倒。
  • 代码:
#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;
}
posted @ 2021-05-07 12:01  u_yan  阅读(139)  评论(0编辑  收藏  举报