Files
python/TangDou/CSP-J/AcWingTraining/MaiChePaiTeaching.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
899 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
int n, k;
const int N = 10010;
int a[N];
int x;
int res = 0x3f3f3f3f;
/*
6 5
898196
*/
struct Node {
int idx;
int num;
} b[N];
bool cmp(int p, int q) {
return abs(p - x) < abs(q - x);
}
int main() {
cin >> n >> k; // n个长度,k个相同的数字
string s;
cin >> s;
for (int i = 0; i < n; i++) a[i] = s[i] - '0';
// for (int i = 0; i < n; i++) cout << a[i] << " ";
for (x = 0; x <= 9; x++) { // 枚举最终是哪个数字占了k位
int cost = 0;
for (int i = 0; i < n; i++) b[i] = a[i];
sort(b, b + n, cmp);
for (int i = 0; i < n; i++)
cout << b[i] << " ";
cout << endl;
// for (int i = 0; i < k; i++) cost += abs(b[i] - x);
// res = min(res, cost);
}
cout << res << endl;
return 0;
}