//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
llo n;
llo it[200001];
llo ind[200001];
vector<llo> ans;
vector<pair<llo,llo>> kk;
void brute(llo ind){
if(ind*2>n){
vector<llo> ss;
for(llo i=1;i<=n;i++){
ss.pb(it[i]);
}
ans=min(ans,ss);
}
else if(ind*2==n){
llo x=it[ind];
llo y=it[ind*2];
llo xx=min(x,y);
if(xx==x){
brute(ind+1);
}
else{
swap(it[ind],it[ind*2]);
brute(ind+1);
}
it[ind]=x;
it[ind*2]=y;
}
else{
llo x=it[ind];
llo y=it[ind*2];
llo z=it[ind*2+1];
llo xx=min(x,y);
xx=min(xx,z);
if(xx==x){
brute(ind+1);
}
else if(xx==y){
swap(it[ind],it[ind*2]);
brute(ind+1);
}
else{
//if(x>y){
kk.pb({y,z});
swap(it[ind],it[ind*2+1]);
brute(ind+1);
it[ind]=x;
it[ind*2]=y;
it[ind*2+1]=z;
//}
/*else{
swap(it[ind],it[ind*2]);
swap(it[ind],it[ind*2+1]);
brute(ind+1);
}*/
}
it[ind]=x;
it[ind*2]=y;
it[ind*2+1]=z;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(llo i=0;i<n;i++){
cin>>it[i+1];
ans.pb(it[i+1]);
}
brute(1);
vector<llo> ans2=ans;
for(llo j=0;j<(1<<kk.size());j++){
vector<llo> cur=ans;
for(llo i=0;i<n;i++){
ind[ans[i]]=i;
}
for(llo i=0;i<kk.size();i++){
if((1LL<<i)&j){
swap(cur[ind[kk[i].a]],cur[ind[kk[i].b]]);
swap(ind[kk[i].a],ind[kk[i].b]);
}
else{
}
}
ans2=min(ans2,cur);
}
/*for(auto j:kk){
cout<<j.a<<":"<<j.b<<endl;
}*/
for(auto j:ans2){
cout<<j<<" ";
}
cout<<endl;
//cout<<ans<<endl;
return 0;
}
Compilation message
swap.cpp: In function 'int main()':
swap.cpp:87:16: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(llo i=0;i<kk.size();i++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |