Va-10163 storage keepers (01 backpack)

Source: Internet
Author: User

(A 10163 storage keepers (01 backpack)


Now there are m shopkeepers and N stores, each of which has a capacity value, and then a guardian can keep K stores, the security of these stores is PI/K. The store's security depends on the one with the lowest security level provided by the person guarding it. The maximum security of these stores depends on the store with the lowest security. I am asking how to hire these people to make the store the most secure, with the lowest cost.


Solution:

With the highest security requirements, it is necessary to determine whether every store owner needs to hire it and keep it secure after hiring it ). DP [I] [J] indicates the maximum security value of the front I stores when the front J daemon has been guarded. DP [I] [J] = max (DP [I] [J-1], min (DP [k] [J-1], PJ/K )) k> = 0 & K <I. you can save the J dimension and convert it into a rolling array.

Then, the highest security of these stores comes out of ANS, which requires the least cost. When the security of each store is maintained, the maximum number of stores that employ this person can guard is PI/ANS. If there are more stores, the security will not meet the requirements, then we thought the same way as above: only DP [I] [J] is the lowest cost for the front I store to use the front J daemon (to ensure the highest security ). DP [I] [J] = min (DP [I] [J-1], DP [k] [J-1] + PJ ).

If security is required, then K has corresponding requirements.


Code:

#include <cstdio>#include <cstring>#include <algorithm>#include <functional>using namespace std;const int N = 1005;const int M = 35;const int INF = 3000000;int p[M];int dp[N];int y[N];int Min (const int a, const int b) {return a < b ? a: b; }int Max (const int a, const int b) {return a > b ? a: b; }int main () {int n, m;while (scanf ("%d%d", &n, &m) , n || m) { for (int i = 1; i <= m; i++)scanf ("%d", &p[i]);sort (p + 1, p + 1 + m, greater<int>());memset (dp, 0, sizeof (dp));dp[0] = INF;for (int i = 1; i <= m; i++) for (int j = n; j >= 1; j--) {for (int k = 1; k <= j; k++) {dp[j] = Max (dp[j], Min (dp[j - k] , p[i] / k));}}int ans = dp[n];if (ans) {for (int i = 1; i <= n; i++)dp[i] = INF;dp[0] = 0;for (int i = 1; i <= m; i++)for (int j = n; j >= 1; j--) for (int k = Min(p[i]/ans, j); k >= 1; k--) {dp[j] = Min (dp[j], dp[j - k] + p[i]);}} elsedp[n] = 0;printf ("%d %d\n", ans, dp[n]);}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.