【CF1257E】The Contest【线段树】


题意:给定三个序列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【线段树】的更多相关文章
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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 ...
- 计蒜客 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 ...
- 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 ...
- AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图
AtCoder Regular Contest 069 F Flags 二分,2-sat,线段树优化建图 链接 AtCoder 大意 在数轴上放上n个点,点i可能的位置有\(x_i\)或者\(y_i\ ...
- UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- 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 ...
- 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 ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
随机推荐
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_04 数据结构_2_数据结构_队列
先进先出 队列 队列:queue,简称队,它同堆栈一样,也是一种运算受限的线性表,其限制是仅允许在表的一端进行插入, 而在表的另一端进行删除. 简单的说,采用该结构的集合,对元素的存取有如下的特点: ...
- windows10下安装docker报错:error during connect
详细报错信息如下: C:\Users\zig>docker info error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engin ...
- 测开之路九十二:css之背景色和背景
引用css 设置背景色: 背景图片 整个页面的背景图片 图片当局部背景图 也可以简写 css /* css基本样式 */ /* 设置p标签的文字前景色.背景色 */p{ /*字体颜色为蓝色*/ col ...
- IntelliJ IDEA的常用设置
1.设置IDEA主题样式 ①设置方法: ②效果:设置为Darcula之后整体的风格就是暗黑主题,如上图. 2.设置编辑区主题 ①设置方法: 注:由于IDEA自带的编辑区主题比较少,想要更多的编辑区主题 ...
- [LeetCode] 834. Sum of Distances in Tree
LeetCode刷题记录 传送门 Description An undirected, connected treewith N nodes labelled 0...N-1 and N-1 edge ...
- Kubernetes kubeadm 安装记录
Kubernetes kubeadm 安装记录 注:比较乱,都是一些预见到的错误 kubernetes yum 源 cat /etc/yum.repos.d/kubernetes.repo [kube ...
- Navicat批量导出mysql的DDL语句
工作中有的时候需要将某个库中的表.视图.函数.存储过程等创建语句导出,又不需要表中的数据.同时最好放在同一个文件里面,这样方便拷贝. 方法一:需要拷贝的创建语句条数不多,不至于让你拷到头脑发晕的地步, ...
- DelayQueue详解
一.DelayQueue是什么 DelayQueue是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走.这种队列是有序的,即队头对 ...
- selenium 等待时间3种方式
强制等待 sleep() -- 最不建议用 缺点:sleep(10)网络不好的情况,到10秒就抛出异常网络很好,1秒钟就响应了,白白等待多9秒 隐式等待 -- 也不是很理想的方法implicitly_ ...
- WinForm的RadioButton使用小技巧
http://www.cnblogs.com/sjrhero/articles/1883155.html 当多个RadioButton同在一个容器里面的时候,多半的操作都是要得到其中一个的值这个时候我 ...