#include <bits/stdc++.h>
#ifdef OP_DEBUG
#include <algo/debug.h>
#else
#define debug(...) 26
#endif
using namespace std;
#define int long long // maybe tle?
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define TcT template <class T
const int MOD = (int)1e9 + 7, INF = (int)2e9 + 9, INF64 = (int)4e18 + 18;
TcT> bool minimize(T &val, const T &upd) { return upd < val ? val = upd, 1 : 0; }
TcT> bool maximize(T &val, const T &upd) { return upd > val ? val = upd, 1 : 0; }
TcT, class S> istream &operator >> (istream &scanf, pair <T, S> &u) { return scanf >> u.first >> u.second; }
TcT, class S> ostream &operator << (ostream &print, pair <T, S> &u) { return print << u.first << ' ' << u.second; }
void solve();
int32_t main() {
cin.tie(nullptr), cout.tie(nullptr) -> sync_with_stdio(0);
int testcases = 1;
#define TC 0
if (TC) { cin >> testcases; } for (; testcases--;) { solve(); }
}
/* [Pham Hung Anh - 11I - Tran Hung Dao High School for Gifted Student] */
const int N = 1e5 + 5;
int k, n;
int same_side;
vector <int> buildings;
vector <pair <int, int>> opposite;
namespace sub12 { // O(n)
void solve() {
int ans = same_side;
if (sz(buildings) == 0)
return void(cout << ans << '\n');
int m = sz(buildings);
int median = buildings[(m + 1) / 2];
for (int i = 0; i < m; ++i)
ans += abs(median - buildings[i]);
cout << ans + sz(buildings) / 2 << '\n';
}
}
namespace sub3 { // O(n^3)
void solve() {
int ans = same_side;
if (sz(buildings) == 0)
return void(cout << ans << '\n');
int res = INF64;
for (int i = 0; i < sz(buildings); ++i)
for (int j = i + 1; j < sz(buildings); ++j) {
int bridge1 = buildings[i], bridge2 = buildings[j];
int sum = 0;
for (auto p : opposite)
sum += min(abs(p.first - bridge1) + abs(p.second - bridge1), abs(p.first - bridge2) + abs(p.second - bridge2)) + 1;
minimize(res, sum);
}
cout << ans + res << '\n';
}
}
namespace sub4 { // O(log3(n)^2 * n)
int get(int bridge1, int bridge2) {
int sum = same_side;
for (auto p : opposite)
sum += min(abs(p.first - bridge1) + abs(p.second - bridge1), abs(p.first - bridge2) + abs(p.second - bridge2)) + 1;
return sum;
}
int ternary_search(int bridge1) {
int res = INF64;
int lo = bridge1 + 1, hi = sz(buildings) - 1;
while (lo <= hi) {
int mid1 = lo + (hi - lo) / 3;
int mid2 = hi - (hi - lo) / 3;
int sum1 = get(buildings[bridge1], buildings[mid1]);
int sum2 = get(buildings[bridge1], buildings[mid2]);
minimize(res, min(sum1, sum2));
if (sum1 < sum2)
hi = mid2 - 1;
else if (sum1 > sum2)
lo = mid1 + 1;
else
lo = mid1 + 1, hi = mid2 - 1;
}
return res;
}
void solve() {
int ans = INF64;
int lo = 0, hi = sz(buildings) - 2;
while (lo <= hi) {
int mid1 = lo + (hi - lo) / 3;
int mid2 = hi - (hi - lo) / 3;
int ans1 = ternary_search(mid1);
int ans2 = ternary_search(mid2);
minimize(ans, min(ans1, ans2));
if (ans1 < ans2)
hi = mid2 - 1;
else if (ans1 > ans2)
lo = mid1 + 1;
else
lo = mid1 + 1, hi = mid2 - 1;
}
cout << ans << '\n';
}
}
namespace sub5 {
int pref[N];
int suml = 0, sumr = 0;
multiset <int> lo, hi;
void balance() {
while(sz(lo) > sz(hi)) {
int x = *lo.rbegin();
hi.emplace(x);
suml -= x;
sumr += x;
lo.erase(lo.find(x));
}
while(sz(hi) > sz(lo)) {
int x = *hi.begin();
lo.emplace(x);
suml += x;
sumr -= x;
hi.erase(hi.find(x));
}
}
void add(int x) {
lo.emplace(x);
suml += x;
balance();
}
void solve() {
sort(opposite.begin(), opposite.end(), [&](pair <int, int> opposite, pair <int, int> b) -> bool {
return opposite.first + opposite.second < b.first + b.second;
});
for(int i = 0; i < sz(opposite); ++i) {
add(opposite[i].first);
add(opposite[i].second);
pref[i + 1] = sumr - suml;
}
int ans = pref[n];
lo.clear();
hi.clear();
suml = sumr = 0;
for(int i = n - 1; i >= 0; --i) {
add(opposite[i].first);
add(opposite[i].second);
minimize(ans, pref[i] + sumr - suml);
}
cout << ans + n + same_side << '\n';
}
}
void solve() {
cin >> k >> n;
for (int i = 1; i <= n; ++i) {
char p, q; int s, t; cin >> p >> s >> q >> t;
if (p == q)
same_side += abs(s - t);
else
buildings.push_back(s), buildings.push_back(t),
opposite.emplace_back(min(s, t), max(s, t));
}
sort(all(buildings));
if (k == 1)
sub12 :: solve();
else {
if (n <= 100)
sub3 :: solve();
else if (n <= 1000)
sub4 :: solve();
else
sub5 :: solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
16 ms |
3796 KB |
Output is correct |
13 |
Correct |
33 ms |
3784 KB |
Output is correct |
14 |
Correct |
30 ms |
3792 KB |
Output is correct |
15 |
Correct |
19 ms |
2504 KB |
Output is correct |
16 |
Correct |
26 ms |
4076 KB |
Output is correct |
17 |
Correct |
23 ms |
3784 KB |
Output is correct |
18 |
Correct |
27 ms |
3780 KB |
Output is correct |
19 |
Correct |
31 ms |
3784 KB |
Output is correct |
20 |
Correct |
22 ms |
3784 KB |
Output is correct |
21 |
Correct |
28 ms |
3780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
468 KB |
Output is correct |
10 |
Correct |
5 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
472 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
472 KB |
Output is correct |
10 |
Correct |
5 ms |
472 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
524 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
3 ms |
520 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
528 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Incorrect |
155 ms |
13588 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |