QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#630828 | #5137. Tower | 7islands# | TL | 0ms | 0kb | C++17 | 2.7kb | 2024-10-11 20:34:18 | 2024-10-11 20:34:20 |
answer
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define pii pair<int,int>
using namespace std;
int a[510],ans[510];
int e[510];
int n,m;
struct node{
int pos,val;
bool operator < (const node a)const{
return val<a.val;
}
}ee[510];
void solve(){
cin>>n>>m;
priority_queue<pii> q;
for(int i = 1;i<=n;i++){
cin>>a[i];
ans[i] = 0;
q.push(make_pair(a[i],i));
}
int cnt = 0;
int res = 1e17;
int l = 1,r = q.top().first;
while(l<=r){
int mid = l+r>>1;
int down=0,up = 0,on = 0;
int t = 0;
for(int i = 1;i<=n;i++){
if(a[i] < mid) down ++;
else if(a[i] > mid) up++;
else on++;
ee[i].val = abs(a[i]-mid) + ans[i];
ee[i].pos = i;
t+=abs(a[i]-mid) + ans[i];
}
sort(ee+1,ee+1+n);
for(int i = n;i>0&&i>n-m;i--){
t -=ee[i].val;
if(a[ee[i].pos]>mid) up--;
else if(a[ee[i].pos]<mid) down--;
else on--;
}
if(t<res) res = t;
if(up<down){
r=mid-1;
}
else if(down<up){
l = mid+1;
}
else break;
}
while(!q.empty()){
int pos = q.top().second;
int val = q.top().first;
q.pop();
if(val == 1) break;
val /= 2;
a[pos] = val;
cnt ++;
q.push(make_pair(val,pos));
ans[pos] ++ ;
int l = 1,r = q.top().first;
while(l<=r){
int mid = l+r>>1;
int down=0,up = 0,on = 0;
int t = 0;
for(int i = 1;i<=n;i++){
if(a[i] < mid) down ++;
else if(a[i] > mid) up++;
else on++;
ee[i].val = abs(a[i]-mid) + ans[i];
ee[i].pos = i;
t+=abs(a[i]-mid) + ans[i];
}
sort(ee+1,ee+1+n);
for(int i = n;i>0&&i>n-m;i--){
t -=ee[i].val;
if(a[ee[i].pos]>mid) up--;
else if(a[ee[i].pos]<mid) down--;
else on--;
}
if(t<res) res = t;
if(up<down){
r=mid-1;
}
else if(down<up){
l = mid+1;
}
}
}
cout<<res<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 2 0 2 6 5 0 1 2 3 4 5 5 3 1 2 3 4 5