Submission #1005918

# Submission time Handle Problem Language Result Execution time Memory
1005918 2024-06-23T07:54:27 Z 정민찬(#10832) Ideal city (IOI12_city) C++14
0 / 100
28 ms 5316 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const ll mod = 1e9;

int N;

vector<int> adj[100010];

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

ll getDist(int st) {
    vector<int> d(N, -1);
    d[st] = 0;
    ll ans = 0;
    queue<int> 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;
    int mnX = *min_element(X, X+N);
    int mnY = *min_element(Y, Y+N);
    for (int i=0; i<N; i++) {
        X[i] -= mnX;
        Y[i] -= mnY;
    }
    vector<int> SX(N, 0), SY(N, 0);
    map<pair<int,int>, int> mp;
    for (int i=0; i<N; i++) {
        mp[{X[i], Y[i]}] = i;
        SX[X[i]] ++;
        SY[Y[i]] ++;
    }
    for (int i=1; i<N; i++) {
        SX[i] += SX[i-1];
        SY[i] += SY[i-1];
    }
    for (int i=0; i<N; i++) {
        for (int k=0; k<4; k++) {
            int nx = X[i] + dx[k];
            int 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<int> q;
    q.push(0);
    while (!q.empty()) {
        int 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 (int i=0; i<N; i++) {
        ans += totd[i];
        ans %= mod;
    }
    return ans / 2;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 27 ms 5172 KB Output is correct
2 Incorrect 28 ms 5316 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 5200 KB Output isn't correct
2 Halted 0 ms 0 KB -