#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=998244353;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=200007;
int a[4*N];
int is[4*N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=n+1;i<=4*n;i++) a[i]=inf;
for(int i=1;i<=n;i++)
{
int mn=inf,k,x=i;
if(is[i]!=1&&is[2*i]!=1&&is[2*i+1]!=1)
{
mn=a[i];
k=i;
}
while(x>1)
{
//cout<<i<<" "<<x<<" "<<is[x]<<endl;
if(is[x]==2) break;
bool L=x%2;
x/=2;
if(L)
{
if(is[2*x]!=1&&is[x]!=1&&mn>a[x])
{
mn=a[x];
k=x;
}
if(is[2*x]!=2&&mn>a[2*x])
{
mn=a[2*x];
k=2*x;
}
if(is[2*x]==1) break;
}
else
{
if(is[x]!=1&&mn>a[x])
{
mn=a[x];
k=x;
}
}
}
int c=min({mn,a[2*i],a[2*i+1]});
cout<<c<<" ";
if(c==mn)
{
x=i;
if(k==i)
{
is[i]=2;
x=0;
}
while(x>1)
{
is[x]=1;
bool L=x%2;
x/=2;
if(L)
{
if(k==x)
{
is[2*x]=2;
is[x]=2;
break;
}
if(k==2*x)
{
is[2*x]=1;
break;
}
is[2*x]=2;
}
else
{
if(k==x)
{
is[x]=2;
break;
}
}
}
is[2*i]=2;
is[2*i+1]=2;
}
else if(c==a[2*i])
{
is[2*i]=1;
is[2*i+1]=2;
}
else is[2*i+1]=1;
}
return 0;
}
Compilation message
swap.cpp: In function 'int main()':
swap.cpp:90:21: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
90 | if(k==2*x)
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
15 ms |
2016 KB |
Output is correct |
17 |
Correct |
16 ms |
2004 KB |
Output is correct |
18 |
Correct |
11 ms |
2012 KB |
Output is correct |
19 |
Correct |
13 ms |
2004 KB |
Output is correct |
20 |
Correct |
17 ms |
1984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
15 ms |
2016 KB |
Output is correct |
17 |
Correct |
16 ms |
2004 KB |
Output is correct |
18 |
Correct |
11 ms |
2012 KB |
Output is correct |
19 |
Correct |
13 ms |
2004 KB |
Output is correct |
20 |
Correct |
17 ms |
1984 KB |
Output is correct |
21 |
Correct |
46 ms |
7504 KB |
Output is correct |
22 |
Correct |
55 ms |
7528 KB |
Output is correct |
23 |
Correct |
63 ms |
7548 KB |
Output is correct |
24 |
Correct |
70 ms |
7524 KB |
Output is correct |
25 |
Correct |
64 ms |
7472 KB |
Output is correct |