Submission #1350204

#TimeUsernameProblemLanguageResultExecution timeMemory
1350204alexddTable Tennis (JOI24_tabletennis)C++20
57 / 100
1098 ms209132 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int INF = 1e12;

int comb2(int x)
{
    return x * (x-1) / 2;
}

int n,m;


bool verif(vector<int> v)
{
    int pref = 0;
    for(int i=1;i<=n;i++)
    {
        if(i > 1 && v[i-1] > v[i])
            return 0;
        pref += v[i];
        if(pref < comb2(i))
            return 0;
    }
    if(pref != comb2(n))
        return 0;
    return 1;
}

int calc(vector<int> v)
{
    int tot = 0;
    for(int i=1;i<=n;i++)
        tot += comb2(v[i]);
    return tot;
}

bool done;
vector<int> sol;


void recursiv(vector<int> v)
{

    /*if(visited[v])
        return;
    visited[v] = 1;*/

    vector<int> unde, newv;
    while(1)
    {
        int mycalc = calc(v);

        if(mycalc == m)
        {
            done = 1;
            sol = v;
            return;
        }

        int curdist = abs(mycalc - m);
        int closest = curdist;

        bool easy = (mycalc > (m + n));

        unde.clear();
        newv.clear();

        for(int lun=n-1;lun>=1;lun--)
        {
            bool found = 0;
            for(int to=1;to+lun<=n;to++)
            {
                int from = to + lun;
                newv = v;
                newv[from]--;
                newv[to]++;
                if(newv[to] < n && newv[from] >= 0 && newv[from] >= newv[from-1] && newv[to] <= newv[to+1] /*&& verif(newv)*/)
                {
                    //assert(calc(newv) == mycalc - v[from] + 1 + v[to]);

                    int newdist = calc(newv);

                    int mydist = abs(newdist - m);

                    if(mydist < closest)
                    {
                        closest = mydist;
                        unde = newv;
                        found = 1;
                    }
                }
            }
            if(easy && found)
                break;
        }

        if(unde.empty())
            return;

        v = unde;

    }
}

int mat[5005][5005];
void reconstruct(vector<int> v)
{
    assert(verif(v));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            mat[i][j] = 0;

    vector<pair<int,int>> aux;
    for(int i=1;i<=n;i++)
        aux.push_back({v[i], i});
    while(!aux.empty())
    {
        sort(aux.begin(), aux.end());
        if(aux.back().first == 0)
            break;
        assert(aux.back().first > 0);

        for(int i=0;i<aux.back().first;i++)
        {
            mat[aux.back().second][aux[i].second] = 1;
        }
        for(int i=aux.back().first;i<(int)aux.size()-1;i++)
        {
            aux[i].first--;
            mat[aux[i].second][aux.back().second] = 1;
        }
        aux.back().first = 0;
        aux.pop_back();
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            assert(mat[i][j] + mat[j][i] == 1);
            if(mat[i][j] + mat[j][i] != 1)
            {
                //cerr<<i<<" "<<j<<": "<<mat[i][j]<<" & "<<mat[j][i]<<" zzz\n";
            }
        }
    }

    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<i;j++)
            cout<<mat[i][j];
        cout<<"\n";
    }
}

void solve()
{
    cin>>n>>m;
    m = n * (n-1) * (n-2) / 6 - m;

    //get close to m then do backtrack
    done = 0;

    vector<int> init(n+2,0);
    for(int i=1;i<=n;i++)
        init[i] = i - 1;

    assert(verif(init));

    if(m > calc(init))
    {
        cout<<"No\n";
        return;
    }

    recursiv(init);



    if(!done)
    {
        cout<<"No\n";
        return;
    }

    assert(calc(sol) == m);

    //for(int i=1;i<=n;i++) cerr<<sol[i]<<" ";cerr<<"sol\n";

    cout<<"Yes\n";
    reconstruct(sol);
}

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int t;
    cin>>t;
    while(t--)
        solve();
    return 0;
}

/*

for each "bad" tuple (x,y,z) of nodes,
exactly one of x,y,z will have outgoing edges towards the other 2
=> M = total - sum(comb(cnt_outgoing(x), 2))

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...