题目意思: 给长度为n(n<=2000)的数字串,数字只能为1或者2,可以将其中一段区间[l,r]翻转,求翻转后的最长非递减子序列长度。

题解:求出1的前缀和,2的后缀和,以及区间[i,j]的最长不递增子序列。

f[i][j][0]表示区间i-j以1结尾的最长不递增子序列;

f[i][j][1]表示区间i-j以2结尾的最长不递增子序列,显然是区间i-j 2的个数;

所以转移方程为:

f[i][j][1] = f[i][j-1][1] + (a[j]==2);
   f[i][j][0] = max(f[i][j-1][0], f[i][j-1][1]) + (a[j]==1);(1<=i<=n,i<=j<=n)

//#include"bits/stdc++.h"
#include <sstream>
#include <iomanip>
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"time.h"
#include"iostream"
#include"stdlib.h"
#include"algorithm"
#define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i,n) for(int i=0;i<n;i++)
const int N = 2e3 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
using namespace std;
int a[N];
int l[N],r[N];
int f[N][N][];
int main()
{
int n;
ci(n);
for(int i=;i<=n;i++) ci(a[i]),l[i]=l[i-]+(a[i]==);
for(int i=n;i>=;i--) r[i]=r[i+]+(a[i]==);
int ma=-;
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
f[i][j][]=f[i][j-][]+(a[j]==);
f[i][j][]=max(f[i][j-][],f[i][j-][])+(a[j]==);
ma=max(ma,f[i][j][]+l[i-]+r[j+]);
ma=max(ma,f[i][j][]+l[i-]+r[j+]);
}
}
pi(ma);
return ;
}

超强O(n) 解法

翻转后的合法子序列翻转前一定是 一坨1 + 一坨2 + 一坨1 + 一坨2形式,坨可以为空,于是可以用4个变量分别维护前1坨,前2坨,前3坨,前4坨的最大值,更新时每个变量也只用上一轮最多两个变量更新,比如当前元素为2,那么前两坨的最大值ma2 = max(ma1, ma2) + 1,ma4 = max(ma3, ma4) + 1。因为将2接到ma1或ma2后的序列都是合法的ma2形式。代码十分简短。时间复杂度O(n),空间复杂度O(1)。

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cctype>
#include <cassert>
#include <bitset>
#include <ctime> using namespace std; #define pau system("pause")
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define clr(a, x) memset(a, x, sizeof(a)) const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
const double EPS = 1e-; int ma1, ma2, ma3, ma4, x, n;
int main() {
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &x);
if ( == x) {
++ma1;
ma3 = max(ma3, ma2) + ;
} else {
ma2 = max(ma1, ma2) + ;
ma4 = max(ma4, ma3) + ;
}
}
printf("%d\n", max(ma3, ma4));
return ;
}

Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)的更多相关文章

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

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] ans初值值为a[1..n]中1的个数. 接下来考虑以2为结尾的最长上升子序列的个数. 枚举中间点i. 计算1..i-1中1的个数c ...

  2. Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维

    & -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...

  3. Codeforces Round #462 (Div. 2) C. A Twisty Movement

    C. A Twisty Movement time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  4. Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation

    还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com) 传送门  Problem - D - Codeforces 题意 n个数字,n ...

  5. Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读

    http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...

  6. Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分

    Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...

  7. Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学

    — This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...

  8. Codeforces Round #439 (Div. 2) Problem B (Codeforces 869B)

    Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...

  9. Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力

    Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...

随机推荐

  1. linux的deamon后台运行

    有的时候需要将程序一直跑在后台,比如一些服务类代码,或者一些监控类代码.使用deamon是正确的一种思路. 以前我们在看<unix环境高级编程>的时候,有专门的整章详细介绍如何编写一个后台 ...

  2. ROS编译:catkin简析

    博客转载自:https://blog.csdn.net/zyh821351004/article/details/50388429 Catkin tutorials: http://wiki.ros. ...

  3. 39.FORMAT() 函数

    FORMAT() 函数 FORMAT 函数用于对字段的显示进行格式化. SQL FORMAT() 语法 SELECT FORMAT(column_name,format) FROM table_nam ...

  4. js 遮罩层请稍后

    this.WaitMessage = function (msg) { $("<div class=\"datagrid-mask\"></div> ...

  5. 实践作业3:白盒测试---细化明确任务DAY5

    收到老师给我写的评论,感觉老师真的太认真,每个博客都有仔细的,参考了老师发给我的博客,我才明白老师想要的博客内容原来是具体实际的进展记录.我们组其实这些东西早就确定了,会议也开了,但是我之前不明白博客 ...

  6. js实现选项卡切换

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  7. 使用 Vue.component

    引入 vue.js. HTML <div id="app"></div> CSS .greeting { padding: 3rem 1.5rem; bac ...

  8. DataAnnotationsModelValidator-基于数据注解方式的model验证器

    http://www.cnblogs.com/artech/archive/2012/04/10/how-mvc-works.html http://www.cnblogs.com/artech/ar ...

  9. (转)初试konckout+webapi简单实现增删改查

    原文地址:http://www.cnblogs.com/flykai/p/3361064.html 前言 konckout.js本人也是刚刚接触,也是初学,本文的目的是使用ko和asp.net mvc ...

  10. JAVA的编码转换测试

    package test; import java.io.UnsupportedEncodingException; /** * * @author jim */ public class Test ...