HDU 3433 (DP + 二分) A Task Process

题意:

有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少

思路:

DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到。

dp[i][j]表示在t时间内前i个人完成j件A任务后所能完成B任务的最大数量。

代码中还有一些注释。

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 int dp[55][205], a[205], b[205];
 8 int _, n, x, y, kase = 0;
 9
10 bool check(int t)
11 {
12     memset(dp, -1, sizeof(dp));
13     dp[0][0] = 0;
14
15     for(int i = 1; i <= n; ++i)
16     {
17         if(dp[i][x] >= y)    return true;
18         for(int j = 0; j <= x; ++j)
19         {
20             if(dp[i - 1][j] != -1)
21             {
22                 int temp = min(t/a[i], x-j);    //枚举第i个人可以做的A任务的个数
23                 for(int k = 0; k <= temp; ++k)
24                 {
25                     int t1 = (t - a[i] * k) / b[i];    //计算第i个人做k件A任务,所能做的B任务的个数
26                     dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] + t1);    //选最优解
27                 }
28             }
29         }
30     }
31
32     if(dp[n][x] >= y)    return true;
33     return false;
34 }
35
36 int main(void)
37 {
38     #ifdef LOCAL
39         freopen("3433in.txt", "r", stdin);
40     #endif
41
42     scanf("%d", &_);
43     while(_--)
44     {
45         scanf("%d%d%d", &n, &x, &y);
46         for(int i = 1; i <= n; ++i)
47             scanf("%d%d", &a[i], &b[i]);
48
49         int l = 0, r = a[1] * x + b[1] * y;
50         int ans = r;
51         while(l <= r)
52         {
53             int mid = (l + r) >> 1;
54             if(check(mid))
55             {
56                 ans = mid;
57                 r = mid - 1;
58             }
59             else    l = mid + 1;
60         }
61
62         printf("Case %d: %d\n", ++kase, ans);
63     }
64
65     return 0;
66 }

代码君

时间: 2024-10-09 03:10:53

HDU 3433 (DP + 二分) A Task Process的相关文章

hdu 1025 dp+二分求最长递增子序列

//dp[i],长度为i的递增子序列的最后一位的最小值 //对于这种处理得到的dp[i]必然是递增的 //对于第i个数a[i],只需要找到最小的大于它的位置pos, //由于是最小的大于a[i] ,pos - 1 的值必然小于a[i] //那么dp[pos] = a[i] ; #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 50001

二分+DP HDU 3433 A Task Process

HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1368    Accepted Submission(s): 684 Problem Description There are two kinds of tasks, namely A and B. There are N workers

(DP+二分) hdu 3433

A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1273    Accepted Submission(s): 631 Problem Description There are two kinds of tasks, namely A and B. There are N workers and the i

hdu 1025 Constructing Roads In JGShining&#39;s Kingdom(DP + 二分)

此博客为转发 Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in t

hdu 3586 树形dp+二分

题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵 树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线.现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所 有前线与司令部联系所花费的总费用少于m时的最小limit.1<=n<=1000,1<=m<=100万 题目要问的是最小的最大限制,必然二分答案 然后对于每一个值,树形DP判定是否可行 dp[i]表示要切断以i为根的其它所有子树的最小代价. 其中设定叶子结点的代价为无穷大

hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列,但数据太多.转化为求最长不下降子序列.太NB了.复杂度n*log(n). 解法:dp+二分 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <

hdu3586 树形dp+二分求解

http://acm.hdu.edu.cn/showproblem.php?pid=3586 Problem Description In the battlefield , an effective way to defeat enemies is to break their communication system. The information department told you that there are n enemy soldiers and their network w

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

hdu 5389 dp类似背包

http://acm.hdu.edu.cn/showproblem.php?pid=5389 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft. Stilwell is enjoying the first chapter of this