luogu P2190 小Z的车厢
题目链接:luogu P2190 小Z的车厢
题目大意:
题解:
一开始认为很简单,就是一个普通的模拟,然后全WA。
// 错误代码
#include <iostream>
using namespace std;
#define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define N 1000000 + 10
int n, m, up[N], down[N], train, sum;
int main() {
io_speed_up;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y, z;
cin >> x >> y >> z;
up[x] += z;
down[y] += z;
}
for (int i = 1; i <= n; ++i) {
train += up[i] - down[i];
sum = sum >= train ? sum : train;
}
if (sum % 36) {
sum = sum / 36 + 1;
} else {
sum /= 36;
}
cout << sum;
return 0;
}
发现题目里的细节“环形铁轨”,所以存在\(x\)比\(y\)大的情况,所以转换一下思想,假如\(x\)比\(y\)大,那就相当于在\(x\)上车,在\(n+1\)下车,再在\(1\)上车,在\(y\)下车。(由于到\(n\)就结束,所以\(n+1\)的下车可以忽略)
#include <iostream>
using namespace std;
#define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define N 1000000 + 10
int n, m, up[N], down[N], train, sum;
int main() {
io_speed_up;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y, z;
cin >> x >> y >> z;
up[x] += z;
down[y] += z;
if (x > y) {
up[1] += z;
}
}
for (int i = 1; i <= n; ++i) {
train += up[i] - down[i];
sum = sum >= train ? sum : train;
}
if (sum % 36) {
sum = sum / 36 + 1;
} else {
sum /= 36;
}
cout << sum;
return 0;
}