【Codeforces 933A】A Twisty Movement
【链接】 我是链接,点我呀:)
【题意】
【题解】
因为只有1和2.
所以最后肯定是若干个1接着若干个2的情况。
即11...11222...222这样的。
1.首先考虑没有翻转的情况。
那么就直接枚举1和2的边界i在什么地方,即1..i全是1,i+1..n全是2.
只需统计某个范围里面1和2的个数就可以了。
然后取最大值就ok.
2.考虑有翻转的情况.
假设翻转的区间是[i..j]
我们最好先固定j然后让i从j递减到1这样变化.
这样的话我们可以利用[i+1,j]这一段得到的东西继续搞,不用每次都重新算.
(注意倒过来以后相当于j是开始位置
我们可以对于这段区间维护两个变量
int pre1max = 0,pre2max = 0;
他们俩分别表示[i..j]这一段倒过来以后,以1作为结尾的LIS的长度和以2作为结尾的LIS的长度.
然后遇到了1的话,pre1max=pre1max+1.(因为只能在前面的1后面再接一个1
如果遇到了2的话,pre2max=max(pre2max,pre1max)+1;(表示既能在2后面接一个2也能在1后面接一个2).
然后对于每一段的i..j求出来一个pre1max和pre2max之后,都
用"1到i-1里面1的个数"+pre2max+"j+1到n里面2的个数"来更新最大值
(注意这个翻转肯定只能包括上面说的1和2的边界处翻转才有效果,否则如果是在11111..11这一段或者在22222..22这一段翻转的话,显然不会影响答案.
(这种情况就相当于没有翻转的情况了.
【代码】
#include <bits/stdc++.h>
using namespace std;
const int N = 2000;
int n;
int a[N+10];
int pre[N+10][3];
int getsum(int p,int l,int r){
if (l==0) return pre[r][p];
if (l>r) return 0;
return pre[r][p]-pre[l-1][p];
}
int main()
{
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 = 0+getsum(2,1,n);
for (int i = 1;i <= n;i++)
ans = max(ans,getsum(1,1,i)+getsum(2,i+1,n));
for (int j = n;j >= 1;j--){
int pre1max = 0,pre2max = 0;
if (a[j]==1) pre1max = 1;else pre2max = 1;
for (int i = j-1;i >= 1;i--){
if (a[i]==1){
pre1max++;
}else{
pre2max=max(pre2max,pre1max)+1;
}
ans = max(ans,getsum(1,1,i-1)+pre2max+getsum(2,j+1,n));
}
}
cout<<ans<<endl;
return 0;
}
【Codeforces 933A】A Twisty Movement的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 750D】New Year and Fireworks
time limit per test2.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- Swift版本UIWebView长按保存图片
起因 最近需要做个IOS的壳子,用到长按保存图片的功能,发现百度出来的全是OC语法的例子,很多都不是全面,只能自己写一份Swift版本的,图片下面附上Github地址 效果图 Github地址:htt ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- Java中的锁概念
掌握Java中锁是Java多线程编程中绕不开的知识,只有知道理解Java各种锁才能在编码过程中灵活运用,写出更高效的多线程程序.而理解掌握锁的第一步,可从宏观上对比理解一下各种锁概念. 公平锁/非公平 ...
- codeforces round #420 div2
A:暴力枚举 模拟 #include<bits/stdc++.h> using namespace std; ; int n; int a[N][N]; int main() { scan ...
- (Go)09.指针赋值修改示例
答案: 1 package main 2 import ( 3 "fmt" 4 ) 5 6 7 func modify(p *int) { 8 fmt.Println(p) 9 ...
- E20170907-ts
flash vt. 使闪光,使闪烁; 拍出,发出(电报等); 〈口〉炫耀; adj. 闪光的,闪耀的,一闪而过的; 浮华的; 庞大的; n. 闪光; 闪光灯下摄 ...
- ul和li里面的list-style
对ul list-type 只是设置ul的样式对li list-type 是对li的综合样式设定 语法是 li-style:list-style-type/list-style-imag ...
- [Apple开发者帐户帮助]五、管理标识符(5)创建一个iCloud容器
您必须拥有一个或多个iCloud容器才能启用iCloud. 所需角色:帐户持有人或管理员. 在“ 证书”,“标识符和配置文件”中,从左侧的弹出菜单中选择操作系统. 在“标识符”下,选择“iCloud容 ...
- Oracle占用内存过高解决办法
1.cmd sqlplus system账户登录 2.show parameter sga; --显示内存分配情况 3.alter system set sga_max_size=200m scope ...
- Springboot 版本+ jdk 版本 + Maven 版本的对应关系
Spring boot 版本 Spring Framework jdk 版本 maven 版本 1.2.0 版本之前 6 3.0 1.2.0 4.1.3+ 6 3.2+ 1.2.1 4.1.3+ ...