UVA 10163 Storage Keepers 两次dp
2016-08-18 11:28
351 查看
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 ÷ 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 = minUj
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.
uva的题目总是屎一样的粘贴,嘛,就当是索引了。
题意就是说给你几个m保安 看守n个仓库,每个保安有pi的能力,看守的仓库数量均分能力值,一个仓库不能被多次看守,问当使仓库看守度最小的那个最大的情况下,该值和使用的能力值之和最小的值。
蛮绕的 理解起来倒是不麻烦
首先就是dp解决看守度最小的仓库值最大的那个值,转移方程也很好写出来
接下来就是dp寻找最少的花费了,那就再次dp,寻找满足条件的最小花费值,注意当0的时候要特判,原因不解释了
代码:
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
int a[150];
int dp[55][105];
int g[105][105];
int m,n;
int main()
{
while(scanf("%d %d",&n,&m)!=EOF&&(n+m))
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++)
scanf("%d",a+i);
for(int i=1;i<=m;i++)
{
dp[i-1][0]=inf;
for(int j=1;j<=n;j++)
{
dp[i][j]=dp[i-1][j]; // k=0
for(int k=1;k<=j;k++)
dp[i][j]=max(dp[i][j],min(dp[i-1][j-k],a[i]/k));
}
}
printf("%d ",dp[m]
);
int sav=dp[m]
;
memset(g,inf,sizeof(g));
if(sav==0) printf("0\n");
else
{
for(int i=1;i<=m;i++)
{
g[i-1][0]=0;
for(int j=1;j<=n;j++)
{
g[i][j]=g[i-1][j];
for(int k=1;k<=j;k++)
{
if(a[i]/k>=sav)
g[i][j]=min(g[i][j],g[i-1][j-k]+a[i]);
}
}
}
printf("%d\n",g[m]
);
}
}
return 0;
}
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 ÷ 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 = minUj
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.
uva的题目总是屎一样的粘贴,嘛,就当是索引了。
题意就是说给你几个m保安 看守n个仓库,每个保安有pi的能力,看守的仓库数量均分能力值,一个仓库不能被多次看守,问当使仓库看守度最小的那个最大的情况下,该值和使用的能力值之和最小的值。
蛮绕的 理解起来倒是不麻烦
首先就是dp解决看守度最小的仓库值最大的那个值,转移方程也很好写出来
接下来就是dp寻找最少的花费了,那就再次dp,寻找满足条件的最小花费值,注意当0的时候要特判,原因不解释了
代码:
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
int a[150];
int dp[55][105];
int g[105][105];
int m,n;
int main()
{
while(scanf("%d %d",&n,&m)!=EOF&&(n+m))
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++)
scanf("%d",a+i);
for(int i=1;i<=m;i++)
{
dp[i-1][0]=inf;
for(int j=1;j<=n;j++)
{
dp[i][j]=dp[i-1][j]; // k=0
for(int k=1;k<=j;k++)
dp[i][j]=max(dp[i][j],min(dp[i-1][j-k],a[i]/k));
}
}
printf("%d ",dp[m]
);
int sav=dp[m]
;
memset(g,inf,sizeof(g));
if(sav==0) printf("0\n");
else
{
for(int i=1;i<=m;i++)
{
g[i-1][0]=0;
for(int j=1;j<=n;j++)
{
g[i][j]=g[i-1][j];
for(int k=1;k<=j;k++)
{
if(a[i]/k>=sav)
g[i][j]=min(g[i][j],g[i-1][j-k]+a[i]);
}
}
}
printf("%d\n",g[m]
);
}
}
return 0;
}
相关文章推荐
- UVA 10163 Storage Keepers(两次DP)
- DP(两次) UVA 10163 Storage Keepers
- HDU 5445 Food Problem、UVa 10163 Storage Keepers、POJ 3260 The Fewest Coins(两次dp)
- UVA - 10163 - Storage Keepers(两次dp)
- UVa 10163 Storage Keepers(两次DP)
- UVA 10163 Storage Keepers(两次DP)
- UVA 10163-Storage Keepers(DP)
- UVA 10163 - Storage Keepers(dp)
- UVA 10163 - Storage Keepers(dp)
- uva10163(两次dp)
- uva 10163 Storage Keepers (dp)
- UVA 10163 - Storage Keepers(dp)
- UVA-10163 Storage Keepers (DP多次)
- uva 10163 Storage Keepers (DP)
- uva_10163 - Storage Keepers ( 普通DP )
- 100道动态规划——13 UVA 10163 Storage Keepers 有约束条件下的DP,递推,不能使用结构体作为基本单位
- uva 10163 两次Dp
- Storage Keepers - UVa 10163 dp
- uva 10163 Storage Keepers
- UVA - 10163 Storage Keepers 记忆化搜索