poj 3544 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个村庄的利润与每只猪的质量进行从小到大排序,则对应位置的猪出售到对应位置编号的村庄。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #define LL long long 5 using namespace std; 6 7 typedef struct{ 8 LL value; 9 int postion; 10 }Node; 11 12 Node weight[1005], earn[1005]; 13 14 bool cmp(Node a, Node b){ 15 return a.value < b.value; 16 } 17 18 int main(){ 19 int n, i; 20 LL t; 21 while(scanf("%d %lld", &n, &t) != EOF){ 22 for(i = 1; i <= n; i++){ 23 scanf("%lld", &weight[i].value); 24 weight[i].postion = i; 25 } 26 LL dis[1005]; 27 for(i = 1; i <= n; i++){ 28 scanf("%lld", &dis[i]); 29 } 30 31 for(i = 1; i <= n; i++){ 32 LL x; 33 scanf("%lld", &x); 34 earn[i].value = x - dis[i] * t; 35 earn[i].postion = i; 36 } 37 38 sort(weight + 1, weight + n + 1, cmp); 39 sort(earn + 1, earn + n + 1, cmp); 40 41 int ans[1005]; 42 43 for(i = 1; i <= n; i++) 44 ans[earn[i].postion] = weight[i].postion; 45 46 for(i = 1; i < n; i++) 47 printf("%d ", ans[i]); 48 printf("%d\n", ans[n]); 49 } 50 return 0; 51 }