답안 #244411

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
244411 2020-07-03T23:47:35 Z WhaleVomit Palembang Bridges (APIO15_bridge) C++14
100 / 100
804 ms 36112 KB
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()

#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)

#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;

const int MAX_N = 100100;
int k, n;
vector<pi> arr;
map<int,int> startsAt;
map<int,int> endsAt;
vi points;

ll getMinDis1() {
    ll cur = 0;
    ll numBefore = 0;
    ll numAfter = 0;
    for(pi p: arr) if(p.f > points[0]) {
        numAfter++;
        cur += p.f - points[0];
    }
    ll ans = cur;
    MOO(i, 1, sz(points)) {
        cur -= numAfter*(points[i] - points[i-1]);
        numAfter -= startsAt[points[i]];
        numBefore += endsAt[points[i-1]];
        cur += numBefore*(points[i] - points[i-1]);
        ans = min(ans, cur);
    }
    return ans;
}

vector<ll> getVals(vector<pi>& v) {
    ll val = 0;
    ll startAfter = 0;
    ll endBefore = 0;
    multiset<pi> stuff; // (x,0) = start at x, (x,1) = end at x
    map<int,int> ea, sa;
    stuff.insert(mp(v[0].f, 0)); stuff.insert(mp(v[0].s, 1));
    sa[v[0].f]++; ea[v[0].s]++;
    vector<ll> res; res.pb(0);
    res.pb(val);
    auto it = stuff.begin();
    MOO(i, 1, sz(v)) {
        stuff.insert(mp(v[i].f, 0));
        sa[v[i].f]++;
        if(v[i].f > (*it).f) {
            val += v[i].f - (*it).f;
            startAfter++;
        }
        stuff.insert(mp(v[i].s, 1));
        ea[v[i].s]++;
        if(v[i].s < (*it).f) {
            val += (*it).f - v[i].s;
            endBefore++;
        }
        while(startAfter > endBefore) {
            if(next(it) == stuff.end()) break;
            ll oval = val;
            int curx = (*it).f;
            int nextx = (*(next(it))).f;
            if(curx == nextx) {
                it++;
                continue;
            }
            val -= startAfter*(nextx - curx);
            if((*it).s == 1) endBefore += ea[curx];
            it++;
            if((*it).s == 0) startAfter -= sa[nextx];
            val += endBefore*(nextx - curx);
            if(oval < val) {
                it--;
                val = oval;
                endBefore -= ea[curx];
                startAfter += sa[nextx];
                break;
            }
        }
        while(startAfter < endBefore) {
            if(it == stuff.begin()) break;
            ll oval = val;
            int curx = (*it).f;
            int nextx = (*(prev(it))).f;
            if(curx == nextx) {
                it--;
                continue;
            }
            val -= endBefore*(curx - nextx);
            if((*it).s == 0) startAfter += sa[curx];
            it--;
            if((*it).s == 1) endBefore -= ea[nextx];
            val += startAfter*(curx - nextx);
            if(oval < val) {
                it++;
                val = oval;
                startAfter -= sa[curx];
                endBefore += ea[nextx];
                break;
            }
        }
        res.pb(val);
    }
    return res;
}

ll getMinDis2() {
    vector<pair<int, pi>> tmp;
    M00(i, sz(arr)) {
        tmp.pb(mp(arr[i].f + arr[i].s, arr[i]));
    }
    sort(all(tmp));
    vector<pi> sorted;
    M00(i, sz(tmp)) sorted.pb(tmp[i].s);
    vector<ll> v1 = getVals(sorted);
    reverse(all(sorted));
    vector<ll> v2 = getVals(sorted);
    reverse(all(v2));
    M00(i, sz(v1)) {
    }
    ll ans = 1e18;
    M00(i, sz(v1)) {
        ans = min(ans, v1[i]+v2[i]);
    }
    return ans;
}

int getDis(int x, pi inter) {
    if(inter.f <= x && x <= inter.s) return 0;
    if(x < inter.f) return inter.f - x;
    return x - inter.s;
}

