| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3300 | Accepted: 1010 |
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 ninteger 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题目链接:http://poj.org/problem?id=3544
题意:从起点出发到终点途中要经过n个村庄,每到达一个村庄就需卖出一头猪来增加旅费,现在给定每头猪的重量,每个村庄距离起点的距离,每个村庄猪肉的价格和每公斤猪每走一公里维持体重所需的花费t。输出获得的最大旅费。
解题思路:贪心!将每个村庄的能卖出的实际价格(当地价格减去每公斤猪一路上的花费)升序排序,再讲猪的重量升序排序,重量最大的猪在价格最贵的村庄卖出。
AC代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int NUM = 1000+10;
typedef long long LL;
struct Village{
LL p;//每个村猪肉的价格
LL no;//村庄的编号
LL d;//村庄距离起点的距离
};
struct Pig{
LL w;//猪的重量
LL no;//猪的编号
};
Village vill[NUM];//村庄数组
Pig pig[NUM];//猪的数组
LL n,t;
bool cmp1(Pig p1,Pig p2)//将猪的重量升序排序
{
return p1.w<=p2.w;
}
bool cmp2(Village v1,Village v2)//将每个村庄的猪肉价格升序排序
{
return v1.p<=v2.p;
}
void solve()
{
int i,j;
int flag;
for(i=1,flag=1;i<=n;i++) //因为是要输出1-n个村庄所卖出猪的序号
{
for(j=0;j<n;j++)
{
if(vill[j].no==i) //找到第i个村庄,已经排好序按照贪心的思想,第i个村庄的下标对应着猪的下标
{
if(flag)//格式控制
{
printf("%lld",pig[j].no);
flag=0;
}
else printf(" %lld",pig[j].no);
}
}
}
printf("\n");
}
int main(void)
{
int i;
while(scanf("%lld%lld",&n,&t)==2)//输如村庄个数和每公斤猪每公里消耗的费用
{
for(i=0;i<n;i++)
{
scanf("%lld",&pig[i].w); //输入猪的重量
pig[i].no=i+1; //记录猪的编号
}
for(i=0;i<n;i++)
{
scanf("%lld",&vill[i].d); //输入村庄的距离
vill[i].no=i+1; //记录村庄的编号
}
for(i=0;i<n;i++)
{
scanf("%lld",&vill[i].p); //村庄猪肉的价格
vill[i].p=vill[i].p-t*vill[i].d; //实际价格要等于当地价格减去达到村庄的路上每公斤猪肉所消耗的钱
}
sort(pig,pig+n,cmp1);//将猪肉重量按升序排序
sort(vill,vill+n,cmp2);//实际价格按照升序排序
solve();
}
return 0;
}
本文介绍了一道关于如何在旅行中通过卖猪赚取最大收益的问题。通过将村庄的猪肉实际售价排序,并对猪的重量进行排序,采用贪心算法求解最优解。
528

被折叠的 条评论
为什么被折叠?



