pat甲级1033

1033 To Fill or Not to Fill (25 point(s))

原地址

思路:

  1. 模拟。先思考你会怎么一步步做,写出伪代码,然后转换成代码。

先求出从当前位置出发,加满油能跑的距离。
若没有能到达的点,即到不了终点,输出。
若有能到达的点,则从近到远,查看可以到达的站点,如果找到汽油价格比当前价格低的就前往这个站点。
如果不存在这样的站点,就看看能到的最远的站点是不是终点,若是终点就到终点。

若不是终点,就加满油前往该站点(这里加满也到不了终点,为了价格最低所以加满)。

代码:

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

using namespace std;
const int maxn = 1e6+5;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
#define pb push_back

struct {
double price;
int dis;
bool operator < (const node& b) {
return dis > b.dis;
}
}a[maxn];
vector<node> v;

bool cmp (const node&a,const node&b) {
return a.dis < b.dis;
}

bool vis[maxn]={false};

int main() {
#ifdef ONLIGE_JUDGE
#else

#endif // ONLIGE_JUDGE
int c,dis,run,n,tmpD = 0;
double res = 0,tmpP = -1,tmpC = 0;
cin >> c >> dis >> run >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i].price >> a[i].dis;
if (a[i].dis == 0) tmpP = a[i].price;
}
if (tmpP < 0) {//can't reach next station
return printf("The maximum travel distance = %.2fn",(double)tmpD),0;
}
sort(a,a+n,cmp);
node d;d.dis = dis;
a[n] = d;
int j = 0;
while (tmpD < dis) {
int maxDis = tmpD + c * run;
v.clear();
for (int i = 1; i <= n; ++i)
if (tmpD < a[i].dis && maxDis >= a[i].dis) {
v.pb(a[i]);
}
if (v.size()) {//can reach next station
j = 0;
while (v[j].price > tmpP) j++;
if (j >= v.size()) {//can't find cheaper station
j = min_element(v.begin(),v.end()) - v.begin();//chose the maxdis station from available station
if (v[j].dis == dis) {
res += (v[j].dis - tmpD) / (double)run * tmpP;
} else {
res += (c - tmpC) * tmpP;
tmpC = c - (v[j].dis - tmpD) / (double)run;
}
tmpD = v[j].dis;
tmpP = v[j].price;
} else {
tmpC -= (v[j].dis - tmpD) / (double)run;
if (tmpC < 0) {
res += fabs(tmpC) * tmpP;
tmpC = 0;
}
tmpD = v[j].dis;
tmpP = v[j].price;
}
} else {//can't reach next station
printf("The maximum travel distance = %.2fn",(double)maxDis);
break;
}
} if (tmpD >= dis) {
printf("%.2fn",res);
}
return 0;
}