题意: 砍树, 树会向左或者向右倒,数不能倒重叠, 问最多可以砍多少树
思路: 贪心 + Dp吧, 树要尽可能网左倒,这样对后面的树影响较小, 才是最优状态
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1e5 + 131; struct Tree { int pos, val; int MaxPos; }T[maxn]; int Dp[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; memset(Dp,0,sizeof(Dp)); for(int i = 1; i <= n; ++i) { cin >> T[i].pos >> T[i].val; T[i].MaxPos = T[i].pos; } Dp[1] =1; if(n == 1) cout << 1 <<endl; else if(n == 2) cout <<2 << endl; else { for(int i = 2; i < n; ++i) { int P = T[i].pos, H = T[i].val; int pre = T[i-1].MaxPos; int l = Dp[i-1] , r = Dp[i-1]; if(P + H < T[i+1].pos) r++ , T[i].MaxPos = P+H; if(P - H > pre) l++ , T[i].MaxPos = P; Dp[i] = max(Dp[i-1], max(l, r) ); } Dp[n] = Dp[n-1] + 1; cout << Dp[n] << endl; } }
时间: 2024-10-06 15:56:34