题目大意:牛在花园里吃花,牛i每分钟吃花Di,牵走牛i再回来的时间为2Ti,求最小损失花数。
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N = 100005;
struct cow {
int T, D;
} a[MAX_N];
int N, rest_D;
long long ans;
bool cmp(cow b, cow c)
{
return b.T*c.D < c.T*b.D;
}
void init()
{
rest_D = 0;
for(int i=0; i<N; i++){
scanf("%d%d", &a[i].T, &a[i].D);
rest_D += a[i].D;
}
ans = 0;
}
void f()
{
for(int i=0; i<N; i++){
rest_D -= a[i].D;
ans += 2 * a[i].T * rest_D;
}
}
int main()
{
while(~scanf("%d", &N)){
init();
sort(a, a+N, cmp);
f();
printf("%lld", ans);
}
return 0;
}
牛吃花问题求解
本文介绍了一种解决“牛吃花”问题的算法。该问题要求计算在牛群吃花园里的花时如何通过合理安排牵走每头牛的时间来最小化花的损失总数。通过排序和动态更新剩余花朵数量的方法,实现了一个高效的解决方案。
494

被折叠的 条评论
为什么被折叠?



