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 ...
随机推荐
- PIVOT内置函数实现行转列
PIVOT用于将列值旋转为列名(即行转列),PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P 完整语法: table_source PIVOT( 聚合函数(va ...
- 基于SpringBoot从零构建博客网站 - 新增创建、修改、删除专栏功能
守望博客是支持创建专栏的功能,即可以将一系列相关的文章归档到专栏中,方便用户管理和查阅文章.这里主要讲解专栏的创建.修改和删除功能,至于专栏还涉及其它的功能,例如关注专栏等后续会穿插着介绍. 1.创建 ...
- 二进制文件安装安装etcd
利用二进制文件安装安装etcd etcd组件作为一个高可用强一致性的服务发现存储仓库. etcd作为一个受到ZooKeeper与doozer启发而催生的项目,除了拥有与之类似的功能外,更专注于以下四点 ...
- VSTO之PowerPoint(PPT)插件开发常用API汇总
VSTO简介 VSTO(Visual Studio Tools for Office )是VBA的替代,使得开发Office应用程序更加简单,并且用VSTO来开发office应用程序可以使用Visua ...
- PID算法资料【视频+PDF介绍】
最近一直有网友看到我的博客后,加我好友,问我能不能给发一些PID的资料,今天找了一些资料放到百度网盘上,给大家下载: 视频资料 链接:https://pan.baidu.com/s/12_IlLgBI ...
- Gunicorn-Django部署
1. 简单部署 1. sudo pip3 install gunicorn 2. cd 到django项目中 sudo python3 manage.py migrate 3.启动服务:sudo py ...
- oracle-11g2下载安装笔记
一.下载链接地址 http://download.oracle.com/otn/nt/oracle11g/112010/win64_11gR2_database_1of2.zip http://dow ...
- Java编程基础阶段笔记 day 07 面向对象编程(上)
面向对象编程 笔记Notes 面向对象三条学习主线 面向过程 VS 面向对象 类和对象 创建对象例子 面向对象的内存分析 类的属性:成员变量 成员变量 VS 局部变量 类的方法 方法的重载 可变个 ...
- Handler 使用详解
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- 缓存的有效期和淘汰策略【Redis和其他缓存】【刘新宇】
缓存有效期与淘汰策略 有效期 TTL (Time to live) 设置有效期的作用: 节省空间 做到数据弱一致性,有效期失效后,可以保证数据的一致性 Redis的过期策略 过期策略通常有以下三种: ...