• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
UVa 10163 Storage Keepers (二分 + DP)

题意:有n个仓库,m个管理员,每个管理员有一个能力值P,每个仓库只能由一个管理员看管,但是每个管理员可以看管k个仓库(但是这个仓库分配到的安全值只有p/k,k=0,1,...),雇用的管理员的工资即为他们的能力值p和,问,使每个仓库的安全值最高的前提下,使的工资总和最小。

析:首先使用二分安全值,然后使用DP来判断是不是能够达到这个安全值,这个DP就是一个01背包,dp[i] 表示看管 i 个仓库的最少费用多少,dp[j] = min{dp[j], d[j-x] + cost[i]}。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("in.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1100 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r > 0 && r <= n && c > 0 && c <= m;
}

int a[maxn], b[maxn];
int dp[maxn];
int ans;

bool judge(int mid, bool ok){
  ms(dp, INF);  dp[0] = 0;
  for(int i = 0; i < m; ++i){
    int x = a[i] / mid;
    for(int j = 1050; j >= x; --j)
      dp[j] = min(dp[j], dp[j - x] + a[i]);
  }
  int res = INF;
  for(int i = n; i <= 1050; ++i)
    res = min(res, dp[i]);
  if(ok)  ans = res;
  return res != INF;
}

int main(){
  while(scanf("%d %d", &n, &m) == 2 && n+m){
    int l = 1, r = 0;
    for(int i = 0; i < m; ++i)  scanf("%d", a + i), r = max(r, a[i]);
    while(l <= r){
      int m = l + r >> 1;
      if(judge(m, false))  l = m + 1;
      else r = m - 1;
    }
    if(l == 1){ printf("0 0\n");  continue; }
    judge(l - 1, 1);
    printf("%d %d\n", l - 1, ans);
  }
  return 0;
}

  

posted on 2018-03-21 17:38  dwtfukgv  阅读(105)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3