Submission #653451

# Submission time Handle Problem Language Result Execution time Memory
653451 2022-10-26T23:05:45 Z ayalla Palembang Bridges (APIO15_bridge) C++14
100 / 100
1473 ms 64468 KB
#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);
    if (x > 0)
    {
      int x = (*s.find_by_order((sz / 2) - 1)).fir;
      ans = min(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 1 ms 340 KB Output is correct
7 Correct 1 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
# 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 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 464 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 87 ms 14356 KB Output is correct
13 Correct 398 ms 36644 KB Output is correct
14 Correct 239 ms 14176 KB Output is correct
15 Correct 222 ms 20964 KB Output is correct
16 Correct 138 ms 14308 KB Output is correct
17 Correct 218 ms 36752 KB Output is correct
18 Correct 262 ms 36740 KB Output is correct
19 Correct 301 ms 36092 KB Output is correct
20 Correct 99 ms 14368 KB Output is correct
21 Correct 206 ms 36676 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 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 340 KB Output is correct
12 Correct 1 ms 340 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 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 212 KB Output is correct
7 Correct 0 ms 212 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
13 Correct 2 ms 468 KB Output is correct
14 Correct 3 ms 468 KB Output is correct
15 Correct 5 ms 888 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 852 KB Output is correct
22 Correct 4 ms 852 KB Output is correct
23 Correct 2 ms 492 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 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 340 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 5 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 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 852 KB Output is correct
22 Correct 4 ms 852 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 4 ms 852 KB Output is correct
25 Correct 295 ms 17468 KB Output is correct
26 Correct 406 ms 18520 KB Output is correct
27 Correct 1421 ms 61528 KB Output is correct
28 Correct 1413 ms 64452 KB Output is correct
29 Correct 1473 ms 64372 KB Output is correct
30 Correct 578 ms 33844 KB Output is correct
31 Correct 314 ms 19128 KB Output is correct
32 Correct 707 ms 64468 KB Output is correct
33 Correct 756 ms 64008 KB Output is correct
34 Correct 571 ms 63240 KB Output is correct
35 Correct 338 ms 19364 KB Output is correct
36 Correct 743 ms 64204 KB Output is correct
37 Correct 400 ms 18248 KB Output is correct