bzoj1634 / P2878 [USACO07JAN]保护花朵Protecting the Flowers

P2878 [USACO07JAN]保护花朵Protecting the Flowers

难得的信息课......来一题水题吧。

经典贪心题

我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并没有影响。

我们设距离为$a_{i}$,每分钟贡献$b_{i}$

$a_{1}$        $b_{1}$

$a_{2}$        $b_{2}$

先抓第一头的代价:$a_{1}b_{1}+2a_{1}b_{2}+a_{2}b_{2}$

先抓第二头的代价:$a_{2}b_{2}+2a_{2}b_{1}+a_{1}b_{1}$

显然,我们按照$a_{1}b_{2}<a_{2}b_{1}$的顺序排序,蓝后贪心就行了

注意在决定抓某只奶牛时它就已经不能吃花了

 

退役后做题,内心有好多感慨......然鹅没时间写了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 struct data{
 7     int a,b;
 8     bool operator < (const data &tmp) const{
 9         return a*tmp.b<b*tmp.a;//a1*b2<a2*b1
10     }
11 }c[100002];
12 int n; long long ans,ti;
13 int main(){
14     scanf("%d",&n);
15     for(int i=1;i<=n;++i)
16         scanf("%d%d",&c[i].a,&c[i].b);
17     sort(c+1,c+n+1);
18     for(int i=1;i<=n;++i)
19         ans=ans+ti*c[i].b,ti+=c[i].a*2;
20     printf("%lld",ans);
21     return 0;
22 }
View Code

 

posted @ 2018-11-22 16:39  kafuuchino  阅读(220)  评论(0编辑  收藏  举报