[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 }

 

转载于:https://www.cnblogs.com/tjsuhst/archive/2013/01/22/2871672.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值