Submission #380803

# Submission time Handle Problem Language Result Execution time Memory
380803 2021-03-23T03:00:12 Z jass921026 Palembang Bridges (APIO15_bridge) C++14
100 / 100
399 ms 14420 KB
#include<bits/stdc++.h>
using namespace std;
#define jizz ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef pair<int,int> pii;
#define F first
#define S second
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define mkp make_pair
bool cmp(pii a, pii b){
    return a.F+a.S<b.F+b.S;
}
int abs(int x){
    return x>0?x:-x;
}
vector<pii> segs, pos;
map<int,int> ls, lb, rs, rb;
void add(map<int,int> &mp, int pos){
    mp[pos]++;
}
void del(map<int,int> &mp, int pos){
    mp[pos]--;
    if(mp[pos]==0) mp.erase(pos);
}
int main(){ jizz
    int k, n;
    cin>>k>>n;
    ll tmp=0;
    for(int i=0;i<n;i++){
        char p, q;
        int s, t;
        cin>>p>>s>>q>>t;
        if(s>t) swap(s,t);
        tmp+=(t-s);
        if(p!=q) {
            tmp++;
            segs.pb(mkp(s,t));
            pos.pb(mkp(s,0));
            pos.pb(mkp(t,1));
        }
    }
    ll ans=tmp;
    if(segs.empty()){
        cout<<tmp<<"\n";
        return 0;
    }
    sort(ALL(segs),cmp);
    sort(ALL(pos));
    int sz=segs.size();
    ll lsum=0, rsum=0;
    for(int i=0;i<sz;i++){
        add(rs,pos[i].F);
        if(pos[i].S==1) rsum-=2*(pos[i].F);
    }
    for(int i=sz;i<sz*2;i++){
        add(rb,pos[i].F);
        if(pos[i].S==0) rsum+=2*(pos[i].F);
    }
    if(k==1){
        cout<<ans+lsum+rsum<<"\n";
        return 0;
    }
    //cout<<lsum<<" "<<rsum<<"\n";
    ll ans2=ans+lsum+rsum;
    for(int i=0;i<sz;i++){
        /*cout<<"rs=\n";
        for(auto j:rs){
            cout<<j.F<<" "<<j.S<<"\n";
        }
        cout<<"rb=\n";
        for(auto j:rb){
            cout<<j.F<<" "<<j.S<<"\n";
        }
        cout<<"ls=\n";
        for(auto j:ls){
            cout<<j.F<<" "<<j.S<<"\n";
        }
        cout<<"lb=\n";
        for(auto j:lb){
            cout<<j.F<<" "<<j.S<<"\n";
        }*/
        del(rs,segs[i].F);
        if(segs[i].S<rb.begin()->F){
            del(rs,segs[i].S);
            rsum+=2*segs[i].S;
            auto p=rb.begin();
            rsum-=2*(p->F);
            del(rb,p->F);
            add(rs,p->F);
        } else{
            del(rb,segs[i].S);
        }
        add(lb,segs[i].S);
        if(segs[i].F<lb.begin()->F){
            add(ls,segs[i].F);
        } else{
            add(lb,segs[i].F);
            lsum+=2*segs[i].F;
            auto p=lb.begin();
            lsum-=2*(p->F);
            del(lb,p->F);
            add(ls,p->F);
        }
        //cout<<lsum<<" "<<rsum<<"\n";
        ans2=min(ans2,ans+lsum+rsum);
    }
    cout<<ans2<<"\n";
    return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
2 3
A 3 B 3
A 4 B 4
A 5 B 5
*/

# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 40 ms 3160 KB Output is correct
13 Correct 113 ms 12116 KB Output is correct
14 Correct 62 ms 3160 KB Output is correct
15 Correct 60 ms 7260 KB Output is correct
16 Correct 41 ms 3160 KB Output is correct
17 Correct 85 ms 12136 KB Output is correct
18 Correct 88 ms 12140 KB Output is correct
19 Correct 105 ms 11604 KB Output is correct
20 Correct 44 ms 3160 KB Output is correct
21 Correct 97 ms 12116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 2 ms 492 KB Output is correct
21 Correct 2 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 2 ms 364 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 42 ms 3948 KB Output is correct
26 Correct 65 ms 4056 KB Output is correct
27 Correct 399 ms 13268 KB Output is correct
28 Correct 368 ms 14420 KB Output is correct
29 Correct 373 ms 14420 KB Output is correct
30 Correct 159 ms 7940 KB Output is correct
31 Correct 43 ms 4564 KB Output is correct
32 Correct 247 ms 14420 KB Output is correct
33 Correct 169 ms 14036 KB Output is correct
34 Correct 253 ms 14036 KB Output is correct
35 Correct 58 ms 4820 KB Output is correct
36 Correct 254 ms 14164 KB Output is correct
37 Correct 35 ms 3928 KB Output is correct