题意

有一个只包含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. 2.2.2python的BeautifulSoup库

    from bs4 import BeautifulSoupimport rebroken_html = '<ul class="country"><li>A ...

  2. Chrome 使用 Evernote 插件

    Chrome 插件不能登印象笔记进行裁剪,被困扰有段时间了.昨天偶然在知乎上找到了解决方法: 链接:https://www.zhihu.com/question/20340803/answer/291 ...

  3. Bean Validation完结篇:你必须关注的边边角角(约束级联、自定义约束、自定义校验器、国际化失败消息...)

    每篇一句 没有任何技术方案会是一种银弹,任何东西都是有利弊的 相关阅读 [小家Java]深入了解数据校验:Java Bean Validation 2.0(JSR303.JSR349.JSR380)H ...

  4. 【有容云干货-容器系列】Kubernetes调度核心解密:从Google Borg说起

    在之前“容器生态圈脑图大放送”文章中我们根据容器生态圈脑图,从下至上从左至右,依次介绍了容器生态圈中8个组件,其中也提到Kubernetes ,是一个以 Google Borg 为原型的开源项目.可实 ...

  5. Java悲观锁Pessimistic-Lock常用实现场景

    1:商品库存秒杀采用悲观锁Pessimistic-Lock主要好处是安全,充分利用了数据库的性能来做的一种锁机制. 悲观锁的实现: (1)环境:mysql + jdbctemplate (2)商品表g ...

  6. RocketMq中网络通信之服务端

    一,Broker服务端入口(NettyServer端) 首先RocketMq网络通信采用的Netty通信.服务端主要集中在Broker中.我们先看一下Broker的启动类BrokerStartup 显 ...

  7. Go标准库--net/http学习

    Go中对网络的支持提供了标准库,net包提供了可移植的网络I/O接口,包括TCP/IP.UDP.域名解析和Unix域socket. http包提供了HTTP客户端和服务端的实现. 一般我们用http肯 ...

  8. ssh的执行流畅

    SSH运行流程 1. 服务器启动,创建Struts2的Filter控制器,创建Spring容器对象. 实例化Struts2控制器时,加载struts.xml,struts-default.xml,de ...

  9. iOS项目之多Targets和多环境配置

    项目中使用的同一套代码,但需要开发多个app,app中内容基本上相同,只有一些小小的区别,例如名称等等,每个app中又需要分开发环境(Dev).测试环境(Test).正式环境(Pro). 下面就开始搭 ...

  10. Multiple dex files define Lokhttp3/internal/wsWebSocketProtocol

    Multiple dex files define Lokhttp3/internal/wsWebSocketProtocol 老套路,先晒图 图一:如题,在编译打包时遇到了如上错误,很明显这是一个依 ...