Submission #1095148

# Submission time Handle Problem Language Result Execution time Memory
1095148 2024-10-01T12:38:53 Z NguyenPhucThang Palembang Bridges (APIO15_bridge) C++14
0 / 100
4 ms 600 KB
#include <bits/stdc++.h>
#define forr(i, a, b) for (int i = (a); i <= (b); i++)
#define ford(i, a, b) for (int i = (a); i >= (b); i--)
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(), v.end()
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"

using namespace std;
const int base = 31;
const ll mod = 1e9 + 7;
const ll oo = 1e16;
const int N = 1e6 + 5;
const int M = 1e6;


vector<pii> v;

bool cmp(pii a, pii b){
    return (a.fi + a.se) < (b.fi + b.se);
}

struct node{
    ll cnt, sum;
};

struct segtree{
    node st[4 * N];

    node combine(node a, node b){
        return {a.cnt + b.cnt, a.sum + b.sum};
    }

    void update(int id, int l, int r, int i, int v){
        if (i < l || r < i) return;
        if (l == r){
            st[id].cnt += v;
            st[id].sum = l * st[id].cnt;
            return;
        }

        int mid = (l + r) >> 1;
        update(2 * id, l, mid, i, v), update(2 * id + 1, mid + 1, r, i, v);
        st[id] = combine(st[2 * id], st[2 * id + 1]);
    }

    node get(int id, int l, int r, int u, int v){
        if (v < l || r < u) return {0, 0};
        if (u <= l && r <= v) return st[id];
        int mid = (l + r) >> 1;
        return combine(get(2 * id, l, mid, u, v), get(2 * id + 1, mid + 1, r, u, v));
    }

    int find_kth(int id, int l, int r, int k){
        if (l == r) return l;
        int mid = (l + r) >> 1;
        if (st[2 * id].cnt >= k) return find_kth(2 * id, l, mid, k);
        return find_kth(2 * id + 1, mid + 1, r, k - st[2 * id].cnt);
    }

    ll find_cost(int sz){
        ll med = find_kth(1, 0, M, (1 + sz) >> 1);
        node lower_ans = get(1, 0, M, 0, med);
        node higher_ans = get(1, 0, M, med + 1, M);
        return med * lower_ans.cnt - lower_ans.sum + higher_ans.sum - med * higher_ans.cnt;
    }

} Left, Right, All;


int main()
{
   ios_base::sync_with_stdio(0);
   cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("test.inp", "r", stdin);
    freopen("test.out", "w", stdout);
#endif // ONLINE_JUDGE
    int K, n;
    cin >> K >> n;

    ll res = 0;

    forr(i, 1, n) {
        char b1, b2;
        int x1, x2;
        cin >> b1 >> x1 >> b2 >> x2;
        if (b1 == b2) res += abs(x1 - x2);
        else
            v.pb({x1, x2});
    }

    res += v.size();

    sort(all(v), cmp);

    // K = 1
    forf(i, 0, v.size()){
        All.update(1, 0, M, v[i].fi, 1);
        All.update(1, 0, M, v[i].se, 1);
    }

   ll cost1 = All.find_cost(2 * v.size());

   ll cost2 = oo;

   if (K == 2 && v.size() >= 2){
      forf(i, 0, v.size()) {
         Right.update(1, 0, M, v[i].fi, 1);
         Right.update(1, 0, M, v[i].se, 1);
      }

      int cntLeft = 0;
      forr(i, 0, v.size() - 2){
         Left.update(1, 0, M, v[i].fi, 1);
         Left.update(1, 0, M, v[i].se, 1);
         Right.update(1, 0, M, v[i].fi, -1);
         Right.update(1, 0, M, v[i].se, -1);
         cntLeft++;
         cost2 = min(cost2, Left.find_cost(2 * cntLeft) + Right.find_cost(2 * (v.size() - cntLeft)));
      }
   }

   cout << res + min(cost1, cost2) << endl;

    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:4:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forf(i, a, b) for (int i = (a); i < (b); i++)
      |                                           ^
bridge.cpp:109:5: note: in expansion of macro 'forf'
  109 |     forf(i, 0, v.size()){
      |     ^~~~
bridge.cpp:4:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forf(i, a, b) for (int i = (a); i < (b); i++)
      |                                           ^
bridge.cpp:119:7: note: in expansion of macro 'forf'
  119 |       forf(i, 0, v.size()) {
      |       ^~~~
bridge.cpp:2:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define forr(i, a, b) for (int i = (a); i <= (b); i++)
      |                                           ^
bridge.cpp:125:7: note: in expansion of macro 'forr'
  125 |       forr(i, 0, v.size() - 2){
      |       ^~~~
bridge.cpp:87:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     freopen("test.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     freopen("test.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -