【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个数的序列(序列的值有正有负),找到该序列的区间最大 ...
随机推荐
- KETTLE——(例)简单的字段转换
一个简单的小例子:结合数据抽取.简单的字段转换.数据输出. 资源库连接.数据输入.数据输出.参见之前的文章. 基本的转换结构是这样的,我们从表中输入,选择我们需要的字段,已经对应的名称,然后将数据 ...
- 06 使用bbed提交delete的数据--01
使用bbed模拟delete提交操作 --session 1 TEST@ orcl )); Table created. TEST@ orcl ,'AAAAA'); row created. TEST ...
- Java ——Character 类
本节重点思维导图 Character 类用于对单个字符进行操作. Character 类在对象中包装一个基本类型 char 的值 char ch = 'a'; // Unicode 字符表示形式 ch ...
- Spring Boot系列(三) Spring Boot 之 JDBC
数据源 类型 javax.sql.DataSource javax.sql.XADataSource org.springframework.jdbc.datasource.embedded,Enbe ...
- Python3的基本数据类型
2.1. Python3中六个标准的基本数据类型: Number(数字) String(字符串) Sets(集合) Tuple(元组) List(列表) Dictionary(字典) 2.2. Pyt ...
- 前端 CSS的选择器 伪类选择器 CSS3 nth-child()
first-child 选中第一个标签 应用CSS样式 <!DOCTYPE html> <html lang="en"> <head> < ...
- Maven依赖项Scope属性设置含义
Idea的File->Project Structure->左侧Modules: 选择自己的项目如图: 每个依赖项的Scope选项进行该jar包的相关操作设置,默认为compile,根据需 ...
- Gorgeous Sequence(线段树)
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- Java反射学习-3 - 反射获取属性,方法,构造器
package cn.tx.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import ...
- css炫酷动画收藏
1.按钮.hover.input动画(cssfx) https://cssfx.dev/ 2.svg 矢量定制icon(ikonate) https://www.ikonate.com/#conten ...