Submission #429954

# Submission time Handle Problem Language Result Execution time Memory
429954 2021-06-16T10:42:09 Z JeanBombeur Ideal city (IOI12_city) C++17
55 / 100
611 ms 5808 KB
#include <iostream>
#include <cstdio>
#include <unordered_map>
#include <algorithm>
using namespace std;

const long long MOD = (1000 * 1000 * 1000);
const int MAX_POINTS = (100 * 1000);

struct info {
    int lig, col;
    long long dist;
};

int Dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

unordered_map <long long, int> Points;

info File[MAX_POINTS];
int deb = 0, fin = 0;

long long Hash(int lig, int col) {
    return ((long long)lig << 32) + (long long)col;
}

void Add(info cur) {
    for (int i = 0; i < 4; i ++)
    {
        int nvLig = cur.lig + Dir[i][0], nvCol = cur.col + Dir[i][1];
        if (Points[Hash(nvLig, nvCol)] -- > 0)
            File[fin ++] = {nvLig, nvCol, cur.dist + 1};
    }
    return;
}

long long Bfs(info dep) {
    Points[Hash(dep.lig, dep.col)] --;
    deb = 0, fin = 0;
    File[fin ++] = dep;
    long long ans = 0;
    while (deb < fin)
    {
        // printf("%lld ", File[deb].dist);
        ans += File[deb].dist;
        Add(File[deb ++]);
    }
    for (int i = 0; i < fin; i ++)
    {
        Points[Hash(File[i].lig, File[i].col)] = 1;
    }
    return ans % MOD;
}

int DistanceSum(int nbPoints, int *X, int *Y) {

    long long sumX = 0, sumY = 0;
    
    for (int i = 0; i < nbPoints; i ++)
    {
        Points[Hash(X[i], Y[i])] ++;
        sumX += X[i], sumY += Y[i];
    }
    
    long long ans = 0;
    
    if (nbPoints <= 2000)
    {
        for (int i = 0; i < nbPoints; i ++)
        {
            ans += Bfs({X[i], Y[i], 0LL});
            // printf("%lld ", ans);
        }
        ans %= MOD;
        return ans / 2;
    }
    
    sort(X, X + nbPoints);
    sort(Y, Y + nbPoints);
    
    for (int i = 0; i < nbPoints; i ++)
    {
        ans += sumX - (nbPoints - i) * X[i];
        sumX -= X[i];
        ans += sumY - (nbPoints - i) * Y[i];
        sumY -= Y[i];
        ans %= MOD;
    }
    
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 5 ms 204 KB Output is correct
5 Correct 4 ms 204 KB Output is correct
6 Correct 7 ms 324 KB Output is correct
7 Correct 8 ms 204 KB Output is correct
8 Correct 7 ms 204 KB Output is correct
9 Correct 6 ms 204 KB Output is correct
10 Correct 5 ms 204 KB Output is correct
11 Correct 8 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 332 KB Output is correct
2 Correct 145 ms 372 KB Output is correct
3 Correct 345 ms 460 KB Output is correct
4 Correct 329 ms 416 KB Output is correct
5 Correct 604 ms 500 KB Output is correct
6 Correct 571 ms 468 KB Output is correct
7 Correct 611 ms 500 KB Output is correct
8 Correct 593 ms 468 KB Output is correct
9 Correct 536 ms 580 KB Output is correct
10 Correct 541 ms 472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1268 KB Output is correct
2 Correct 9 ms 1228 KB Output is correct
3 Correct 23 ms 2992 KB Output is correct
4 Correct 23 ms 2988 KB Output is correct
5 Correct 64 ms 5808 KB Output is correct
6 Correct 48 ms 5748 KB Output is correct
7 Correct 53 ms 5732 KB Output is correct
8 Correct 45 ms 5792 KB Output is correct
9 Correct 44 ms 5748 KB Output is correct
10 Correct 45 ms 5708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 1228 KB Output isn't correct
2 Halted 0 ms 0 KB -