Advertisement
pb_jiang

LC2547 WA1

Jan 22nd, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. class Solution {
  2.     using ll = long long;
  3.     vector<vector<ll>> dp;
  4.     ll search(vector<int>& ns, int k, int beg, int end) {
  5.         if (dp[beg][end] != LLONG_MAX / 2) return dp[beg][end];
  6.         if (beg + 1 == end) {
  7.             return dp[beg][end] = k;
  8.         }
  9.         /*
  10.         // map<int, int> cnt;
  11.         vector<int> cnt(1 << 10);
  12.         for (int i = beg; i < end; ++i) cnt[ns[i]]++;
  13.         ll dup = 0;
  14.         for (auto p: cnt) if (p >= 2) dup += p;
  15.        
  16.         dp[beg][end] = min(dp[beg][end], dup + k);
  17.         */
  18.        
  19.         for (int i = beg + 1; i < end; ++i) {
  20.             ll opt = search(ns, k, beg, i) + search(ns, k, i, end);
  21.             printf("beg:%d i:%d end:%d opt:%lld\n", beg, i, end, opt);
  22.             dp[beg][end] = min(dp[beg][end], opt);
  23.         }
  24.         return dp[beg][end];
  25.     }
  26. public:
  27.     int minCost(vector<int>& nums, int k) {
  28.         int n = nums.size();
  29.         dp = vector<vector<ll>>(n + 1, vector<ll>(n + 1, LLONG_MAX / 2));
  30.         for (int i = 0; i < n; ++i) {
  31.             vector<int> cnt(1 << 10);
  32.             ll val = k;
  33.             for (int j = i; j < n; ++j) {
  34.                 cnt[nums[j]]++;
  35.                 if (cnt[nums[j]] == 2) val += 2;
  36.                 else if (cnt[nums[j]] > 2) val += 1;
  37.                 dp[i][j + 1] = val;
  38.                 cout << dp[i][j + 1] << ' ';
  39.             }
  40.             cout << endl;
  41.         }
  42.         return (int)search(nums, k, 0, nums.size());
  43.     }
  44. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement