首页 > 代码库 > USACO milk
USACO milk
/* ID:kevin_s1 PROG:milk LANG:C++ */ #include <iostream> #include <string> #include <cstring> #include <cstdio> #include <algorithm> #define MAXN 5001 using namespace std; int N,M; struct farmer{ int Pi; int Ai; }farmers[MAXN]; int cmp(farmer a,farmer b){ return a.Pi < b.Pi; } int main(){ freopen("milk.in","r",stdin); freopen("milk.out","w",stdout); cin>>N>>M; for(int i = 1; i <= M; i++){ cin>>farmers[i].Pi>>farmers[i].Ai; } sort(farmers + 1, farmers + M + 1, cmp); int count = 0; int index = 1; int money = 0; while(count < N){ if(count + farmers[index].Ai <= N){ count = count + farmers[index].Ai; money += farmers[index].Pi * farmers[index].Ai; index++; } if(count + farmers[index].Ai > N){ money += farmers[index].Pi * (N - count); count = N; index ++; } } cout<<money<<endl; return 0; }
//greedy algorithm
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。