-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoj1437.cpp
104 lines (96 loc) · 1.61 KB
/
oj1437.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <algorithm>
using namespace std;
struct gasStation
{
float pr; //price
float di; //distance
} buf[501];
float Cmax,D,Davg;
int n;
float gas;
bool cmp(gasStation a,gasStation b){
return a.di<b.di;
}
float fee(gasStation a,gasStation b){
float money;
float addgas;
if(a.pr>b.pr)
addgas=(b.di-a.di)/Davg-gas;
else
addgas=Cmax-gas;
gas=gas+addgas-(b.di-a.di)/Davg;
money=addgas*a.pr;
return money;
}
int findStation(int a,int b){
int min=a;
for(int i=a;i<b;i++)
if(buf[i].pr<buf[min].pr)
min=i;
if(min==a)
return -1;
else
return min;
}
int findLong(int a){
int max;
float tmp=Cmax*Davg;
for(int i=a;i<=n;i++)
{
if(buf[i].di-buf[a].di<=tmp)
max=i;
else
break;
}
if(max==a)
return -1;
else
return max;
}
int main(){
while(scanf("%f%f%f%d",&Cmax,&D,&Davg,&n)!=EOF){
for(int i=0;i<n;i++)
scanf("%f%f",&buf[i].pr,&buf[i].di);
sort(buf,buf+n,cmp);
buf[n].di=D;
buf[n].pr=0;
int start=0;
int end=0;
float distance=0;
float price=0;
int flag=0;
gas=0;
if(buf[0].di!=0)
{
flag=1;
goto print;
}
while(start!=n)
{
end=findLong(start);
if(end==-1)
{
distance=distance+Davg*Cmax;
flag=1;
break;
}
int tmp=0;
while(tmp=findStation(start,end))
{
if(tmp!=-1)
end=tmp;
else
break;
}
price=price+fee(buf[start],buf[end]);
distance=buf[end].di;
start=end;
}
print:if(flag)
printf("The maximum travel distance = %.2f\n",distance);
else
printf("%.2f\n",price);
}
return 0;
}