답안 #960167

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960167 2024-04-09T19:13:24 Z MilosMilutinovic Cheerleaders (info1cup20_cheerleaders) C++14
62 / 100
1304 ms 3536 KB
#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],tmp[1<<20],ttmp[1<<20];
ll cst[20];
string str;
 
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++;
	}
}

void go(vector<int> v,int d){
	if(d<0) return;
	for(int i=v.size()-1;i>=0;i--){
		cst[d]-=ask(h[v[i]]);
		change(h[v[i]],1);
	}
	for(int i:v) change(h[i],-1);
	for(int i=v.size()-1;i>=0;i--){
		cst[d]+=ask(h[v[i]]^(1<<d));
		change(h[v[i]]^(1<<d),1);
	}
	for(int i:v) change(h[i]^(1<<d),-1);
	vector<int> vec[2];
	for(int i:v) vec[h[i]>>d&1].pb(i);
	go(vec[0],d-1);
	go(vec[1],d-1);
}
 
ll work(int x){
	for(int i=0;i<(1<<n);i++) tmp[i]=h[i];
	for(int iter=0;iter<x;iter++) op2();
	int msk=0;
	ll bef=solve();
	str="";
	for(int i=0;i<x;i++) str+='2';
	for(int i=n-1;i>=0;i--){
		for(int j=0;j<(1<<n);j++) ttmp[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];
		if(solve()<bef) msk^=(1<<i);
		for(int j=0;j<(1<<n);j++) h[j]=ttmp[j];
	}
	for(int j=0;j<(1<<n);j++) ttmp[j]=h[j];
	for(int j=0;j<(1<<n);j++) a[j^msk]=h[j];
	for(int j=0;j<(1<<n);j++) h[j]=a[j];
	ll ret=solve();
	for(int j=0;j<(1<<n);j++) h[j]=ttmp[j];
	for(int i=n-1;i>=0;i--){
		if(msk>>i&1) str+='1';
		str+='2';
	}
	for(int i=0;i<(1<<n);i++) h[i]=tmp[i];
	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;
	}
	assert(res==work(bst));
	printf("%lld\n",res);
	for(char c:str) printf("%c",c);
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct!
2 Correct 0 ms 348 KB Correct!
3 Correct 1 ms 500 KB Correct!
4 Correct 1 ms 348 KB Correct!
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Correct number of inversions, wrong sequence of operations
2 Correct 1 ms 348 KB Correct!
3 Correct 1 ms 348 KB Correct number of inversions, wrong sequence of operations
4 Correct 1 ms 348 KB Correct number of inversions, wrong sequence of operations
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 528 KB Correct number of inversions, wrong sequence of operations
2 Correct 6 ms 348 KB Correct number of inversions, wrong sequence of operations
3 Correct 7 ms 356 KB Correct number of inversions, wrong sequence of operations
4 Correct 7 ms 348 KB Correct number of inversions, wrong sequence of operations
# 결과 실행 시간 메모리 Grader output
1 Incorrect 137 ms 1168 KB Correct number of inversions, wrong sequence of operations
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1138 ms 3536 KB Correct number of inversions, wrong sequence of operations
2 Correct 1201 ms 3408 KB Correct number of inversions, wrong sequence of operations
3 Correct 1292 ms 3536 KB Correct number of inversions, wrong sequence of operations
4 Correct 1301 ms 3300 KB Correct number of inversions, wrong sequence of operations
5 Correct 1304 ms 3296 KB Correct number of inversions, wrong sequence of operations