이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
}
set<tuple<db, int, int>> st;
set<int> rows, columns;
vector<int> last(n + m);
int iter = 0;
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 - 1; ++i) {
st.emplace(a[i + 1] - a[i], i, 0);
}
for (int i = n; i < n + m - 1; ++i) {
st.emplace(b[i + 1 - n] - b[i - n], i, 0);
}
vector<int> when(n + m, -1);
while (!st.empty()) {
auto [delta, i, iteration] = *st.begin();
st.erase(st.begin());
if (last[i] != iteration) {
continue;
}
when[i] = ++iter;
if (i >= n) {
auto it = columns.erase(columns.find(i - n));
if (it != columns.begin()) {
int j = *it, p = *prev(it);
st.emplace(db(b[j] - b[p]) / (j - p), p + n, last[p + n] = iter);
}
} else {
auto it = rows.erase(rows.find(i));
if (it != rows.begin()) {
int j = *it, p = *prev(it);
st.emplace(db(a[j] - a[p]) / (j - p), p, last[p] = iter);
}
}
}
int x = 0, y = 0;
ll ans = 0;
while (x < n - 1 || y < m - 1) {
if (y < m - 1 && (x == n - 1 || when[x] > when[y + n])) {
ans += a[x];
y += 1;
} else {
ans += b[y];
x += 1;
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |