Submission #1010328

# Submission time Handle Problem Language Result Execution time Memory
1010328 2024-06-28T18:02:13 Z GrindMachine Palembang Bridges (APIO15_bridge) C++17
100 / 100
120 ms 9676 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<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

/*

points on the same side: cost = abs(x-y)
only consider points on opposite sides
wlog, x < y

for k = 1, find s s.t:
sum{ abs(x-s)+abs(y-s) } is minimized
consider each x and y point on a number line (total = 2n points)
best s = median of these 2n points (the nth point)

for k = 2, visualize each (x,y) point on the 2d plane (x = row, y = col)
pick 2 points (s,s) and (t,t) s.t:
sum{ min(abs(x-s)+abs(y-s),abs(x-t)+abs(y-t)) } is minimized
can be rewritten as:
sum{ min(dis(x,y,s,s),dis(x,y,t,t)) } (dis(x1,y1,x2,y2) = manhattan distance between points (x1,y1) and (x2,y2))

the line connecting (s,s) and (t,t) is a diagonal
rotate the plane by 45 deg => it is now a straight line
draw the perpendicular bisector of the line (at the midpoint)
points on one side go to s and points on the other side go to t

on the original graph, the perpendicular bisector is an antidiagonal passing through ((s+t)/2,(s+t)/2)

\  / => bisector
 \/
 /\
/  \ => line connecting (s,s) and (t,t)

all points on the left with x+y <= s+t go to s
all points on the right with x+y > s+t go to t
sort all points by x+y
some pref of points go to s, the rest of the points go to t
for each splitting, find the best min cost for all points to meet at a single point (like k = 1 case)
can be sped up using fenwick trees that can handle lower_bound queries (can also use ordered set for this, but anyways need a fenwick tree for finding the sum on a segment)

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct fenwick {
    int n;
    vector<T> tr;
    int LOG = 0;

    fenwick() {

    }

    fenwick(int n_) {
        n = n_;
        tr = vector<T>(n + 1);
        while((1<<LOG) <= n) LOG++;
    }

    void reset(){
        fill(all(tr),0);
    }

    int lsb(int x) {
        return x & -x;
    }

    void pupd(int i, T v) {
        for(; i <= n; i += lsb(i)){
            tr[i] += v;
        }
    }

    T sum(int i) {
        T res = 0;
        for(; i; i ^= lsb(i)){
            res += tr[i];
        }
        return res;
    }

    T query(int l, int r) {
        if (l > r) return 0;
        T res = sum(r) - sum(l - 1);
        return res;
    }

    int lower_bound(T s){
        // first pos with sum >= s
        if(sum(n) < s) return n+1;
        int i = 0;
        rev(bit,LOG-1,0){
            int j = i+(1<<bit);
            if(j > n) conts;
            if(tr[j] < s){
                s -= tr[j];
                i = j;
            }
        }

        return i+1;
    }

    int upper_bound(T s){
        return lower_bound(s+1);
    }
};

void solve(int test_case)
{
    ll k,n; cin >> k >> n;
    vector<array<ll,3>> a;
    ll same_side = 0;

    rep1(i,n){
        char p,q; ll x,y;
        cin >> p >> x >> q >> y;
        if(p == q){
            same_side += abs(x-y);
        }
        else{
            if(x > y) swap(x,y);
            a.pb({x+y,x,y});
        }
    }

    sort(all(a));

    auto go = [&](vector<array<ll,3>> &curr){
        vector<ll> b;
        for(auto [val,x,y] : curr){
            b.pb(x), b.pb(y);
        }

        sort(all(b));
        ll s = -1;
        if(!b.empty()){
            s = b[sz(b)/2];
        }

        ll res = 0;
        trav(x,b){
            res += abs(x-s);
        }

        return res;
    };

    if(k == 1 or a.empty()){
        ll ans = go(a);
        ans += same_side+sz(a);
        cout << ans << endl;
        return;
    }

    n = sz(a);
    a.insert(a.begin(),{-1,-1,-1});
    vector<ll> b;
    rep1(i,n) b.pb(a[i][1]), b.pb(a[i][2]);
    b.pb(-1);
    sort(all(b));
    b.resize(unique(all(b))-b.begin());
    ll siz = sz(b);

    vector<ll> pref(n+5), suff(n+5);
    fenwick<ll> fenw_cnt(siz+5), fenw_sum(siz+5);

    auto ins = [&](ll x){
        ll i = lower_bound(all(b),x)-b.begin();
        fenw_cnt.pupd(i,1);
        fenw_sum.pupd(i,x);
    };

    auto get = [&](ll mid){
        ll i = fenw_cnt.lower_bound(mid);
        ll cnt1 = fenw_cnt.query(1,i-1), sum1 = fenw_sum.query(1,i-1);
        ll cnt2 = fenw_cnt.query(i+1,siz), sum2 = fenw_sum.query(i+1,siz);
        return b[i]*cnt1-sum1+sum2-b[i]*cnt2;
    };

    rep1(i,n){
        ins(a[i][1]), ins(a[i][2]);
        pref[i] = get(i);
    }

    fenw_cnt.reset(), fenw_sum.reset();

    rev(i,n,1){
        ins(a[i][1]), ins(a[i][2]);
        suff[i] = get(n-i+1);
    }

    ll ans = inf2;

    rep1(i,n){
        amin(ans,pref[i]+suff[i+1]);
    }

    ans += same_side+n;
    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 344 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 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 0 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 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 19 ms 6348 KB Output is correct
13 Correct 34 ms 6280 KB Output is correct
14 Correct 28 ms 5828 KB Output is correct
15 Correct 21 ms 3560 KB Output is correct
16 Correct 21 ms 6228 KB Output is correct
17 Correct 22 ms 6264 KB Output is correct
18 Correct 25 ms 6348 KB Output is correct
19 Correct 33 ms 6336 KB Output is correct
20 Correct 25 ms 6348 KB Output is correct
21 Correct 28 ms 6348 KB Output is correct
# Verdict Execution time Memory 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 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 0 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 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 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 0 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 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 26 ms 6244 KB Output is correct
26 Correct 35 ms 6312 KB Output is correct
27 Correct 111 ms 8896 KB Output is correct
28 Correct 118 ms 9660 KB Output is correct
29 Correct 120 ms 9676 KB Output is correct
30 Correct 58 ms 5104 KB Output is correct
31 Correct 28 ms 6600 KB Output is correct
32 Correct 86 ms 9420 KB Output is correct
33 Correct 77 ms 9404 KB Output is correct
34 Correct 92 ms 9408 KB Output is correct
35 Correct 32 ms 6336 KB Output is correct
36 Correct 89 ms 9508 KB Output is correct
37 Correct 30 ms 6340 KB Output is correct