Hello, I don’t quite understand the solution to this problem.
With the following example:
2
on 5 5
none 3 10
The output should be :
0 5
5 10
but the solution gives :
0 5
3 10
Why is this?
At the end, there will be at least 5 because 5 has been there since the beginning.
Here’s my solution, which gives the correct solution on my example and on the USACO site :
#include <bits/stdc++.h>
struct Data {
std::string s;
int x, y;
};
int
main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#ifndef LOCAL
std::freopen("traffic.in", "r", stdin);
std::freopen("traffic.out", "w", stdout);
#endif
int n;
std::cin >> n;
std::vector<Data> a(n);
for (int i = n - 1; i >= 0; i--)
std::cin >> a[i].s >> a[i].x >> a[i].y;
int min = 0;
int max = 1e9;
for (const auto &i : a) {
if (i.s == "none") {
min = std::max(min, i.x);
max = std::min(max, i.y);
} else if (i.s == "on") {
min = std::max(0, min - i.y);
max -= i.x;
} else {
min += i.x;
max += i.y;
}
}
std::cout << min << ' ' << max << '\n';
min = 0;
max = 1e9;
std::reverse(a.begin(), a.end());
for (const auto &i : a) {
if (i.s == "none") {
min = std::max(min, i.x);
max = std::min(max, i.y);
} else if (i.s == "off") {
min = std::max(0, min - i.y);
max -= i.x;
} else {
min += i.x;
max += i.y;
}
}
std::cout << min << ' ' << max << '\n';
}
Thanks