CodeForces - 1551E Fixed Points(dp)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1551E Fixed Points(dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一個長度為 nnn 的序列,需要求出刪掉最少的數字,使得剩下的數字至少有 kkk 個位置滿足 a[i]=ia[i]=ia[i]=i 成立
題目分析:看完數據范圍不難想到 n2n^2n2 的 dp,然后轉移的話就是枚舉每個位置是否刪除,一開始是想 dp[i][j]dp[i][j]dp[i][j] 表示到了第 iii 個位置,已經有 jjj 個數滿足條件的最小操作數,答案自然就是 dp[n][k]dp[n][k]dp[n][k] 了,但是不好轉移,于是換種狀態:dp[i][j]dp[i][j]dp[i][j] 表示到了第 iii 個位置,前面刪除了 jjj 個數字后,滿足 a[i]=ia[i]=ia[i]=i 的最大數量
需要注意到的一個點是,如果 a[i]a[i]a[i] 想要回到位置 iii 的話,前面必須要有 a[i]?ia[i]-ia[i]?i 個數才行,根據這一點出發,轉移方程就很簡單了
代碼:
// Problem: E. Fixed Points // Contest: Codeforces - Codeforces Round #734 (Div. 3) // URL: https://codeforces.com/contest/1551/problem/E // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=2e3+100; int a[N],dp[N][N]; int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {int n,k;read(n),read(k);for(int i=1;i<=n;i++) {read(a[i]);}for(int i=0;i<=n;i++) {for(int j=0;j<=n;j++) {dp[i][j]=-inf;}}dp[0][0]=0;for(int i=1;i<=n;i++) {for(int j=0;j<=i;j++) {dp[i][j]=max(dp[i][j],dp[i-1][j]+(i-a[i]==j));//不刪第i個if(j>0) {//刪掉第i個dp[i][j]=max(dp[i][j],dp[i-1][j-1]); }}}int ans=-1;for(int i=0;i<=n;i++) {if(dp[n][i]>=k) {ans=i;break;}}cout<<ans<<endl;}return 0; }總結
以上是生活随笔為你收集整理的CodeForces - 1551E Fixed Points(dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tourist取模模板
- 下一篇: CodeForces - 1551F E