CF 462 C. A Twisty Movement 分段想 线段树 或 dp
题意
有一个只包含1和2的序列,试翻转一个区间,使得结果中非连续非递减数列最长。
思路
一、
作出1的前缀计数和为cnt1,2的后缀计数和为cnt2, 由于要找出【1,1,1】【2,2,2】【1,1,1】【2,2,2】的四段,设中间的分割点是p,k,q,可得到
ans=cnt1[p]+cnt2[p+1]−cnt2[k+1]+cnt1[q]−cnt1[k]+cnt2[q+1]ans=cnt1[p]+cnt2[p+1]−cnt2[k+1]+cnt1[q]−cnt1[k]+cnt2[q+1]
化简得到
ans=cnt1[p]+cnt2[p+1]+cnt1[q]+cnt2[q+1]−cnt1[k]−cnt2[k+1]ans=cnt1[p]+cnt2[p+1]+cnt1[q]+cnt2[q+1]−cnt1[k]−cnt2[k+1]
所以我们可以枚举k, 用线段树维护cnt1【i】 + cnt2【i+1】 的最大值,我这样的公式,需要线段树区间为【0,n】。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/
const int maxn = ;
int a[maxn],cnt1[maxn],cnt2[maxn];
int t[maxn<<]; void build(int l,int r,int rt){
if(l == r){
t[rt] = cnt1[l] + cnt2[l+];
return ;
}
int mid = (l + r) >> ;
build(l, mid, rt<<);
build(mid+,r,rt<<|);
t[rt] = max(t[rt<<], t[rt<<|]);
}
int query(int L,int R,int l,int r,int rt){
if(l >= L && r <= R){
return t[rt];
}
int mx = ;
int mid = (l + r) >> ;
if(mid >= L) mx = max(mx, query(L, R, l, mid, rt<<));
if(mid < R) mx = max(mx, query(L, R, mid+, r, rt<<|));
return mx;
}
int main(){
int n; scanf("%d", &n);
rep(i, , n) scanf("%d", &a[i]); rep(i, , n){
if(a[i] == ) cnt1[i] = cnt1[i-] + ;
else cnt1[i] = cnt1[i-];
}
for(int i=n; i>=; i--) {
if(a[i] == ) cnt2[i] = cnt2[i+] + ;
else cnt2[i] = cnt2[i+];
} build(, n, );
int ans = ;
for(int i=; i<=n; i++){
int tmp = query(,i ,,n,) + query(i,n,,n,) - cnt1[i] - cnt2[i+];
ans = max(ans, tmp);
}
cout<<ans<<endl;
return ;
}
二、
这道题中分四段是关键,我们可以定义dp【i】【1】表示1~i个分一段的答案,dp【i】【2】表示分两段,dp【i】【3】表示分三段,dp【i】【4】表示分四段。
/*-----------------------showtime----------------------*/ int dp[];
int main(){
int n; scanf("%d", &n);
rep(i, , n) {
int x; scanf("%d", &x);
dp[] = dp[] + (x == );
dp[] = max(dp[], dp[] + (x==));
dp[] = max(dp[], dp[] + (x==));
dp[] = max(dp[], dp[] + (x==));
}
cout<<dp[]<<endl; }
CF 462 C. A Twisty Movement 分段想 线段树 或 dp的更多相关文章
- (困难) CF 484E Sign on Fence,整体二分+线段树
Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...
- CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset
题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...
- CF E2 - Array and Segments (Hard version) (线段树)
题意给定一个长度为n的序列,和m个区间.对一个区间的操作是:对整个区间的数-1可以选择任意个区间(可以为0个.每个区间最多被选择一次)进行操作后,要求最大化的序列极差(极差即最大值 - 最小值).ea ...
- CF R638 div2 F Phoenix and Memory 贪心 线段树 构造 Hall定理
LINK:Phoenix and Memory 这场比赛标题好评 都是以凤凰这个单词开头的 有凤来仪吧. 其实和Hall定理关系不大. 不过这个定理有的时候会由于 先简述一下. 对于一张二分图 左边集 ...
- 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 ...
- CF#462 div1 D:A Creative Cutout
CF#462 div1 D:A Creative Cutout 题目大意: 原网址戳我! 题目大意: 在网格上任选一个点作为圆中心,然后以其为圆心画\(m\)个圆. 其中第\(k\)个圆的半径为\(\ ...
- Codeforces 934C - A Twisty Movement
934C - A Twisty Movement 思路:dp 很容易想到要预处理出1的前缀和pre[i]和2的后缀和suf[i] 然后枚举区间,对于每个区间如果能求出最长递减序列的长度,那么就能更新答 ...
- Codeforces 934.C A Twisty Movement
C. A Twisty Movement time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF933A A Twisty Movement
题意翻译 给定一个序列 A,你可以翻转其中的一个区间内的数,求翻转后的序列的最长不下降子序列的长度.(∣A∣≤2000,1≤ai≤2|A|\le 2000,1\le a_i \le 2∣A∣≤2000 ...
随机推荐
- python 感悟
* 优美胜于丑陋.* 显式胜于隐式.* 简单胜于复杂.* 复杂胜于难懂.* 扁平胜于嵌套.* 稀疏胜于紧密.* 可读性应当被重视.* 尽管实用性会打败纯粹性,特例也不能凌驾于规则之上.* 不要忽略任何 ...
- spring 的权限控制:security
下面我们将实现关于Spring Security3的一系列教程. 最终的目标是整合Spring Security + Spring3MVC 完成类似于SpringSide3中mini-web的功能. ...
- request获取url链接和参数
//Returns the part of this request's URL from the protocol name up to the query string in th ...
- MySQL操作命令梳理(2)
一.表操作 在mysql运维操作中会经常使用到alter这个修改表的命令,alter tables允许修改一个现有表的结构,比如增加或删除列.创造或消去索引.改变现有列的类型.或重新命名列或表本身,也 ...
- 为什么建立数据仓库需要使用ETL工具?
在做项目时是不是时常让客户有这样的困扰: 1.开发时间太长 2.花费太多 3.需要太多资源 4.集成多个事务系统数据总是需要大量人力成本 5.找不到合适的技能和经验的人 6.一旦建立,数据仓库无法足够 ...
- Mybatis学习笔记之---动态sql中标签的使用
动态Sql语句中标签的使用 (一)常用标签 1.<if> if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段 ...
- ABAP 查看采购订单行项目已开票金额和已清金额
FUNCTION zmm_fm_po_invence. *"----------------------------------------------------------------- ...
- C#连接sqlserver分页查询的两个简单的方法
/// <summary> /// 分页查询函数 /// </summary> /// <param name="co ...
- .net软件开发脚本规范-JS标准
一. JS标准 新增页面表单检查方法名称固定为checkForm. 查询页面表单检查方法名称固定为checkSearchForm. 检查表单方法checkForm与checkSearchForm固定放 ...
- Springboot 优雅停止服务的几种方法
在使用Springboot的时候,都要涉及到服务的停止和启动,当我们停止服务的时候,很多时候大家都是kill -9 直接把程序进程杀掉,这样程序不会执行优雅的关闭.而且一些没有执行完的程序就会直接退出 ...