bool test() {
    int n = 6;
    int MX = 10;
    arr.clear();
    M00(i, n) {
        int a = rand()%MX;
        int b = rand()%MX;
        if(a > b) swap(a,b);
        arr.pb(mp(a,b));
    }
    ll cnt1 = 1e9;
    int besi = 0; int besj = 0;
    M00(i, MX) {
        MOO(j, i, MX) {
            ll cnt = 0;
            M00(k, n) {
                cnt += min(getDis(i, arr[k]), getDis(j, arr[k]));
            }
            if(cnt < cnt1) {
                cnt1 = cnt;
                besi = i;
                besj = j;
            }
        }
    }
    ll cnt2 = getMinDis2();
    if(cnt1 == cnt2) {
        cout << "OK\n";
        return 1;
    }
    else {
        cout << "BAD\n";
        cout << n << "\n";
        M00(i, n) {
            cout << arr[i].f << " " << arr[i].s << "\n";
        }
        return 0;
    }
}

int main() { FAST
    /*M00(i, 100) {
        if(!test()) return 0;
    }*/
    cin >> k >> n;
    int cnt = 0;
    ll ans = 0;
    M00(i, n) {
        string s1, s2;
        int i1, i2;
        cin >> s1 >> i1 >> s2 >> i2;
        if(i1 > i2) swap(i1, i2);
        if(s1 == s2) {
            ans += (i2-i1);
        } else {
            cnt++;
            ans += (i2-i1);
            arr.pb(mp(i1,i2));
            startsAt[i1]++;
            endsAt[i2]++;
            points.pb(i1);
            points.pb(i2);
        }
    }
    sort(all(points));
    points.erase(unique(all(points)), points.end());
    if(sz(arr) == 0) {
        finish(ans);
    }
    if(k == 1) {
        ans += getMinDis1()*2LL + cnt;
    } else {
        ans += getMinDis2()*2LL + cnt;
    }
    finish(ans);
}

Compilation message

bridge.cpp: In function 'bool test()':
bridge.cpp:165:9: warning: variable 'besi' set but not used [-Wunused-but-set-variable]
     int besi = 0; int besj = 0;
         ^~~~
bridge.cpp:165:23: warning: variable 'besj' set but not used [-Wunused-but-set-variable]
     int besi = 0; int besj = 0;
                       ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 512 KB Output is correct
8 Correct 6 ms 512 KB Output is correct
9 Correct 6 ms 512 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 6 ms 512 KB Output is correct
8 Correct 6 ms 512 KB Output is correct
9 Correct 6 ms 512 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 38 ms 2164 KB Output is correct
13 Correct 248 ms 20660 KB Output is correct
14 Correct 79 ms 2792 KB Output is correct
15 Correct 136 ms 12392 KB Output is correct
16 Correct 48 ms 2156 KB Output is correct
17 Correct 148 ms 20704 KB Output is correct
18 Correct 173 ms 20704 KB Output is correct
19 Correct 216 ms 19752 KB Output is correct
20 Correct 49 ms 2156 KB Output is correct
21 Correct 168 ms 20700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 7 ms 512 KB Output is correct
15 Correct 7 ms 640 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 6 ms 512 KB Output is correct
20 Correct 6 ms 640 KB Output is correct
21 Correct 8 ms 640 KB Output is correct
22 Correct 7 ms 768 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 7 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 7 ms 512 KB Output is correct
15 Correct 7 ms 768 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 6 ms 512 KB Output is correct
20 Correct 7 ms 640 KB Output is correct
21 Correct 7 ms 768 KB Output is correct
22 Correct 7 ms 640 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 7 ms 768 KB Output is correct
25 Correct 154 ms 15680 KB Output is correct
26 Correct 176 ms 15812 KB Output is correct
27 Correct 804 ms 34272 KB Output is correct
28 Correct 744 ms 36048 KB Output is correct
29 Correct 744 ms 35964 KB Output is correct
30 Correct 297 ms 19136 KB Output is correct
31 Correct 158 ms 16596 KB Output is correct
32 Correct 343 ms 35984 KB Output is correct
33 Correct 346 ms 35612 KB Output is correct
34 Correct 411 ms 36112 KB Output is correct
35 Correct 163 ms 16700 KB Output is correct
36 Correct 378 ms 35748 KB Output is correct
37 Correct 159 ms 15672 KB Output is correct