题意:给定三个序列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. appium xpath元素定位

    1.id定位 写法:driver.find_element_by_id("这里是resource-id") 2.name定位 name定位就是通过UI Automator工具查看的 ...

  2. windows7 玩 WinKawaks kof2002为什么提示couldn't initialise DirectSound?

    插上 耳机  或者 音响 就ok 呵呵 http://wenwen.sogou.com/z/q200172744.htm windows7 玩 WinKawaks kof2002为什么提示couldn ...

  3. YOLOv3 算法的详细说明

    YOLOv3没有太多的创新,主要是借鉴一些好的方案融合到YOLO里面.不过效果还是不错的,在保持速度优势的前提下,提升了预测精度,尤其是加强了对小物体的识别能力. 本文主要讲v3的改进,由于是以v1和 ...

  4. tips for using shortcuts

    tips for using shortcuts for mac: command+ctrl+F:full screen(当前应用全屏之后有一个好处 就是 使用 4 tap 的手势 可以在全屏的界面之 ...

  5. Robot Framework课件汇总

    http://www.testclass.net/rf/ 测试教程网http://www.testclass.net/all

  6. raid10 五块硬盘/raid5(三块使用,两块备份)

    raid 10五块硬盘 第一步:在虚拟机中在添加五块硬盘 第二步:使用mdadm命令创建RAID10,名称为“/dev/md0/” -C代表创建操作,-v显示创建过程,-a yes检查RAID名称,- ...

  7. g50打开无线设置 fn+f7

  8. AtCoder Beginner Contest 133 B - Good Distance

    地址:https://atcoder.jp/contests/abc133/tasks/abc133_b 核心问题:判断一个浮点数开方是否为整数 ; double ans1=sqrt(ans); if ...

  9. 1.Dockerfile

    1.docker build docker build 这个动作有一个context 上下文的概念 docker build -f /path/to/a/Dockerfile .这个动作 通过 -f ...

  10. Leetcode Lect7 哈希表

    传统的哈希表 对于长度为n的哈希表,它的存储过程如下: 根据 key 计算出它的哈希值 h=hash(key) 假设箱子的个数为 n,那么这个键值对应该放在第 (h % n) 个箱子中 如果该箱子中已 ...