#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
using ll = long long;
struct road
{
ll n;
ll i1;
ll i2;
int t; //0 = horizontal = A, 1 = vertical = B
};
bool operator < (road A, road B)
{
if(A.n*(B.i2 - B.i1) != B.n*(A.i2 - A.i1))
return A.n*(B.i2 - B.i1) < B.n*(A.i2 - A.i1);
else if(A.t != B.t)
return A.t < B.t;
else
return A.i1 < B.i1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int H, W;
cin >> H >> W;
ll A[1+H], B[1+W];
for(ll i = 1; i <= H; i++) cin >> A[i];
for(ll j = 1; j <= W; j++) cin >> B[j];
set<road> R;
for(ll i = 1; i < H; i++)
R.insert({A[i] - A[i+1], i, i+1, 0});
for(ll j = 1; j < W; j++)
R.insert({B[j] - B[j+1], j, j+1, 1});
set<ll> I, J; //alive indices, where there is a valid road.
for(ll i = 1; i <= H; i++) I.insert(i);
for(ll j = 1; j <= W; j++) J.insert(j);
ll ci = 1, cj = 1;
ll cost = 0;
// for(road r : R) cerr << r.t << ' ' << r.n << '/' << r.i2-r.i1 << '\n';
// cerr << "road = \n";
// for(auto r : R) cerr << r.n << ' ' << r.i1 << ' ' << r.i2 << ' ' << r.t << '\n';
while(!(ci == H && cj == W))
{
// cerr << "\n\n\n";
if(*I.begin() > ci)
{
cost += B[*J.begin()] * (*I.begin() - ci);
ci = *I.begin();
// cerr << "go down\n";
continue;
}
if(*J.begin() > cj)
{
cost += A[*I.begin()] * (*J.begin() - cj);
cj = *J.begin();
// cerr << "go right\n";
continue;
}
road r = *R.rbegin();
R.erase(r);
// cerr << "erase\n";
if(r.t == 0)
{
ll ri = r.i1;
ll rnxt = r.i2;
// cerr << "remove horizontal road " << ri << '\n';
I.erase(ri);
auto it = I.lower_bound(ri);
if(it == I.begin()) continue;
it--;
ll rprv = *it;
R.erase({A[rprv] - A[ri], rprv, ri, 0});
R.insert({A[rprv] - A[rnxt], rprv, rnxt, 0});
// cerr << "insert\n";
}
else
{
ll ri = r.i1;
ll rnxt = r.i2;
// cerr << "remove vertical road " << ri << '\n';
J.erase(ri);
auto it = J.lower_bound(ri);
if(it == J.begin()) continue;
it--;
ll rprv = *it;
R.erase({B[rprv] - B[ri], rprv, ri, 1});
R.insert({B[rprv] - B[rnxt], rprv, rnxt, 1});
// cerr << "insert\n";
}
// cerr << "road = \n";
// for(auto r : R) cerr << r.n << ' ' << r.i1 << ' ' << r.i2 << ' ' << r.t << '\n';
}
cout << cost << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 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 |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
183 ms |
11172 KB |
Output is correct |
5 |
Correct |
64 ms |
10316 KB |
Output is correct |
6 |
Correct |
60 ms |
4668 KB |
Output is correct |
7 |
Correct |
641 ms |
26912 KB |
Output is correct |
8 |
Correct |
663 ms |
26908 KB |
Output is correct |
9 |
Correct |
625 ms |
26912 KB |
Output is correct |
10 |
Correct |
665 ms |
26900 KB |
Output is correct |
11 |
Correct |
339 ms |
26912 KB |
Output is correct |
12 |
Correct |
631 ms |
26956 KB |
Output is correct |
13 |
Correct |
661 ms |
26944 KB |
Output is correct |
14 |
Correct |
173 ms |
26956 KB |
Output is correct |
15 |
Correct |
202 ms |
26788 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 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 |
1 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 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 |
1 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 |
1 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 |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
183 ms |
11172 KB |
Output is correct |
33 |
Correct |
64 ms |
10316 KB |
Output is correct |
34 |
Correct |
60 ms |
4668 KB |
Output is correct |
35 |
Correct |
641 ms |
26912 KB |
Output is correct |
36 |
Correct |
663 ms |
26908 KB |
Output is correct |
37 |
Correct |
625 ms |
26912 KB |
Output is correct |
38 |
Correct |
665 ms |
26900 KB |
Output is correct |
39 |
Correct |
339 ms |
26912 KB |
Output is correct |
40 |
Correct |
631 ms |
26956 KB |
Output is correct |
41 |
Correct |
661 ms |
26944 KB |
Output is correct |
42 |
Correct |
173 ms |
26956 KB |
Output is correct |
43 |
Correct |
202 ms |
26788 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
236 ms |
12008 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
11 ms |
1520 KB |
Output is correct |
58 |
Correct |
36 ms |
3860 KB |
Output is correct |
59 |
Correct |
675 ms |
26952 KB |
Output is correct |
60 |
Correct |
629 ms |
26908 KB |
Output is correct |
61 |
Correct |
608 ms |
26904 KB |
Output is correct |
62 |
Correct |
610 ms |
26912 KB |
Output is correct |
63 |
Correct |
164 ms |
26908 KB |
Output is correct |
64 |
Correct |
657 ms |
26948 KB |
Output is correct |
65 |
Correct |
648 ms |
26908 KB |
Output is correct |
66 |
Correct |
208 ms |
26828 KB |
Output is correct |
67 |
Correct |
205 ms |
26908 KB |
Output is correct |