#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>
struct queue_2stack {
std::vector<long long> s1, s2, min2;
long long min1 = LLONG_MAX;
void push(long long x) {
s1.push_back(x);
min1 = std::min(min1, x);
}
long long query_min() {
ensure_s2_ready();
return std::min(min2.back(), min1);
}
void pop() {
ensure_s2_ready();
s2.pop_back();
min2.pop_back();
}
void ensure_s2_ready() {
if (!s2.empty()) {
return;
}
min1 = LLONG_MAX;
while (!s1.empty()) {
// pop one
long long x = s1.back();
s1.pop_back();
// insert to s2
s2.push_back(x);
min2.push_back(x);
}
for (int i = min2.size() - 2; i >= 0; i--) {
min2[i] = std::min(min2[i], min2[i + 1]);
}
}
};
int main() {
std::cin.tie(NULL)->sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<long long> dp(1, 0);
std::vector<int> diff(n);
long long ans = 0;
long long total = 0;
for (int i = 0; i < n; i++) {
int a, b;
std::cin >> a >> b;
total += a - b;
diff[i] = total > 0 ? -1 : 1;
// subtask 1
ans += std::abs(total);
// subtask 3
queue_2stack q;
std::vector<long long> dp_nxt(dp.size() + a, LLONG_MAX);
for (int j = 0; j < dp.size() + a; j++) {
if (j - a - 1 >= 0) {
q.pop();
}
if (j < dp.size()) {
q.push(dp[j]);
}
dp_nxt[j] = q.query_min();
// for (int k = std::max(0, j - a); k <= j && k < dp.size(); k++) {
// dp_nxt[j] = std::min(dp_nxt[j], dp[k]);
// }
dp_nxt[j] += std::abs(total - j);
}
// std::cerr << "take_min[" << i << "]:"; for (auto x: dp_nxt) std::cerr << " " << x; std::cerr << std::endl;
// for (int j = 0; j < dp.size() + a; j++) {
// dp_nxt[j] += std::abs(total - j);
// }
// std::cerr << "dp[" << i << "]:"; for (auto x: dp_nxt) std::cerr << " " << x; std::cerr << std::endl;
std::swap(dp, dp_nxt);
}
if (total == 1) {
// subtask 2
long long current = ans, total_diff = 0;
while (!diff.empty()) {
total_diff += diff.back();
diff.pop_back();
ans = std::min(ans, current + total_diff);
}
}
// std::cout << ans << std::endl;
std::cout << dp[total] << std::endl;
return 0;
}
Compilation message
bulves.cpp: In function 'int main()':
bulves.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int j = 0; j < dp.size() + a; j++) {
| ~~^~~~~~~~~~~~~~~
bulves.cpp:74:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | if (j < dp.size()) {
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1095 ms |
24096 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1095 ms |
24096 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
34 ms |
724 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1095 ms |
24096 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
1095 ms |
24096 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |