Submission #232931

# Submission time Handle Problem Language Result Execution time Memory
232931 2020-05-18T17:01:17 Z Charis02 Library (JOI18_library) C++14
19 / 100
591 ms 420 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++)
        M[i] = 0;

    for(int i = N-1;i>=0;i--)
    {
        M[i]=1;
        suffix[i] = adjacencies(M);
    }

	for(int i = 0;i < N;i++)
    {
        bool prr = false;
        if(i >= 0 && prefix[i-1] < prefix[i])
        {
            prr=true;
            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(prefix[i]-prefix[i-1] == 2)
            {
                low = pos+1;
                high=i-1;
                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(i < N-1 && suffix[i+1] < suffix[i])
        {
            int low = i+1;
            int high = N-1;
            int pos = i+1;

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

                if(join_suf(mid,i) > suffix[mid])
                {
                    low=mid+1;
                    pos=max(pos,mid);
                }
                else
                    high=mid-1;
            }

            if(prr)
                adj[i].second = pos;
            else
            {
                adj[i].first=pos;

                if(suffix[i]-suffix[i+1]==1)
                    continue;
                low = i+1;
                high= pos-1;
                pos = i+1;

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

                    if(join_suf(mid,i) > suffix[mid]+1)
                    {
                        low=mid+1;
                        pos=max(pos,mid);
                    }
                    else
                        high=mid-1;
                }

                adj[i].second = pos;
            }
        }
    }

	vector<int> res(N);

	int cur = 0;
	int par = 0;

	for(int i = 0;i < N;i++)
    {
        if(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 54 ms 384 KB # of queries: 2944
2 Correct 50 ms 384 KB # of queries: 2931
3 Correct 47 ms 384 KB # of queries: 3086
4 Correct 60 ms 384 KB # of queries: 3092
5 Correct 60 ms 384 KB # of queries: 3093
6 Correct 46 ms 380 KB # of queries: 3052
7 Correct 48 ms 384 KB # of queries: 3098
8 Correct 45 ms 384 KB # of queries: 2935
9 Correct 60 ms 384 KB # of queries: 3109
10 Correct 33 ms 384 KB # of queries: 1820
11 Correct 4 ms 384 KB # of queries: 2
12 Correct 5 ms 384 KB # of queries: 6
13 Correct 5 ms 384 KB # of queries: 11
14 Correct 5 ms 384 KB # of queries: 17
15 Correct 6 ms 384 KB # of queries: 118
16 Correct 12 ms 384 KB # of queries: 267
# Verdict Execution time Memory Grader output
1 Correct 54 ms 384 KB # of queries: 2944
2 Correct 50 ms 384 KB # of queries: 2931
3 Correct 47 ms 384 KB # of queries: 3086
4 Correct 60 ms 384 KB # of queries: 3092
5 Correct 60 ms 384 KB # of queries: 3093
6 Correct 46 ms 380 KB # of queries: 3052
7 Correct 48 ms 384 KB # of queries: 3098
8 Correct 45 ms 384 KB # of queries: 2935
9 Correct 60 ms 384 KB # of queries: 3109
10 Correct 33 ms 384 KB # of queries: 1820
11 Correct 4 ms 384 KB # of queries: 2
12 Correct 5 ms 384 KB # of queries: 6
13 Correct 5 ms 384 KB # of queries: 11
14 Correct 5 ms 384 KB # of queries: 17
15 Correct 6 ms 384 KB # of queries: 118
16 Correct 12 ms 384 KB # of queries: 267
17 Incorrect 480 ms 384 KB Wrong Answer [3]
18 Correct 558 ms 384 KB # of queries: 19881
19 Incorrect 589 ms 384 KB Wrong Answer [3]
20 Correct 558 ms 388 KB # of queries: 18750
21 Correct 461 ms 384 KB # of queries: 17648
22 Incorrect 539 ms 384 KB Wrong Answer [3]
23 Incorrect 533 ms 384 KB Wrong Answer [3]
24 Correct 199 ms 384 KB # of queries: 9334
25 Correct 591 ms 384 KB # of queries: 19682
26 Correct 469 ms 384 KB # of queries: 18363
27 Correct 181 ms 420 KB # of queries: 9295
28 Correct 519 ms 384 KB # of queries: 19452
29 Correct 536 ms 384 KB # of queries: 19430
30 Correct 559 ms 384 KB # of queries: 19452