Protecting the Flowers POJ 3262

The original title

Topic Link

Topic analysis

Greedy little difficult questions, first consider how to make a minimum loss in the same time t, it can be controlled so that the same time, consider a sub-issue, if only two cows, that last is certainly the end of time is the same, this time how to make losses least, direct comparison t1 * d2 and t2 * d1 on the line, if it is the first small cattle selected from 1, selected from bovine or two. extension, is to be sorted to all cattle, cows for i, j, in accordance with ti * dj <tj * di predetermined priority rules, and finally conveyed simply according to the order on the list of cattle.

Code

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set> 
13 
14 using namespace std;
15 typedef long long LL;
16 const int INF_INT=0x3f3f3f3f;
17 const LL INF_LL=0x3f3f3f3f3f3f3f3f;
18 typedef pair<int,int> P;
19 
20 P num[100000];
21 
22 bool cmp(P a,P b)
23 {
24     return a.first*b.second<b.first*a.second;
25 }
26 
27 int main()
28 {
29 //    freopen("black.in","r",stdin);
30 //    freopen("black.out","w",stdout);
31     int n;
32     cin>>n;
33     long long sum=0;
34     for(int i=0;i<n;i++) scanf("%d %d",&num[i].first,&num[i].second),sum+=num[i].second;
35     sort(num,num+n,cmp);
36     long long ans=0;
37     for(int i=0;i<n;i++)
38     {
39         sum-=num[i].second;
40         ans+=sum*2*num[i].first;
41     }
42     cout<<ans<<endl;
43     return 0;
44 } 

 

Guess you like

Origin www.cnblogs.com/VBEL/p/11404774.html