zoukankan      html  css  js  c++  java
  • Atcoder Regular Contest 076 F

    题目链接:F - Exhausted?
    题目大意:有m个椅子在数轴上排列,第i张椅子(1≤i≤m)的坐标为i。 高桥君和他的朋友一共有n个人。高桥君他们因为玩了太久的游戏,大家的腰和背都很痛,所以他们很有必要坐在椅子上休息一下。高桥君他们每个人坐的椅子的坐标都很讲究,第i个人想坐在坐标在li以下(包括li)的椅子上,或者坐在坐标在ri以上(包括ri)的椅子上。当然,一个的椅子只能坐一个人。 可这样计算下去,可能会让他们不能都坐在椅子上休息。青木君关心高桥君他们的健康,尽可能多地增加椅子,让高桥君他们都能够坐在椅子上休息。 椅子可以添加到任意的实数坐标上,请求出需要添加椅子数量的最小值。
    题解:这一道题一开始就会想到贪心,先考虑弱化版:只有(l_i),那么很简单,再加上(r_i)的话,就是当(l_i)不行时,把坐在(l_i)之前的(r_i)最小值给拿出来,放到后面,这样肯定是最优的。
    代码:

    #include <queue>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    #define Maxn 200000
    int tmp[Maxn+5];
    struct Person{
    	int l,r;
    	friend bool operator <(Person p,Person q){
    		if(p.l==q.l){
    			return p.r<q.r;
    		}
    		return p.l<q.l;
    	}
    }a[Maxn+5];
    priority_queue<int,vector<int>,greater<int> > q;
    int main(){
    	int n,m;
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++){
    		scanf("%d%d",&a[i].l,&a[i].r);
    	}
    	sort(a+1,a+1+n);
    	int h=1,len=0;
    	for(int i=1;i<=n;i++){
    		q.push(a[i].r);
    		if(h<=a[i].l){
    			h++;
    		}
    		else{
    			tmp[++len]=q.top();
    			q.pop();
    		}
    	}
    	sort(tmp+1,tmp+1+len);
    	int ans=0,t=m;
    	for(int i=len;i>0;i--){
    		if(t>=h&&t>=tmp[i]){
    			t--;
    		}
    		else{
    			ans++;
    		}
    	}
    	printf("%d
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    python的函数
    Python的条件语句和循环语句
    Python的输入与输出
    Python变量和类型
    Python的运算符
    Python的注释
    pycharm基本使用
    推特史上最大规模黑客入侵案:17岁问题少年的隐秘人生
    进程和线程的区别及线程的介绍
    python接口自动化42
  • 原文地址:https://www.cnblogs.com/withhope/p/11293698.html
Copyright © 2011-2022 走看看