Submission #1367798

#TimeUsernameProblemLanguageResultExecution timeMemory
1367798alexddChameleon's Love (JOI20_chameleon)C++20
100 / 100
36 ms1504 KiB
#include "chameleon.h"
#include <bits/stdc++.h>
using namespace std;

mt19937 rnd(2134721);

int side[1005];
vector<int> con[1005];
void dfs(int nod, int where)
{
    if(side[nod] != -1)
    {
        assert(side[nod] == where);
        return;
    }
    side[nod] = where;
    for(int adj:con[nod])
        dfs(adj, 1 - where);
}

int qry(const vector<int>&nodes, int le, int ri, int newv)
{
    vector<int> aux;
    for(int i=le;i<=ri;i++)
        aux.push_back(nodes[i]);
    aux.push_back(newv);
    return Query(aux);
}

bool good[1005][1005];
void Solve(int N)
{
    for(int i=1;i<=2*N;i++)
    {
        for(int j=1;j<i;j++)
            side[j] = -1;
        for(int j=1;j<i;j++)
            if(side[j] == -1)
                dfs(j, 0);

        for(int c=0;c<2;c++)
        {
            vector<int> nodes;
            for(int j=1;j<i;j++)
                if(side[j] == c)
                    nodes.push_back(j);

            //shuffle(nodes.begin(), nodes.end(), rnd);

            int ult = 0;
            while(con[i].size() < 3)
            {
                
                int st = ult, dr = (int)nodes.size() - 1, ans = -1;

                if(qry(nodes, st, dr, i) == (dr - st + 1) + 1)
                    break;
                
                while(st <= dr)
                {
                    int mij = (st + dr) / 2;
                    int idk = qry(nodes, ult, mij, i);
                    if(idk < (mij - ult + 1) + 1)
                    {
                        ans = mij;
                        dr = mij - 1;
                    }
                    else
                        st = mij + 1;
                }

                assert(ans != -1);
                
                if(ans == -1)
                    break;

                con[i].push_back(nodes[ans]);
                con[nodes[ans]].push_back(i);

                ult = ans + 1;
            }
        }
    }



    for(int i=1;i<=2*N;i++)
    {
        assert(con[i].size() == 1 || con[i].size() == 3);

        //for(int j:con[i]) cerr<<i<<" "<<j<<" con\n";

        if(con[i].size() == 1)
        {
            good[i][con[i][0]] = 1;
        }
        else
        {
            int cate = 0;
            for(int r=0;r<3;r++)
            {
                int a = con[i][r], b = con[i][(r+1)%3];
                if(Query({i, a, b}) == 1)
                {
                    good[i][a] = good[i][b] = 1;
                    break;
                }
            }
        }
    }

    for(int i=1;i<=2*N;i++)
        for(int j=1;j<i;j++)
            if(good[i][j] && good[j][i])
                Answer(i, j);
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...