初见安~这里是两个传送门:洛谷P5194 & bzoj P1673
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<2^30)时,天平就会被损坏. 砝码按照它们质量的大小被排成一行.并且,这一行中从第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
Sample Output
11
HINT
约翰有3个砝码,质量分别为1,10,20个单位.他的天平最多只能承受质量为15个单位的物体.用质量为1和10的两个砝码可以称出质量为11的牛.这3个砝码所能组成的其他的质量不是比11小就是会压坏天平
Sol
首先,洛谷上这个题是个紫题,我在独立做出来了过后表示这分明就是个绿题……【增强你们解决这个问题的信心!!!】
我们分析一下题意——就是给你一堆东西和一个容器让你做到装的东西重量最大。而bzoj上题目是有疏漏的,2^30硬是打成了230害得我差点写个01背包上去【要不是看得到最后一组最大的数据我就真的以为范围是230了……】所以很明显我们不能用背包来做。不用背包用什么?我们可以很自然地联想到fence8(栅栏)这个题和小猫爬山。是不是感觉很像!!!很好,从大到小排序过后直接上dfs爆搜吧!!!管他的剪枝!!!直接暴力搜!!!【划掉】直接枚举各个物品是选还是不选,在V以内能不能选。
然后最后一组数据就卡爆了。【噗】好吧看来还是要剪枝。
这个题相比栅栏真的是可以说很良心了,因为两步剪枝就可以完美过去了,完全不需要考虑那种看起来很没用实际上作用很大的极端剪枝。怎么减——
1.很明显的,如果你在中途某一次装东西的过程中已经能做到刚好装满了,那么答案就是V了,不要再搜了,加个bool flag标记就可以了。
2.加了上面那个还是会卡,所以这个才是最关键的:【我也没试过去掉上面那个会怎样哈……】如果你在已经得出一次ans过后,在搜到某一步时发现就算你把剩下的全部装进来都不会比之前的ans更优,那你就不要搜了,这个方案搜下去没有意义了。所以我们可以开一个前缀和数组来做到这一步优化。
可能你还是有些问题——明明n<=1000,这么大的范围dfs怎么都会爆啊,为什么剪一下枝就过了?因为题目有一个类似于斐波那契的条件——从第三个开始,每一个的重量都大于前两个的和。要满足这个条件的同时兼顾重量<2^30,n一定是小于43的【亲测】。所以可以放心大胆地搜啦~
下面是代码及详解——
#include<bits/stdc++.h>
#define maxn 1005
#define ll long long
using namespace std;
int n, V, w[maxn];
ll num[maxn];
int read()
{
int x = 0, f = 1, ch = getchar();
while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
while(isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
return x * f;
}
int ans = 0;
bool flag = 0;
void dfs(int now, ll sum)
{
if(flag) return;//剪枝1
if(now > n) {if(sum > ans) ans = sum; return;}//到达尽头,老老实实判断吧
if(sum + (num[n] - num[now - 1]) <= ans) return;//剪枝2
if(sum + w[now] == V) {flag = 1; ans = V; return;}//剪枝1
else if(sum + w[now] < V) dfs(now + 1, sum + w[now]);//能搜就搜
if(flag) return;//得到答案了,也不要搜了
dfs(now + 1, sum);//再试试不装这个物品
}
int main()
{
n = read(), V = read();
for(int i = n; i > 0; i--)//本身是从小到大的顺序,但是我们dfs需要的是从大到小
w[i] = read();
for(int i = 1; i <= n; i++)
num[i] = num[i - 1] + w[i];//前缀和
dfs(0, 0);
printf("%d\n", ans);
return 0;
}
27ms完美通过。
迎评:)
——End——