HDU - 2639 Bone Collector II 题 解

Subject to the effect

A collection of human bones, there are n bones, each bone volume and value have asked the backpack can be packed in a capacity of V, the first k can get large (duplicates removed) value is.

Sample

Sample input 1

5 10 2
1 2 3 4 5
5 4 3 2 1

Sample output 1

12

Sample input 2

5 10 12
1 2 3 4 5
5 4 3 2 1

Sample output 2

2

analysis

  • Running violence is obviously not good, each item is optional selectable up to \ (2 ^ n \) different schemes, it corresponds so much value, if all apparently able to save reordering the output, simply wrong, but apparently time and space bear.
  • Of course, when running backpack, every time we find an optimum solution decisions are current stage in front of each state basis. If you simply want to finally put all the values ​​in the array are used to sort, nor feasible. The whole transfer process does not guarantee that the optimal solution is smaller than the second largest value.
  • We can j for each capacity while maintaining the k value, to preserve the value of k large capacity j into the time before. On this basis, we consider the transfer of items for the i:
    • Defined \ (F [i] [j] [k] \) represents the i items placed in front capacity backpack j, k-th largest value is obtained, since the state of the i-th item when picking up the transfer of only the near i-1 state, and therefore also simplifies a two-dimensional, i.e. \ (F [J] [K] \) .
    • And i put a hold on the maximum value of items come from? Obviously, the two values is common whichever is greater transfer equation: \ (max \ {F [J] [. 1], F [VI-J] [. 1] + Wi \} \) , i.e., with no release each placed a maximum value as a maximum select from among them.
    • Continue to consider the second-largest candidate values ​​What?
      1. The article may have an extremely low price, it is clearly not vote for it may be the best, so that the maximum possible and the second largest value are letting go of the article corresponding value;
      2. The article may have cost high, then obviously it may be the best choice, it is a possible maximum value and the second largest values ​​are put down to the value corresponding to the article;
      3. Thus a large value of the secondary candidate value may be 4: \ (F [J] [. 1], F [J] [2], F [VI-J] [. 1] + Wi, F [VI-J] [2 ] Wi + \) ;
      4. And so on, for each capacity, there will be 2k candidate values. If \ (f [j] \) and \ (f [j-vi] \) there are k different values, then the current \ (f [j] \) can certainly derive k different values.
    • Another problem is how to select k distinct values:
      1. You can sort it again to go heavy, simple and crude valid
      2. The combined thrift merge sort, because \ (f [j] [k ] \) and \ (f [j-vi] [k] + wi \) respectively in descending sequence, so engage in two arrays, which continue to take combined number on it, the code is slightly longer, but a lot of high efficiency

Code (merged Method)

// 归并排序法合并两部分,取前kth个数
void bag() {
    for (int i = 1; i <= n; ++i) {
        for (int j = vol; j >= cost[i]; --j) {
            int a[K] = {}, b[K] = {};
            for (int k = 1; k <= kth; ++k) {
                a[k] = dp[j][k];
                b[k] = dp[j-cost[i]][k]+val[i];
            }
            a[kth+1] = -1;
            b[kth+1] = -1;
            
            // 合并
            int k = 1, x = 1, y = 1;
            while (k <= kth && (x <= kth || y <= kth)) {
                dp[j][k] = a[x] > b[y] ? a[x++] : b[y++];
                if (dp[j][k] != dp[j][k-1]) {
                    k++;
                }
            }
        }
    }
    printf("%d\n", dp[vol][kth]);
}

Guess you like

Origin www.cnblogs.com/kuangbiaopilihu/p/12152557.html