이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n;
int h[1<<20],cnt[1<<20],a[1<<20],b[1<<20];
int ask(int p){
int ret=0;
while(p>=0){
ret+=cnt[p];
p=(p&(p+1))-1;
}
return ret;
}
void change(int p,int v){
while(p<(1<<n)){
cnt[p]+=v;
p|=(p+1);
}
}
ll solve(){
for(int i=0;i<(1<<n);i++) cnt[i]=0;
ll ret=0;
for(int i=(1<<n)-1;i>=0;i--){
ret+=ask(h[i]);
change(h[i],1);
}
return ret;
}
void op2(){
int x=0,y=0;
for(int i=0;i<(1<<n);i++){
if(i%2==0) a[x]=h[i],x++;
if(i%2==1) b[y]=h[i],y++;
}
int p=0,q=0;
for(int i=0;i<(1<<n);i++){
if(p<x) h[i]=a[p],p++;
else h[i]=b[q],q++;
}
}
ll work(int x){
for(int iter=0;iter<x;iter++) op2();
int msk=0;
ll ret=solve();
for(int i=0;i<n;i++){
for(int j=0;j<(1<<n);j++) b[j]=h[j];
for(int j=0;j<(1<<n);j++) a[j^(1<<i)]=h[j];
for(int j=0;j<(1<<n);j++) h[j]=a[j];
ll cur=solve();
if(cur<ret){
ret=cur;
msk^(1<<i);
}else{
for(int j=0;j<(1<<n);j++) h[j]=b[j];
}
}
for(int i=0;i<n;i++){
if(!(msk>>i&1)) continue;
for(int j=0;j<(1<<n);j++) a[j^(1<<i)]=h[j];
for(int j=0;j<(1<<n);j++) h[j]=a[j];
}
for(int iter=0;iter<n-x;iter++) op2();
return ret;
}
int main(){
n=readint();
for(int i=0;i<(1<<n);i++) h[i]=readint();
ll res=work(0);
int bst=0;
for(int i=1;i<n;i++){
ll c=work(i);
if(c<res) res=c,bst=i;
}
printf("%lld\n",res);
printf("2\n");
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
cheerleaders.cpp: In function 'll work(int)':
cheerleaders.cpp:79:7: warning: statement has no effect [-Wunused-value]
79 | msk^(1<<i);
| ~~~^~~~~~~
cheerleaders.cpp: In function 'int main()':
cheerleaders.cpp:97:6: warning: variable 'bst' set but not used [-Wunused-but-set-variable]
97 | int bst=0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |