1. 程式人生 > >【Codeforces Round #462 (Div. 1) A】 A Twisty Movement

【Codeforces Round #462 (Div. 1) A】 A Twisty Movement

syn mark com [1] move blog ont contest 接下來

【鏈接】 我是鏈接,點我呀:)
【題意】


在這裏輸入題意

【題解】


ans初值值為a[1..n]中1的個數。
接下來考慮以2為結尾的最長上升子序列的個數。

枚舉中間點i.
計算1..i-1中1的個數cnt1。
計算i..n中2的個數cnt2。
ans = max(ans,cnt1+cnt2)
寫個前綴和

翻轉。
斷點在l..r中
f[l][r]表示l..r翻轉後以2結尾的最長上升子序列
簡單DP

ans = max(ans,cnt[l-1][1]+f[l][r]+cnt[n][2]-cnt[r][2]);

【代碼】

#include <bits/stdc++.h>
using namespace std; const int N = 2000; int pre[N+10][3],n,a[N+10],f[N+10][N+10]; int main(){ #ifdef LOCAL_DEFINE freopen("rush_in.txt", "r", stdin); #endif ios::sync_with_stdio(0),cin.tie(0); cin >> n; for (int i = 1;i <= n;i++) cin >> a[i]; for
(int i = 1;i <= n;i++){ for (int j = 1;j <= 2;j++) pre[i][j] = pre[i-1][j]; pre[i][a[i]]++; } int ans = pre[n][1]; for (int i = 1;i <= n;i++){ ans = max(ans,pre[i-1][1]+pre[n][2]-pre[i-1][2]); } for (int i = n;i >= 1;i--) for
(int j = i;j >= 1;j--){ f[j][i] = f[j+1][i]+(a[j]==2); f[j][i] = max(f[j][i],pre[i][1]-pre[j][1]+(a[j]==2)); } for (int i = 1;i <= n;i++) for (int j = i;j <= n;j++){ ans = max(ans,pre[i-1][1]+f[i][j]+pre[n][2]-pre[j][2]); } cout<<ans<<endl; return 0; }

【Codeforces Round #462 (Div. 1) A】 A Twisty Movement