QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#657646#5137. TowerSouthernB#TL 0ms0kbC++141.7kb2024-10-19 15:09:172024-10-19 15:09:17

Judging History

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

  • [2024-10-19 15:09:17]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [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
...

result: