• P2869 [USACO07DEC]美食的食草动物Gourmet Grazers


    P2869 [USACO07DEC]美食的食草动物Gourmet Grazers

    题目描述

    [链接]
    Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass at the Green Grass Grocers store for each of his N (1 ≤ N ≤ 100,000) cows.

    Each cow i demands grass of price at least Ai (1 ≤ Ai ≤ 1,000,000,000) and with a greenness score at least Bi (1 ≤ Bi ≤ 1,000,000,000). The GGG store has M (1 ≤ M ≤ 100,000) different types of grass available, each with a price Ci (1 ≤ Ci ≤ 1,000,000,000) and a greenness score of Di (1 ≤ Di ≤ 1,000,000,000). Of course, no cow would sacrifice her individuality, so no two cows can have the same kind of grass.

    Help Farmer John satisfy the cows' expensive gourmet tastes while spending as little money as is necessary.

    约翰的奶牛对食物越来越挑剔了。现在,商店有M 份牧草可供出售,奶

    牛食量很大,每份牧草仅能供一头奶牛食用。第i 份牧草的价格为Pi,口感为

    Qi。约翰一共有N 头奶牛,他要为每头奶牛订购一份牧草,第i 头奶牛要求

    它的牧草价格不低于Ai,口感不低于Bi。请问,约翰应该如何为每头奶牛选

    择牧草,才能让他花的钱最少?

    输入输出格式
    输入格式:

    • Line 1: Two space-separated integers: N and M.

    • Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

    • Lines N+2..N+M+1: Line i+N+1 contains two space-separated integers: Ci and Di

    输出格式:

    • Line 1: A single integer which is the minimum cost to satisfy all the cows. If that is not possible, output -1.

    输入样例#1:

    4 7
    1 1
    2 3
    1 4
    4 2
    3 2
    2 1
    4 3
    5 2
    5 4
    2 6
    4 4
    

    输出样例#1:

    12
    

    看上去就是就是贪心,可是两个变量,很麻烦,能想办法变成一个变量吗?当然可以。
    对两个cow、grass排序,口感大的在前面,口感相同的随意。。。
    然后取出第 i 只牛,
    1:找到所有口感大于等于这只牛所要求的口感的草,加入某数据结构结构S,发现S中口感似乎已经不重要了。
    2:从S找出最便宜的,加入答案,并把它从S中删除。
    重复1,2,当遍历到任意一头牛时,S中的所有草都是满足这头牛的口感要求的

    
    #include<bits/stdc++.h>
    using namespace std;
    char buf[100000],*L=buf,*R=buf;
    #define gc() L==R&&(R=(L=buf)+fread(buf,1,100000,stdin),L==R)?EOF:*L++;
    template<typename T>
    inline void read(T&x) {
        int flag=x=0;
        char ch=gc();
        while (ch<'0'||ch>'9')flag|=ch=='-',ch=gc();
        while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-48,ch=gc();
        if(flag)x=-x;
    }
    const int MAXN=1e5+10;
    struct Node{
        int a,b;
        bool operator<(const Node&y)const{return b>y.b; }
    }c[MAXN],g[MAXN];
    int n,m;
    int main() {
        freopen("in.txt","r",stdin);
        read(n),read(m);
        if(m<n){cout<<-1;return 0;}
        for(int i=0;i<n;++i)read(c[i].a),read(c[i].b);
        for(int i=0;i<m;++i)read(g[i].a),read(g[i].b);
        sort(c,c+n);
        sort(g,g+m);
        long long ans=0;
        multiset<int>tree;
        multiset<int>::iterator p;
        for(int i=0,j=0;i<n;++i){//遍历牛
            while(j<m&&g[j].b>=c[i].b)tree.insert(g[j++].a);//如果当前的草满足口感要求,加入
            p=tree.lower_bound(c[i].a);//已经满足口感了,查找满足价格的草
            if(p==tree.end()){//没找到
                cout<<-1;
                return 0;
            }
            else{
                ans+=*p;
                tree.erase(p);
            }
        }
        cout<<ans;
        return 0;
    }
    
  • 相关阅读:
    nexus2设置开机自启
    谷粒商城XXL-SSO(三十六)
    谷粒商城SpringSession(三十五)
    CDH安装(二)
    CDH安装(一)
    大数据服务规划
    ClouderaManager的背景和介绍
    大数据架构
    C++ const成员变量和成员函数(常成员函数)
    C++ this指针详解(精辟)
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14144902.html
Copyright © 2020-2023  润新知