经典的贪心算法 Journey with Pigs (POJ 3544)

Description

Farmer John has a pig farm near town A. He wants to visit his friend living in town B. During this journey he will visitnsmall villages so he decided to earn some money. He tooksnpigs and plans to sell one pig in each village he visits.

Pork prices in villages are different, in thej-th village the people would buy a pork atpjrubles per kilogram. The distance from town A to thej-th village along the road to town B isdjkilometers.

Pigs have different weights. Transporting one kilogram of pork per one kilometer of the road needstrubles for addition fuel.

Help John decide, which pig to sell in each town in order to earn as much money as possible.

Input

The first line of the input file contains integer numbersn(1 ≤n≤ 1000) andt(1 ≤t≤ 109). The second line containsninteger numberswi(1 ≤wi≤ 109) — the weights of the pigs. The third line containsninteger numbersdj(1 ≤dj≤ 109) — the distances to the villages from the town A. The fourth line containsninteger numberspj(1 ≤pj≤ 109) — the prices of pork in the villages.

Output

Outputnnumbers, thej-th number is the number of pig to sell in thej-th village. The pigs are numbered from 1 in the order they are listed in the input file.

Sample Input

3 1
10 20 15
10 20 30
50 70 60

Sample Output

3 2 1


#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
typedef long long int64;
struct Pig{
	int64 value;
	int pos;
};
bool cmp(const Pig & p1,const Pig & p2){
	return p1.value < p2.value;
}
int64 n,t;
/* pigsW  pigsVal 存储的数据代表这不同的含义 */
Pig pigsW[1002]; //每只pig的 重量 和 标号
Pig pigsVal[1002];//每只pig的 在位置pos处 的  收益
int64 d[1002],price;
int ans[1002];
int main() {
	int64 i;
	//ifstream cin("D:\\C++\\algo_poj\\猪_poj_3544\\in.txt");

	while(cin >> n >> t){

	for(i=1; i<=n; i++){
		cin >> pigsW[i].value;
		pigsW[i].pos = i;
	}
	for(i=1; i<=n; i++)
		cin >> d[i];
	for(i=1; i<=n; i++){
		cin >> price;
		pigsVal[i].value = price - d[i]*t; //每斤猪肉(针对所有猪而言)在位置i处的收益
		pigsVal[i].pos = i;
	}
	sort(pigsW+1, pigsW +1+ n,cmp); //先按照 猪重量排序
	sort(pigsVal+1, pigsVal +1+ n,cmp); //再按照每个位置的收益排序

	//重量大的猪 在 收益高的位置
	for(i=1; i<=n; i++)
		ans[ pigsVal[i].pos ] = pigsW[i].pos; //可以保证每个位置被用一次。根据贪心策略,可以正确求解
	for(i=1; i<n; i++)
		printf("%d ",ans[i]);
	printf("%d\n",ans[i]);

	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值