跳到正文

个人记录

CF1626D Martial Arts Tournament

c6666约 2 分钟
原文
文章信息与版本

文章信息

发布
2022年10月17日
收录
2025年12月12日
更新
2026年9月12日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1
C
#include<bits/stdc++.h>
using namespace std;
int a[100002];
int main(){
	int T;
	cin>>T;
	while(T--){
		memset(a, 0, sizeof(a));
		int n;
		cin>>n;
		for(int i=1;i<=n;i++){
			int x;
			cin>>x;
			a[x]++;
		}
		int ret=INT_MAX;
		for(int i=0;(1<<i)<=n;i++){
			for(int j=0;(1<<j)<=n;j++){
				int ans=0;
				int x=1<<i;
				int y=1<<j;
				int cnt=1, sm=0;
				//cout<<"x"<<x<<" y"<<y<<endl;
				while(sm+a[cnt]<=x&&cnt<=n){
					sm+=a[cnt];
					cnt++;
				}
				ans+=x-sm;
				//cout<<"ans"<<ans<<endl;
				sm=0;
				while(sm+a[cnt]<=y&&cnt<=n){
					sm+=a[cnt];
					cnt++;
				}
				ans+=y-sm;
				//cout<<"ans"<<ans<<endl;
				sm=0;
				while(cnt<=n){
					sm+=a[cnt];
					cnt++;
				}
				//cout<<"sm"<<sm<<endl;
				for(int l=0;;l++){
					if((1<<l)>=sm){
						ans+=(1<<l)-sm;
						//cout<<"l="<<l<<endl;
						break;
					}
				}
				//cout<<"ans"<<ans<<endl;
				ret=min(ret, ans);
				//cout<<"ret"<<ret<<endl;
			}
		}
		cout<<ret<<endl;
	}

	return 0;
}
原文