1068 Find More Coins (30分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 10^4 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤10^4, the total number of coins) and M (≤10^2, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the face values V1≤V2≤⋯≤Vk such that V1+V2+⋯+Vk=M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output “No Solution” instead.
Note: sequence {A[1], A[2], …} is said to be “smaller” than sequence {B[1], B[2], …} if there exists k≥1 such that A[i]=B[i] for all i<k, and A[k] < B[k].
Sample Input 1:
8 9
5 9 8 7 2 3 4 1
Sample Output 1:
1 3 5
Sample Input 2:
4 8
7 2 4 3
Sample Output 2:
No Solution
题解:
正常的dfs解背包问题,1e4数量级会有样例超时,需要剪枝,剪枝思路如下:
- 如果当前这个物品放进去会超过最大容量,剪掉。
- 如果在之后的所有体积较大(事先排好序)的物品加起来还不够填满背包,剪掉
代码:
#include <bits/stdc++.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ms(x, n) memset(x, n, sizeof(x))
#define wht() \
int _; \
for (scanf("%d", &_); _; _--)
#define For(i, a, b) for (int i = a; i <= (b); i++)
#define Rof(i, a, b) for (int i = a; i >= (b); i--)
#define ioss ios::sync_with_stdio(false)
#define all(a) a.begin(), a.end()
#define int long long
#define input(x) scanf("%lld", &x);
#define print(x) printf("%lld", x);
#define println(x) printf("%lld\n", x);
typedef vector<int> vi;
typedef pair<int, int> pii;
const int maxn = 2e6 + 7;
const int maxm = 5e3 + 7;
const int MOD = 1e9 + 7;
const int mod = 998244353;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// bool cmp(int a, int b) { return a > b; }
const int N = 1e5 + 5;
int n, m, k;
int a[N], b[N];
int f[N];
vector<string> ans;
bool flag;
void dfs(int x, int sum, string s) {
if (flag) return;
if (sum == m) {
flag = 1;
ans.push_back(s);
return;
}
if (sum > m) return;
For(i, x, n) {
if (sum + a[i] <= m && f[n] - f[i - 1] >= m - sum) {
dfs(i + 1, sum + a[i], s + to_string(a[i]) + " ");
}
}
}
void work() {
cin >> n >> m;
For(i, 1, n) { cin >> a[i]; }
sort(a + 1, a + 1 + n);
For(i, 1, n) { f[i] = f[i - 1] + a[i]; }
// For(i, 1, n) { cout << f[i] << "\n"; }
dfs(1, 0, "");
sort(all(ans));
if (ans.size() == 0) {
cout << "No Solution";
} else {
cout << ans[0].substr(0, ans[0].size() - 1);
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
work();
}
193

被折叠的 条评论
为什么被折叠?



