poj 3262 Protecting the Flowers 【贪心】

本文详细介绍了如何解决与hdoj4882类似的问题,通过提供代码实现和解题策略,帮助读者理解并解决该类问题。重点在于比较两个问题之间的相似之处,并给出解决思路。

这道题跟hdoj4882 ZCC Loves Codefires 几乎一模一样,只是改了一下下。

解题策略 :参考hdoj4882的解法 http://blog.youkuaiyun.com/shengweisong/article/details/38316699

题目链接 http://poj.org/problem?id=3262

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef struct{
	double t, d;
	double r;
}str;
str s[100005]; 
int cmp(str a, str b)
{
	return a.r<b.r;
}
int main()
{
	int n, i, j;
	while(scanf("%d", &n) == 1){
		for(i = 0; i < n; i ++){
			scanf("%lf%lf", &s[i].t, &s[i].d);
			s[i].r = s[i].t/s[i].d;
		}
		sort(s, s+n, cmp);
		__int64 sum =0, time = 2*s[0].t; //和hdoj4882 这处有点小小的改变
		for(i = 1; i < n; i ++){
			sum += time *s[i].d;
			time += 2*s[i].t;
		}
		printf("%I64d\n", sum);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值