Submission #1092341

# Submission time Handle Problem Language Result Execution time Memory
1092341 2024-09-23T20:21:24 Z raphaelp Building Skyscrapers (CEOI19_skyscrapers) C++14
0 / 100
344 ms 18460 KB
#include <bits/stdc++.h>
using namespace std;
void dfs(int x, vector<vector<int>> &AR, vector<int> &occ, int &nb)
{
    if (occ[x])
        return;
    nb--;
    occ[x] = 1;
    for (int i = 0; i < AR[x].size(); i++)
    {
        dfs(AR[x][i], AR, occ, nb);
    }
}
int main()
{
    int N, T;
    cin >> N >> T;
    map<pair<int, int>, int> M;
    vector<pair<int, int>> Tab;
    vector<vector<int>> AR, AR2;
    vector<pair<int, int>> trans1 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}, trans2 = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    int buff = 0;
    for (int i = 0; i < N; i++)
    {
        int x, y;
        cin >> x >> y;
        Tab.push_back({x, y});
        M[{x, y}] = buff++;
        AR.push_back({});
        AR2.push_back({});
        for (int j = 0; j < 4; j++)
            if (M.count({x + trans1[j].first, y + trans1[j].second}))
            {
                AR[i].push_back(M[{x + trans1[j].first, y + trans1[j].second}]);
                AR[M[{x + trans1[j].first, y + trans1[j].second}]].push_back(i);
            }
        for (int j = 0; j < 8; j++)
            if (M.count({x + trans2[j].first, y + trans2[j].second}))
            {
                AR2[i].push_back(M[{x + trans2[j].first, y + trans2[j].second}]);
                AR2[M[{x + trans2[j].first, y + trans2[j].second}]].push_back(i);
            }
    }
    vector<int> ans, occ(N);
    int nb = N;
    dfs(0, AR2, occ, nb);
    if (nb)
    {
        cout << "NO" << '\n';
        return 0;
    }
    pair<int, int> minn = {1000000010, 0};
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            int x = Tab[i].first + trans2[j].first, y = Tab[i].second + trans2[j].second;
            if (M.count({x, y}) == 0)
            {
                M[{x, y}] = buff++;
                minn = min(minn, {x, y});
                Tab.push_back({x, y});
                AR.push_back({});
                AR2.push_back({});
                for (int k = 0; k < 4; k++)
                    if (M.count({x + trans1[k].first, y + trans1[k].second}))
                    {
                        AR[buff - 1].push_back(M[{x + trans1[k].first, y + trans1[k].second}]);
                        AR[M[{x + trans1[k].first, y + trans1[k].second}]].push_back(buff - 1);
                    }
            }
        }
    }
    occ.assign(buff, 0);
    vector<int> connex(N, 0);
    occ[M[minn]] = 1;
    queue<int> Q;
    Q.push(M[minn]);
    priority_queue<int> next;
    int best = 0;
    while (!Q.empty())
    {
        int x = Q.front();
        Q.pop();
        for (int i = 0; i < AR[x].size(); i++)
        {
            if (occ[AR[x][i]])
                continue;
            occ[AR[x][i]] = 1;
            if (AR[x][i] >= N)
                Q.push(AR[x][i]);
            else if (connex[AR[x][i]])
                next.push(AR[x][i]);
            if (AR[x][i] < N)
                best = max(best, AR[x][i]);
        }
    }
    connex[best] = 1;
    next.push(best);
    while (!next.empty())
    {
        int x = next.top();
        next.pop();
        ans.push_back(x);
        Q.push(x);
        for (int i = 0; i < AR2[x].size(); i++)
        {
            if (connex[AR2[x][i]])
                continue;
            connex[AR2[x][i]] = 1;
            if (occ[AR2[x][i]])
                next.push(AR2[x][i]);
        }
        while (!Q.empty())
        {
            int x = Q.front();
            Q.pop();
            for (int i = 0; i < AR[x].size(); i++)
            {
                if (occ[AR[x][i]])
                    continue;
                occ[AR[x][i]] = 1;
                if (AR[x][i] >= N)
                    Q.push(AR[x][i]);
                else if (connex[AR[x][i]])
                    next.push(AR[x][i]);
            }
        }
    }
    cout << "YES" << '\n';
    for (int i = N - 1; i >= 0; i--)
    {
        cout << ans[i] + 1 << '\n';
    }
}

Compilation message

skyscrapers.cpp: In function 'void dfs(int, std::vector<std::vector<int> >&, std::vector<int>&, int&)':
skyscrapers.cpp:9:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    9 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
skyscrapers.cpp: In function 'int main()':
skyscrapers.cpp:85:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |         for (int i = 0; i < AR[x].size(); i++)
      |                         ~~^~~~~~~~~~~~~~
skyscrapers.cpp:106:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |         for (int i = 0; i < AR2[x].size(); i++)
      |                         ~~^~~~~~~~~~~~~~~
skyscrapers.cpp:118:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |             for (int i = 0; i < AR[x].size(); i++)
      |                             ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB ans=YES N=1
2 Correct 1 ms 348 KB ans=YES N=4
3 Correct 0 ms 344 KB ans=NO N=4
4 Incorrect 0 ms 348 KB Full cells must be connected
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB ans=YES N=1
2 Correct 1 ms 348 KB ans=YES N=4
3 Correct 0 ms 344 KB ans=NO N=4
4 Incorrect 0 ms 348 KB Full cells must be connected
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB ans=YES N=1
2 Correct 1 ms 348 KB ans=YES N=4
3 Correct 0 ms 344 KB ans=NO N=4
4 Incorrect 0 ms 348 KB Full cells must be connected
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 600 KB ans=NO N=1934
2 Correct 3 ms 600 KB ans=NO N=1965
3 Incorrect 7 ms 860 KB Full cells must be connected
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB ans=YES N=1
2 Correct 1 ms 348 KB ans=YES N=4
3 Correct 0 ms 344 KB ans=NO N=4
4 Incorrect 0 ms 348 KB Full cells must be connected
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 202 ms 14784 KB ans=NO N=66151
2 Correct 111 ms 9876 KB ans=NO N=64333
3 Incorrect 344 ms 18460 KB Full cells must be connected
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 600 KB ans=NO N=1934
2 Correct 3 ms 600 KB ans=NO N=1965
3 Incorrect 7 ms 860 KB Full cells must be connected
4 Halted 0 ms 0 KB -