POJ 3544 Journey with Pigs 贪心 + 不等式排序

来源:http://poj.org/problem?id=3544

题意:一个农民有一些猪,这些猪的重量不同,要运到一些镇上去卖掉。每个镇卖一头猪,起点到每个镇的距离给出,以及运送单位重量走单位距离的价值给出,每头猪对应于每个镇能够卖的价值也给出。问最大能够得到多大的价值。

思路:我刚开始想这题是想到了KM上了,但因为边太多,可以有100万条,所以一定会超时。然后看dis才知道是用这个不等式排序,不错,今天又有新的收获。

不等式排序是这样的:

假设有两个数列,a1 <= a2 <= a3 <= ... <= an, b1 <= b2 <= b3 <= ... <= bn,设c1, c2 ,c3 ... cn是b1,b2,b3,,,bn的任意一个排列,则a1 * bn + a2 * b(n-1)  +... + an * b1 <= a1 *c1 + a2 * c2 + ... + an * cn <= a1 * b1 + a2 * b2 + ... + an * bn,概括起来就是反序和 <= 乱序和 <= 顺序和。

这道题就符合这样的不等式,即采用顺序和的策略即可。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;

typedef long long LL;
const int N = 1010;
struct pig{
	int id;
	LL weight;
}pp[N];
struct village{
	int id;
	LL price;
}vv[N];
LL dis[N],value[N];
bool cmp1(pig a,pig b){
	return a.weight > b.weight;
}
bool cmp2(village a,village b){
	return a.price > b.price;
}
int main(){
	int n;
	LL t;
	while(scanf("%d%lld",&n,&t) != EOF){
		for(int i = 0; i < n; ++i){
		  scanf("%lld",&pp[i].weight);
		  pp[i].id = i + 1;
		}
		sort(pp,pp + n,cmp1);
		for(int i = 0; i < n; ++i){
		  scanf("%lld",&dis[i]);
		}
		for(int i = 0; i < n; ++i){
		  scanf("%lld",&value[i]);
		  vv[i].price = value[i] - dis[i] * t;
		  vv[i].id = i + 1;
		}
		sort(vv,vv+n,cmp2);
		int ans[N];
		for(int i = 0; i < n; ++i){
		  ans[vv[i].id] = pp[i].id;
		}
		for(int i = 1; i < n; ++i)
			printf("%d ",ans[i]);
		printf("%d\n",ans[n]);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值