BZOJ 1673: [Usaco2005 Dec]Scales 天平 启发式搜索

来源:互联网 发布:位面淘宝txt下载 编辑:程序博客网 时间:2024/05/29 16:05

1673: [Usaco2005 Dec]Scales 天平

Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 739 Solved: 271
[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

Silver

题解:
挺水的一道题,但是也挺坑的。。
有一个剪枝就是当把剩下的全部选了仍然凑不到现在的答案时,就可以直接return了。
tm这道题降低了我ac率的两个百分点!!
倒着枚举会比正着枚举优【有点累似于贪心的思想,只要w[i]+tot没有超过范围,那么选i肯定会比选i-1和i-2优,这样也是加大了剪枝的力度。
模拟一下,假设选的其他都一定了,且选w[i]不会超过c,正着枚举会先选i-2,再选i-1,然后无法选i,更新ans,然后不选i-1,不选i,再返回,不选i-2,选i-1,不选i-1,选i-2。而倒着枚举,由于选w[i-2]+w[i-1]< w[i],即剩下的都选了也达不到现在的答案,就会直接return了。

而且先枚举选会比先枚举不选优。【如果先枚举不选,这棵搜索树一定会是巨大的, 而且你几乎每次都可以取到更优答案,所以其实启发式的剪枝都没啥用了。

以及一个小优化:先判断选这个不会超,再选这个。虽然可能和在一开始判断tot是否大于看起来差不多。。。但还是有28ms的差距!

0ms

#include<bits/stdc++.h>#define ll long longusing namespace std;const int N = 50 + 5;int n;ll c,w[N];ll sum[N],ans=0;void dfs(int d,ll tot){    if(d<1) {ans=max(ans,tot);return ;}    if(tot+sum[d]<=ans) return ;    if(tot+w[d]<=c) dfs(d-1,tot+w[d]);    dfs(d-1,tot);}int main(){    scanf("%d%lld",&n,&c);    for(int i=1;i<=n;++i){        scanf("%lld",&w[i]);        if(w[i]>c) n=i-1;        else sum[i]=sum[i-1]+w[i];    }    dfs(n,0);    cout<<ans<<endl;    return 0;}

28ms

#include<bits/stdc++.h>#define ll long longusing namespace std;const int N = 50 + 5;int n;ll c,w[N];ll sum[N],ans=0;void dfs(int d,ll tot){    if(tot>c) return ;    if(d<1) {ans=max(ans,tot);return ;}    if(tot+sum[d]<=ans) return ;    dfs(d-1,tot+w[d]);    dfs(d-1,tot);}int main(){    scanf("%d%lld",&n,&c);    for(int i=1;i<=n;++i){        scanf("%lld",&w[i]);        if(w[i]>c) n=i-1;        else sum[i]=sum[i-1]+w[i];    }    dfs(n,0);    cout<<ans<<endl;    return 0;}

TLE:…

//28 ms/*#include<bits/stdc++.h>#define ll long longusing namespace std;const int N = 50 + 5;int n;ll c,w[N];ll sum[N],ans=0;void dfs(int d,ll tot){    if(tot>c) return ;    if(d<1) {ans=max(ans,tot);return ;}    if(tot+sum[d]<=ans) return ;    dfs(d-1,tot+w[d]);    dfs(d-1,tot);}int main(){    scanf("%d%lld",&n,&c);    for(int i=1;i<=n;++i){        scanf("%lld",&w[i]);        if(w[i]>c) n=i-1;        else sum[i]=sum[i-1]+w[i];    }    dfs(n,0);    cout<<ans<<endl;    return 0;}*/#include<bits/stdc++.h>#define ll long longusing namespace std;const int N = 50 + 5;int n;ll c,w[N];ll sum[N],ans=0;void dfs(int d,ll tot){    if(tot>c) return ;    if(d>n) {ans=max(ans,tot);return ;}    if(tot+sum[n]-sum[d-1]<=ans) return ;    if(tot+w[d]<=c) dfs(d+1,tot+w[d]);    dfs(d+1,tot);}int main(){    scanf("%d%I64d",&n,&c);    for(int i=1;i<=n;++i){        scanf("%I64d",&w[i]);        if(w[i]>c) n=i-1;        sum[i]=sum[i-1]+w[i];    }    dfs(1,0);    cout<<ans<<endl;    return 0;}
阅读全文
0 0