Submission #232955

# Submission time Handle Problem Language Result Execution time Memory
232955 2020-05-18T18:23:58 Z Charis02 Library (JOI18_library) C++14
100 / 100
292 ms 508 KB
#include <cstdio>
#include <vector>
#include<iostream>
#define MAXN 1004
#include "library.h"
using namespace std;

pair < int,int > adj[MAXN];
int prefix[MAXN];
int suffix[MAXN];
int NN;

int adjacencies(vector < int > M)
{
    int ones = 0;
    for(int i = 0;i < NN;i++)
        ones+=M[i];
    return ones-Query(M);
}

int join_pref(int pref,int el)
{
    vector < int > M(NN);

    for(int i = 0;i < NN;i++)
        M[i]=0;
    M[el]=1;
    for(int i = 0;i <= pref;i++)
        M[i] = 1;

    return adjacencies(M);
}

int join_suf(int suf,int el)
{
    vector < int > M(NN);

    for(int i = 0;i < NN;i++)
        M[i]=0;
    M[el]=1;
    for(int i = NN-1;i>=suf;i--)
        M[i] = 1;

    return adjacencies(M);
}

void Solve(int N)
{
    NN=N;
	vector<int> M(N);

	for(int i = 0;i< N;i++)
        M[i] = 0;

	for(int i = 0; i < N; i++)
	{
	    adj[i] = make_pair(-1,-1);
	    M[i] = 1;

        prefix[i] = adjacencies(M);
	}

	for(int i = 0;i < N;i++)
    {
        if(i >= 0 && prefix[i-1] < prefix[i])
        {
            int low = 0;
            int high = i-1;
            int pos = i-1;

            while(low<=high)
            {
                int mid = (low+high)/2;

                if(join_pref(mid,i) > prefix[mid])
                {
                    high=mid-1;
                    pos=min(pos,mid);
                }
                else
                    low=mid+1;
            }

            adj[i].first = pos;

            if(adj[pos].first==-1)
                adj[pos].first=i;
            else
                adj[pos].second=i;
        }
        if(prefix[i]-prefix[i-1] == 2)
        {
            int low = adj[i].first+1;
            int high=i-1;
            int pos = i-1;

            while(low<=high)
            {
                int mid = (low+high)/2;

                if(join_pref(mid,i) > prefix[mid]+1)
                {
                    high=mid-1;
                    pos=min(pos,mid);
                }
                else
                    low=mid+1;
            }

            adj[i].second = pos;
            if(adj[pos].first==-1)
                adj[pos].first=i;
            else
                adj[pos].second=i;
        }
    }

	vector<int> res(N);

	int cur = 0;

	int par = 0;

	for(int i = 0;i < N;i++)
    {
        if(adj[i].first==-1||adj[i].second == -1)
        {
            cur=i;
            break;
        }
    }

    par=-1;

   // cout << endl << endl;

   // cout << "here " << cur << " " << adj[cur].first << "  " << adj[cur].second << " " << suffix[cur] << " " << suffix[cur+1] << " " << join_suf(cur,cur+1) <<endl;

   // cout << "here " << 68 << " " << adj[68].first << " " << adj[68].second << endl;
    for(int i = 0;i < N;i++)
    {
        res[i] = cur+1;
     //   cout << res[i] << endl;

        if(adj[cur].first==par)
        {
            par=cur;
            cur = adj[cur].second;
        }
        else
        {
            par=cur;
            cur=adj[cur].first;
        }
    }

	Answer(res);
}

/*
5
4 2 5 3 1
*/
# Verdict Execution time Memory Grader output
1 Correct 31 ms 384 KB # of queries: 1466
2 Correct 27 ms 384 KB # of queries: 1468
3 Correct 31 ms 384 KB # of queries: 1535
4 Correct 30 ms 384 KB # of queries: 1546
5 Correct 31 ms 384 KB # of queries: 1546
6 Correct 31 ms 392 KB # of queries: 1530
7 Correct 29 ms 384 KB # of queries: 1544
8 Correct 26 ms 384 KB # of queries: 1461
9 Correct 29 ms 384 KB # of queries: 1550
10 Correct 18 ms 384 KB # of queries: 909
11 Correct 5 ms 384 KB # of queries: 1
12 Correct 5 ms 384 KB # of queries: 3
13 Correct 5 ms 384 KB # of queries: 5
14 Correct 5 ms 384 KB # of queries: 8
15 Correct 5 ms 388 KB # of queries: 57
16 Correct 6 ms 384 KB # of queries: 131
# Verdict Execution time Memory Grader output
1 Correct 31 ms 384 KB # of queries: 1466
2 Correct 27 ms 384 KB # of queries: 1468
3 Correct 31 ms 384 KB # of queries: 1535
4 Correct 30 ms 384 KB # of queries: 1546
5 Correct 31 ms 384 KB # of queries: 1546
6 Correct 31 ms 392 KB # of queries: 1530
7 Correct 29 ms 384 KB # of queries: 1544
8 Correct 26 ms 384 KB # of queries: 1461
9 Correct 29 ms 384 KB # of queries: 1550
10 Correct 18 ms 384 KB # of queries: 909
11 Correct 5 ms 384 KB # of queries: 1
12 Correct 5 ms 384 KB # of queries: 3
13 Correct 5 ms 384 KB # of queries: 5
14 Correct 5 ms 384 KB # of queries: 8
15 Correct 5 ms 388 KB # of queries: 57
16 Correct 6 ms 384 KB # of queries: 131
17 Correct 269 ms 384 KB # of queries: 10045
18 Correct 288 ms 384 KB # of queries: 9941
19 Correct 273 ms 484 KB # of queries: 10044
20 Correct 229 ms 388 KB # of queries: 9361
21 Correct 240 ms 508 KB # of queries: 8820
22 Correct 289 ms 384 KB # of queries: 10071
23 Correct 281 ms 504 KB # of queries: 10040
24 Correct 95 ms 384 KB # of queries: 4669
25 Correct 273 ms 384 KB # of queries: 9836
26 Correct 242 ms 384 KB # of queries: 9176
27 Correct 90 ms 384 KB # of queries: 4653
28 Correct 261 ms 396 KB # of queries: 9977
29 Correct 292 ms 384 KB # of queries: 9966
30 Correct 285 ms 504 KB # of queries: 9977