题意

有一个只包含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的更多相关文章

  1. (困难) CF 484E Sign on Fence,整体二分+线段树

    Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence o ...

  2. CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset

    题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...

  3. CF E2 - Array and Segments (Hard version) (线段树)

    题意给定一个长度为n的序列,和m个区间.对一个区间的操作是:对整个区间的数-1可以选择任意个区间(可以为0个.每个区间最多被选择一次)进行操作后,要求最大化的序列极差(极差即最大值 - 最小值).ea ...

  4. CF R638 div2 F Phoenix and Memory 贪心 线段树 构造 Hall定理

    LINK:Phoenix and Memory 这场比赛标题好评 都是以凤凰这个单词开头的 有凤来仪吧. 其实和Hall定理关系不大. 不过这个定理有的时候会由于 先简述一下. 对于一张二分图 左边集 ...

  5. 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 ...

  6. CF#462 div1 D:A Creative Cutout

    CF#462 div1 D:A Creative Cutout 题目大意: 原网址戳我! 题目大意: 在网格上任选一个点作为圆中心,然后以其为圆心画\(m\)个圆. 其中第\(k\)个圆的半径为\(\ ...

  7. Codeforces 934C - A Twisty Movement

    934C - A Twisty Movement 思路:dp 很容易想到要预处理出1的前缀和pre[i]和2的后缀和suf[i] 然后枚举区间,对于每个区间如果能求出最长递减序列的长度,那么就能更新答 ...

  8. Codeforces 934.C A Twisty Movement

    C. A Twisty Movement time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. CF933A A Twisty Movement

    题意翻译 给定一个序列 A,你可以翻转其中的一个区间内的数,求翻转后的序列的最长不下降子序列的长度.(∣A∣≤2000,1≤ai≤2|A|\le 2000,1\le a_i \le 2∣A∣≤2000 ...

随机推荐

  1. android 界面提示框架WisdomProgressHUD,为金典而生

    一:简述 今天给android开发者们,推荐一个金典的界面提示框架WisdomProgressHUD,使用简洁方便. WisdomProgressHUD 是一个半透明的 HUD 指示器. Wisdom ...

  2. maven私服nexus上传第三方jar包以及下载

    私服是一个特殊的远程仓库,它是架设在局域网内的仓库服务.私服代理广域网上的远程仓库,供局域网内的Maven用户使用.当Maven需要下载构建的使用,它先从私服请求,如果私服上没有的话,则从外部的远程仓 ...

  3. JVM内存结构 VS Java内存模型 VS Java对象模型

    前面几篇文章中, 系统的学习了下JVM内存结构.Java内存模型.Java对象模型, 但是发现自己还是对这三者的概念和区别比较模糊, 傻傻分不清楚.所以就有了这篇文章, 本文主要是对这三个技术点再做一 ...

  4. MySQL操作命令梳理(1)

    一.索引 1.创建索引 索引的创建可以在CREATE TABLE语句中进行,也可以单独用CREATE INDEX或ALTER TABLE来给表增加索引.以下命令语句分别展示了如何创建主键索引(PRIM ...

  5. 关于input[type='checkbox']全选的问题

    今天在做一个全选功能的时候,发现了一个问题,就是如果我在选择全选之前,我就已经选择了一个input,然后我再去选择全选并且以后再取消全选的时候,这个我之前选择的input始终处于选择状态,但是他的ch ...

  6. git的使用学习笔记

    一.git Git 是一个开源的分布式版本控制系统,项目版本管理工具,可以在本地提交修改再合并到主分支上,最为出色的是它的合并跟踪(merge tracing)能力. 可以通过Linux命令进行增加, ...

  7. LeetCode——372. Super Pow

    题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...

  8. python基础--列表,元组

    list1 = [1,2,3,4,5,6]list2 = ['wang','cong']# 1对列表中的元素取值(通过索引)print(list1[3]) # 4print(list2[1]) # c ...

  9. 在Linux和Windows系统中输出目录结构

    前言 一直以来就想在写文章时,能以文本形式(而不是截图)附上项目的目录结构,今天终于知道怎么操作了,在这分享一下. Linux 首先说下Linux上输出目录结构的方法. yum安装tree 需要支持t ...

  10. Spring AOP JDK动态代理与CGLib动态代理区别

    静态代理与动态代理 静态代理 代理模式 (1)代理模式是常用设计模式的一种,我们在软件设计时常用的代理一般是指静态代理,也就是在代码中显式指定的代理. (2)静态代理由 业务实现类.业务代理类 两部分 ...