Scales
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3148   Accepted: 851

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.

Output

Line 1: A single integer that is the largest mass that can be accurately and safely measured.

Sample Input

3 15
1
10
20

Sample Output

11

Hint

Explanation of the sample:

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.

The 1 and 10 weights are used to measure 11. Any greater weight that can be formed from the weights will break the balance.

 
 
 
这道题主要是数据巨大,数组开不开原先01套路不能用!
巨大的剪枝DFS
留做参考,立个以后不错这个题的flag;
#include<iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
int a[1005];
ll sum[1005];
ll ans,m,n;
void dfs(ll x, ll cnt)
{
if(cnt>ans)
ans=cnt;
if(x<1)
return ;
for(int i =x;i>=1;i--)
{
if(cnt+sum[i]<=ans)
continue;
if(cnt+a[i]>m)
continue;
dfs(i-1,cnt+a[i]);
}
}
int main()
{ while(~scanf("%lld%lld",&n,&m))
{
ans=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
dfs(n,0);
printf("%lld",ans);
}
}

  

POJ--3172 Scales (DFS 大容量背包 C++)的更多相关文章

  1. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  2. POJ 3172 Scales (01背包暴力)

    题意:给定 n 个数,保证下一个数比上一个数和前一个数之和大,然后给定一个背包,问你最多放多少容积. 析:应该是很明显的01背包,但是可惜的是,数组开不出来,那就得考虑暴力,因为数不多,才几十而已,要 ...

  3. poj 3172 Scales 搜索

    其实这个题目要是注意到了题目的一点关键性的描述就会变得很简单,题意是给出的砝码是至少是前两个的和的,有了这一点,那么砝码的数量应该就在几十左右,这样的话适当剪枝的搜索是应该可以过的. #include ...

  4. 【bzoj4182】Shopping 树的点分治+dfs序+背包dp

    题目描述 给出一棵 $n$ 个点的树,每个点有物品重量 $w$ .体积 $c$ 和数目 $d$ .要求选出一个连通子图,使得总体积不超过背包容量 $m$ ,且总重量最大.求这个最大总重量. 输入 输入 ...

  5. POJ 1745 【0/1 背包】

    题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  6. POJ 3181 Dollar Dayz(全然背包+简单高精度加法)

    POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...

  7. POJ 3211 Washing Clothes(01背包)

    POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...

  8. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

  9. POJ 3628 Bookshelf 2【背包型DFS/选or不选】

    Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11105   Accepted: 4928 Desc ...

随机推荐

  1. CentOS 7 服务器配置--安装Mysql

    #获取mysql的rpm文件(rpm文件地址可以通过官网获取) wget -r -np -nd https://dev.mysql.com/get/mysql57-community-release- ...

  2. 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(四)example代码解析

    一.example逻辑伪码 myRTSPClient附带3个example程序:simple_example.complete_example.common_example.后两个example都是从 ...

  3. C#高级编程:泛型优点和特性

    泛型是CLR 2.0的一个新特性,在CLR 1.0中,要创建一个灵活的类或方法,但该类或方法在编译期间不知道使用什么类,就得以Object类为基础.而Object在编译期间没有类型安全性,因此必须进行 ...

  4. python-ansible api2.0 多进程执行不同的playbook

    自动化运维工具:ansible 多进程调用ansible api的应用场景:   应用系统检查 一个应用系统可能具有20—50台服务器的集群,初步的系统层面检查可以用一个统一的playbook来检查, ...

  5. ueditor单独调用上传附件和图片的功能

    javascript富文本编辑器使我们添加.编辑网站中的文章更加方便和容易.这些富文本编辑器提供了所见即所得(What You See Is What You Get - WYSIWYG)的功能,可以 ...

  6. nginx cpu高排查

    首先查看nginx的error日志,无异常打印. cpu占用如下图所示: top - 10:05:40 up 233 days, 16:28, 4 users, load average: 25.53 ...

  7. C# 逆变与协变

    该文章中使用了较多的 委托delegate和Lambda表达式,如果你并不熟悉这些,请查看我的文章<委托与匿名委托>.<匿名委托与Lambda表达式>以便帮你建立完整的知识体系 ...

  8. sql primary key 约束

    PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主键. SQL PRIMARY KEY ...

  9. java创建泛型数组

    java中创建泛型数组并不是不可能,创建泛型数组通过反射,给构造函数传递两个参数,一个类型标记,一个数组大小.' 简单Demo如下: import java.lang.reflect.Array; / ...

  10. Mybatis --- 映射文件、参数处理、参数值的获取、select元素

    映射文件:指导着MyBatis如何进行数据库增删改查, 有着非常重要的意义:   - cache   命名空间的二级缓存配置 - cache-ref   其他命名空间缓存配置的引用. - result ...