zoukankan      html  css  js  c++  java
  • POJ 3622 Gourmet Grazers(贪心)

    【题目链接】 http://poj.org/problem?id=3622

    【题目大意】

      给出一些物品拥有两个属性值,价格和精美程度
      给出一些需求表示要求获得的物品两个属性值的两种属性下界,
      一个物品只能用一次,问满足所有需求的最小价格。

    【题解】

      我们将物品和需求按照精美程度排序,这样我们就得到了价格的一个序列
      我们降序处理物品,则可以保证对于当前需求,前面的所有物品均满足其精美程度的需求
      这个时候我们只要找到满足条件的最低价格来供给这个需求,就一定是最优的,
      对于最优情况的查找,我们用平衡树来维护。

    【代码】

    #include <cstdio>  
    #include <algorithm>  
    #include <set>  
    using namespace std;
    typedef long long LL;  
    const int N=100000;  
    struct data{int x,y;}p[N],u[N];  
    int n,m;  
    bool cmp(data a,data b){return a.y>b.y;}  
    void solve(){
        LL ans=0;
        int j=0,flag=1; 
        for(int i=0;i<n;i++)scanf("%d%d",&p[i].x,&p[i].y);  
        for(int i=0;i<m;i++)scanf("%d%d",&u[i].x,&u[i].y);  
        sort(p,p+n,cmp); sort(u,u+m,cmp);  
        multiset<int> M;
        for(int i=0;i<n;i++){  
            while(j<m&&u[j].y>=p[i].y)M.insert(u[j++].x);  
            multiset<int>::iterator it=M.lower_bound(p[i].x);  
            if(it==M.end()){flag=0;break;}  
            else{ans+=*it;M.erase(it);}
        }printf("%lld
    ",flag?ans:-1);          
    }
    int main(){  
        while(~scanf("%d%d",&n,&m))solve();
        return 0;  
    }  
  • 相关阅读:
    type 、instanceof、in 和 hasOwnproperty
    从代码中抽离数据的原则
    NSNotificationCenter 通知中心传值
    imageView添加阴影和边框
    block的基本使用
    内存管理总结
    把color转成image的方法
    Instruments检测解决内存泄露以及进行性能测试
    Xcode7 修改项目名完全攻略
    UITextField的使用总结
  • 原文地址:https://www.cnblogs.com/forever97/p/poj3622.html
Copyright © 2011-2022 走看看