Scales
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2591 | Accepted: 679 |
Description
Farmer John has a balance for weighing the cows. He also has a set of N (1 <= N <= 1000) weights with known masses (all of which fit in 31 bits) for use on one side of the balance. He places a cow on one side of the balance and then adds weights to the other side until they balance. (FJ cannot put weights on the same side of the balance as the cow, because cows tend to kick weights in his face whenever they can.) The balance has a maximum mass rating and will break if FJ uses more than a certain total mass C (1 <= C < 2^30) on one side.
The weights have the curious property that when lined up from smallest to biggest, each weight (from the third one on) has at least as much mass as the previous two combined.
FJ wants to determine the maximum mass that he can use his weights to measure exactly. Since the total mass must be no larger than C, he might not be able to put all the weights onto the scale.
Write a program that, given a list of weights and the maximum mass the balance can take, will determine the maximum legal mass that he can weigh exactly.
The weights have the curious property that when lined up from smallest to biggest, each weight (from the third one on) has at least as much mass as the previous two combined.
FJ wants to determine the maximum mass that he can use his weights to measure exactly. Since the total mass must be no larger than C, he might not be able to put all the weights onto the scale.
Write a program that, given a list of weights and the maximum mass the balance can take, will determine the maximum legal mass that he can weigh exactly.
Input
Line 1: Two space-separated positive integers, N and C.
Lines 2..N+1: Each line contains a single positive integer that is the mass of one weight. The masses are guaranteed to be in non-decreasing order.
Lines 2..N+1: Each line contains a single positive integer that is the mass of one weight. The masses are guaranteed to be in non-decreasing order.
Output
Line 1: A single integer that is the largest mass that can be accurately and safely measured.
Sample Input
3 15 1 10 20
Sample Output
11
Hint
Explanation of the sample:
FJ has 3 weights, with masses of 1, 10, and 20 units. He can put at most 15 units on one side of his balance.
The 1 and 10 weights are used to measure 11. Any greater weight that can be formed from the weights will break the balance.
FJ has 3 weights, with masses of 1, 10, and 20 units. He can put at most 15 units on one side of his balance.
The 1 and 10 weights are used to measure 11. Any greater weight that can be formed from the weights will break the balance.
Source
dfs剪枝搜索,搜索的时候判断一下下一步的结果是否比当前的最大值大,否则会超时。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[1010];
ll b[1010];
ll n, c;
ll maxn = 0, ans = 0;
void dfs(int p, ll s){
if(s > maxn) maxn = s;
if(p < 0) {return;}
for(int i = p; i >= 0; --i){
if(s + a[i] <= c){
if(s + b[i] > maxn)
dfs(i - 1, s + a[i]);
}
}
}
int main(){
scanf("%I64d%I64d", &n, &c);
for(int i = 0; i < n; i++){
scanf("%I64d", &a[i]);
}
b[0] = a[0];
for(int i = 1; i < n; i++)
b[i] = b[i - 1] + a[i];
dfs(n - 1, 0);
printf("%I64d\n", maxn);
}
本文介绍了一个关于使用一组特定重量来精确测量最大可能质量的问题。农民John有一组重量,并且这些重量具有特殊的递增属性,同时他的秤有承载限制。文章讨论了如何通过编程算法,在不超过秤承载能力的情况下找到能被精确测量的最大质量。
2万+

被折叠的 条评论
为什么被折叠?



