自动兑换机
作者:
该用户被封禁
,
2021-02-21 18:23:53
,
所有人可见
,
阅读 413
自动兑换机
来自洛谷
比赛时间到了没法验证了, 等待开放的时候再提交试试看.
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, m, t;
int a[N];
int f[110][N];
bool cmp(int a, int b) {
char x[20], y[20];
sprintf(x, "%d", a);
sprintf(y, "%d", b);
int i = 0, j = 0;
while (i < strlen(x) && j < strlen(y)) {
if (x[i] < y[j]) return false;
else if (x[i] > y[j]) return true;
i++, j++;
}
if (i == strlen(x)) return false;
return true;
}
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
double c;
cin >> c;
m = c * 100;
sort(a + 1, a + 1 + n, cmp);
//for (int i = 1; i <= n; i++) cout << a[i] << ' ' ; cout << endl;
memset(f, 0x3f, sizeof(f));
for (int i = 0; i <= n; i++) f[i][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
f[i][j] = f[i - 1][j];
if (j >= a[i])
f[i][j] = min(f[i][j], f[i][j - a[i]] + 1);
}
}
int id = 0, rr = m;
for (int i = 1; i <= n; i++) {
//cout << f[i][m] << ' ';
if (f[i][m] <= rr) { id = i; rr = f[i][m]; }
}
if (id == 0) { cout << "No Solution" << endl; continue; }
cout << f[id][m] << ' ';
int i = id, j = m, all = m;
while (all > 0) {
int cnt = 0;
while (f[i][j] == f[i][j - a[i]] + 1) {
cnt++, all -= a[i], j -= a[i];
}
if (cnt) {
cout << a[i] << '*' << cnt;
if (all) cout << '+';
} else {
i--;
}
}
cout << endl;
}
return 0;
}