답안 #257486

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257486 2020-08-04T10:48:35 Z evpipis Palembang Bridges (APIO15_bridge) C++11
100 / 100
1713 ms 20140 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
typedef pair<int, ll> pil;

const int len = 1e5+5, mx = 2e5, mx_ts = 1e9;
vector<ii> vec;
vector<int> cord, hel;
ll pref[len], suf[len];
map<int, int> mym;

struct bit{
    pil tree[mx+4];
    int rev;

    bit(int r = 0){
        rev = r;
    }

    void init(){
        for (int i = 1; i <= mx; i++)
            tree[i] = mp(0, 0);
    }

    void add(int i, int v){
        if (rev)
            i = mx-i+1;

        while (i <= mx)
            tree[i].fi++, tree[i].se += v, i += i&(-i);
    }

    pil ask(int i){
        if (rev)
            i = mx-i+1;

        pil ans = mp(0, 0);
        while (i > 0)
            ans.fi += tree[i].fi, ans.se += tree[i].se, i -= i&(-i);
        return ans;
    }
};

bit lef = bit(1), rig = bit(0);

ll func(int pos){
    pil le = lef.ask(mym[pos]);
    pil ri = rig.ask(mym[pos]);

    return (le.se - pos*1LL*le.fi) + (pos*1LL*ri.fi - ri.se);
}

ll ts(){
    int l = -1, r = (int)hel.size()-1;
    while (l+1 < r){
        int mid = (l+r)/2;
        if (func(hel[mid]) < func(hel[mid+1]))
            r = mid;
        else
            l = mid;
    }

    return func(hel[l+1]);
}

bool comp(ii a, ii b){
    return (a.fi+a.se < b.fi+b.se);
}

int main(){
    int k, n;
    ll sum = 0;
    scanf("%d %d", &k, &n);
    for (int i = 0; i < n; i++){
        char tpa, tpb;
        int a, b;
        scanf(" %c %d %c %d", &tpa, &a, &tpb, &b);

        if (a > b)
            swap(a, b);
        sum += b-a;

        if (tpa != tpb)
            vec.pb(mp(a, b)), sum++;
    }

    int m = vec.size();
    sort(vec.begin(), vec.end(), comp);

    for (int i = 0; i < m; i++){
        int a = vec[i].fi, b = vec[i].se;
        cord.pb(a), cord.pb(b);
    }

    sort(cord.begin(), cord.end());

    int cnt = 0;
    for (int i = 0; i < cord.size(); i++)
        if (i == 0 || cord[i] != cord[i-1])
            hel.pb(cord[i]), mym[cord[i]] = ++cnt;

    lef.init(), rig.init();
    for (int i = 0; i < m; i++){
        int a = vec[i].fi, b = vec[i].se;
        lef.add(mym[a], a);
        rig.add(mym[b], b);

        pref[i] = ts();
    }

    if (k == 1){
        printf("%lld\n", sum+2*pref[m-1]);
        return 0;
    }

    lef.init(), rig.init();
    for (int i = m-1; i >= 0; i--){
        int a = vec[i].fi, b = vec[i].se;
        lef.add(mym[a], a);
        rig.add(mym[b], b);

        suf[i] = ts();
    }

    ll ans = pref[m-1];
    for (int i = 0; i+1 < m; i++)
        ans = min(ans, pref[i]+suf[i+1]);

    printf("%lld\n", 2*ans+sum);
    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:104:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < cord.size(); i++)
                     ~~^~~~~~~~~~~~~
bridge.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &k, &n);
     ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %c %d %c %d", &tpa, &a, &tpb, &b);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6528 KB Output is correct
