Cpp环境【POJ3622】【Vijos2990】【Usaco2007】挑剔的美食家

Description  【问题描述】

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.


  与很多奶牛一样,FJ那群养尊处优的奶牛们对食物越来越挑剔,随便拿堆草就能打发她们午饭的日子自然是一去不返了。现在FJ不得不去牧草专供商那里购买大量美味多汁的牧草,来满足他那 N 头挑剔的奶牛。

  所有奶牛都对FJ提出了她对牧草的要求:第i头奶牛要求她的食物每份的价钱不低于A_i,并且鲜嫩程度不能低于B_i。商店里供应M种不同的牧草,第i种牧草的定价为C_i,鲜嫩程度为D_i 。

  为了显示她们的与众不同,每头奶牛都要求她的食物是独一无二的,也就是说,没有哪两头奶牛会选择同一种食物。FJ想知道,为了让所有奶牛满意,他最少得在购买食物上花多少钱。

Input  【输入格式】
  • 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

第1行: 2个用空格隔开的整数N 和 M。第2..N+1行: 第i+1行包含2个用空格隔开的整数:A_i、B_i。第N+2..N+M+1行: 第j+N+1行包含2个用空格隔开的整数:C_i、D_i

Output  【输出格式】
  • Line 1: A single integer which is the minimum cost to satisfy all the cows. If that is not possible, output -1.

第1行: 输出1个整数,表示使所有奶牛满意的最小花费。如果无论如何都无法满足所有奶牛的需求,输出-1

Sample Input  【输入样例】

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

Sample Output  【输出样例】

12

【样例解释】

给奶牛1吃价钱为2的2号牧草,奶牛2吃价钱为4的3号牧草,奶牛3分到价钱为2的6号牧草,奶牛4选择价钱为4的7号牧草,这种分配方案的总花费是12,为所有方案中花费最少的。

【Cpp代码】

#include<cstdio>
#include<set>
#include<iostream>
#include<algorithm>
#define maxn 100005
using namespace std;
int n,m;
long long ans=0;
bool vis[maxn];
struct detail
{
    int price,fresh,id;
}cow[maxn],grass[maxn];

bool cmp1(detail a,detail b)
{
    return a.fresh>b.fresh;
}

struct cmp
{
    bool operator ()(detail a,detail b)
    {
        return a.price<b.price;
    }
};
multiset<detail,cmp>st;
multiset<detail,cmp>::iterator it;

void solve()
{
    int j=1;
    for(int i=1;i<=n;i++)
    {
        while(j<=m && grass[j].fresh>=cow[i].fresh)
            st.insert(grass[j++]);

        it=st.lower_bound(cow[i]);
        if(it==st.end())    {ans=-1;return ;}

        detail t=*it;
        vis[t.id]=true;
        ans+=t.price;

        st.erase(it);
    }
}

int main()
{
//  freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);;
    for(int i=1;i<=n;i++)   
        scanf("%d%d",&cow[i].price,&cow[i].fresh),cow[i].id=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&grass[i].price,&grass[i].fresh),grass[i].id=i;
    }
    sort(cow+1,cow+1+n,cmp1);
    sort(grass+1,grass+1+m,cmp1);
    solve();
    cout<<ans;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值