poj 3544 Journey with Pigs——贪心策略

转载于:http://www.voidcn.com/article/p-yhwbesvr-bkh.html

Journey with Pigs

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

题意: 你要从A村到B村去看盆友,途中会经过n个村,你打算带n头猪去卖,每个村卖一头,赚点小钱。每个村的猪肉价格都不一样,而且你驮着这些猪每公斤猪肉每走一公里会额外花掉你的一些钱,这个花销是t。现在要求你要再哪个村卖哪头猪能够使你赚的钱最多。
输入:

  • 第一行:n 和每走一公里一公斤猪肉所需要的运费 t
  • 第二行:所携带的每头猪的重量(按照编号分别是1、2、3……)
  • 第三行;每个村庄到A村的的距离。
  • 第四行:途中经过的村,每个村愿意出的价格。

c++ AC 代码

#include
#include
#include

typedef long long ll;

struct Pig
{
    ll value,pos;
    bool operator <(const Pig a)const
    {
        return value < a.value;
    }
};

Pig pigweight[1005];	// 每只猪的重量和编号
Pig pigmoney[1005];		// 每只猪再pos出的收益

int main()
{
    ll n,t,d[1005];
    scanf("%lld%lld",&n,&t);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld",&pigweight[i].value);
        pigweight[i].pos = i;
    }

    for (int i = 1; i <= n; i++)
        scanf("%lld",&d[i]);

    for (int i = 1; i <= n; i++)
    {
        scanf("%lld",&pigmoney[i].value);
        pigmoney[i].value -= d[i]*t;	// 每斤猪肉在位置i处的收益
        pigmoney[i].pos = i;
    }
    
    std::sort(pigmoney+1,pigmoney+1+n);		// 按照每个位置的收益排序
    std::sort(pigweight+1,pigweight+1+n);	// 按照猪的重量排序

    int ans[1005];
    // 重的猪我们把它在收益高的村庄卖掉
    for(int i=1;i<=n;i++)
        ans[pigmoney[i].pos] = pigweight[i].pos;
    

    for (int i = 1; i < n; i++)
        printf("%d ",ans[i]);
    printf("%d\n",ans[n]);

    return 0;
}

你可能感兴趣的:(贪心)