zoukankan      html  css  js  c++  java
  • Poj2786

    #include<cstdio>
    #include<queue>
    #include<iostream>
    #include<cstdlib>
    #include<algorithm>
    #define N 800000+5
    using namespace std;
    struct g{
    	int cost;
    	int date;
    }a[N];
    int n;
    int cmp(const void *a,const void *b){
    	return (*(struct g *)a).date>(*(struct g *)b).date?1:-1;
    }
    int main(){
    	int i;
    	while(scanf("%d",&n)==1){
    		for(i=0;i<n;i++)
    			scanf("%d%d",&a[i].cost,&a[i].date);
    		qsort(a,n,sizeof(a[0]),cmp);
    		 priority_queue<int> q;
    		int ans=0;
    		for(i=0;i<n;i++){
    			if(ans+a[i].cost<=a[i].date){
    				ans+=a[i].cost;
    				q.push(a[i].cost);
    			}
    			else
    				if(ans+a[i].cost>a[i].date&&!q.empty()&&a[i].cost<q.top()){
    					ans+=a[i].cost;
    					q.push(a[i].cost);
    				}
    				if(ans>a[i].date){
    					ans-=q.top();
    					q.pop();
    				}
    		}
    		printf("%d\n",q.size());
    	}
    	return 0;
    }
    

      

    /*
    用一个优先队列进行模拟(priority_queue)order的情况。
    先按照截止日期due对Order进行从小到大的排序,用pass表示当前完成最多订单需要的最短的时间,
    遍历Order,当pass + order[i].q <= order[i].d的时候,
    表示可以正常完成该订单,进队,同时pass += order[i].q,
    如果pass + order[i].q > order[i].d的时候,
    则需要考虑order[i].q和队列中需要最长时间的订单之间的关系,
    如果order[i].q较大,说明该订单不可能完成,否则入队,pass += order[i].q,
    然后要减去队列中需要最长时间的订单(即队首),一直贪心,最后留在队列中的订单个数就是保留的订单个数。

    */
    //

    keep moving...
  • 相关阅读:
    第二个冲刺 6.3.4.学术诚信与职业道德
    第二个冲刺 Sprint
    css之清除浮动
    style和getComputedStyle(ff)和currentStyle
    php 中间件
    Vue 和 angular
    img 分区响应图
    PHP composer
    php实现文件上传,下载的常见文件配置
    php 命名空间
  • 原文地址:https://www.cnblogs.com/xxx0624/p/2605547.html
Copyright © 2011-2022 走看看