difficult
徐仓仓 Lv3

codeup

To Fill or Not to Fill

初始化每个加油站的start和end,遍历每个加油站可到达的加油站,比较价格,修改start和end。

只过了pat

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
//codeup To Fill or Not to Fill
//汽油容量 距离 每个gas可跑距离 汽油站数量
//59 525 19 2
//单价 距离
//3 314
//3 0
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct station {
double price;
double start;
double end;
};
bool cmp(station a, station b) {
return a.start < b.start;
}
int main() {
int max, meter, per_meter, n;
station sta[510];
while (cin >> max >> meter >> per_meter >> n) {
for (int i = 0; i < n; i++) {
cin >> sta[i].price >> sta[i].start;
sta[i].end = sta[i].start + max * per_meter;
}

sort(sta, sta + n, cmp);
/*for (int i = 0; i < n; i++) {
cout << "排序结果\n";
cout << sta[i].start << " " << sta[i].end << " "<<sta[i].price<<endl;
}*/
if (sta[0].start != 0 || n == 0) {
cout << "The maximum travel distance = ";
printf("%.2f\n",0.00);
break;
}
for (int i = 0; i < n; i++) {
if (sta[i].end > meter) {
sta[i].end = meter;
}
for (int j = i + 1; j < n; j++) {
if (sta[j].start < sta[i].end) {
if (sta[j].price <= sta[i].price) {//新的更便宜
if(sta[i].start>sta[j].start &&sta[i].end<sta[j].end){//被包围
sta[i].end = sta[i].start;
break;
}
sta[i].end = sta[j].start;
}
else {
sta[j].start = sta[i].end;
}
}
else {//没有加油站了
break;
}
}
}
double money = 0, all_meter = 0;
for (int i = 0; i < n; i++) {
//cout << sta[i].start << " " << sta[i].end << endl;
all_meter += sta[i].end - sta[i].start;
money += (sta[i].end - sta[i].start) / per_meter * sta[i].price;
}
if (all_meter < meter) {
cout << "The maximum travel distance = ";
printf("%.2f\n", all_meter);
}
else {
printf("%.2f\n", money);
}
}
}

【递归入门】出栈序列统计

是一道深度优先的递归题,抄了答案。
[8.1小节递归入门E]: http://codeup.hustoj.com/problem.php?cid=100000608&pid=4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int n;
int cnt;
void f(int in, int out) {
if (out > n) {
cnt++;
return;
}
if (in > n || out > in) {
return;
}
else {
f(in + 1, out);
f(in, out + 1);
}
}
int main() {
while (cin >> n) {
cnt = 0;
f(0, 0);
cout << cnt << endl;
}
}
 Comments