#include <iostream>
using namespace std;
int n,ant,act;
bool aint[800010];
bool valoare(int poz,int b,int e,int pozc)
{
if(b==e)
return aint[poz];
if((b+e)/2>=pozc)
return valoare(poz*2,b,(b+e)/2,pozc);
return valoare(poz*2+1,(b+e)/2+1,e,pozc);
}
void blocheaza(int poz,int b,int e,int pozu)
{
if(pozu<b||pozu>e)
return;
if(b==e)
{
aint[poz]=true;
return;
}
blocheaza(poz*2,b,(b+e)/2,pozu);
blocheaza(poz*2+1,(b+e)/2+1,e,pozu);
aint[poz]=(aint[poz*2]&aint[poz*2+1]);
}
int maxim(int poz,int b,int e)
{
if(b==e)
return b;
if(aint[poz*2+1]==false)
return maxim(poz*2+1,(b+e)/2+1,e);
return maxim(poz*2,b,(b+e)/2);
}
int minim(int poz,int b,int e)
{
if(b==e)
return b;
if(aint[poz*2]==false)
return minim(poz*2,b,(b+e)/2);
return minim(poz*2+1,(b+e)/2+1,e);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>ant;
cout<<ant<<' ';
blocheaza(1,1,2*n-1,ant);
for(int i=2;i<=n;++i)
{
cin>>act;
if(act>ant)
{
if(valoare(1,1,2*n-1,act)==false)
{
cout<<act<<' ';
blocheaza(1,1,2*n-1,act);
}
else
{
ant=maxim(1,1,2*n-1);
cout<<ant<<' ';
blocheaza(1,1,2*n-1,ant);
}
ant=maxim(1,1,2*n-1);
cout<<ant<<' ';
blocheaza(1,1,2*n-1,ant);
}
else if(act<ant)
{
if(valoare(1,1,2*n-1,act)==false)
{
cout<<act<<' ';
blocheaza(1,1,2*n-1,act);
}
else
{
ant=minim(1,1,2*n-1);
cout<<ant<<' ';
blocheaza(1,1,2*n-1,ant);
}
ant=minim(1,1,2*n-1);
cout<<ant<<' ';
blocheaza(1,1,2*n-1,ant);
}
else
{
ant=minim(1,1,2*n-1);
cout<<ant<<' ';
blocheaza(1,1,2*n-1,ant);
ant=maxim(1,1,2*n-1);
cout<<ant<<' ';
blocheaza(1,1,2*n-1,ant);
}
ant=act;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
18 ms |
860 KB |
Output is correct |
6 |
Correct |
26 ms |
1368 KB |
Output is correct |
7 |
Correct |
62 ms |
2076 KB |
Output is correct |