bzoj:1673: [Usaco2005 Dec]Scales 天平

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小就是会压坏天平

必须吐槽,传说中所有砝码质量的数值都在31位二进制内,传说中从第三个数字开始,它都大于前面的二个数字之和,那么数据如果要构造得出来,n最多只有30几……剩下的就一阵深搜就好咯,只要深搜不写渣,分分钟0MS……

#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;

ll n,a[40],c,ans=0,q[40];
void dfs(ll p,ll sum){
    if (sum+q[p]<=ans) return;
    if (ans<sum) ans=sum;
    for (ll i=p;i;i--){
        if (sum+a[i]<=c) dfs(i-1,sum+a[i]);
    }
}
int main(){
    scanf("%lld%lld",&n,&c);
    for (ll i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        if (a[i]>c) n=i-1;else q[i]=q[i-1]+a[i];
    }
    dfs(n,0);
    printf("%lld\n",ans);
}
时间: 2024-09-29 20:31:17

bzoj:1673: [Usaco2005 Dec]Scales 天平的相关文章

bzoj1673[Usaco2005 Dec]Scales 天平*

bzoj1673[Usaco2005 Dec]Scales 天平 题意: n个砝码,每个砝码重量大于前两个砝码质量和,天平承重为c,求天平上最多可放多种的砝码.n≤1000,c≤2^30. 题解: 斐波那契数列到30多项就爆int了,所以本题n其实≤30.故爆搜即可,加个剪枝:当前选的砝码质量和+剩下砝码质量和仍≤ans就返回. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #

BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚

题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require their barn to be immaculate. Farmer John, the most

BZOJ 1729: [Usaco2005 dec]Cow Patterns 牛的模式匹配

Description 约翰的N(1≤N≤100000)只奶牛中出现了K(1≤K≤25000)只爱惹麻烦的坏蛋.奶牛们按一定的顺序排队的时候,这些坏蛋总会站在一起.为了找出这些坏蛋,约翰让他的奶牛排好队进入牛棚,同时需要你的慧眼来识别坏蛋,为了区分,约翰给所有奶牛都发了号牌,上面写着一个1..S(1≤S≤25)之间的数字.虽然这不是一个完美的方法,但也能起一点作用.现在,约翰已经不记得坏蛋们的具体号码.但是凭他的记忆,他给出一个"模式串".原坏蛋的号码如果相同,模式串中他们的号码依然相

bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】

bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通行的区域 2:贝茜现在所在的位置 3:骑士们的位置 4:长着贝茜需要的灌木的土地 */ #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace s

bzoj1730 [Usaco2005 dec]Barn Expansion 牛棚扩张

1730: [Usaco2005 dec]Barn Expansion 牛棚扩张 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 134  Solved: 72[Submit][Status][Discuss] Description Farmer John has N (1 <= N <= 25,000) rectangular barns on his farm, all with sides parallel to the X and Y ax

POJ3170 Bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士

1671: [Usaco2005 Dec]Knights of Ni 骑士 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 281  Solved: 180[Submit][Status][Discuss] Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarde

BZOJ1671: [Usaco2005 Dec]Knights of Ni

1671: [Usaco2005 Dec]Knights of Ni Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 175  Solved: 107[Submit][Status][Discuss] Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded b

[BZOJ1672][Usaco2005 Dec]Cleaning Shifts 清理牛棚

1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 953  Solved: 407 [Submit][Status][Discuss] Description Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now require the

【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为了能安全地离开,贝茜不得不按照骑士们的要求,在森林寻找一种特殊的灌木并带一棵给他们.当然,贝茜想早点离开这可怕的森林,于是她必须尽快完成骑士们给的任务,贝茜随身带着这片森林的地图,地图上的森林被放入了直角坐标系,并按x,y轴上的单位长度划分成了W×H(1≤W,H≤1000)块,贝茜在地图上查出了她自己