#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long int
#define endl '\n'
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 200005
#define mod 998244353
struct segtree
{
int n;
vector<int> seg;
int neutral()
{
return 0;
}
int merge(int a, int b)
{
return a + b;
}
segtree() {}
segtree(int sz)
{
n = 1;
while (n < sz)
n <<= 1;
seg.assign(n << 1, neutral());
}
void upd(int i, int value)
{
seg[i += n] += value;
for (i >>= 1; i; i >>= 1)
seg[i] = merge(seg[i << 1], seg[(i << 1) | 1]);
}
int qry(int l, int r)
{
int ansl = neutral(), ansr = neutral();
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1)
{
if (l & 1)
ansl = merge(ansl, seg[l++]);
if (r & 1)
ansr = merge(seg[--r], ansr);
}
return merge(ansl, ansr);
}
};
struct crazy_median
{
int n;
segtree st, st2;
ordered_set<pi> s;
map<int, int> mp;
vector<int> vals;
crazy_median(vector<int> v)
{
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
vals = v;
n = vals.size();
st = segtree(n);
st2 = segtree(n);
}
void add(int i)
{
int pos = lower_bound(vals.begin(), vals.end(), i) - vals.begin();
st.upd(pos, 1);
st2.upd(pos, i);
s.insert({i, mp[i]});
mp[i]++;
}
void rem(int i)
{
int pos = lower_bound(vals.begin(), vals.end(), i) - vals.begin();
st.upd(pos, -1);
st2.upd(pos, -i);
mp[i]--;
s.erase({i, mp[i]});
}
int calc(int x)
{
int ans = 0;
int pos = lower_bound(vals.begin(), vals.end(), x) - vals.begin();
ans += (st.qry(0, pos - 1) * x) - st2.qry(0, pos - 1);
ans += st2.qry(pos + 1, n - 1) - (st.qry(pos + 1, n - 1) * x);
return ans;
}
int solve()
{
int sz = s.size();
int x = (*s.find_by_order(sz / 2)).fir;
int ans = calc(x);
return ans;
}
};
void solve1(int n, int k)
{
int ans = 0;
vector<int> pts;
for (int i = 0; i < n; i++)
{
char c1, c2;
int a, b;
cin >> c1 >> a >> c2 >> b;
if (c1 == c2)
{
ans += abs(a - b);
}
else
{
ans++;
pts.pb(a);
pts.pb(b);
}
}
if (!pts.size())
{
cout << ans << endl;
return;
}
crazy_median cm(pts);
for (auto const &i : pts)
cm.add(i);
ans += cm.solve();
cout << ans << endl;
}
void solve2(int n, int k)
{
vector<pi> aux;
vector<pi> v;
int ans = 0;
for (int i = 0; i < n; i++)
{
char c1, c2;
int a, b;
cin >> c1 >> a >> c2 >> b;
v.pb({a, b});
if (c1 == c2)
{
ans += abs(a - b);
}
else
{
ans++;
aux.pb({a + b, i});
}
}
if (!aux.size())
{
cout << ans << endl;
return;
}
sort(aux.begin(), aux.end());
vector<int> pts;
for (auto const &i : aux)
{
pts.pb(v[i.sec].fir);
pts.pb(v[i.sec].sec);
}
crazy_median cm(pts), cm2(pts);
for (auto const &i : pts)
{
cm.add(i);
}
int need = ans;
vector<int> pts2;
ans += cm.solve();
while (pts.size() > 2)
{
cm.rem(pts.back());
cm2.add(pts.back());
pts.pop_back();
cm.rem(pts.back());
cm2.add(pts.back());
pts.pop_back();
int curr = need + cm.solve() + cm2.solve();
ans = min(ans, curr);
}
cout << ans << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> k >> n;
if (k == 1)
solve1(n, k);
else
solve2(n, k);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
3 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
93 ms |
14296 KB |
Output is correct |
13 |
Correct |
424 ms |
36608 KB |
Output is correct |
14 |
Correct |
263 ms |
14256 KB |
Output is correct |
15 |
Correct |
209 ms |
20968 KB |
Output is correct |
16 |
Correct |
170 ms |
14360 KB |
Output is correct |
17 |
Correct |
223 ms |
36688 KB |
Output is correct |
18 |
Correct |
277 ms |
36736 KB |
Output is correct |
19 |
Correct |
348 ms |
36032 KB |
Output is correct |
20 |
Correct |
104 ms |
14368 KB |
Output is correct |
21 |
Correct |
210 ms |
36684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
4 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
3 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
4 ms |
852 KB |
Output is correct |
21 |
Correct |
6 ms |
852 KB |
Output is correct |
22 |
Correct |
4 ms |
852 KB |
Output is correct |
23 |
Correct |
3 ms |
468 KB |
Output is correct |
24 |
Correct |
4 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
216 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
7 ms |
880 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
4 ms |
852 KB |
Output is correct |
21 |
Correct |
4 ms |
896 KB |
Output is correct |
22 |
Correct |
4 ms |
852 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
4 ms |
904 KB |
Output is correct |
25 |
Correct |
320 ms |
17484 KB |
Output is correct |
26 |
Correct |
407 ms |
17496 KB |
Output is correct |
27 |
Correct |
1666 ms |
59696 KB |
Output is correct |
28 |
Correct |
1795 ms |
62108 KB |
Output is correct |
29 |
Correct |
1435 ms |
62076 KB |
Output is correct |
30 |
Correct |
585 ms |
32500 KB |
Output is correct |
31 |
Correct |
325 ms |
17488 KB |
Output is correct |
32 |
Correct |
656 ms |
62180 KB |
Output is correct |
33 |
Correct |
728 ms |
62112 KB |
Output is correct |
34 |
Correct |
511 ms |
60800 KB |
Output is correct |
35 |
Correct |
333 ms |
17516 KB |
Output is correct |
36 |
Correct |
703 ms |
62172 KB |
Output is correct |
37 |
Correct |
408 ms |
17532 KB |
Output is correct |