题意

有一个只包含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. 最新try2hack全详解

    第一题http://www.try2hack.nl/levels/: 方法:直接右键看网页源码 第二题http://www.try2hack.nl/levels/level2-xfdgnh.xhtml ...

  2. Android Studio 蓝牙开发实例——基于Android 6.0

    因项目需要做一个Android 的蓝牙app来通过手机蓝牙传输数据以及控制飞行器,在此,我对这段时间里写的蓝牙app的代码进行知识梳理和出现错误的总结. 该应用的Compile Sdk Version ...

  3. git项目版本处理--远程分支重新拉取本地代码如何处理

    最近在eclipse 上用git拉取分支,提交代码因为提交代码提交了一些配置文件造成 后续同事提交代码一直出现代码冲突问题 项目老大又重新拉取了一条代码 同样的分支名字 当时有点蒙不知道接着怎么弄:场 ...

  4. 夯实Java基础(六)——包装类

    1.包装类简介 我们都知道Java是面向对象编程语言,包含了8种基本数据类型,但是这8种基本数据类型并不支持面向对象的特征,它们既不是类,也不能调用方法.这在实际使用时存在很多的不便,比如int类型需 ...

  5. 微服务SpringCloud之Spring Cloud Config配置中心SVN

    在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本 ...

  6. 单元测试python unittest

    记录自己学习单元测试框架的一篇博客 菜鸟的学习之路比较艰辛到处找资料一把辛酸泪啊 1.首先是创建一个类里面设计一些简单的函数方便写用例: 原谅我蹩脚的英文直接用拼音命名了 : 2.接着就是创建用例文件 ...

  7. Python基础总结之初步认识---class类的继承(下)。第十五天开始(新手可相互督促

    年薪百万的步伐慢了两天hhhh严格意义是三天.最近买了新的玩具,在家玩玩玩!~~~~ 今天开始正式认识类的继承.类的继承是怎么继承呢?看下代码: class Animal(object): #父类 d ...

  8. 【POJ - 3176】牛保龄球 (简单dp)

    牛保龄球 直接中文了 Descriptions 奶牛打保龄球时不使用实际的保龄球.它们各自取一个数字(在0..99范围内),然后排成一个标准的保龄球状三角形,如下所示: 7 3 8 8 1 0 2 7 ...

  9. 算法与数据结构基础 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  10. 上手mongodb

    上手MongoDB MongoDB 是一个跨平台的,面向文档的数据库,如果你了解spring-data-jpa的使用, 那么恭喜你,你已经可以使用mongodb做开发了 使用这种类型的数据库还是挺方便 ...