QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#657646 | #5137. Tower | SouthernB# | TL | 0ms | 0kb | C++14 | 1.7kb | 2024-10-19 15:09:17 | 2024-10-19 15:09:17 |
answer
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 510;
const int inf = 1e18;
int n, m, a[N], ans;
vector <int> pos, val;
void get(int x)
{
pos.emplace_back(x);
while(x >> 1){
int x1 = x, x2 = x >> 1;
int mid = (x1 + x2) >> 1;
pos.emplace_back(mid);
if(x1 - mid > mid - x2) pos.emplace_back(mid + 1);
x >>= 1;
pos.emplace_back(x);
}
}
void solve()
{
scanf("%lld%lld", &n, &m);
for(int i = 1; i <= n; i++){
scanf("%lld", a + i);
get(a[i]);
}
ans = inf;
sort(pos.begin(), pos.end());
auto ed = unique(pos.begin(), pos.end());
for(auto it = pos.begin(); it < ed; it++){
int x = *it;
if(x <= 0) continue;
val.clear();
for(int i = 1; i <= n; i++){
if(a[i] <= x) val.emplace_back(x - a[i]);
else{
int mn = a[i] - x, now = a[i], rv = 0;
while(now){
if(now > x) mn = min(mn, now - x + rv);
else mn = min(mn, x - now + rv);
now >>= 1, rv++;
}
val.emplace_back(mn);
//if(x == 2) cout << mn << endl;
}
}
sort(val.begin(), val.end(), greater<int>());
int res = 0;
for(int i = m; i < n; i++) res += val[i];
ans = min(ans, res);
}
printf("%lld\n", ans);
pos.clear();
}
signed main()
{
int T;
scanf("%d", &T);
while(T--) solve();
return 0;
}
/*
3
2 0
2 6
5 0
1 2 3 4 5
5 3
1 2 3 4 5
*/
詳細信息
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
output:
2 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...