[USACO 1.3.1]混合牛奶

地址:http://hustoj.sinaapp.com/problem.php?id=1825

第一次用C++写,因为是对二维数组排序,所以先在网上了解了一下,这里二维数组用int **来表示了

对于sort的用法还要深入了解

思路就是先按单价排序,然后将数量一个一个加起来比较

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int n,m;
 6 int **f=new int*[5000];
 7 
 8 bool cmp(int *a,int *b)
 9 {
10     if(a[0]==b[0]) return a[1]<b[1];
11     else return a[0]<b[0];
12 }
13 
14 int main()
15 {
16     int count=0,money=0;
17     cin>>n>>m;
18     for(int i=0;i<m;i++)
19     {
20         f[i]=new int[2];
21         cin>>f[i][0]>>f[i][1];
22     }
23     sort(f,f+m,cmp);
24     for(int i=0;i<m && count<n;i++)
25     {
26         if(count+f[i][1]<=n)
27         {
28             money+=(f[i][0]*f[i][1]);
29             count+=f[i][1];
30         }
31         else
32         {
33             money+=(f[i][0]*(n-count));
34             count+=(n-count);
35         }
36     }
37     cout<<money<<endl;
38     return 0;
39 }

 

posted @ 2013-01-22 16:49  tjsuhst  阅读(237)  评论(0编辑  收藏  举报