QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#371572#5137. TowerDjangle162857TL 3575ms4392kbC++141.7kb2024-03-30 14:05:432024-03-30 14:05:45

Judging History

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

  • [2024-03-30 14:05:45]
  • 评测
  • 测评结果:TL
  • 用时:3575ms
  • 内存:4392kb
  • [2024-03-30 14:05:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " == " << x << endl
#define el '\n'
#define fir first
#define sec second
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
const int N = 200020;
int getans(int x, int y)
{
	if (x <= y)
		return y - x;
	if (x >= y * 2)
		return getans(x / 2, y) + 1;
	return min(y - (x / 2) + 1, x - y);
}
void solve()
{
	int n, m, mn = inf;
	cin >> n >> m;
	vector<int> a(n + 1, 0);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		mn = min(mn, a[i]);
	}
	ll ans = inf;
	map<int, int> mp;
	vector<int> q;
	for (int x = 1; x <= n; x++) {
		int L = max(a[x] - 29, 1), R = a[x] + 29;
		for (int i = L; i <= R; i++) {
			if (mp.find(i) == mp.end()) {
				mp[i] = 1;
			}
			else {
				q.push_back(i);
				mp[i]++;
			}
		}
		int rr = a[x];
		int tmp = 1;
		while (rr >= 1) {
			rr = rr / 2;
			L = max(rr - 29 + tmp, 1), R = rr + 29 - tmp;
			for (int i = L; i <= R; i++) {
				if (mp.find(i) == mp.end()) {
					mp[i] = 1;
				}
				else {
					q.push_back(i);
					mp[i]++;
				}
			}
			tmp++;
		}
	}
	for (int i : q) {
		// debug(i);
		vector<int> res;
		for (int j = 1; j <= n; j++) {
			int now = getans(a[j], i);
			// cout << now << " ";
			res.push_back(now);
		}
		// cout << endl;
		sort(res.begin(), res.end());
		ll sum = 0;
		for (int j = 0; j <= n - m - 1; j++) {
			sum += res[j];
		}
		ans = min(ans, sum);
	}
	cout << ans << el;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T = 1;
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

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: 0
Accepted
time: 3575ms
memory: 4392kb

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
108
570
636
994
227
656
50

result:

ok 10 numbers

Test #3:

score: 0
Accepted
time: 3095ms
memory: 4136kb

input:

10
133 89
20 70 6 45 4 72 38 7 18 1 82 39 69 85 5 36 1 62 30 47 68 55 7 41 7 42 7 61 11 82 2 80 80 93 29 30 42 58 73 26 99 67 60 94 61 46 47 54 44 50 35 88 61 17 23 97 90 28 16 47 75 35 28 14 42 63 26 40 95 58 26 25 26 83 93 56 17 27 7 90 91 28 53 49 47 84 55 52 11 34 14 74 40 65 84 32 99 46 1 21 31...

output:

88
1361
128
246
29
83
3
677
96
382

result:

ok 10 numbers

Test #4:

score: -100
Time Limit Exceeded

input:

10
500 50
67 93 11 58 54 40 37 3 92 96 91 20 46 5 21 43 3 2 7 47 27 81 14 53 86 21 46 51 86 22 42 14 52 38 42 25 34 29 84 42 43 96 11 100 27 60 48 15 13 69 58 16 14 58 17 94 8 71 39 38 25 37 100 58 99 56 65 84 94 63 25 34 13 73 83 83 69 60 70 15 15 90 7 11 88 69 13 26 99 28 16 97 32 40 76 62 41 5 9 ...

output:


result: