Submission #550023

# Submission time Handle Problem Language Result Execution time Memory
550023 2022-04-17T05:03:32 Z CyberSleeper Palembang Bridges (APIO15_bridge) C++14
100 / 100
338 ms 11328 KB
#include <bits/stdc++.h>
#define fastio      ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x)    cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x)      x.begin(), x.end()
#define fi          first
#define se          second
#define mp          make_pair
#define pb          push_back
#define ll          long long
#define ull         unsigned long long
#define pll         pair<ll, ll>
#define pii         pair<int, int>
#define ld          long double
#define nl          endl
#define tb          '\t'
#define sp          ' '
#define sqr(x)      (x)*(x)
#define arr3        array<int, 3>
using namespace std;
 
const int MX=30005, MOD=1000000007, BLOCK=160, INF=2e9+7, LG=62;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;
 
ll N, K, ans, sumL[2], sumR[2], best;
multiset<ll> L[2], R[2];
vector<pll> A;

void balance(int idx){
    while(R[idx].size() && L[idx].size()<R[idx].size()){
        ll tmp=*R[idx].begin();
        R[idx].erase(R[idx].begin());
        L[idx].insert(tmp);
        sumR[idx]-=tmp;
        sumL[idx]+=tmp;
    }
    while(L[idx].size() && L[idx].size()-1>R[idx].size()){
        ll tmp=*L[idx].rbegin();
        L[idx].erase(prev(L[idx].end()));
        R[idx].insert(tmp);
        sumL[idx]-=tmp;
        sumR[idx]+=tmp;
    }
}
void ins(ll x, ll idx){
    if(L[idx].empty() || x<=*L[idx].rbegin()){
        L[idx].insert(x);
        sumL[idx]+=x;
    }else{
        R[idx].insert(x);
        sumR[idx]+=x;
    }
    balance(idx);
}
void era(ll x, ll idx){
    if(R[idx].count(x)){
        R[idx].erase(R[idx].find(x));
        sumR[idx]-=x;
    }else{
        L[idx].erase(L[idx].find(x));
        sumL[idx]-=x;
    }
    balance(idx);
}
ll comp(ll x, ll y){
    return (L[x].size()*y-sumL[x]) + (sumR[x]-R[x].size()*y);
}
 
int main(){
    fastio;
    cin >> K >> N;
    for(int i=0; i<N; i++){
        char a, c;
        int b, d;
        cin >> a >> b >> c >> d;
        if(a==c){
            ans+=abs(b-d);
        }else{
            ans++;
            if(a=='B')
                swap(b, d);
            A.pb({b+d, min(b, d)});
        }
    }
    sort(all(A));
    N=A.size();
    for(int i=0; i<N; i++){
        ll op=A[i].se, ed=A[i].fi-A[i].se;
        ins(op, 1);
        ins(ed, 1);
    }
    best=INFF;
    if(L[1].size())
        best=min(best, comp(1, *L[1].rbegin()));
    if(R[1].size())
        best=min(best, comp(1, *R[1].begin()));
    if(K==2){
        for(int i=0; i<N; i++){
            ll op=A[i].se, ed=A[i].fi-A[i].se;
            ins(op, 0);
            ins(ed, 0);
            era(op, 1);
            era(ed, 1);
            if(L[0].size() && L[1].size())
                best=min(best, comp(0, *L[0].rbegin()) + comp(1, *L[1].rbegin()));
            if(R[0].size() && L[1].size())
                best=min(best, comp(0, *R[0].begin()) + comp(1, *L[1].rbegin()));
            if(L[0].size() && R[1].size())
                best=min(best, comp(0, *L[0].rbegin()) + comp(1, *R[1].begin()));
            if(R[0].size() && R[1].size())
                best=min(best, comp(0, *R[0].begin()) + comp(1, *R[1].begin()));
        }
    }
    if(best==INFF)
        best=0;
    cout << ans+best << nl;
}
# 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 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 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 2 ms 340 KB Output is correct
11 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 1 ms 340 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 340 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 69 ms 11268 KB Output is correct
13 Correct 116 ms 11200 KB Output is correct
14 Correct 96 ms 10048 KB Output is correct
15 Correct 61 ms 6744 KB Output is correct
16 Correct 76 ms 11232 KB Output is correct
17 Correct 83 ms 11208 KB Output is correct
18 Correct 88 ms 11244 KB Output is correct
19 Correct 91 ms 11200 KB Output is correct
20 Correct 86 ms 11276 KB Output is correct
21 Correct 89 ms 11216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 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 324 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 348 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 192 ms 11192 KB Output is correct
26 Correct 182 ms 11272 KB Output is correct
27 Correct 331 ms 11272 KB Output is correct
28 Correct 338 ms 11276 KB Output is correct
29 Correct 330 ms 11328 KB Output is correct
30 Correct 136 ms 6088 KB Output is correct
31 Correct 203 ms 11328 KB Output is correct
32 Correct 194 ms 11240 KB Output is correct
33 Correct 222 ms 11276 KB Output is correct
34 Correct 202 ms 11236 KB Output is correct
35 Correct 196 ms 11216 KB Output is correct
36 Correct 197 ms 11276 KB Output is correct
37 Correct 204 ms 11152 KB Output is correct