답안 #1005921

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1005921 2024-06-23T07:56:42 Z 정민찬(#10832) 이상적인 도시 (IOI12_city) C++14
0 / 100
31 ms 5724 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll mod = 1e9;

ll N;

vector<ll> adj[100010];

ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};

ll getDist(ll st) {
    vector<ll> d(N, -1);
    d[st] = 0;
    ll ans = 0;
    queue<ll> q;
    q.push(st);
    while (!q.empty()) {
        auto x = q.front();
        q.pop();
        ans += d[x];
        for (auto &nx : adj[x]) {
            if (d[nx] == -1) {
                d[nx] = d[x] + 1;
                q.push(nx);
            }
        }
    }
    return ans;
}

int DistanceSum(int _N, int *X, int *Y) {
    N = _N;
    ll mnX = *min_element(X, X+N);
    ll mnY = *min_element(Y, Y+N);
    for (ll i=0; i<N; i++) {
        X[i] -= mnX;
        Y[i] -= mnY;
    }
    vector<ll> SX(N, 0), SY(N, 0);
    map<pair<ll,ll>, ll> mp;
    for (ll i=0; i<N; i++) {
        mp[{X[i], Y[i]}] = i;
        SX[X[i]] ++;
        SY[Y[i]] ++;
    }
    for (ll i=1; i<N; i++) {
        SX[i] += SX[i-1];
        SY[i] += SY[i-1];
    }
    for (ll i=0; i<N; i++) {
        for (ll k=0; k<4; k++) {
            ll nx = X[i] + dx[k];
            ll ny = Y[i] + dy[k];
            if (mp.find({nx, ny}) != mp.end()) {
                adj[i].push_back(mp[{nx, ny}]);
            }
        }
    }
    vector<ll> totd(N, 0);
    totd[0] = getDist(0);
    queue<ll> q;
    q.push(0);
    while (!q.empty()) {
        ll x = q.front();
        q.pop();
        for (auto &nx : adj[x]) {
            if (totd[nx]) continue;
            totd[nx] = totd[x];
            if (X[x] < X[nx]) {
                totd[nx] += SX[X[x]];
                totd[nx] -= N - SX[X[x]];
            }
            else if (X[x] > X[nx]){
                totd[nx] += N - SX[X[nx]];
                totd[nx] -= SX[X[nx]];
            }
            else if (Y[x] < Y[nx]) {
                totd[nx] += SY[Y[x]];
                totd[nx] -= N - SY[Y[x]];
            }
            else if (Y[x] > Y[nx]){
                totd[nx] += N - SY[Y[nx]];
                totd[nx] -= SY[Y[nx]];
            }
            q.push(nx);
        }
    }
    ll ans = 0;
    for (ll i=0; i<N; i++) {
        ans += totd[i];
        ans %= mod;
    }
    return ans / 2;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 5724 KB Output is correct
2 Incorrect 31 ms 5716 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 5548 KB Output isn't correct
2 Halted 0 ms 0 KB -