Problem C.Storage Keepers 

Background

Randy Company has N (1<=N<=100) storages. Company wants some men to keep them safe. Now there are M (1<=M<=30) men asking for the job. Company will choose several from them. Randy Company employs men following these rules:

1.       Each keeper has a number Pi (1<=Pi<=1000) , which stands for their ability.

2.       All storages are the same as each other.

3.       A storage can only be lookd after by one keeper. But a keeper can look after several storages. If a keeper’s ability number is Pi, and he looks after K storages, each storage that he looks after has a safe number Uj=Pi div K.(Note: Uj, Pi and K are all integers). The storage which is looked after by nobody will get a number 0.

4.       If all the storages is at least given to a man, company will get a safe line L=min Uj

5.       Every month Randy Company will give each employed keeper a wage according to his ability number. That means, if a keeper’s ability number is Pi, he will get Pi dollars every month. The total money company will pay the keepers every month is Y dollars.

Now Randy Company gives you a list that contains all information about N,M,P, your task is give company a best choice of the keepers to make the company pay the least money under the condition that the safe line L is the highest.

Input

The input file contains several scenarios. Each of them consists of 2 lines:

The first line consists of two numbers (N and M), the second line consists of M numbers, meaning Pi (I=1..M). There is only one space between two border numbers.

The input file is ended with N=0 and M=0.

Output

For each scenario, print a line containing two numbers L(max) and Y(min). There should be a space between them.

Sample Input

2 1

7

1 2

10 9

2 5

10 8 6 4 1

5 4

1 1 1 1

0 0

Sample Output

3 7

10 10

8 18

0 0

题意:有m个仓库, n个小伙伴,每个小伙伴有个能力值p,要这些小伙伴去守护仓库,每个小伙伴的雇佣金是p,每个小伙伴看守的仓库安全值为p/k(每个小伙伴看守仓库数)。仓库的安全值为所有仓库中,安全值最小的仓库的安全值。

要求出最大安全值和最大安全值下的最小开销。

