poj 3544 Journey with Pigs

时间:2024-12-26 13:34:56
Journey with Pigs
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3004   Accepted: 922

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 visit n small villages so he decided to earn some money. He tooks n pigs and plans to sell one pig in each village he visits.

Pork prices in villages are different, in the j-th village the people would buy a pork at pj rubles per kilogram. The distance from town A to the j-th village along the road to town B is dj kilometers.

Pigs have different weights. Transporting one kilogram of pork per one kilometer of the road needs t rubles 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 numbers n (1 ≤ n ≤ 1000) and t (1 ≤ t ≤ 109). The second line contains n integer numbers wi (1 ≤ wi ≤ 109) — the weights of the pigs. The third line contains n integer numbers dj (1 ≤ dj ≤ 109) — the distances to the villages from the town A. The fourth line contains n integer numbers pj (1 ≤ pj ≤ 109) — the prices of pork in the villages.

Output

Output n numbers, the j-th number is the number of pig to sell in the j-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

思路:
问题中每斤猪肉被出售到第j个村庄的利润为:猪肉单价 - 路费单价 * 路程;第一行按照猪的质量从小到大排序的数;第二行按照利润从小到大排序的数;
两行数相互相乘所有积的和有这样的规律:逆序积的和 <= 乱序积的和 <= 顺序积的和(这是一种贪心的思想)。
具体步骤如下:
step1:根据输入计算每斤猪肉被出售到第j个村庄的利润(猪肉单价 - 路费单价 * 路程)。
step2:将每斤猪肉被出售到第j个村庄的利润与每只猪的质量进行从小到大排序,则对应位置的猪出售到对应位置编号的村庄。
 #include <iostream>
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std; typedef struct{
LL value;
int postion;
}Node; Node weight[], earn[]; bool cmp(Node a, Node b){
return a.value < b.value;
} int main(){
int n, i;
LL t;
while(scanf("%d %lld", &n, &t) != EOF){
for(i = ; i <= n; i++){
scanf("%lld", &weight[i].value);
weight[i].postion = i;
}
LL dis[];
for(i = ; i <= n; i++){
scanf("%lld", &dis[i]);
} for(i = ; i <= n; i++){
LL x;
scanf("%lld", &x);
earn[i].value = x - dis[i] * t;
earn[i].postion = i;
} sort(weight + , weight + n + , cmp);
sort(earn + , earn + n + , cmp); int ans[]; for(i = ; i <= n; i++)
ans[earn[i].postion] = weight[i].postion; for(i = ; i < n; i++)
printf("%d ", ans[i]);
printf("%d\n", ans[n]);
}
return ;
}