답안 #759881

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
759881 2023-06-17T02:24:55 Z Username4132 Palembang Bridges (APIO15_bridge) C++14
100 / 100
166 ms 25420 KB
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define forsn(i, s, n) for(int i=s; i<(int)n; ++i)
#define dforn(i, n) for(int i=n-1; i>=0; --i)
#define F first
#define S second

const int INF=1000000010, MAXN=100010;
int k, n, cnt;
ll ans, res[2][MAXN], base[2*MAXN];
pii arr[MAXN];

struct med{
    set<pii> st;
    set<pii>::iterator itr;
    int pos=0;
    ll prSum=0, sum=0;
    med(){
        st.insert({INF, -1}), st.insert({-INF, -2});
        itr=st.begin();
    }
    void stabilize(){
        int target = (st.size() - 1)/2;
        while(pos > target){
            prSum-=itr->F, --pos, --itr;
        }
        while(pos < target){
            ++pos, ++itr, prSum+=itr->F;
        }
    }

    void insert(pii x){
        st.insert(x);
        if(x < *itr) ++pos, prSum+=x.F;
        sum+=x.F;
        stabilize();
    }


    pii debug(){
        return *itr;
    }

    ll calc(){
        ll off = (st.size()&1)? itr->F : 0;
        return sum - 2*prSum + off;
    }
};

int main(){
    scanf("%d %d", &k, &n);
    forn(i, n){
        char c1, c2;
        int p1, p2;
        scanf(" %c %d %c %d", &c1, &p1, &c2, &p2);
        if(c1==c2) ans+=abs(p1-p2);
        else{
            base[2*cnt]=p1;
            base[2*cnt+1]=p2;
            arr[cnt++]={p1, p2};
        }
    }
    ans+=cnt;
    if(k==1){
        sort(base, base+2*cnt);
        forn(i, cnt) ans-=base[i];
        forsn(i, cnt, 2*cnt) ans+=base[i];
        printf("%lld\n", ans);
    }
    if(k==2){
        sort(arr, arr+cnt, [](pii a, pii b){
            return (a.F+a.S) < (b.F+b.S);
        });
        med aa, bb;
        forn(i, cnt){
            aa.insert({arr[i].F, 2*i});
            aa.insert({arr[i].S, 2*i+1});
            res[0][i+1]=aa.calc();
        }
        dforn(i, cnt){
            bb.insert({arr[i].F, 2*i});
            bb.insert({arr[i].S, 2*i+1});
            res[1][i]=bb.calc();
        }
        ll mn=4000000000000000000;
        forn(i, cnt+1) mn=min(mn, res[0][i]+res[1][i]);
        ans+=mn;
        printf("%lld\n", ans);
    }
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |     scanf("%d %d", &k, &n);
      |     ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         scanf(" %c %d %c %d", &c1, &p1, &c2, &p2);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 320 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 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 352 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 29 ms 3360 KB Output is correct
13 Correct 37 ms 4944 KB Output is correct
14 Correct 28 ms 3608 KB Output is correct
15 Correct 22 ms 2956 KB Output is correct
16 Correct 25 ms 4252 KB Output is correct
17 Correct 27 ms 4920 KB Output is correct
18 Correct 31 ms 4468 KB Output is correct
19 Correct 35 ms 4940 KB Output is correct
20 Correct 28 ms 4420 KB Output is correct
21 Correct 33 ms 4668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 212 KB Output is correct
6 Correct 1 ms 212 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 308 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 2 ms 432 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 468 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 444 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 1 ms 472 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 312 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 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 352 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 1 ms 340 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 476 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 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 444 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 448 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 91 ms 23736 KB Output is correct
26 Correct 122 ms 23884 KB Output is correct
27 Correct 166 ms 24612 KB Output is correct
28 Correct 155 ms 25304 KB Output is correct
29 Correct 159 ms 25420 KB Output is correct
30 Correct 79 ms 13524 KB Output is correct
31 Correct 90 ms 24644 KB Output is correct
32 Correct 87 ms 25420 KB Output is correct
33 Correct 84 ms 24928 KB Output is correct
34 Correct 79 ms 25248 KB Output is correct
35 Correct 87 ms 24808 KB Output is correct
36 Correct 92 ms 24952 KB Output is correct
37 Correct 85 ms 23692 KB Output is correct