[arc076F]Exhausted?
来源:互联网 发布:js读取excel 编辑:程序博客网 时间:2024/06/01 09:17
题目大意
有m个椅子,第i个在位置i,每个椅子只能坐一个人。
有n个人,第i个人能坐的椅子的位置j需满足j≤Li或j≥Ri。
现在你可以添加若干个椅子,可以放在任意实数位置。问最少加多少个
n,m≤200000
分析
题目就是问你最多坐多少个人…
首先考虑只有L或R的限制。以只有L为例,可以按位置从小到大枚举椅子,在Li处决定i坐在哪。很显然的贪心是:枚举每往后一个椅子就多一个位置放人,然后已知Lj=i的有多少个人,让他们能放就放,如果不够放(即空余的位置小于人数,则多出来的就不放,并把空位个数赋为0)
现在考虑加上R的限制怎么做。同样地把每个人挂在L位置上,然后把每个人能放就尽量放,如果有一个人放不进去了,他其实可以替代掉一个已经有位置的人。能替换的条件显然是他的R比那个已经被放的人的R大(为了使被踢出来的人被放回去的可能性更大),所以可以同时用小根堆维护已经放进去的人的R。
枚举完后,把已经放的人放到最前面,然后给剩下的人做一遍只考虑R的贪心即可。
#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;const int N=2e5+5;typedef long long LL;int n,m,L,R,hr[N],ans;priority_queue <int,vector <int> , greater <int> > h;vector <int> hl[N];char c;int read(){ int x=0,sig=1; for (c=getchar();c<'0' || c>'9';c=getchar()) if (c=='-') sig=-1; for (;c>='0' && c<='9';c=getchar()) x=x*10+c-48; return x*sig;}int main(){ n=read(); m=read(); for (int i=0,l,r;i<n;i++) { l=read(); r=read(); hl[l].push_back(r); } L=0; for (int i=1,t;i<=m;i++) { for (vector <int> ::iterator it=hl[i].begin();it!=hl[i].end();it++) { if (L<i) { L++; h.push(*it); continue; } t=h.top(); if (t>= *it) hr[*it]++;else { h.pop(); hr[t]++; h.push(*it); } } } for (vector <int> ::iterator it=hl[0].begin();it!=hl[0].end();it++) hr[*it]++; R=m+1; ans=hr[m+1]; for (int i=m;i;i--) { for (;hr[i]>0;hr[i]--) { if (i<R && L<R-1) R--;else ans++; } } printf("%d\n",ans); return 0;}
阅读全文
0 0
- [arc076f]Exhausted?
- [arc076F]Exhausted?
- over-exhausted of youngsters
- Oracle Exhausted Resultset !!!
- A Exhausted Day
- ORA-15041: diskgroup space exhausted
- ld Memory exhausted一解
- Cannot get a connection, pool exhausted解决办法
- Cannot get a connection, pool exhausted解决办法
- Cannot get a connection, pool exhausted解决办法
- Cannot get a connection, pool exhausted
- ORA-15260 diskgroup space exhausted Problem
- Allowed memory size of 8388608 bytes exhausted
- [Tomcat]Cannot get a connection, pool exhausted
- request body stream exhausted错误解决方案
- Cannot get a connection, pool exhausted
- virtual memory exhausted:cannot allocate memory
- Allowed memory size of 134217728 bytes exhausted
- 025day(学习结构(struct))
- Java性能调优的11个实用技巧
- eclipse怎么把Java项目转换为web项目
- 对称加密和非对称加密
- NOIP2017赛前冲刺RoundA~D总结
- [arc076F]Exhausted?
- 不相交集合的数据结构-并查集
- Java基础——向上转型、向下转型
- linux系统分区管理
- Ubuntu16配置LAMP
- Ceoi2014 bzoj 4356 考托福又不会考 国家集训队作业题
- MT 204 Financial Markets Direct Debit Message金融市场直接借记电文
- #Thinking in Java阅读笔记# 第九章 接口
- 阿里云服务器上建立网站的域名解析与配置(tomcat服务器)