【BZOJ】1673: [Usaco2005 Dec]Scales 天平(dfs背包)
http://www.lydsy.com/JudgeOnline/problem.php?id=1673
bzoj翻译过来的c<=230不忍吐槽。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
这题很奇葩。。
因为这些数像fib数一样递增,所以n<=45。。。。。。。。。。。。。。。。。。。。。。
。。。
dfs背包即可。。。
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; } inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } inline const int max(const int &a, const int &b) { return a>b?a:b; } inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005; int n, m, ans=-1; long long a[N], sum[N]; void dfs(int x, long long tot) { if(tot>m) return; if(sum[x-1]+tot<=m) { ans=max(ans, sum[x-1]+tot); return; } ans=max(ans, tot); for1(i, 1, x-1) { tot+=a[i]; dfs(i, tot); tot-=a[i]; } } int main() { read(n); read(m); for1(i, 1, n) read(a[i]), sum[i]=sum[i-1]+a[i]; dfs(n+1, 0); printf("%d", ans); return 0; }
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.
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.
第2到N+1行:每一行仅包含一个正整数,即某个砝码的质量.保证这些砝码的质量是一个不下降序列
Output
* Line 1: A single integer that is the largest mass that can be accurately and safely measured.
一个正整数,表示用所给的砝码能称出的不压坏天平的最大质量.
Sample Input
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
HINT
约翰有3个砝码,质量分别为1,10,20个单位.他的天平最多只能承受质量为15个单位的物体.用质量为1和10的两个砝码可以称出质量为11的牛.这3个砝码所能组成的其他的质量不是比11小就是会压坏天平