POJ 3262 Protecting the Flowers(贪心,需要小小优化一下)


Protecting the Flowers

Time Limit: 2000MS


Memory Limit: 65536K

Total Submissions: 5692


Accepted: 2237

Description

Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.

Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own barn. Furthermore, while waiting for transport, she destroys Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ can only transport one cow at a time back to her barn. Moving cow i to its barn requires 2 × Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.

Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

Input

Line 1: A single integer N 
Lines 2..N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow's characteristics

Output

Line 1: A single integer that is the minimum number of destroyed flowers

Sample Input

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample Output

86

Hint

FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.


题意:农夫有n头牛跑到花坪上吃草,农夫要把它们送回自己的牛舍,所化的时间分别为 t_i,(单程时间为t_i),每头牛留在华坪上每单位时间内吃花量分别为d_i。华坪上花最少被破坏的数量为多少?

题解:因为农夫每抱走一头牛,剩下的n-1头牛的问题还是和上面的问题一致。故要想使最终结果最优,所以必须要求当前最优解,所以可以判定是贪心问题。

刚开始,以为先优先d值大的,d值相等时优先t值小的。 WA了,应该优先 t/d 小的。下面给出证明:
在证明中我们假设只有两头牛a,b。 分别有 t_a ,  d_a  ;  t_b , d_,b 。 

若先取走a牛,则食花量为 2 * t_a * d_b ;
若先取走b牛,则食花量为 2 * t_b * d_a ;
两式分别除以 d_a * d_b ;分别为 2 * t_a / d_a         2 * t_b / d_b
所以要优先 t/d 值小的。

对于贪心问题n与n-1的策略是相同的,所以由n=2的情况推广到任意n的情况。

注意:O(n^2)算法是会超时的,除去排序情况,其他部分可以优化到O(n)的。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll __int64
using namespace std;
ll sum[100010];
struct node
{
	ll t,d; 
}cow[100010];

int cmp(node a,node b)
{
	return (a.t*1.0/a.d)<(b.t*1.0/b.d);
}
 
int main()
{
	int n,i,j;
	ll ans;
	while(scanf("%d",&n)!=EOF)
	{
		for(i=0;i<n;++i)
			scanf("%I64d%I64d",&cow[i].t,&cow[i].d);
		sort(cow,cow+n,cmp);
		sum[n-1]=0;
		for(i=n-2;i>=0;i--)
			sum[i]=sum[i+1]+cow[i+1].d;
		ans=0;
		for(i=0;i<n;++i)
			ans+=2*cow[i].t*sum[i];
		printf("%I64d\n",ans);
	}
	return 0;
} 

  
 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值