思路: 背包, 首先是第一个问题,我们把每个小伙伴看成物品,要看守的仓库数看成背包容量,每个小伙伴看守的仓库数为k,价值为p[i]/k。 状态转移方程为dp[j] = max(dp[j], min(dp[j - k], p[i]/k).。

然后是第二个问题。在第一个问题求出的最大安全值maxx下,求最小价值,依然是背包,k表示每个小伙伴看守的仓库数,状态转移方程为dp[j] = min(dp[j], dp[j - k] + p[i]);

代码:

#include <stdio.h>
#include <string.h> const int INF = 1 << 30;
int n, m, p[105], i, j, k, dp[1005], maxx, minn; int max(int a, int b) {
return a > b ? a : b;
} int min(int a, int b) {
return a < b ? a : b;
} int main() {
while (~scanf("%d%d", &m, &n) && m || n) {
memset(dp, 0, sizeof(dp));
dp[0] = INF;
for (i = 0; i < n; i ++)
scanf("%d", &p[i]);
for (i = 0; i < n; i ++) {
for (j = m; j >= 0; j --) {
for (k = 1; k <= p[i] && k <= j; k ++) {
dp[j] = max(dp[j], min(dp[j - k], p[i] / k));
}
}
}
maxx = dp[m];
if (maxx == 0) {
printf("0 0\n");
continue;
}
for (i = 1; i <= m; i ++)
dp[i] = INF;
dp[0] = 0;
for (i = 0; i < n; i ++)
for (j = m; j >= 0; j --)
for (k = min(j, p[i]/maxx); k > 0; k --) {
dp[j] = min(dp[j], dp[j - k] + p[i]);
}
printf("%d %d\n", maxx, dp[m]);
}
return 0;
}

UVA 10163 Storage Keepers(dp + 背包)的更多相关文章

  1. uva 10163 - Storage Keepers(01背包)

    题目链接:10163 - Storage Keepers 题目大意:给出m为仓库的数量, 给出n为有守夜人的数量, 然后给出n个数值,为对应守夜人应付的酬劳,每个守夜人的能力与他需要的酬劳是相等的,并 ...

  2. UVA 10163 Storage Keepers(两次DP)

    UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...

  3. DP(两次) UVA 10163 Storage Keepers

    题目传送门 /* 题意:(我懒得写,照搬网上的)有n个仓库,m个人看管.一个仓库只能由一个人来看管,一个人可以看管多个仓库. 每个人有一个能力值pi,如果他看管k个仓库,那么所看管的每个仓库的安全值为 ...

  4. UVa 10163 Storage Keepers (二分 + DP)

    题意:有n个仓库,m个管理员,每个管理员有一个能力值P,每个仓库只能由一个管理员看管,但是每个管理员可以看管k个仓库(但是这个仓库分配到的安全值只有p/k,k=0,1,...),雇用的管理员的工资即为 ...

  5. UVA 10163 - Storage Keepers(dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题意 有n个仓库,让m个人来看管.一个仓库只能由一个人来看管,一个人可以看管多个仓库. 每个人 ...

  6. uva 10163 Storage Keepers

    题意: 有n个仓库,m个人,一个仓库只能由一个人托管,每个人可以托管多个仓库. 每个人有一个能力值a,如果说他托管了k个仓库,那么这些仓库的安全值都是a/k. 雇佣一个人的花费也是a. 如果一个仓库没 ...

  7. UVA-10163 Storage Keepers (0-1背包)

    题目大意:有n个仓库,m个应聘者,每人对应一个能力值.一个人可以看多个仓库,一间仓库只能被一个人看.如果一个能力为p的人看k间仓库,那么安全系数为p/k,求出最大的最小安全系数,并且求出在此情况下所有 ...

  8. UVA-10163 Storage Keepers DP

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. UVA 10163 十六 Storage Keepers

    十六 Storage Keepers Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. Inno Setup 安装前卸载原程序(转)

    很多時候我們需要在安裝文件之前卸載原有的程序而不是覆盖安装,本文的code就是实现了这样的功能. 实现原理是:從注冊表'UninstallString'項中读取卸载信息,用Exec进行静默卸载. 下面 ...

  2. codility上的练习(3)

    今天发现又出了lesson 3... 不过题目都很简单…… (1) Min-avg-slice 给定一个长度为n的整数数组,找到一个连续的子数组,数组元素的平均值最小. 数据范围N [1..10^5] ...

  3. 使用Node.js快速搭建WebSocket server

    原文地址:http://my.oschina.net/yushulx/blog/309413 目录[-] 安装 服务端 客户端 参考 安装 ? 1 npm install ws 服务端 server. ...

  4. linux脚本之简单实例

    利用脚本计算10的阶乘 简单说明一下: #!/bin/bash说明该shell使用的bash shell程序.这一句不可少for i in `seq 1 10`还可以写成for i in 1 2 3 ...

  5. Hadoop 4、Hadoop MapReduce的工作原理

    一.MapReduce的概念 MapReduce是hadoop的核心组件之一,hadoop要分布式包括两部分,一是分布式文件系统hdfs,一部是分布式计算框就是mapreduce,两者缺一不可,也就是 ...

  6. 网易云课堂_C++程序设计入门(上)_第1单元:C++概览_第1单元作业 - 写代码 - 互评 (难度:易)

    第1单元作业 - 写代码 - 互评 (难度:易) 查看帮助 返回   提交作业(截止时间已过) 完成并提交作业     作业批改 互评训练   互评作业   自评作业     成绩公布 查看成绩 温 ...

  7. Search gold(dp)

    Search gold Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  8. 【JSON异常系列】new JSONObject对象时卡死原因

    8:47 2015/7/11 昨天晚上在使用JSON时,在创建JSONObject对象的时候不报错也不抛出异常.但就是new 不出来JSONObject的对象,这是一个非常奇葩的现象. 最后才发现原来 ...

  9. OAuth2.0授权机制说明

    授权机制说明   1 简介 优酷对第三方应用用户授权采用OAuth2.0标准 2 OAuth2.0 授权方式 优酷支持OAuth 2.0的三种授权方式,请根据平台选用不同的授权方式: 2.1 通用授权 ...

  10. 【网络协议】TCP的拥塞控制机制

    前言 计算机网络中的带宽.交换节点中的缓存和处理机等,都是网络的资源,在某段时间内,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就要变坏,这样的情况就叫做拥塞. 所谓拥塞控制,就 ...