*Protecting the Flowers(POJ 3262, 贪心)

牛吃花问题求解
本文介绍了一种解决“牛吃花”问题的算法。该问题要求计算在牛群吃花园里的花时如何通过合理安排牵走每头牛的时间来最小化花的损失总数。通过排序和动态更新剩余花朵数量的方法,实现了一个高效的解决方案。

题目链接

       题目大意:牛在花园里吃花,牛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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值