USACO SEC.1.3 No.1 Mixing Milk
题意:需要收购总数为N的牛奶,现在有M个牛奶供应商(总量足够),给出总数和单价,求最小的花销。
核心:基本的贪心解法,按单价排序逐个选取。
目的在于熟悉基本的贪心法的基本方法和思路
/* ID: lsswxr1 PROG: milk LANG: C++ */ #include <iostream> #include <vector> #include <map> #include <list> #include <set> #include <deque> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <cctype> #include <cstdio> #include <iomanip> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <fstream> using namespace std; ///宏定义 const int INF = 1000000000; const int MAXN = 5015; const int maxn = MAXN; ///全局变量 和 函数 #define USACO #ifdef USACO #define cin fin #define cout fout #endif ////////////////////////////////////////////////////////////////////////// int N, M; struct farm { int price, total; bool operator < (const farm& t) const { return price < t.price; } }; farm f[maxn]; int main() { #ifdef USACO ofstream fout ("milk.out"); ifstream fin ("milk.in"); #endif ///变量定义 while (cin >> N >> M) { for (int i = 0; i < M; i++) { cin >> f[i].price >> f[i].total; } sort(f, f + M); int remainsNeed = N, cost = 0; for (int i = 0; i < M; i++) { if (f[i].total > remainsNeed) { cost += f[i].price * remainsNeed; break; } cost += f[i].price * f[i].total; remainsNeed -= f[i].total; } cout << cost << endl; } ///结束 return 0; }
浙公网安备 33010602011771号