【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个组按照时间顺序来餐馆; 每个组由一个人或两个人组成; 每当有一个 ...
随机推荐
- MyBatis数据持久化(三)增删改查
上篇文章中我们使用mybatis成功建立数据库会话,并从表中查询出相应的数据,本文在此基础上介绍MyBatis另外几种操作,即插入.修改.删除记录. 1.修改User.xml文件,增加几条sql语句: ...
- 【原创】Linux下的ngix服务器安装步骤
1.首先下载ngix的源码linux版本[1.5.8版本] http://nginx.org/en/download.html 2.下载PCRE library,是安装ngix的必备包之一 [root ...
- img标签IE下有边距——2017/7/21
设置css 在全局变量的是和给img标签设置 img{ border:0;} 1,img{float:left}2,img{display:block}
- 使用js获取url中的get参数并转成json格式
写在前面的 没啥说的 上代码 思路就是先获取到?后面的参数区,然后 利用字符串转数组方法获取到各个参数 var json = {}; var url = 'https://www.baidu.com/ ...
- BZOJ 4668 冷战(按秩合并并查集+LCA)
4668: 冷战 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 627 Solved: 303[Submit][Status][Discuss] D ...
- 【Paper Reading】Bayesian Face Sketch Synthesis
Contribution: 1) Systematic interpretation to existing face sketch synthesis methods. 2) Bayesian fa ...
- win系统安装node出现这个2503和2502解决办法
一: 今天在公司的新电脑要安装appium,所以要搭建appium的环境,所以在安装到node的时候,出现了内部错误2503和2502,安装中断. 这种错误可能是权限不足导致,一般“.exe”程序可以 ...
- POJ 3155 Hard Life
Hard Life Time Limit: 8000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- LeetCode 之 Merge Sorted Array(排序)
[问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
- 在jqueryEasyUI界面将时间以日期加时分秒的格式显示
问题描写叙述: oracle 10G中用户表有一个字段是日期型.数据格式为yyyy-MM-dd HH:mm:ss,前端显示时仅仅能显示成yyyy-MM-dd 后面的 HH:mm:ss不显示. 经过一番 ...