#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++)
{
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(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(suffix[i]-suffix[i+1]==1)
adj[i].second = pos;
else
{
adj[i].first=pos;
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;
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
384 KB |
# of queries: 2944 |
2 |
Incorrect |
49 ms |
384 KB |
Wrong Answer [5] |
3 |
Correct |
55 ms |
384 KB |
# of queries: 3086 |
4 |
Incorrect |
57 ms |
384 KB |
Wrong Answer [5] |
5 |
Incorrect |
58 ms |
384 KB |
Wrong Answer [5] |
6 |
Incorrect |
58 ms |
384 KB |
Wrong Answer [5] |
7 |
Incorrect |
39 ms |
384 KB |
Wrong Answer [5] |
8 |
Incorrect |
53 ms |
384 KB |
Wrong Answer [5] |
9 |
Correct |
52 ms |
384 KB |
# of queries: 3109 |
10 |
Incorrect |
34 ms |
384 KB |
Wrong Answer [5] |
11 |
Correct |
5 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 |
7 ms |
384 KB |
# of queries: 267 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
384 KB |
# of queries: 2944 |
2 |
Incorrect |
49 ms |
384 KB |
Wrong Answer [5] |
3 |
Correct |
55 ms |
384 KB |
# of queries: 3086 |
4 |
Incorrect |
57 ms |
384 KB |
Wrong Answer [5] |
5 |
Incorrect |
58 ms |
384 KB |
Wrong Answer [5] |
6 |
Incorrect |
58 ms |
384 KB |
Wrong Answer [5] |
7 |
Incorrect |
39 ms |
384 KB |
Wrong Answer [5] |
8 |
Incorrect |
53 ms |
384 KB |
Wrong Answer [5] |
9 |
Correct |
52 ms |
384 KB |
# of queries: 3109 |
10 |
Incorrect |
34 ms |
384 KB |
Wrong Answer [5] |
11 |
Correct |
5 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 |
7 ms |
384 KB |
# of queries: 267 |
17 |
Incorrect |
529 ms |
384 KB |
Wrong Answer [3] |
18 |
Incorrect |
535 ms |
384 KB |
Wrong Answer [5] |
19 |
Incorrect |
518 ms |
384 KB |
Wrong Answer [3] |
20 |
Correct |
489 ms |
504 KB |
# of queries: 18750 |
21 |
Correct |
444 ms |
384 KB |
# of queries: 17648 |
22 |
Incorrect |
540 ms |
384 KB |
Wrong Answer [3] |
23 |
Incorrect |
533 ms |
384 KB |
Wrong Answer [3] |
24 |
Correct |
184 ms |
384 KB |
# of queries: 9334 |
25 |
Correct |
523 ms |
480 KB |
# of queries: 19682 |
26 |
Correct |
471 ms |
384 KB |
# of queries: 18363 |
27 |
Incorrect |
193 ms |
384 KB |
Wrong Answer [5] |
28 |
Correct |
507 ms |
504 KB |
# of queries: 19452 |
29 |
Correct |
502 ms |
384 KB |
# of queries: 19430 |
30 |
Correct |
500 ms |
388 KB |
# of queries: 19452 |