上饶市彦顺网络科技有限公司
首页 | 联系方式 | 加入收藏 | 设为首页 | 手机站

产品目录

联系方式

联系人:业务部
电话: 00137-363357
邮箱:service@yuandamt.com

当前位置:首页 >> 新闻中心 >> 正文

一道贪心题目(To Fill or Not to Fill )

字号:
摘要:一道贪心题目(To Fill or Not to Fill )


遇到一道挺不错的贪心题目,记录一下:


/*
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
 Input Specification: 
Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.
 Output Specification: 
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.
Sample Input 1:50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
 Sample Output 1:749.17
 Sample Input 2:50 1300 12 2
7.10 0
7.00 600
 Sample Output 2:The maximum travel distance = 1200.00
*/
总体思路:

当然首先按照距离将加油站排序,由近到远。我们的策略如下:

一、当从当前加油站往后可以寻找到第一个油价比当前加油站便宜的加油站,若能找到则做如下考虑:

1、如果当前剩余油量足够行驶到这个油价更便宜的加油站,则不必加油,直接行驶至此加油站,计算剩余油量。

2、如果当前油量不足以行驶到这个油价更便宜的加油站,则加油到刚好能够到达此加油站的油量,行驶过去,此时,油量剩余为0。

二、当从当前加油站往后可达的加油站序列中寻找不到油价比当前加油站价格更低的加油站,则寻找到在可达范围内油价最便宜的加油站(此时这个加油站的油价>当前加油站的油价),这种情况,在当前加油站加满油,行驶过去,计算剩余油量。

三、在新的起点,新的加油站,重复以上策略。

四、考虑,如果第一个加油站如果不在起点,则最大行驶距离为0,特殊处理。

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
typedef struct station{
 double price;
 double dist;
 bool operator<(const station& a)const
 {
 return dist<a.dist;
 }
}station;
station curr[505];
int main()
{
 double Cmax,Dist,Davg;
 int i,j,N,next;
 double total_cost,min_price,remain_gas,full_dist;
 freopen("f:/in.txt","r",stdin);
 while(~scanf("%lf%lf%lf%d",&Cmax,&Dist,&Davg,&N))
 {
 for(i=0;i<N;i++)
 {
 scanf("%lf%lf",&curr[i].price,&curr[i].dist);
 }
 sort(curr,curr+N);
 full_dist=Cmax*Davg;//加满油最多可以跑的距离
 curr[N].dist=Dist;
 curr[N].price=0;
 if(curr[0].dist!=0) // 起始点没有加油站,特殊处理
 {
 printf("The maximum travel distance = 0.00\n");
 continue;
 }
 total_cost=0;
 remain_gas=0;
 for(i=0;i<N;)
 {
 min_price=curr[i+1].price;
 next=i+1;
 for(j=i+1;j<=N;j++) //寻找下一个加油站
 {
 if(curr[j].dist<=curr[i].dist+full_dist&&curr[j].price<=curr[i].price)
 {
 next=j;
 break;
 }
 if(curr[j].dist<=curr[i].dist+full_dist&&min_price>curr[i].price)
 {
 min_price=curr[j].price;
 next=j;
 }
 else
 break; //无法到达此加油站以及更远的加油站
 }
 if(curr[next].dist<=curr[i].dist+full_dist) //加满油,可以到达下一站?
 {
 if(curr[next].price<=curr[i].price)
 {
 if(remain_gas>=(curr[next].dist-curr[i].dist)/Davg)//剩余油足够跑到下一个价格更低的加油站
 {
 remain_gas-=(curr[next].dist-curr[i].dist)/Davg; //跑到下一个加油站next后剩余的油数
 }
 else //加够油量,刚好跑到下一个价格更低的加油站
 { 
 total_cost+=((curr[next].dist-curr[i].dist)/Davg-remain_gas)*curr[i].price;
 remain_gas=0; //跑到下一个加油站next后剩余的油数
 }
 }
 else
 {
 total_cost+=(Cmax-remain_gas)*curr[i].price*1.0;//如果下一个加油站的油价比当前加油站油价贵,在当前加油站加满油
 remain_gas=Cmax-(curr[next].dist-curr[i].dist)/Davg; //跑到下一个加油站next后剩余的油数
 }
 }
 else
 break;
 i=next; //更新此时的位置,下一个加油站,
 }
 if(i==N)
 printf("%.2lf\n",total_cost);
 else
 printf("The maximum travel distance = %.2lf\n",curr[i].dist+full_dist);
 }
 fclose(stdin);
 return 0;
}