您的位置:首页 > 其它

1033. To Fill or Not to Fill

2018-01-28 21:38 423 查看


1033. To Fill or Not to Fill (25)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

ZHANG, Guochuan

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.如果在该点能够到达的所有点中,如果没有油价低于当前油价的点,则将油箱加满,到达距离该点最远的点,因为其他点油更贵,所以还不在该点加满油。

其实想这个题的时候,我只完整的想到了第一点,至于第二点,我当初的想法是寻找这些点中价格最低的点,结果越想越麻烦,每次找最小的点都很麻烦,而且我从没想过会让油箱加满油,只想让油箱里加入刚好走到下一个点的油。总之,这个题是我在知道思路后解决了,没有看源码。所以是一道很不错的题,开拓了自己的思维。

注意两点:1.起始点的坐标也许不是0。

2.刚开始输入的油箱容量,距离,以及单位数量的油行驶的距离都是浮点类型的。

这两点完全没想到,考虑问题不周密,以下是我写的代码:

 #include<bits/stdc++.h>
using namespace std;
struct node{
double p;
double x;
}a[505];
bool cmp(node x,node y){
return x.x<y.x;
}
int main(){

double c,d,s;
int n,len=0,len1=0;
scanf("%lf%lf%lf%d",&c,&d,&s,&n);
for(int i=1;i<=n;i++){
scanf("%lf%lf", &a[i].p, &a[i].x);
}
n++;
a
.x=d;
a
.p=0;
sort(a+1,a+1+n,cmp);
if(a[1].x!=0){
printf("The maximum travel distance = 0.00\n");
}else{
int start=1,f=1;
double rest=0,sum=0;
node e;
while(1){
if(start==n||!f) break;
int flag=0,x;
for(int i=start+1;i<=n;i++){
if(a[i].p>=a[start].p&&a[i].x-a[start].x<=c*s){
e.p=a[i].p;
e.x=a[i].x;
x=i;
}else if(a[i].p<a[start].p&&a[i].x-a[start].x<=c*s){
flag=1;
e.p=a[i].p;
e.x=a[i].x;
x=i;
break;
}else if(a[i].x-a[start].x>c*s&&i==n){
f=0;
x=i-1;
break;
}else{
break;
}
}
if(f){
if(flag){
if(rest>=(e.x-a[start].x)/s){
rest=rest-(e.x-a[start].x)/s;
}else{
sum=sum+((e.x-a[start].x)/s-rest)*a[start].p;
rest=0;
}
}else{
sum=sum+(c-rest)*a[start].p;
rest=c-(e.x-a[start].x)/s;
}
}
start=x;
}
if(f){
printf("%.2lf\n",sum);
}else{
printf("The maximum travel distance = %.2lf\n",a[start].x+c*s);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: