题意:给定三个序列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. Delphi XE2 之 FireMonkey 入门(4) - 控件天生可做容器

    1.新建 FM(HD) 工程, 先添加 TLine(默认名称是 Line1);2.在 Line1 选择状态下添加 Button1;3.取消选择后添加 Button2 此时, Button1.Paren ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第5节 String类_5_字符串的获取相关方法

    original:原来的.开始的 如果有多次的情况 因为查找的是第一次的出现的位置的索引值 如果要查找的字符串压根没有的话返回的就是-1

  3. usb接口类型 简单分类辨识

    usb接口类型 简单分类辨识 - [相似百科] 庆欣 0.0 4 人赞同了该文章 1. 先放图,随着越来越多的接触智能设备,会遇到各种各样的usb接口,对于很多人来说,接口类型只有:usb接口,安卓接 ...

  4. Monte Carlo Control

    Problem of State-Value Function Similar as Policy Iteration in Model-Based Learning, Generalized Pol ...

  5. DevOps的前世今生

    From今日头条:https://www.toutiao.com/a6534660729453150723/?tt_from=weixin&utm_campaign=client_share& ...

  6. Layerui 弹出层的位置设置

    距顶 offset: '300px' 例1: layer.msg("请先选择项!", { offset: '300px' });例2: layer.confirm("确定 ...

  7. idea 社区版本创建javaweb项目 使用jetty

    idea社区版本 创建javaweb项目后使用jetty启动 <dependencies> <dependency> <groupId>javax.servlet& ...

  8. Java-Lambda表达式第一篇认识Lambda表达式

    1.Lambda表达式时Java 8新增的特性.Lambda表达式支持将代码块作为方法参数,Lambda表达式允许使用更简洁的代码创建只有一个抽象方法的接口(即函数式接口)的实例. 2.当使用Lamb ...

  9. 旧接口注册LED字符驱动设备(静态映射)

    #include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...

  10. MFC框架各部分指针获取方式

    MFC框架各部分指针获取方式 前人在CSDN总结的,曾经帮助过我,整理总结一下,希望也能帮助一下别人. 获得CWinApp 获得CMainFrame 获得CChildFrame 获得CDocument ...