답안 #487345

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
487345 2021-11-15T09:00:52 Z leaked Palembang Bridges (APIO15_bridge) C++14
100 / 100
1156 ms 2280 KB
#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define vec vector
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define fast_iati ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;
template <class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template <class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll inf=1e18;
signed main(){
    fast_iati;

    int k,n;
    cin>>k>>n;
    vec<int>l,r;
    ll ans=0;
    vec<int>kek;
    for(int i=0;i<n;i++){
        char f,s;
        int x,y;
        cin>>f>>x>>s>>y;
        if(f==s) ans+=abs(x-y);
        else{
            if(x>y) swap(x,y);
            l.pb(x);r.pb(y);
        }
    }
    n=sz(l);
    for(int i=0;i<n;i++){
        kek.pb(l[i]);
        kek.pb(r[i]);
    }
    sort(all(kek));kek.erase(unique(all(kek)),kek.end());
    if(k==1){
        if(!sz(kek)){
            cout<<ans;
            return 0;
        }
        auto f=[&](int i){
            int x=kek[i];
            ll me=0;
            for(int k=0;k<n;k++){
                me+=abs(x-l[k])+abs(x-r[k])+1;
            }
            return me;
        };
        int tl=0,tr=sz(kek)-1;
        while(tr-tl>=3){
            int t1=tl+(tr-tl)/3;
            int t2=tr-(tr-tl)/3;
            if(f(t1)<f(t2)) tr=t2;
            else tl=t1;
        }
        ll mn=inf;
        for(int i=tl;i<=tr;i++)
            umin(mn,f(i));
        cout<<ans+mn;
    }
    else{
        if(!sz(kek)){
            cout<<ans;
            return 0;
        }
        auto f1=[&](int i,int j){
            int x=kek[i],y=kek[j];
            ll me=0;
            for(int k=0;k<n;k++){
                me+=min(abs(y-l[k])+abs(y-r[k]),abs(x-l[k])+abs(x-r[k]))+1;
            }
            return me;
        };
        auto f=[&](int i){
            int tl=0,tr=sz(kek)-1;
            while(tr-tl>=10){
                int t1=tl+(tr-tl)/3;
                int t2=tr-(tr-tl)/3;
                if(f1(i,t1)<f1(i,t2)) tr=t2;
                else tl=t1;
            }
            ll mn=inf;
            for(int j=tl;j<=tr;j++) umin(mn,f1(i,j));
            return mn;
        };
        int tl=0,tr=sz(kek)-1;
        while(tr-tl>=10){
            int t1=tl+(tr-tl)/3;
            int t2=tr-(tr-tl)/3;
            if(f(t1)<f(t2)) tr=t2;
            else tl=t1;
        }
        ll mn=inf;
        for(int i=tl;i<=tr;i++)
            umin(mn,f(i));
        cout<<ans+mn;
    }
    return 0;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 244 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 19 ms 2236 KB Output is correct
13 Correct 45 ms 2224 KB Output is correct
14 Correct 33 ms 2148 KB Output is correct
15 Correct 27 ms 1480 KB Output is correct
16 Correct 24 ms 2232 KB Output is correct
17 Correct 35 ms 2208 KB Output is correct
18 Correct 40 ms 2172 KB Output is correct
19 Correct 45 ms 2200 KB Output is correct
20 Correct 27 ms 2164 KB Output is correct
21 Correct 44 ms 2248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 6 ms 340 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 4 ms 332 KB Output is correct
21 Correct 4 ms 332 KB Output is correct
22 Correct 5 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 6 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 5 ms 332 KB Output is correct
21 Correct 5 ms 332 KB Output is correct
22 Correct 6 ms 328 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 5 ms 332 KB Output is correct
25 Correct 21 ms 2224 KB Output is correct
26 Correct 163 ms 2260 KB Output is correct
27 Correct 1039 ms 2144 KB Output is correct
28 Correct 1066 ms 2232 KB Output is correct
29 Correct 1156 ms 2220 KB Output is correct
30 Correct 496 ms 1480 KB Output is correct
31 Correct 26 ms 2232 KB Output is correct
32 Correct 1075 ms 2192 KB Output is correct
33 Correct 1083 ms 2176 KB Output is correct
34 Correct 1049 ms 2280 KB Output is correct
35 Correct 28 ms 2224 KB Output is correct
36 Correct 1063 ms 2232 KB Output is correct
37 Correct 52 ms 2168 KB Output is correct