PAT 1033

题目 : To Fill or Not to Fill

分值 : 25
难度 : 难题
思路 : 贪心,我模拟一下从起点到终点的各个路径最便宜的油价,加起来就好了
坑点 : 就是能否到达终点,这里会有较多测试点,我一开始输在了起点就没加油站的case上
评语 : 没有一次ac很不爽

具体代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <algorithm>
using namespace std ;
#define Max_ 9999999999
typedef struct Node
{
int start ;
int end ;
double price ;
}Nodes ;
int dis[1010] ;
Nodes Gas[501] ;
bool cmp(int a ,int b)
{
return b >a ;
}
int main() {
int Cmax , Dis ,Per , N ;
cin >> Cmax >> Dis >>Per >>N ;
for(int i = 0 ; i< N ; i++)
{
cin>>Gas[i].price >>Gas[i].start ;
Gas[i].end = Gas[i].start + Per*Cmax ;
dis[i*2] = Gas[i].start ;
dis[i*2+1] = Gas[i].end ;
}

sort(dis , dis + 2*N , cmp ) ;
double cost = 0 ;
double min_price = 0;
int old_temp = 0 ;
int temp = 0 ;
for(int i = 0 ; i< 2*N ; i++)
{
temp = dis[i] ;
if(min_price==Max_ || (!i&&temp) ){temp = old_temp ; break ; }
if(temp >=Dis) { temp = Dis; cost += (temp-old_temp)*min_price ; break ;}

cost += (temp-old_temp)*min_price ;
//cout<< "在"<<old_temp <<":"<<temp<<"之间选择了价格"<<min_price <<endl ;
min_price =Max_;
for(int i = 0 ; i< N ; i++)
{
if(temp <Gas[i].end && temp >= Gas[i].start)
{
if(Gas[i].price < min_price)
min_price = Gas[i].price ;
}
}
old_temp = temp ;
//cout <<temp<<endl ;
}
if(temp <Dis)
printf("The maximum travel distance = %.2lf\n",(double)temp) ;
else
printf("%.2lf", cost/Per) ;
//cout << cost/Per <<endl ;
}