2 Correct 5 ms 6528 KB Output is correct
3 Correct 5 ms 6656 KB Output is correct
4 Correct 7 ms 6656 KB Output is correct
5 Correct 7 ms 6656 KB Output is correct
6 Correct 5 ms 6656 KB Output is correct
7 Correct 7 ms 6676 KB Output is correct
8 Correct 6 ms 6656 KB Output is correct
9 Correct 7 ms 6784 KB Output is correct
10 Correct 5 ms 6656 KB Output is correct
11 Correct 8 ms 6656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 6528 KB Output is correct
2 Correct 6 ms 6656 KB Output is correct
3 Correct 5 ms 6656 KB Output is correct
4 Correct 7 ms 6656 KB Output is correct
5 Correct 7 ms 6656 KB Output is correct
6 Correct 5 ms 6656 KB Output is correct
7 Correct 7 ms 6656 KB Output is correct
8 Correct 6 ms 6656 KB Output is correct
9 Correct 7 ms 6656 KB Output is correct
10 Correct 5 ms 6656 KB Output is correct
11 Correct 10 ms 6656 KB Output is correct
12 Correct 56 ms 9072 KB Output is correct
13 Correct 867 ms 19180 KB Output is correct
14 Correct 261 ms 9188 KB Output is correct
15 Correct 452 ms 13936 KB Output is correct
16 Correct 62 ms 9064 KB Output is correct
17 Correct 746 ms 19304 KB Output is correct
18 Correct 697 ms 19048 KB Output is correct
19 Correct 743 ms 18612 KB Output is correct
20 Correct 69 ms 9196 KB Output is correct
21 Correct 769 ms 19176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6528 KB Output is correct
2 Correct 4 ms 6528 KB Output is correct
3 Correct 5 ms 6528 KB Output is correct
4 Correct 5 ms 6528 KB Output is correct
5 Correct 5 ms 6528 KB Output is correct
6 Correct 5 ms 6528 KB Output is correct
7 Correct 5 ms 6528 KB Output is correct
8 Correct 5 ms 6656 KB Output is correct
9 Correct 5 ms 6528 KB Output is correct
10 Correct 5 ms 6528 KB Output is correct
11 Correct 5 ms 6528 KB Output is correct
12 Correct 5 ms 6528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6528 KB Output is correct
2 Correct 5 ms 6528 KB Output is correct
3 Correct 5 ms 6528 KB Output is correct
4 Correct 5 ms 6656 KB Output is correct
5 Correct 5 ms 6528 KB Output is correct
6 Correct 5 ms 6528 KB Output is correct
7 Correct 5 ms 6528 KB Output is correct
8 Correct 6 ms 6656 KB Output is correct
9 Correct 5 ms 6656 KB Output is correct
10 Correct 5 ms 6656 KB Output is correct
11 Correct 5 ms 6656 KB Output is correct
12 Correct 5 ms 6656 KB Output is correct
13 Correct 5 ms 6656 KB Output is correct
14 Correct 7 ms 6656 KB Output is correct
15 Correct 10 ms 6656 KB Output is correct
16 Correct 5 ms 6656 KB Output is correct
17 Correct 6 ms 6656 KB Output is correct
18 Correct 6 ms 6656 KB Output is correct
19 Correct 5 ms 6656 KB Output is correct
20 Correct 10 ms 6784 KB Output is correct
21 Correct 8 ms 6656 KB Output is correct
22 Correct 9 ms 6656 KB Output is correct
23 Correct 5 ms 6656 KB Output is correct
24 Correct 9 ms 6656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6528 KB Output is correct
2 Correct 5 ms 6528 KB Output is correct
3 Correct 4 ms 6528 KB Output is correct
4 Correct 5 ms 6528 KB Output is correct
5 Correct 5 ms 6528 KB Output is correct
6 Correct 4 ms 6528 KB Output is correct
7 Correct 5 ms 6528 KB Output is correct
8 Correct 5 ms 6656 KB Output is correct
9 Correct 5 ms 6528 KB Output is correct
10 Correct 5 ms 6656 KB Output is correct
11 Correct 5 ms 6528 KB Output is correct
12 Correct 5 ms 6656 KB Output is correct
13 Correct 6 ms 6656 KB Output is correct
14 Correct 6 ms 6656 KB Output is correct
15 Correct 9 ms 6656 KB Output is correct
16 Correct 5 ms 6656 KB Output is correct
17 Correct 6 ms 6656 KB Output is correct
18 Correct 6 ms 6656 KB Output is correct
19 Correct 5 ms 6656 KB Output is correct
20 Correct 9 ms 6772 KB Output is correct
21 Correct 8 ms 6784 KB Output is correct
22 Correct 13 ms 6656 KB Output is correct
23 Correct 6 ms 6656 KB Output is correct
24 Correct 9 ms 6656 KB Output is correct
25 Correct 67 ms 9832 KB Output is correct
26 Correct 169 ms 9796 KB Output is correct
27 Correct 1577 ms 19044 KB Output is correct
28 Correct 1713 ms 20140 KB Output is correct
29 Correct 1636 ms 20072 KB Output is correct
30 Correct 708 ms 13680 KB Output is correct
31 Correct 75 ms 9960 KB Output is correct
32 Correct 1442 ms 20016 KB Output is correct
33 Correct 1317 ms 20068 KB Output is correct
34 Correct 1431 ms 19512 KB Output is correct
35 Correct 86 ms 9832 KB Output is correct
36 Correct 1474 ms 20096 KB Output is correct
37 Correct 86 ms 9832 KB Output is correct