比赛 20160412 评测结果 C
题目名称 饭堂 最终得分 0
用户昵称 KZNS 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2016-04-12 12:10:04
显示代码纯文本
//KZNS
#include <fstream>
#include <cstring>
#include <cmath>
using namespace std;
//
ifstream fin ("fancy.in");
ofstream fout ("fancy.out");
const int Nmax = 10003;
class poi {
public:
	int a, b;
	bool y;
};
inline bool cmd(poi a, poi b) {
	if (a.a == b.a) {
		if (a.y && b.y)
			return a.b < b.b;
		else {
			if (a.y)
				return true;
			if (b.y)
				return false;
			return a.b > b.b;
		}
	}
	else
		return a.a < b.a;
}
//
int N, K;
string S;
poi ls[Nmax];

//
int main() {
	fin >> N >> K;
	fin >> S;
	int edu = 0x7fffffff;
	string sed;
	string bed;
	for (int i = 0; i <= 9; i++) {
		for (int j = 0; j < S.length(); j++) {
			ls[j].a = abs(S[j]-'0' - i);
			ls[j].b = j;
			if (i <= S[j]-'0')
				ls[j].y = true;
			else
				ls[j].y = false;
		}
		sort(ls, ls+N, cmd);
		int u = 0;
		sed = S;
		for (int j = 0; j < K; j++) {
			sed[ls[j].b] = i+'0';
			u+=ls[j].a;
		}
		if (u < edu) {
			bed = sed;
			edu = u;
		}
		else if (u == edu) {
			bed = min(bed, sed);
		}
	}
	fout << edu << endl;
	fout << bed;
	return 0;
}
//UBWH