【Codeforces Round #462 (Div. 1) A】 A Twisty Movement
【链接】  我是链接,点我呀:) 
 【题意】
在这里输入题意
【题解】
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的更多相关文章
- 【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 用多项式除法除 ...
 - Codeforces Round #462 (Div. 2), problem: (C) A Twisty Movement (求可以转一次区间的不递增子序列元素只有1,2)
		
题目意思: 给长度为n(n<=2000)的数字串,数字只能为1或者2,可以将其中一段区间[l,r]翻转,求翻转后的最长非递减子序列长度. 题解:求出1的前缀和,2的后缀和,以及区间[i,j]的最 ...
 - 【Codeforces Round #432 (Div. 1) B】Arpa and a list of numbers
		
[链接]h在这里写链接 [题意] 定义bad list是一个非空的.最大公约数为1的序列.给定一个序列,有两种操作:花费x将一个元素删除.花费y将一个元素加1,问你将这个序列变为good list所需 ...
 - 【Codeforces Round #420 (Div. 2) C】Okabe and Boxes
		
[题目链接]:http://codeforces.com/contest/821/problem/C [题意] 给你2*n个操作; 包括把1..n中的某一个数压入栈顶,以及把栈顶元素弹出; 保证压入和 ...
 - 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
		
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
 - 【Codeforces Round #420 (Div. 2) A】Okabe and Future Gadget Laboratory
		
[题目链接]:http://codeforces.com/contest/821/problem/A [题意] 给你一个n*n的数组; 然后问你,是不是每个位置(x,y); 都能找到一个同一行的元素q ...
 - 【Codeforces Round #423 (Div. 2) C】String Reconstruction
		
[Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...
 - 【Codeforces Round #423 (Div. 2) B】Black Square
		
[Link]:http://codeforces.com/contest/828/problem/B [Description] 给你一个n*m的格子; 里面包含B和W两种颜色的格子; 让你在这个格子 ...
 - 【Codeforces Round #423 (Div. 2) A】Restaurant Tables
		
[Link]:http://codeforces.com/contest/828/problem/A [Description] 有n个组按照时间顺序来餐馆; 每个组由一个人或两个人组成; 每当有一个 ...
 
随机推荐
- POJ 3264 Balanced Lineup【线段树】
			
题意:给出n个数,a1,a2,a3,---,an,再给出q次询问区间al到ar之间的最大值和最小值的差 学习线段树的第一道题目 学习的这一篇 http://www.cnblogs.com/kuangb ...
 - 重设数据文件大小sql语句
			
set verify off column file_name format a50 word_wrapped column smallest format 999,990 heading " ...
 - 关于VS2013 Browser Link 新功能
			
今天小明在编写MVC项目,设计前端项目的时候,突然遇到一个问题,就是无论什么页面,当每次执行调试的时候,页面中都会自动的加上这样一行代码: <!-- Visual Studio Browser ...
 - 【jQuery05】通过按键 来切换 class
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - Axios 使用时遇到的问题
			
最近使用 vue 构建一个小项目,在使用 axios 发送 post 请求的时候,发现 axios 发送数据默认使用 json 格式,百度搜了下,更改 ContentType 不管用,最终问题原来是: ...
 - python学习(二):基本数据类型:整型,字符型
			
整型: type():显示数据类型 # 整型,int # python3里,不管数字有多大,都是int类型 # python2里,有大小区分,长整型:long int a = " print ...
 - [luogu] P4514 上帝造题的七分钟 (树状数组,二维差分)
			
P4514 上帝造题的七分钟 题目背景 裸体就意味着身体. 题目描述 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a ...
 - 2015 Multi-University Training Contest 3 hdu 5326 Work
			
Work Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
 - LibSVM-windows
			
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50112477 官方Web: https ...
 - 华夏60 战斗机(最短路dijkstra)
			
华夏60 战斗机(最短路dijkstra) 华夏60 超音速战斗机是当今世界上机动性能最先进的战斗机.战斗过程中的一个关键问题是如何在最短的时间内使飞机从当前的飞行高度和速度爬升/俯冲到指定的高度并达 ...