您的位置:首页 > 其它

1634: [Usaco2007 Jan]Protecting the Flowers 护花

2017-07-18 22:47 501 查看
题目链接

题目大意:把n只奶牛送回牛棚,运送一头奶牛需要t[i]的时间,在运送的时候其他奶牛每分钟吃d[i]朵花(已经或正在被送回去的不吃),安排顺序,使得被吃的花尽量少

题解:调整法应用

对于相邻的x,y

x在y前面 吃的花的数量是d[y]*2*t[x],

y在x前面 吃的花的数量是d[x]*2*t[y],

若x在y前面更优(吃的花数量少),需满足d[y]*t[x]<d[x]*t[y]

我的收获:套路+1

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define ll long long
int n;
ll s,t;
struct cow{
int w,v;
}c[100005];
bool cmp(cow a,cow b){
return a.w*b.v<b.w*a.v;
}
void init()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d%d",&c[i].w,&c[i].v);
sort(c+1,c+1+n,cmp);
}
int main()
{
init();
for(int i=1;i<=n;i++)
{
s+=c[i].v*t;
t+=c[i].w*2;
}
printf("%lld\n",s);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: