首页 > 其他 > 详细

POJ-3262

时间:2020-07-20 01:12:50      阅读:88      评论:0      收藏:0      [点我收藏+]

链接:http://poj.org/problem?id=3262

题意:有n头牛,第i头牛送回牛棚需要Ti分钟,每分钟吃Di朵花。当牛送回时就不会再吃花。求被吃的花最小数。

思路:贪心。但开始简单以为优先送回吃的多的就行,W了两发幡然醒悟,应该这样排序:对两头牛a、b,若a被送回,b吃的花为b.d*a.t;若b被送回,a吃的花为a.d*b.t,因此对这两个进行比较,优先选出较大值。

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 int n;
 5 long long ans,total;
 6 struct cow
 7 {
 8     long long t,d;
 9 }c[110000];
10 bool cmp(cow a,cow b)
11 {
12     long long p1=a.d*b.t,p2=a.t*b.d;
13     return p1>p2;
14 }
15 int main(void)
16 {
17     cin>>n;
18     for(int i=1;i<=n;i++)
19     {
20         cin>>c[i].t>>c[i].d;
21         total+=c[i].d;
22     }
23     sort(c+1,c+1+n,cmp);
24     for(int i=1;i<=n;i++)
25     {
26         total-=c[i].d;
27         ans+=total*2*c[i].t;
28     }
29     cout<<ans<<endl;
30     return 0;
31 }

 

POJ-3262

原文:https://www.cnblogs.com/yanying7/p/13341769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!