답안 #97524

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
97524 2019-02-16T18:00:57 Z Alexa2001 이상적인 도시 (IOI12_city) C++17
100 / 100
277 ms 22548 KB
#include <bits/stdc++.h>

using namespace std;

const int Mod = 1e9, Nmax = 1e5 + 5;
typedef long long ll;

int n, x[Nmax], y[Nmax], w[Nmax];
vector<int> edge[Nmax], v[Nmax];

void dfs(int node, int dad = 0)
{
    for(auto it : edge[node])
        if(it != dad)
        {
            dfs(it, node);
            w[node] += w[it];
            w[node] %= Mod;
        }
}

void add_edge(set< pair<int,int> > &Edges, int x, int y)
{
    if(Edges.find({x, y}) != Edges.end()) return;
    edge[x].push_back(y); edge[y].push_back(x);
    Edges.insert({x, y});
}

int calc_dist() /// vertical
{
    int i, j;
    map< pair<int,int> , int > where;
    set< pair<int,int> > Edges;

    int nr = 0;
    for(i=1; i<=n; ++i) w[i] = 0, edge[i].clear(), v[i].clear();

    for(i=0; i<n; ++i) v[x[i]].push_back(y[i]);

    for(i=1; i<=n; ++i)
    {
        sort(v[i].begin(), v[i].end());

        for(j=0; j<v[i].size(); ++j)
        {
            if(!j || v[i][j] != v[i][j-1] + 1) ++nr;
            where[{i, v[i][j]}] = nr; w[nr] ++;
        }
    }

    for(i=1; i<=n; ++i)
        for(auto it : v[i])
            if(where[{ i+1, it }])
                add_edge(Edges, where[{ i+1, it }], where[{ i, it }]);
    dfs(1);
    ll ans = 0;
    for(i=1; i<=nr; ++i)
    {
        ans += (ll) w[i] * (w[1] - w[i]);
        ans %= Mod;
    }
    return ans;
}

int DistanceSum(int N, int *X, int *Y)
{
    int i, xmin = 2e9, ymin = 2e9; n = N;
    for(i=0; i<N; ++i)
    {
        x[i] = X[i];
        y[i] = Y[i];
        xmin = min(xmin, x[i]);
        ymin = min(ymin, y[i]);
    }

    for(i=0; i<N; ++i)
        x[i] -= xmin - 1, y[i] -= ymin - 1;

    int ans = calc_dist();
    for(i=0; i<n; ++i) swap(x[i], y[i]);
    ans += calc_dist();
    return ans % Mod;
}

Compilation message

city.cpp: In function 'int calc_dist()':
city.cpp:44:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(j=0; j<v[i].size(); ++j)
                  ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4864 KB Output is correct
2 Correct 10 ms 4992 KB Output is correct
3 Correct 6 ms 4992 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 8 ms 4992 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 7 ms 4992 KB Output is correct
8 Correct 6 ms 4992 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 9 ms 5116 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 5248 KB Output is correct
2 Correct 12 ms 5376 KB Output is correct
3 Correct 9 ms 5248 KB Output is correct
4 Correct 9 ms 5248 KB Output is correct
5 Correct 10 ms 5376 KB Output is correct
6 Correct 12 ms 5376 KB Output is correct
7 Correct 11 ms 5376 KB Output is correct
8 Correct 9 ms 5248 KB Output is correct
9 Correct 9 ms 5248 KB Output is correct
10 Correct 9 ms 5248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 7224 KB Output is correct
2 Correct 41 ms 7288 KB Output is correct
3 Correct 108 ms 10104 KB Output is correct
4 Correct 100 ms 10464 KB Output is correct
5 Correct 197 ms 14824 KB Output is correct
6 Correct 206 ms 15652 KB Output is correct
7 Correct 195 ms 15608 KB Output is correct
8 Correct 206 ms 14840 KB Output is correct
9 Correct 206 ms 15736 KB Output is correct
10 Correct 227 ms 22548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 8324 KB Output is correct
2 Correct 40 ms 7928 KB Output is correct
3 Correct 127 ms 13020 KB Output is correct
4 Correct 126 ms 11908 KB Output is correct
5 Correct 251 ms 20984 KB Output is correct
6 Correct 223 ms 17272 KB Output is correct
7 Correct 277 ms 20988 KB Output is correct
8 Correct 253 ms 17532 KB Output is correct
9 Correct 219 ms 16528 KB Output is correct
10 Correct 237 ms 16504 KB Output is correct