1673: [Usaco2005 Dec]Scales 天平 Time Limit: 5 Sec Memory Limit: 64 MB
[ Submit][ Status][ Discuss]
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. 約翰有一架用來稱牛的體重的天平.與之配套的是N(1≤N≤1000)個已知品質的砝碼(所有砝碼品質的數值都在31位二進位內).每次稱牛時,他都把某頭奶牛安置在天平的某一邊,然後往天平另一邊加砝碼,直到天平平衡,於是此時砝碼的總品質就是牛的品質(約翰不能把砝碼放到奶牛的那邊,因為奶牛不喜歡稱體重,每當約翰把砝碼放到她的蹄子底下,她就會嘗試把砝碼踢到約翰臉上).天平能承受的物體的品質不是無限的,當天平某一邊物體的品質大於C(1≤C<230)時,天平就會被損壞. 砝碼按照它們品質的大小被排成一行.並且,這一行中從第3個砝碼開始,每個砝碼的品質至少等於前面兩個砝碼(也就是品質比它小的砝碼中品質最大的兩個)的品質的和. 約翰想知道,用他所擁有的這些砝碼以及這架天平,能稱出的品質最大是多少.由於天平的最大承重能力為C.他不能把所有砝碼都放到天平上. 現在約翰告訴你每個砝碼的品質,以及天平能承受的最大品質.你的任務是選出一些砝碼, 使它們的品質和在不壓壞天平的前提下是所有組合中最大的. 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. 第1行:兩個用空格隔開的正整數N和C.
第2到N+1行:每一行僅包含一個正整數,即某個砝碼的品質.保證這些砝碼的品質是一個不下降序列 Output
* Line 1: A single integer that is the largest mass that can be accurately and safely measured.
一個正整數,表示用所給的砝碼能稱出的不壓壞天平的最大品質. Sample Input 3 15// 三個物品,你的"包包"體積為15,下面再給出三個數字,從第三個數字開始,它都大於前面的二個數字之和,這個條件太重要
1
10
20
INPUT DETAILS:
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.
Sample Output 11 HINT
約翰有3個砝碼,品質分別為1,10,20個單位.他的天平最多隻能承受品質為15個單位的物體.用品質為1和10的兩個砝碼可以稱出品質為11的牛.這3個砝碼所能組成的其他的品質不是比11小就是會壓壞天平 Source 暴搜就行了
#include<bits/stdc++.h>using namespace std;int n,c,a[1005],ans;long long sum[1005];void dfs( int x, int s ){if( sum[x] + s <= ans ) return ;ans = max( s, ans );for( int i = x; i >= 1; i-- )if( s + a[i] <= c )dfs( i-1, s + a[i] );}int main(){scanf("%d%d", &n, &c);for( int i = 1; i <= n; i++ ) scanf("%d", &a[i]);while( a[n] > c ) n--;for( int i = 1; i <= n; i++ ) sum[i] = sum[i-1] + a[i];dfs( n, 0 ); printf("%d\n", ans);return 0;}/*3 1511020*/