【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

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的更多相关文章

  1. 【Codeforces Round #462 (Div. 1) B】A Determined Cleanup

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设\(设f(x)=a_d*x^{d}+a_{d-1}*x^{d-1}+...+a_1*x+a_0\) 用它去除x+k 用多项式除法除 ...

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

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

  3. 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers

    [链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...

  4. 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes

    [题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...

  5. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  6. 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory

    [题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...

  7. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

  8. 【Codeforces Round #423 (Div. 2) B】Black Square

    [Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...

  9. 【Codeforces Round #423 (Div. 2) A】Restaurant Tables

    [Link]:http://codeforces.com/contest/828/problem/A [Description] 有n个组按照时间顺序来餐馆; 每个组由一个人或两个人组成; 每当有一个 ...

随机推荐

  1. MyBatis数据持久化(二)建立数据库会话

    上篇文章中介绍了我们使用mybatis前的准备工作,并在程序中通过jdbc与mysql数据库成功建立连接,接下来介绍如何使用mybatis与数据库建立会话.我们需要以下几个步骤: 1.在build p ...

  2. Maven配置文件中配置指定JDK版本

    1. 在setting.xml文件中的<profiles>标签加入如下配置: <profile> <id>jdk-1.8</id> <activa ...

  3. HDU 3342 Legal or Not【拓扑排序】

    题意:给出n,m,人的编号为 0到n-1,再给出m个关系,问能不能够进行拓扑排序 #include<iostream> #include<cstdio> #include< ...

  4. FCC编程题之中级算法篇(下)

    介绍 本篇是"FCC编程题之中级算法篇"系列的最后一篇 这期完结后,下期开始写高级算法,每篇一题 目录 1. Smallest Common Multiple 2. Finders ...

  5. JAVA 程序生成jar包运行报错 Exception in thread "Thread-1" java.lang.NoClassDefFoundError: javax/xml/rpc 的解决方法

    最近开发支付宝生活缴费的项目,java程序要使用.NET 的WebService服务,后来正式部署出现这错误,网上查资料是少了一个“jaxrpc.jar”文件,但是我本地调试正常,最后是删除我目前导出 ...

  6. PID三种参数的理解

    来源:http://blog.gkong.com/liaochangchu_117560.ashx PID是比例.积分.微分的简称,PID控制的难点不是编程,而是控制器的参数整定.参数整定的关键是正确 ...

  7. 【codeforces 411B】Multi-core Processor

    [题目链接]:http://codeforces.com/problemset/problem/411/B [题意] 处理器有n个核;然后有k个存储单元; 有m轮工作;每轮工作都会给每个核确定一个数字 ...

  8. Java设置Client Socket链接Server超时时间

    Java设置Client Socket链接Server超时时间 学习了:http://blog.csdn.net/tterminator/article/details/52494141 http:/ ...

  9. [MST] Attach Behavior to mobx-state-tree Models Using Actions

    Models are not just a nifty feature for type checking. They enable you to attach behavior to your ac ...

  10. JS 去除字符串中的最后一个字符

    var str = 'Hello World!'; str = str.substr(0,str.length-1); alert(str);