洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers

洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers

贪心

 这题的话 如果研究一下相邻的两个 看看交换的结果就好 好像是一种很经典的方法啊、、

如果x后拿的多一点 2Tx*Dy<=2Ty*Dx 所以当Dy/Ty<=Dx/Tx时 x应该在y前面被拿

排序一下就好了

 1 #include <cstdio> 
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <iostream> 
 6 #include <iomanip> 
 7 #include <string> 
 8 #include <algorithm> 
 9 #define LL long long 
10 #define For(i,j,k) for(int i=j;i<=k;i++) 
11 #define Dow(i,j,k) for(int i=j;i>=k;i--) 
12 using namespace std ;  
13 
14 const int N = 100011 ; 
15 const double eps = 1e-7 ; 
16 struct node{
17     int T,D ; 
18 }a[N];
19 int n,sum ; 
20 LL ans ; 
21 
22 inline int read() 
23 {
24     int x = 0 , f = 1 ; 
25     char ch = getchar() ; 
26     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar(); } 
27     while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar(); } 
28     return x * f ; 
29 }
30 
31 inline bool cmp(node a,node b) 
32 {
33     if(fabs(1.0*a.T/a.D-1.0*b.T/b.D)>eps)  //  注意要特判比值相等的情况 
34         return 1.0*a.T/a.D < 1.0*b.T/b.D ; 
35     return a.T < b.T ; 
36 }
37 
38 int main() 
39 {
40     n = read() ; 
41     For(i,1,n) a[i].T=read() , a[i].D=read(),sum+=a[i].D ; 
42     sort(a+1,a+n+1,cmp) ; 
43     
44     For(i,1,n) {
45         sum-=a[i].D ; 
46         ans=ans+2*a[i].T*sum ; 
47     }
48     printf("%lld\n",ans) ; 
49     return 0 ; 
50 }

 

posted @ 2017-10-03 14:17  third2333  阅读(179)  评论(0编辑  收藏  举报