Loading

【dp每日一题】CF545C. Woodcutters

大意:

给出n个树的位置(递增),以及每个树的高度,工人可以将树向左砍倒,也可以向右砍倒,但是不能压到其他的树(无论是砍了还是没被砍的树都不能被压到),问最多能砍倒多少树

思路:

一开始想复杂了,只需要根据类似贪心的思想,首先判断能不能向左砍,如果不能,就判断能否向右砍,如果能的话更新一下\(a[i]+=h[i]\),这样可以保证是最优的,因为优先向左砍,这样砍树是最优的

#include<bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;
typedef long long LL;
LL n,a[N],h[N],res;
int main(){
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> a[i] >> h[i];
    }
    for (int i = 1; i <= n;i++){
        if(i==1||i==n)
            res++;
        else if(a[i-1]<a[i]-h[i])
            res++;
        else if(a[i]+h[i]<a[i+1]){
            res++;
            a[i] += h[i];
        }
    }
    cout << res << endl;
    return 0;
}
posted @ 2020-12-02 15:50  WinterFa1L  阅读(100)  评论(0)    收藏  举报