admin 管理员组文章数量: 887006
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;
}
本文标签: Cpp环境POJ3622Vijos2990Usaco2007挑剔的美食家
版权声明:本文标题:Cpp环境【POJ3622】【Vijos2990】【Usaco2007】挑剔的美食家 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732356540h1534592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论