QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661796#5137. TowerTytytony#WA 3ms3664kbC++231.2kb2024-10-20 18:12:442024-10-20 18:12:45

Judging History

你现在查看的是最新测评结果

  • [2024-10-20 18:12:45]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3664kb
  • [2024-10-20 18:12:44]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 510
#define ll long long
using namespace std;

ll n,m;
ll a[N],b[N];

ll find(ll x,ll aim){
	if(aim>=x) return aim-x;
	ll tmp=x,cnt=0;
	while(tmp-aim-1>abs(tmp/2-aim)&&tmp>aim){
		tmp/=2;
		cnt++;
	}
	return cnt+abs(tmp-aim);
}

ll check(ll aim){
	for(int i=0;i<n;i++) b[i]=find(a[i],aim);
	sort(b,b+n);
	ll cnt=0;
	for(int i=0;i<n-m;i++) cnt+=b[i];
	return cnt;
}

void sol(){
	ll l=0,r=1e9;
	ll ans=1e18;
	while(l!=r){
		ll mid1=l+(r-l)/3,mid2=l+(r-l)/3*2;
		if(r-l==2){
			ans=min(ans,check(l));
			ans=min(ans,check(r));
			ans=min(ans,check(l+1));
			break;
		}
		if(r-l==1){
			ans=min(ans,check(l));
			ans=min(ans,check(r));
			break;
		}
		ll chk1=check(mid1),chk2=check(mid2);
		ans=min(chk1,ans);
		ans=min(chk2,ans);
		if(chk1<chk2){
			r=mid2;
			continue;
		}
		if(chk1>chk2){
			l=mid1;
			continue;
		}
		if(chk1==chk2){
			l=mid1;
			r=mid2;
			continue;
		}
	}
	cout<<ans<<endl;
}

int main(){
	int T; cin>>T;
	while(T--){
		cin>>n>>m;
		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		sol();
	}
	return 0;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3664kb

input:

3
2 0
2 6
5 0
1 2 3 4 5
5 3
1 2 3 4 5

output:

2
4
1

result:

ok 3 number(s): "2 4 1"

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 3660kb

input:

10
272 118
11 14 49 94 71 62 46 45 74 22 15 36 7 37 27 35 96 85 75 78 76 64 23 59 17 35 71 28 96 82 5 66 2 48 57 31 88 10 61 73 79 23 19 52 39 76 48 98 5 39 48 51 90 90 60 27 47 24 24 56 48 27 39 21 38 18 20 9 62 83 47 15 51 22 73 74 7 80 64 60 86 74 59 7 84 38 99 31 42 60 52 41 63 88 59 90 77 40 68...

output:

454
3
436
114
570
636
994
227
656
51

result:

wrong answer 4th numbers differ - expected: '108', found: '114'