标签:cas line tip acm repr sum without tail present
终于补完第二次期末考了……果然考场上心态不好导致好多会做的题都没时间写
题目链接:
http://acm.hit.edu.cn/hoj/problem/view?id=1485
题目描述:
Submitted : 713, Accepted : 320
Abraham and Calford are Moving their things from dormitory A11 to A9,and they have their own carry limit,they can‘t carry things which is beyond their limit and they will not carry one bag together. They want things to be carried in one time,when they can‘t deal with so many things, they will hire a good helper to help them, who can only carry one bag of things, regardless of the weight of it. Your task is to tell whether they can carry their bags in one time,or not.
Input
There are multiple test cases.
First line of each test case contains two nonnegative numbers, A and C, (0 < A,C <=1000) representing the limit of Abraham and Calford,then the second line contains an integer N ( 0 < N <= 200 ) ,representing the number of bags,followed by N positive integers,each of them is not more than 100.
Output
For each test case
Output "Yes" if they can carry the bags without the help of the helper in one time.
Output "Need a helper" if they can only carry the bags with the help of the helper in one time.
Output "No" if they can‘t carry the bags in one time.
Sample Input
7 7 3 5 5 5 7 7 3 5 2 5 7 7 3 7 8 7 7 7 3 7 8 9
Sample Output
Need a helper Yes Need a helper No
题目大意:
两个人搬东西,给你各自能搬的最大重量,这时如果还剩一个东西搬不动,可以请一个帮手,帮手只能搬一个东西但不限重量
问能否一次办完并是否需要帮手
思路:
先贪心取得最大重量得物品给帮手搬,求出剩下的和
以A的容量做0-1背包,看剩下的容量C能否搬动
如果C的容量减去剩余的容量还能把帮手手中的物品搬动,则不需要帮手
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int N = 210; 8 9 int num[N], dp[5 * N] = { 0 }; 10 11 int main() { 12 int A, C, n, sum, best, res; 13 while (cin >> A >> C) { 14 cin >> n; 15 sum = best = 0; 16 for (int i = 1; i <= n; ++i) { 17 scanf("%d", &num[i]); 18 sum += num[i]; 19 if (num[i] > best) { best = num[i]; res = i; } 20 } 21 sum -= best; //除去最大后的剩余和 22 memset(dp, 0, sizeof(dp)); 23 for (int i = 1; i <= n; ++i) if (i != res) //0-1背包 24 for (int j = A; j >= num[i]; --j) 25 dp[j] = max(dp[j], dp[j - num[i]] + num[i]); 26 if (sum - dp[A] > C)printf("No\n"); //有帮手C也拿不动 27 else if (sum - dp[A] + best <= C)printf("Yes\n"); //C还可以再拿帮手拿的那个东西 28 else printf("Need a helper\n"); //C拿不动最大的东西,但剩下的可以和A分掉 29 } 30 }
标签:cas line tip acm repr sum without tail present
原文地址:http://www.cnblogs.com/hyp1231/p/6972628.html