Problem Description
ACdream王国有一条贯穿整个王国的高速公路,一天,你要驾驶着一辆油箱容量为P的车从高速公路的一头驶向另一头,总路程为L千米,每单位体积的汽油可维持行驶W千米,显然路途遥远~总有不够油的情况,所以就要加油~!
再高速公路上总共有N个加油站,但是由于是不同人开的,因此定价也参差不齐。
现在你知道每个加油站的位置,以及每个加油站的单价,问你最少需要多少钱才能到达另一头?
Input
多组数据,每组数据首先是四个整数,P(1<=P<=100),L(1<=L<=30000),W(1<=W<=20),N(1<=N<=500),分别代表油箱容量,路程,每单位体积的路程,加油站数目。
接下来是N行,每行包括一个精确到百分位的实数X(9
Output
对于每组数据,如果能顺利抵达终点,则先输出一个”YES”,然后输出最少花费。
否则输出一个”NO”,再输出最远可以到达的位置。
字符串不包括双引号,注意大小写,输出数字只需要精确到百分之一即为正确。
Sample Input
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
50 1300 12 2
7.10 0
7.00 600
Sample Output
YES
749.17
NO
1200.00
My Problem Report
这道题看起来很简单,但是用到了一些常用技巧:离散化,暴力,贪心。首先我们将每公里路离散化(空间允许),然后对每公里路用一个price数组来维护走完该公里路需要付出的代价。枚举每一个加油站,分别扫描该加油站往后1~p*w距离的公路,每公里路按最小代价更新。
Trick
一开始车是一点油都没有的,注意价格是按每升算。
My Code
// Created by Chlerry in 2015.
// Copyright (c) 2015 Chlerry. All rights reserved.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
struct station
{
int loc;
double p;
}a[600];
bool cmp(station a,station b)
{
return a.p<b.p;
}
int main()
{
freopen("in.txt","r",stdin);
int v,s,w,n;
double price[32010],cnt;
while(cin>>v)
{
cin>>s>>w>>n;
cnt=0.0;
for(int i=0;i<n;i++)
cin>>a[i].p>>a[i].loc;
sort(a,a+n,cmp);
for(int i=1;i<=s;i++)
price[i]=INT_MAX/10;
for(int i=0;i<n;i++)
for(int j=1;j<=v*w;j++)
if(price[a[i].loc+j]>a[i].p)
price[a[i].loc+j]=a[i].p;
int k;
for(k=1;k<=s;k++)
if(price[k]==INT_MAX/10)
break;
else
cnt+=price[k];
if(k<=s)
printf("NO\n%d\n",k-1);
else
printf("YES\n%.2f\n",cnt/w);
}
return 0;
}