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<cstdio>
#include<iostream>
#include<algorithm>

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值