#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int j = 0; j < m; ++j) {
cin >> b[j];
}
vector<int> cant(n + m, -1); //0 - right, 1 - down
set<pair<db, int>, greater<>> st;
set<int> rows, columns;
vector<db> last(n + m);
auto update = [&](int i) {
st.erase({last[i], i});
if (i < n) {
auto it = rows.upper_bound(i);
if (it != rows.end()) {
int j = *it;
st.emplace(last[i] = db(a[j] - a[i]) / (j - i), i);
}
} else {
i -= n;
auto it = columns.upper_bound(i);
if (it != columns.end()) {
int j = *it;
st.emplace(last[i + n] = db(b[j] - b[i]) / (j - i), i + n);
}
}
};
for (int i = 0; i < n; ++i) {
rows.insert(rows.end(), i);
}
for (int i = 0; i < m; ++i) {
columns.insert(columns.end(), i);
}
for (int i = 0; i < n + m; ++i) {
update(i);
}
while (!st.empty() && rows.size() > 1 && columns.size() > 1) {
auto [delta, i] = *st.begin();
st.erase(st.begin());
if (i < n && !rows.count(i) || i >= n && !columns.count(i - n)) {
continue;
}
// cout << delta << " " << i << endl;
if (i >= n) {
cant[i] = 1;
auto it = columns.erase(columns.find(i - n));
if (it != columns.begin()) {
update(*prev(it) + n);
}
} else {
cant[i] = 0;
auto it = rows.erase(rows.find(i));
if (it != rows.begin()) {
update(*prev(it));
}
}
}
// cant[n - 1] = cant[n + m - 1] = -1;
// cant[n - 1] = cant[n + m - 1] = cant[0] = cant[n] = 0;
// for (int i = 0; i < n; ++i) {
// cout << cant[i] << " ";
// }
//
// cout << endl;
//
// for (int j = 0; j < m; ++j) {
// cout << cant[n + j] << " ";
// }
//
// cout << endl;
map<pair<int, int>, ll> saved;
constexpr ll inf = 3e18;
auto solve = [&](auto self, int x, int y) -> ll {
if (y >= m || x >= n) {
return inf;
} else if (x == n - 1 && y == m - 1) {
return 0;
}
if (saved.count({x, y})) {
return saved[{x, y}];
}
ll ans = inf;
if (cant[y + n] != -1 || y == m - 1 || x == 0 && y == 0) {
ans = min(ans, self(self, x + 1, y) + b[y]);
}
if (cant[x] != -1 || x == n - 1 || x == 0 && y == 0) {
ans = min(ans, self(self, x, y + 1) + a[x]);
}
// assert(cant[x] != -1 || cant[y + n] != -1);
return saved[{x, y}] = ans;
};
cout << solve(solve, 0, 0) << '\n';
return 0;
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:71:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
71 | if (i < n && !rows.count(i) || i >= n && !columns.count(i - n)) {
| ~~~~~~^~~~~~~~~~~~~~~~~
kyoto.cpp: In instantiation of 'main()::<lambda(auto:23, int, int)> [with auto:23 = main()::<lambda(auto:23, int, int)>; ll = long long int]':
kyoto.cpp:144:30: required from here
kyoto.cpp:130:55: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
130 | if (cant[y + n] != -1 || y == m - 1 || x == 0 && y == 0) {
| ~~~~~~~^~~~~~~~~
kyoto.cpp:134:51: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
134 | if (cant[x] != -1 || x == n - 1 || x == 0 && y == 0) {
| ~~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
26 ms |
3836 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
839 ms |
63240 KB |
Output is correct |
10 |
Correct |
838 ms |
63164 KB |
Output is correct |
11 |
Correct |
725 ms |
63260 KB |
Output is correct |
12 |
Correct |
718 ms |
63272 KB |
Output is correct |
13 |
Correct |
766 ms |
63288 KB |
Output is correct |
14 |
Correct |
787 ms |
63208 KB |
Output is correct |
15 |
Correct |
804 ms |
63120 KB |
Output is correct |
16 |
Correct |
795 ms |
63060 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
247 ms |
24944 KB |
Output is correct |
4 |
Execution timed out |
2093 ms |
131996 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
26 ms |
3836 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
839 ms |
63240 KB |
Output is correct |
10 |
Correct |
838 ms |
63164 KB |
Output is correct |
11 |
Correct |
725 ms |
63260 KB |
Output is correct |
12 |
Correct |
718 ms |
63272 KB |
Output is correct |
13 |
Correct |
766 ms |
63288 KB |
Output is correct |
14 |
Correct |
787 ms |
63208 KB |
Output is correct |
15 |
Correct |
804 ms |
63120 KB |
Output is correct |
16 |
Correct |
795 ms |
63060 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
247 ms |
24944 KB |
Output is correct |
32 |
Execution timed out |
2093 ms |
131996 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |