Submission #931770

# Submission time Handle Problem Language Result Execution time Memory
931770 2024-02-22T10:43:51 Z boris_mihov Chameleon's Love (JOI20_chameleon) C++17
40 / 100
2 ms 600 KB
#include "chameleon.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <set>

const int MAXN = 1000 + 10;

namespace
{
    int n;
    int perm[MAXN];
    int before[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';
            if (x[j] != before[y[idx]]) curr.push_back(x[j]);
        }

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

    return r;
}

void Solve(int n) 
{
    ::n = n;
    if (n <= 50)
    {
        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';
        }
    } else
    {
        // x.push_back(1);
        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 = 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;
            }

            for (const int &y : candidates[x[i]])
            {
                candidates[y].push_back(x[i]);
            }

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

                // std::cout << "before: " << candidates[x[i]].back() << " = " << x[i] << '\n';
                before[candidates[x[i]].back()] = x[i];
                candidates[x[i]].pop_back();
            }

            // 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)
        {
            // if (found[y[i]] == true)
            // {
            //     continue;
            // }

            // if (before[y[i]] != 0) 
            // {
            //     candidates[y[i]].push_back(before[y[i]]);
            // }

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

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

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

            // 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:37:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |         if (Query(curr) == curr.size()) l = mid;
      |             ~~~~~~~~~~~~^~~~~~~~~~~~~~
chameleon.cpp: In function 'int runBinaryY(int, int)':
chameleon.cpp:60:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         if (Query(curr) == curr.size()) l = mid;
      |             ~~~~~~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Runtime error 1 ms 600 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 596 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 596 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 2 ms 344 KB Output is correct
11 Correct 2 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 2 ms 344 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Runtime error 1 ms 600 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Runtime error 1 ms 600 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -