大厂笔试题
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.

50 lines
2.1 KiB

  1. /*
  2. n a1,a2,.·,an :
  3. 1. a1x
  4. 2.k*MEX(a)MEX(a)a中未出现过的最小非负整数[0,1,2,4] MEX为3
  5. a
  6. T代表数据组数n,k,x
  7. n个整数 a1, a2,...,an表示数组元素
  8. a之和不超过 2x105.
  9. */
  10. /*
  11. +
  12. dp[i]i往后考虑的最小花费dp[0]
  13. mex是多少dp的过程中
  14. O(n)
  15. */
  16. #include <iostream>
  17. #include <vector>
  18. #include <algorithm>
  19. #include <set>
  20. using namespace std;
  21. int main() {
  22. int T;
  23. cin >> T; // 读取测试数据组数
  24. while (T--) {
  25. long long n, k, x;
  26. cin >> n >> k >> x;
  27. vector<long long> a(n);
  28. for (int i = 0; i < n; ++i) {
  29. cin >> a[i];
  30. }
  31. // 动态规划数组dp[i]表示从i往后考虑的最小花费,最后最小花费就是dp[0]或者直接删除后续所有元素
  32. vector<long long> dp(n + 1, LLONG_MAX);
  33. dp[n] = 0;
  34. int suffix_mex = 0;
  35. set<int> vst;
  36. for (int i = n-1; i >= 0; --i) {
  37. vst.insert(a[i]);
  38. while(vst.count(suffix_mex)){
  39. suffix_mex++;
  40. }
  41. dp[i] = min(dp[i + 1] + x, k * suffix_mex);
  42. }
  43. // 输出最小花费
  44. cout << dp[0] << endl;
  45. }
  46. return 0;
  47. }