QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#759253 | #5137. Tower | frankly6 | TL | 0ms | 0kb | C++17 | 1.4kb | 2024-11-17 23:27:24 | 2024-11-17 23:27:25 |
answer
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
const int MX=505;
const int inf=0x3fffffff;
int T, N, M;
int ar[MX];
map<int,int> mp;
int read()
{
int r=0, f=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') {r=r*10+ch-'0'; ch=getchar();}
return r*f;
}
int main()
{
// freopen("testdata.in","r",stdin);
T=read();
while(T--)
{
mp.clear();
N=read(); M=read();
for(int i=1;i<=N;i++)
{
ar[i]=read();
int tmp=ar[i];
while(tmp) mp[tmp]=1, tmp/=2;
}
int ans=inf;
for(auto [tar,tag]:mp)
{
int br[MX]={}, cr[MX]={}, ta=0;
memcpy(cr,ar,sizeof(ar));
for(int i=1;i<=N;i++)
{
if(cr[i]<tar) br[i]=tar-cr[i];
else
{
while(abs(cr[i]/2-tar)>cr[i]-tar-1) cr[i]/=2, br[i]++;
br[i]+=abs(cr[i]-tar);
}
// cout << " br[i]=" << br[i] << '\n';
}
sort(br+1,br+1+N);
for(int i=1;i<=N-M;i++)
ta+=br[i];
// cout << "tar=" << tar << ", ta=" << ta << '\n';
ans=min(ans,ta);
}
cout << ans << '\n';
}
return (0-0);
}
详细
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