题意:给定三个序列abc,问最少操作几次使得满足a<b<c

题解:将三个序列合并起来,设cnt[i][1/2/3]表示前i个数有几个是来自序列1/2/3的。

枚举第一个序列要到i,此时对于第一个序列的操作次数就是cnt[i][2]+cnt[i][3]+cnt[n][1]-cnt[i][1]

对于第二个序列,暴力枚举要到j,此时的操作次数就是cnt[j][3]-cnt[i][3]+cnt[n][2]-cnt[j][2]

将两个加起来就是答案,求出最小的那个

显然这样做是O(n^2)的,考虑优化

可以观察到,对于一个确定的i,cnt[i][3]是定值,将式子改写为cnt[j][3]+cnt[n][2]-cnt[j][2]-cnt[i][3],设f[i]=cnt[i][3]+cnt[n][2]-cnt[i][2],那么f[i]是一个确定的函数,式子进一步改写为f[j]-cnt[i][3]

相当于对于每个i,求一个最小的f[j]-cnt[i][3],用一个线段树即可

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int k1,k2,k3,a,n,ans=1e9;
int cnt[][];
struct node
{
int v,bh;
}q[];
bool cmp(const node &T1,const node &T2){return T1.v<T2.v;}
class Segtree
{
public:
int v[*],fl[*]; void pushup(int fxy)
{
v[fxy]=min(v[fxy<<],v[fxy<<|]);
}
void pushdown(int fxy)
{
if(fl[fxy])
{
v[fxy<<]+=fl[fxy];
v[fxy<<|]+=fl[fxy];
fl[fxy<<]+=fl[fxy];
fl[fxy<<|]+=fl[fxy];
fl[fxy]=;
}
}
void build(int l,int r,int fxy)
{
if(l==r)
{
v[fxy]=cnt[l][]+cnt[n][]-cnt[r][];
return;
}
int mid=l+r>>;
build(l,mid,fxy<<);
build(mid+,r,fxy<<|);
pushup(fxy);
}
void change(int l,int r,int al,int ar,int tv,int fxy)
{
if(l==al && r==ar)
{
v[fxy]+=tv;
fl[fxy]+=tv;
return;
}
pushdown(fxy);
int mid=l+r>>;
if(ar<=mid)change(l,mid,al,ar,tv,fxy<<);
if(al>mid)change(mid+,r,al,ar,tv,fxy<<|);
if(al<=mid && ar>mid)
{
change(l,mid,al,mid,tv,fxy<<);
change(mid+,r,mid+,ar,tv,fxy<<|);
}
pushup(fxy);
}
int ask(int l,int r,int al,int ar,int fxy)
{
if(l==al && r==ar)return v[fxy];
pushdown(fxy);
int mid=l+r>>;
if(ar<=mid)return ask(l,mid,al,ar,fxy<<);
if(al>mid)return ask(mid+,r,al,ar,fxy<<|);
return min(ask(l,mid,al,mid,fxy<<),ask(mid+,r,mid+,ar,fxy<<|));
}
}segtree;
int main()
{
scanf("%d%d%d",&k1,&k2,&k3);
for(int i=;i<=k1;i++){scanf("%d",&a);q[i]=(node){a,};}
for(int i=;i<=k2;i++){scanf("%d",&a);q[i+k1]=(node){a,};}
for(int i=;i<=k3;i++){scanf("%d",&a);q[i+k1+k2]=(node){a,};}
n=k1+k2+k3;
sort(q+,q++n,cmp);
for(int i=;i<=n;i++)
{
cnt[i][]=cnt[i-][]+(q[i].bh==);
cnt[i][]=cnt[i-][]+(q[i].bh==);
cnt[i][]=cnt[i-][]+(q[i].bh==);
}
int t;
segtree.build(,n,);
for(int i=;i<n;i++)
{
t=segtree.ask(,n,i+,n,);
t=min(t,cnt[n][]-cnt[i][]);
ans=min(ans,t+cnt[i][]+cnt[i][]+cnt[n][]-cnt[i][]);
if(q[i+].bh==)segtree.change(,n,i+,n,-,);
}
ans=min(ans,cnt[n][]+cnt[n][]);
printf("%d\n",ans);
return ;
}

【CF1257E】The Contest【线段树】的更多相关文章

  1. hdu 1698 线段树 区间更新 区间求和

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  4. Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树

    B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...

  5. AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图

    AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...

  6. UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  7. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

  8. HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)

    原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...

  9. The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...

随机推荐

  1. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_07 Collections工具类_3_Collections集合工具类的方法

    第二个参数传递了一个匿名内部类.结果就出现了下面的代码 源码里面有Compare方法,对比两个参数 要重写比较的方法 对对象进行排序 创建学生类.对学生类进行排序 重写Person的ToString方 ...

  2. SoapUI乱码问题处理方法

    前言 每个工具都会有一些意想不到的“坑”,SoapUI也不例外.无论是参数或者响应报文,大家可能都遇到过乱码问题,这里记录一下几种解决乱码的方法. 一.修改显示字体 在File>>> ...

  3. 【MM系列】SAP MM模块-组织结构介绍

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-组织结构介绍   ...

  4. elementUi--->实现上传图片效果(upload+formData)

    现在谈一下elelmentui中使用Upload 上传通过点击或者拖拽上传文件(图片) <el-upload name="multfile"    //上传的文件字段名 cl ...

  5. react 样式的写法之一 ---》styled-components的基本使用

    [react]---styled-components的基本使用---[WangQi]   一.官网地址 https://www.styled-components.com/ 二.styled-com ...

  6. The Log-Structured Merge-Tree(译)

    coming from http://duanple.blog.163.com/blog/static/7097176720120391321283/

  7. CTF夺旗赛丨网络内生安全试验场第四季圣诞赛明日开赛!

    期待许久的圣诞狂欢就要来喽 <Jingle bell>欢快的旋律 在耳边翩翩起舞 白胡子老爷爷骑着麋鹿准时来送礼物 2019圣诞节 i 春秋做你的圣诞老人 参加CTF欢乐圣诞赛 你提交答案 ...

  8. (4.31)quotename函数

    操作sql server尤其是写存储过程时,要用到各种各样的函数,今天就总结一个quotename()的用法. 1.语法: quotename(‘character_string’[,‘quote_c ...

  9. React手稿 - Context

    Context Context提供了除props之外的传参数的方式. Context是全局跨组件传递数据的. API React.createContext ``` const {Provider, ...

  10. 生日蛋糕 (poj1190) (dfs剪枝)

    [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为 ...