USACO Section 1.3: Mixing Milk

 1 /*

 2 ID: leetcod3

 3 PROG: milk

 4 LANG: C++

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 #include <cmath>

17 #include <list>

18 #include <cstdio>

19 #include <cstdlib>

20 #include <limits>

21 #include <stack>

22 

23 using namespace std;

24 

25 ofstream fout ("milk.out");

26 ifstream fin ("milk.in");

27 struct farmer {

28     int price;

29     int amount;

30     farmer(int p, int a) : price(p), amount(a) { }

31     farmer() : price(0), amount(0) { }

32 };

33 

34 bool cmp(const farmer &a, const farmer &b) {

35     return a.price < b.price;

36 }

37 

38 int main() {

39     int N, M;

40     fin >> N >> M;

41     vector<farmer> F(M);

42     for (int i = 0; i < M; i++) fin >> F[i].price >> F[i].amount;

43     sort(F.begin(), F.end(), cmp);

44     int ans = 0;

45     int curamount = 0;

46     int index = 0;

47     while (curamount < N) {

48         if (curamount + F[index].amount < N) {

49             ans += F[index].amount * F[index].price;

50             curamount += F[index].amount;

51             index++;

52         }

53         else {

54             ans += (N - curamount) * F[index].price;

55             break;

56         }

57     }

58     fout << ans << endl;

59     return 0;

60 }

 

你可能感兴趣的:(USACO)