1. 程式人生 > 其它 >DP專輯-Codeforces-1575L - Longest Array Deconstruction

DP專輯-Codeforces-1575L - Longest Array Deconstruction

題意:給個數組,可以進行無限次操作,每次操作可以去掉其中任意個數,求得最大得值 a[i] ==i

思路:要考慮DP得順序,這也是題目的亮點, 定義每個索引 i,其值為a[i], 定義res為 i -a[i],我們從小到大列舉這個res,進行DP。

即先列舉移動次數,再從小大大列舉i進行dp,我們可以用樹狀陣列log 求字首最大值,這種我們可以看到列舉的情況必然是合法的,不會多算,也不會少算,細節請看程式碼:

#include<bits/stdc++.h>
using namespace std;
int c[200005];
set<int>s[200005];
int n;
void
update(int x,int y) { for(; x<=n+2; x+=x&-x) c[x]=max(c[x],y); } int ask(int x) { int ans =0; for(; x; x-=x&-x) ans =max(ans,c[x]); return ans; } int main() { cin>>n; for(int i =1 ; i<=n; i++) { int x; cin>>x;
if(i-x>=0)s[i-x].insert(x);//相同res放一起 } int ans =0; for(int i =0; i<=n; i++) { for(auto x:s[i]) {//DP ans = ask(x-1);//小於等於x update(x,ans+1); } } cout<<ask(n)<<endl; return 0; }