Submission #1342130

#TimeUsernameProblemLanguageResultExecution timeMemory
1342130spycoderytPalembang Bridges (APIO15_bridge)C++20
22 / 100
23 ms2552 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define EB emplace_back
#define ary(k) array<int,k>
template<class A,class B> void cmx(A& x, B y) {x=max<A>(x,y);}
template<class A,class B> void cmn(A& x, B y) {x=min<A>(x,y);}
typedef pair<int, int> pii;
typedef vector<int> vi;

signed main() {
	cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
    int ans=0, k,n,a,b;
    char c1,c2;
    int mn=1e9, mx = 0;
    cin >> k >> n;
    vector<pii> v;
    for(int i = 0;i<n;i++) {
        cin >> c1 >> a >> c2 >> b;
        if(c1 != c2) {
            if(c1 == 'B') swap(a,b); // always A: pos, B: pos
            v.EB(a,b);
            cmn(mn,min(a,b));
            cmx(mx,max(a,b));
            ans++;
        } else ans += abs(a-b);
    }
    // bsearch from mn to max
    int l = mn, r = mx;
    while(l<r){
        int m1 = (l+r)/2;
        int m2 = m1 + 1;
        int cur1 = 0, cur2 = 0;
        for(auto [a,b] : v) {
            cur1 += abs(a-m1) + abs(b-m1);
            cur2 += abs(a-m2) + abs(b-m2);
        }
        if(cur1 < cur2) {
            r = m1;
        } else l = m1+1;
    }
    //assert(l==r);
//    cerr << "chose " << l << "\n";
    for(auto [a,b]:v) ans += abs(a-l) + abs(b-l);
    cout << ans;
}
#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...