#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int inf = 1e9 + 5;
auto compare_frac = [](pii a, pii b) -> bool { return (ll)a.first * b.second < (ll)b.first * a.second; };
auto convoluted_cmpfrac = [](pair<pii,int> a, pair<pii,int> b) -> bool { return compare_frac(a.first, b.first) || (!compare_frac(a.first, b.first) && !compare_frac(b.first, a.first) && a.second < b.second); };
struct Turneu {
set<int> appears;
int *a;
set<pair<pii, int>, decltype(convoluted_cmpfrac)> heap;
Turneu(): heap(convoluted_cmpfrac) {;}
int n;
template<class iteratorlmao>
void insertIt(iteratorlmao it) {
if(*next(it) == n + 1 || *it == -1) return;
//cerr << *it << '\t' << a[*next(it)] << ' ' << a[*it] << '\n';
//cerr << "inseram\n";
//cerr << a << '\t' << *it << ", " << *next(it) << '\t';
//cerr << a[*next(it)] - a[*it] << ' ' << *next(it) - *it << '\t' << *it << '\n';
heap.emplace(pii{a[*next(it)] - a[*it], *next(it) - *it}, *it);
}
template<class iteratorlmao>
void eraseIt(iteratorlmao it) {
if(*next(it) == n + 1 || *it == -1) return;
//cerr << a << '\t' << *it << ' ' << *next(it) << '\t';
//cerr << a[*next(it)] - a[*it] << ' ' << *next(it) - *it << '\t' << *it << '\n' << "iata ";
heap.erase(heap.find(pair<pii,int>{pii{a[*next(it)] - a[*it], *next(it) - *it}, *it}));
//cerr << "ca nu-i aici greseala\n";
}
void init(int _n, int *v) {
n = _n;
a = v;
appears.insert(-1);
appears.insert(n + 1);
for(int i = 1; i <= n; i++)
insertIt(prev(appears.insert(i).first));
heap.emplace(pii{inf, 0}, -1);
}
void erase(int p) {
assert(p > -1);
//cerr << "stergem " << p << "\n--\n";
auto it = appears.find(p);
eraseIt(it);
eraseIt(prev(it));
auto nvit = prev(it);
appears.erase(it);
//cerr << *nvit << '\n';
insertIt(nvit);
}
};
const int nmax = 1e5 + 5;
int A[nmax], B[nmax];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int H, W;
cin >> H >> W;
for(int i = 1; i <= H; i++) cin >> A[i];
for(int i = 1; i <= W; i++) cin >> B[i];
Turneu linii, coloane;
linii.init(H, A);
coloane.init(W, B);
ll cost = 0;
int x = 1, y = 1;
auto move_col = [&]() {
int nvy = *next(coloane.appears.begin());
cost += (ll)(nvy - y) * A[x];
y = nvy;
};
auto move_row = [&]() {
int nvx = *next(linii.appears.begin());
cost += (ll)(nvx - x) * B[y];
x = nvx;
};
while(x != H || y != W) {
//auto [Ax, Ay] = linii.heap.begin() -> first;
//auto [Bx, By] = coloane.heap.begin() -> first;
//cerr << Ax << "/" << Ay << '\t' << Bx << "/" << By << '\n';
if(compare_frac(linii.heap.begin() -> first, coloane.heap.begin() -> first))
//cerr << "1\n",
linii.erase(linii.heap.begin() -> second);
else
//cerr << "2\n",
coloane.erase(coloane.heap.begin() -> second);
move_col(); // gen, merge sa le fac pe ambele albeit e retardat
move_row();
}
cout << cost << '\n';
}
/**
Anul asta nu se da centroid
-- Rugaciunile mele
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
128 ms |
9940 KB |
Output is correct |
5 |
Correct |
49 ms |
9640 KB |
Output is correct |
6 |
Correct |
39 ms |
4448 KB |
Output is correct |
7 |
Correct |
450 ms |
24656 KB |
Output is correct |
8 |
Correct |
477 ms |
24680 KB |
Output is correct |
9 |
Correct |
472 ms |
24660 KB |
Output is correct |
10 |
Correct |
491 ms |
24656 KB |
Output is correct |
11 |
Correct |
170 ms |
24416 KB |
Output is correct |
12 |
Correct |
506 ms |
24704 KB |
Output is correct |
13 |
Correct |
479 ms |
24732 KB |
Output is correct |
14 |
Correct |
123 ms |
24684 KB |
Output is correct |
15 |
Correct |
98 ms |
24872 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
432 KB |
Output is correct |
22 |
Correct |
1 ms |
800 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
600 KB |
Output is correct |
32 |
Correct |
128 ms |
9940 KB |
Output is correct |
33 |
Correct |
49 ms |
9640 KB |
Output is correct |
34 |
Correct |
39 ms |
4448 KB |
Output is correct |
35 |
Correct |
450 ms |
24656 KB |
Output is correct |
36 |
Correct |
477 ms |
24680 KB |
Output is correct |
37 |
Correct |
472 ms |
24660 KB |
Output is correct |
38 |
Correct |
491 ms |
24656 KB |
Output is correct |
39 |
Correct |
170 ms |
24416 KB |
Output is correct |
40 |
Correct |
506 ms |
24704 KB |
Output is correct |
41 |
Correct |
479 ms |
24732 KB |
Output is correct |
42 |
Correct |
123 ms |
24684 KB |
Output is correct |
43 |
Correct |
98 ms |
24872 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
344 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
432 KB |
Output is correct |
50 |
Correct |
1 ms |
800 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
131 ms |
11604 KB |
Output is correct |
56 |
Correct |
2 ms |
604 KB |
Output is correct |
57 |
Correct |
10 ms |
1372 KB |
Output is correct |
58 |
Correct |
31 ms |
3676 KB |
Output is correct |
59 |
Correct |
534 ms |
25756 KB |
Output is correct |
60 |
Correct |
609 ms |
25852 KB |
Output is correct |
61 |
Correct |
627 ms |
25752 KB |
Output is correct |
62 |
Correct |
623 ms |
25868 KB |
Output is correct |
63 |
Correct |
118 ms |
25668 KB |
Output is correct |
64 |
Correct |
634 ms |
25876 KB |
Output is correct |
65 |
Correct |
619 ms |
25872 KB |
Output is correct |
66 |
Correct |
153 ms |
25684 KB |
Output is correct |
67 |
Correct |
110 ms |
26344 KB |
Output is correct |