大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.0 KiB

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5. bool canFillBox(int N, const vector<int>& toys, int c) {
  6. // dp[i] indicates if we can fill exactly i capacity with given toys and fillers
  7. vector<bool> dp(N + 1, false);
  8. dp[0] = true;
  9. for (int toy : toys) {
  10. for (int j = N; j >= toy; --j) {
  11. if (dp[j - toy]) {
  12. dp[j] = true;
  13. }
  14. }
  15. }
  16. // After using all toys, check if we can fill the remaining space with fillers
  17. return dp[N] || (N <= c);
  18. }
  19. int main() {
  20. int T;
  21. cin >> T;
  22. vector<string> res;
  23. while (T--) {
  24. int N, n, c;
  25. cin >> N >> n >> c;
  26. vector<int> toys(n);
  27. for (int i = 0; i < n; ++i) {
  28. cin >> toys[i];
  29. }
  30. if (canFillBox(N, toys, c)) {
  31. res.push_back("YES");
  32. } else {
  33. res.push_back("NO");
  34. }
  35. }
  36. for(int i = 0; i < res.size(); i++){
  37. cout << res[i] << endl;
  38. }
  39. return 0;
  40. }