POJ 3544 Journey with Pigs

本文介绍了一道关于如何在旅行中通过卖猪赚取最大收益的问题。通过将村庄的猪肉实际售价排序,并对猪的重量进行排序,采用贪心算法求解最优解。

Journey with Pigs
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;
}


独立储能的现货电能量与调频辅助服务市场出清协调机制(Matlab代码实现)内容概要:本文围绕“独立储能的现货电能量与调频辅助服务市场出清协调机制”展开,提出了一种基于Matlab代码实现的优化模型,旨在协调独立储能系统在电力现货市场与调频辅助服务市场中的联合出清问题。文中结合鲁棒优化、大M法和C&CG算法处理不确定性因素,构建了多市场耦合的双层或两阶段优化框架,实现了储能资源在能量市场和辅助服务市场间的最优分配。研究涵盖了市场出清机制设计、储能运行策略建模、不确定性建模及求解算法实现,并通过Matlab仿真验证了所提方法的有效性和经济性。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事电力市场、储能调度相关工作的工程技术人员。; 使用场景及目标:①用于研究独立储能在多电力市场环境下的协同优化运行机制;②支撑电力市场机制设计、储能参与市场的竞价策略分析及政策仿真;③为学术论文复现、课题研究和技术开发提供可运行的代码参考。; 阅读建议:建议读者结合文档中提供的Matlab代码与算法原理同步学习,重点关注模型构建逻辑、不确定性处理方式及C&CG算法的具体实现步骤,宜在掌握基础优化理论的前提下进行深入研读与仿真调试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值