Submission #105322

# Submission time Handle Problem Language Result Execution time Memory
105322 2019-04-11T10:01:49 Z Alexa2001 Towns (IOI15_towns) C++17
100 / 100
28 ms 1024 KB
#include "towns.h"
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 505;

int x, y;
int D0[Nmax], D1[Nmax], Up[Nmax], L[Nmax];

map< int, vector<int> > gr;
map< pair<int,int> , int > mp;

int getDist(int x, int y)
{
    if(x == y) return 0;
    if(mp[{x,y}]) return mp[{x, y}];
    return mp[{x, y}] = mp[{y, x}] = getDistance(x, y);
}

void prepare(int n)
{
    int i;
    for(i=0; i<n; ++i) D0[i] = getDist(0, i);

    x = max_element(D0, D0+n) - D0;

    for(i=0; i<n; ++i) D1[i] = getDist(x, i);

    y = max_element(D1, D1+n) - D1;

    // for(i=0; i<n; ++i) D2[i] = getDist(y, i);
}


bool check(vector<int> &v, int n)
{
    if(v.size() <= n/2) return 1;

    vector<int> lista;
    int bucket = 0;

    for(auto it : v)
        if(lista.empty()) lista.push_back(it);
            else
            {
                int x = lista.back();

                if(getDist(x, it) == Up[x] + Up[it])
                {
                    lista.push_back(it);

                    if(bucket)
                    {
                        lista.push_back(x);
                        --bucket;
                    }
                }
                else ++bucket;
            }


    int cnt = bucket;
    int candidat = lista.back();

    while(lista.size())
    {
        int x = lista.back();

        if(getDist(x, candidat) != Up[x] + Up[candidat])
        {
            ++cnt;
            if(lista.size() >= 2) lista.resize(lista.size() - 2);
                else
                {
                    bucket++;
                    lista.pop_back();
                }
        }
        else
        {
            if(bucket == 0) return 1;
            lista.pop_back();
            --bucket;
        }
    }

    return (cnt <= n/2);
}

int hubDistance(int n, int subtask)
{
    gr.clear();
    mp.clear();

    prepare(n);

    int total = D1[y];
    int ans = 2e9;
    int i;

    for(i=0; i<n; ++i)
    {
        Up[i] = D0[i] + D1[i] - D0[x];
        assert(Up[i] % 2 == 0);
        Up[i] /= 2;

        L[i] = D1[i] - Up[i];
        gr[L[i]].push_back(i);
    }

    for(i=0; i<n; ++i)
        if(L[i] <= L[y]) ans = min(ans, max(L[i], total - L[i]));

    bool balanced = 0;
    int cnt1 = 0, cnt2 = n;

    for(auto &it : gr)
    {
        if(it.first > L[y]) break;

        cnt2 -= it.second.size();

        if(max(it.first, total - it.first) == ans && cnt1 <= n/2 && cnt2 <= n/2)
            balanced |= check(it.second, n);

        cnt1 += it.second.size();
    }

    return ans * (balanced ? 1 : -1);
}

Compilation message

towns.cpp: In function 'int getDist(int, int)':
towns.cpp:14:25: warning: declaration of 'y' shadows a global declaration [-Wshadow]
 int getDist(int x, int y)
                         ^
towns.cpp:8:8: note: shadowed declaration is here
 int x, y;
        ^
towns.cpp:14:25: warning: declaration of 'x' shadows a global declaration [-Wshadow]
 int getDist(int x, int y)
                         ^
towns.cpp:8:5: note: shadowed declaration is here
 int x, y;
     ^
towns.cpp: In function 'void prepare(int)':
towns.cpp:26:31: warning: conversion to 'int' from 'long int' may alter its value [-Wconversion]
     x = max_element(D0, D0+n) - D0;
         ~~~~~~~~~~~~~~~~~~~~~~^~~~
towns.cpp:30:31: warning: conversion to 'int' from 'long int' may alter its value [-Wconversion]
     y = max_element(D1, D1+n) - D1;
         ~~~~~~~~~~~~~~~~~~~~~~^~~~
towns.cpp: In function 'bool check(std::vector<int>&, int)':
towns.cpp:38:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(v.size() <= n/2) return 1;
        ~~~~~~~~~^~~~~~
towns.cpp:47:21: warning: declaration of 'x' shadows a global declaration [-Wshadow]
                 int x = lista.back();
                     ^
towns.cpp:8:5: note: shadowed declaration is here
 int x, y;
     ^
towns.cpp:68:13: warning: declaration of 'x' shadows a global declaration [-Wshadow]
         int x = lista.back();
             ^
towns.cpp:8:5: note: shadowed declaration is here
 int x, y;
     ^
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:122:32: warning: conversion to 'int' from 'std::vector<int>::size_type {aka long unsigned int}' may alter its value [-Wconversion]
         cnt2 -= it.second.size();
                                ^
towns.cpp:127:32: warning: conversion to 'int' from 'std::vector<int>::size_type {aka long unsigned int}' may alter its value [-Wconversion]
         cnt1 += it.second.size();
                                ^
towns.cpp:91:28: warning: unused parameter 'subtask' [-Wunused-parameter]
 int hubDistance(int n, int subtask)
                            ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 384 KB Output is correct
2 Correct 15 ms 384 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 20 ms 384 KB Output is correct
5 Correct 24 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 384 KB Output is correct
2 Correct 16 ms 384 KB Output is correct
3 Correct 22 ms 384 KB Output is correct
4 Correct 24 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 384 KB Output is correct
2 Correct 23 ms 444 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 24 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 512 KB Output is correct
2 Correct 23 ms 384 KB Output is correct
3 Correct 28 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 384 KB Output is correct
2 Correct 27 ms 512 KB Output is correct
3 Correct 24 ms 904 KB Output is correct