传送门
文章目录
- 题意:
- 思路:
题意:
给你一个长度为nnn的只包含1,21,21,2的序列aaa,你可以至多翻转一段区间,求翻转之后最长非递减子序列是多长。
思路:
考虑如果翻转的话,翻转的子区间肯定是222211112222111122221111这种类型的,再加上前面可能有111,后面可能有222,那么我们求的最长子序列的类型一定是类似于111222111222111222111222111222111222,所以我们只需要求一个类似于这种的序列最长是多少即可。
定义aaa为111111111类型的最长长度,bbb为112211221122类型的最长长度,ccc为112211112211112211类型的最长长度,ddd为112211221122112211221122类型的最长长度。
如果当前为111,那么a=a+1,c=max(c+1,b+1)a=a+1,c=max(c+1,b+1)a=a+1,c=max(c+1,b+1)。
如果当前为222,那么b=max(b+1,a+1),d=max(d+1,c+1)b=max(b+1,a+1),d=max(d+1,c+1)b=max(b+1,a+1),d=max(d+1,c+1)。
直接输入的时候转移即可。
// Problem: C. A Twisty Movement
// Contest: Codeforces - Codeforces Round #462 (Div. 2)
// URL: https://codeforces.com/contest/934/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int x,y,z,w;x=y=z=w=0;scanf("%d",&n);for(int i=1;i<=n;i++) {int now; scanf("%d",&now);if(now==1) {x=x+1;z=max(z+1,y+1);}else {y=max(y+1,x+1);w=max(w+1,z+1);}}printf("%d\n",max(z,w));return 0;
}
/**/