Submission #1031948

#TimeUsernameProblemLanguageResultExecution timeMemory
1031948_8_8_Palembang Bridges (APIO15_bridge)C++17
22 / 100
35 ms10692 KiB
#include <bits/stdc++.h>
 
using namespace std;
    
typedef long long ll;
const int  N = 3e5 + 21, MOD = (int)1e9+7;
 
#define int ll
int n,k;
ll res = 0;
array<int,2> a[N];
ll L[N],R[N];
vector<pair<int,int>> sc;
void prec(){
    for(int i = 1;i <= n;i++){
        sc.push_back({a[i][0],0});
        sc.push_back({a[i][1],1});
    }
    sort(sc.begin(),sc.end());
    ll sum = 0,col = 0;
    for(int i = 0;i < (int)sc.size();i++){
        L[i] = (sc[i].first * 1ll * col) - sum;
        if(sc[i].second == 1){
            col++;
            sum += sc[i].first;
        }
    }
    col = sum =0 ;
    for(int i = (int)sc.size() - 1;i >= 0;i--){
        R[i] = sum - (sc[i].first * 1ll * col);
        if(sc[i].second == 0){
            col++;
            sum += sc[i].first;
        }
    }
}
ll solve(){
    if(!n) return 0;
    ll res = 1e18;
    for(int i = 0;i < (int)sc.size();i++){
        res = min(res,2*(L[i] + R[i]));
    }
    return res;
}
ll calc(int l,int r){
    ll sum = 0;
    for(int i = 1;i <= n;i++){
        if(a[i][0] > l && a[i][1] < r){
            sum += min(2ll*(a[i][0] - l),2*1ll*(r - a[i][1]));
        }
    }
    return sum;
}
void test() {
    cin >> k >> n;
    int _i = 0;
    for(int i = 1;i <= n;i++) {
        char x,y;
        int l,r;
        cin >> x >> l >> y >> r;
        if(l > r){
            swap(l,r);
            swap(x,y);
        }
        res += (r - l);
        if(x == y){
            continue;
        }
        res++;
        a[++_i] = {l,r};
    }
    n = _i;
    prec();
    if(k == 1){
        cout << res + solve();
        return;
    }
    if(n == 0){
        cout << res;
        return;
    }
    ll cur = 1e18,d = 1e18;
    int j = -1;
    int pos_;
    while(j + 1 < (int)sc.size()){
        ll val = L[0] * 2ll + R[j + 1] * 2ll + calc(sc[0].first,sc[j + 1].first);
        j++;
        if(val <= cur){
            pos_ = j + 1;
            cur = val;
        }
    }
    j = pos_;
    d = cur;
    for(int i = 1;i < (int)sc.size();i++){
        if(j < i) j = i;
        cur = L[i] * 2ll + R[j] *2ll + calc(sc[i].first,sc[j].first);
        while(j + 1 < (int)sc.size()){
            ll val = L[i] * 2ll + R[j + 1] * 2ll + calc(sc[i].first,sc[j + 1].first);
            if(val <= cur){
                cur = val;
                j++;
            }else break;
        }
        // cout << sc[i].first << ' ' << sc[j].first <<  ' ' << cur << '\n';
        d = min(d,cur);
    }
    cout << res + d << '\n';
}
signed main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    int t = 1; 
    // cin >> t;
    while(t--) {
        test();
    }
}

Compilation message (stderr)

bridge.cpp: In function 'void test()':
bridge.cpp:84:9: warning: 'pos_' may be used uninitialized in this function [-Wmaybe-uninitialized]
   84 |     int pos_;
      |         ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...