POJ-3262

链接: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 }
原文地址:https://www.cnblogs.com/yanying7/p/13341769.html