zoukankan      html  css  js  c++  java
  • POJ 1456

    由于这道题设置,一件商品的售出需要一天,因此倘若考虑用一个结构记录当前打算售出的件数,这和结构中记录元素数量是一致的。

    这道题比较巧妙的是比贪心更近一步,使用最小堆进行优化

    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <string>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <stack>
    #include <map>
    #include <set>
    #include <deque>
    using namespace std;
    
    const int maxn= 1e4+5;
    
    struct Prod
    {
    	int p, d;
    	Prod (int _p= 0, int _d= 0) : p(_p), d(_d) {}
    	bool operator < (const Prod &rhs) const
    	{
    		return p> rhs.p;
    	}
    }pdt[maxn];
    priority_queue<Prod> hp;
    
    bool cmp (const Prod &lhs, const Prod &rhs)
    {
    	return lhs.d < rhs.d;
    }
    
    int main(int argc, char const *argv[])
    {
    	int n;
    	while (~scanf("%d", &n)){
    		while (!hp.empty()){
    			hp.pop();
    		}
    		for (int i= 0; i< n; ++i){
    			scanf("%d %d", &pdt[i].p, &pdt[i].d);
    		}
    		sort(pdt, pdt+n, cmp);
    
    		for (int i= 0; i< n; ++i){
    			if (pdt[i].d > (int)(hp.size())){
    				hp.push(pdt[i]);
    			}
    			else if ((int)(hp.size())== pdt[i].d){
    				Prod can= hp.top();
    				if (can.p < pdt[i].p){
    					hp.pop();
    					hp.push(pdt[i]);
    				}
    			}
    		}
    
    		int ans= 0;
    		while (!hp.empty()){
    			Prod cur= hp.top();
    			hp.pop();
    			ans+= cur.p;
    		}
    
    		printf("%d
    ", ans);
    	}	
    	return 0;
    }
    
  • 相关阅读:
    java基础_面试题笔记
    ACM-ICPC 2018 Xuzhou Online Contest题解
    覆盖点问题总结
    2018icpc沈阳网络赛题解(转发)
    树链剖分
    树状数组
    线段树板子
    sdoi2016生成魔咒
    洛谷3804
    大佬博文收集
  • 原文地址:https://www.cnblogs.com/Idi0t-N3/p/14876668.html
Copyright © 2011-2022 走看看