Submission #287234

# Submission time Handle Problem Language Result Execution time Memory
287234 2020-08-31T14:11:24 Z stoyan_malinin Ideal city (IOI12_city) C++14
55 / 100
146 ms 5760 KB
//#include "grader.cpp"

#include <set>
#include <map>
#include <queue>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int mod = 1e9;

struct Point
{
    long long x, y;

    Point(){}
    Point(long long x, long long y)
    {
        this->x = x;
        this->y = y;
    }
};

int n;
vector <Point> v;

void init()
{
    int ind = 0;
    set <int> s;
    map <int, int> code;

    for(Point p: v) s.insert(p.x);
    for(int x: s) code[x] = ind, ind++;
    for(Point &p: v) p.x = code[p.x];

    //-----------------------

    ind = 0;
    s.clear();
    code.clear();

    for(Point p: v) s.insert(p.y);
    for(int y: s) code[y] = ind, ind++;
    for(Point &p: v) p.y  = code[p.y];
}

namespace Subtask2
{
    const vector <pair <int, int>> jumps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    const int MAXN = 2005;

    bool exists[MAXN][MAXN];
    int dist[MAXN][MAXN];

    void bfs(Point p)
    {
        queue <Point> q;
        for(Point p: v) dist[p.x][p.y] = -1;

        q.push(p);
        dist[p.x][p.y] = 0;

        while(q.empty()==false)
        {
            p = q.front();
            q.pop();

            for(pair <int, int> j: jumps)
            {
                if(p.x+j.first>=0 && p.y+j.second>=0
                   && dist[p.x+j.first][p.y+j.second]==-1
                   && exists[p.x+j.first][p.y+j.second]==true)
                {
                    q.push(Point(p.x+j.first, p.y+j.second));
                    dist[p.x+j.first][p.y+j.second] = dist[p.x][p.y] + 1;
                }
            }
        }
    }

    long long solve()
    {
        for(Point p: v) exists[p.x][p.y] = true;

        long long answer = 0;
        for(Point p: v)
        {
            bfs(p);
            for(Point other: v) answer = (answer + dist[other.x][other.y])%mod;
        }

        return answer;
    }
};

namespace Subtask3
{
    long long solve()
    {
        long long answer = 0;

        sort(v.begin(), v.end(),
        [&](Point A, Point B)
        {
            if(A.x!=B.x) return A.x<B.x;
            return A.y<B.y;
        });

        long long sum = 0;
        for(int i = 0;i<n;i++)
        {
            answer = (answer + (i*1LL*v[i].x - sum))%mod;
            sum += v[i].x;
        }

        sort(v.begin(), v.end(),
        [&](Point A, Point B)
        {
            if(A.y!=B.y) return A.y<B.y;
            return A.x<B.x;
        });

        sum = 0;
        for(int i = 0;i<n;i++)
        {
            answer = (answer + (i*1LL*v[i].y - sum))%mod;
            sum += v[i].y;
        }

        return answer;
    }
};

int DistanceSum(int N, int *X, int *Y)
{
    n = N;
    for(int i = 0;i<n;i++) v.push_back(Point(X[i], Y[i]));

    init();

    if(n<=2*1000) return Subtask2::solve()/2;
    return Subtask3::solve();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 896 KB Output is correct
2 Correct 33 ms 640 KB Output is correct
3 Correct 82 ms 1016 KB Output is correct
4 Correct 77 ms 896 KB Output is correct
5 Correct 143 ms 1024 KB Output is correct
6 Correct 121 ms 1016 KB Output is correct
7 Correct 146 ms 1404 KB Output is correct
8 Correct 126 ms 1400 KB Output is correct
9 Correct 120 ms 768 KB Output is correct
10 Correct 120 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 1148 KB Output is correct
2 Correct 14 ms 1148 KB Output is correct
3 Correct 34 ms 1916 KB Output is correct
4 Correct 35 ms 1916 KB Output is correct
5 Correct 74 ms 3320 KB Output is correct
6 Correct 72 ms 3312 KB Output is correct
7 Correct 76 ms 3320 KB Output is correct
8 Correct 69 ms 3312 KB Output is correct
9 Correct 72 ms 3312 KB Output is correct
10 Correct 92 ms 5760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 1148 KB Output isn't correct
2 Halted 0 ms 0 KB -