답안 #931722

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
931722 2024-02-22T10:04:05 Z boris_mihov 카멜레온의 사랑 (JOI20_chameleon) C++17
0 / 100
24 ms 600 KB
#include "chameleon.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

const int MAXN = 500 + 10;

namespace
{
    int n;
    int perm[MAXN];
    bool found[2 * MAXN];
    std::vector <int> candidates[MAXN];
    std::vector <int> x, y;
}

int runBinaryX(int fromPos, int idx)
{
    int l = fromPos - 1, r = n, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        std::vector <int> curr;
        curr.push_back(x[idx]);

        // std::cout << "Add: " << x[idx] << '\n';
        for (int j = fromPos ; j <= mid ; ++j)
        {
            // std::cout << "add: " << y[j] << '\n';
            curr.push_back(y[j]);
        }

        if (Query(curr) == curr.size()) l = mid;
        else r = mid;
    }

    return r;
}

int runBinaryY(int fromPos, int idx)
{
    int l = fromPos - 1, r = n, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        std::vector <int> curr;
        curr.push_back(y[idx]);

        // std::cout << "Add: " << x[idx] << '\n';
        for (int j = fromPos ; j <= mid ; ++j)
        {
            // std::cout << "add: " << y[j] << '\n';
            curr.push_back(x[j]);
        }

        if (Query(curr) == curr.size()) l = mid;
        else r = mid;
    }

    return r;
}

void Solve(int n) 
{
    ::n = n;
    x.resize(n);
    y.resize(n);
    std::iota(x.begin(), x.end(), 1);
    std::iota(y.begin(), y.end(), n + 1);

    // std::cout << "x, y: \n";
    // for (const int &i : x) std::cout << i << ' '; std::cout << '\n';
    // for (const int &i : y) std::cout << i << ' '; std::cout << '\n';
    // for (int i = 1 ; i <= 2 * n ; ++i)
    // {
    //     for (int j = 1 ; j <= 2 * n ; ++j)
    //     {
    //         if (i == j)
    //         {
    //             continue;
    //         }

    //         if (Query({i, j}) == 1)
    //         {
    //             // std::cout << "here: " << i << ' ' << j << '\n';
    //             candidates[i].push_back(j);
    //         }
    //     }

    //     if (candidates[i].size() == 3)
    //     {
    //         if (Query({candidates[i][0], candidates[i][2], i}) == 1)
    //         {
    //             std::swap(candidates[i].back(), candidates[i][1]);
    //         } else if (Query({candidates[i][1], candidates[i][2], i}) == 1)
    //         {
    //             std::swap(candidates[i].back(), candidates[i][0]);
    //         }

    //         candidates[i].pop_back();
    //     }

    //     // std::cout << "size: " << i << ' ' << candidates[i].size() << '\n';
    //     // for (const int &j : candidates[i]) std::cout << j << ' '; std::cout << '\n';
    // }

    for (int i = 0 ; i < n ; ++i)
    {
        int lastAdded = -1;
        std::vector <int> added;
        for (int j = 0 ; j < 3 && lastAdded + 1 < n ; ++j)
        {
            int curr = runBinaryX(lastAdded + 1, i);
            if (curr != n)
            {
                candidates[x[i]].push_back(y[curr]);
            }

            lastAdded = curr;
        }

        // std::cout << "candidates: " << x[i] << '\n';
        // for (const int &idx : candidates[x[i]])
        // {
        //     std::cout << idx << ' ';
        // }

        // std::cout << '\n';
    }

    for (int i = 0 ; i < n ; ++i)
    {
        int lastAdded = -1;
        std::vector <int> added;
        for (int j = 0 ; j < 3 && lastAdded + 1 < n ; ++j)
        {
            int curr = runBinaryY(lastAdded + 1, i);
            if (curr != n)
            {
                candidates[y[i]].push_back(x[curr]);
            }

            lastAdded = curr;
        }

        // std::cout << "candidates: " << y[i] << '\n';
        // for (const int &idx : candidates[y[i]])
        // {
        //     std::cout << idx << ' ';
        // }

        // std::cout << '\n';
    }

    for (int i = 1 ; i <= 2 * n ; ++i)
    {
        if (candidates[i].size() == 3)
        {
            if (Query({candidates[i][0], candidates[i][2], i}) == 1)
            {
                std::swap(candidates[i].back(), candidates[i][1]);
            } else if (Query({candidates[i][1], candidates[i][2], i}) == 1)
            {
                std::swap(candidates[i].back(), candidates[i][0]);
            }

            candidates[i].pop_back();
        }
    }

    std::iota(perm + 1, perm + 1 + 2 * n, 1);
    std::sort(perm + 1, perm + 1 + 2 * n, [&](int x, int y)
    {
        return candidates[x].size() < candidates[y].size();
    });

    for (int idx = 1 ; idx <= 2 * n ; ++idx)
    {
        int i = perm[idx];
        if (found[i])
        {
            continue;
        }

        if (candidates[i].size() == 1)
        {
            Answer(i, candidates[i][0]);
            found[candidates[i][0]] = true;
            // std::cout << "answer: " << i << ' ' << candidates[i][0] << '\n';   
            continue;
        }
        
        assert(candidates[i].size() == 2);
        bool shouldBreak = false;

        for (const int &j : candidates[i])
        {
            for (const int &x : candidates[j])
            {
                if (x == i)
                {
                    // std::cout << "answer: " << i << ' ' << j << '\n';
                    Answer(i, j);
                    found[j] = true;
                    shouldBreak = true;
                    break;
                }
            }
            
            if (shouldBreak)
            {
                break;
            }
        }
    }
}

Compilation message

chameleon.cpp: In function 'int runBinaryX(int, int)':
chameleon.cpp:35:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |         if (Query(curr) == curr.size()) l = mid;
      |             ~~~~~~~~~~~~^~~~~~~~~~~~~~
chameleon.cpp: In function 'int runBinaryY(int, int)':
chameleon.cpp:58:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         if (Query(curr) == curr.size()) l = mid;
      |             ~~~~~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Runtime error 20 ms 600 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 344 KB Wrong Answer [6]
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 344 KB Wrong Answer [6]
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Runtime error 24 ms 592 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Runtime error 20 ms 600 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -