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. SQl语句收藏(转)

    /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...

  2. 读Zepto源码之Ajax模块

    Ajax 模块也是经常会用到的模块,Ajax 模块中包含了 jsonp 的现实,和 XMLHttpRequest 的封装. 读 Zepto 源码系列文章已经放到了github上,欢迎star: rea ...

  3. CNAME关联githubPage域名及中文域名,创建个人网站

    对于前端开发来说,部署一个自己的个人网站部署服务器等比较麻烦,如果只是做静态页面的展示GitHubPage完全够用,而且有300M免费的空间,完全满足需求. 首先你要有GitHubPage项目,具体怎 ...

  4. Ubuntu 发行版的 Linux 操作系统

    Ubuntu 发行版的 Linux 操作系统.. ------------------------------------- ------------------------------------- ...

  5. 阿里云服务器(Windows)如何下载文件

    背景:公司只有我一个技术,在我之前还有一个老技术,属于兼职状态,为了尽快熟悉公司网站及app项目情况,我联系了老技术,请他尽快将代码发给我,他说代码文件过大,问我能不能连上服务器下载.百度了很多,都不 ...

  6. mysql事件机制——定时任务

    定时任务是老生常谈了,因为我们总是需要定时修改特定的数据. 实现它的方法肯定不止一种,但我在相当长一段时间里都是用程序编码去做的,今天突然想到“为什么一定要采用调用的方式?”,用数据库自身的能力去实现 ...

  7. spring MVC处理请求过程及配置详解

    本文主要梳理下Spring MVC处理http请求的过程,以及配置servlet及业务application需要的常用标签,及其包含的意义. spring MVC处理请求过程 首先看一个整体图 简单说 ...

  8. 【Linux部署 · GIT】在linux系统安装git和配置实现SSH

    领导给了一个不开放ftp的测试库,让我部署项目.拿到一个全新的环境,真是个练手的好机会. 该操作系统为:CentOs release 6.5(Final) 由于不开放ftp,所以上传下载代码是非常麻烦 ...

  9. Ext.grid.EditorGridPanel联动设置单元格是否可以编辑和背景色

    listeners:{ "beforeedit":function(iEventobj){ var col = iEventobj.row;//获取行 var record = i ...

  10. postman 第4节 切换环境和设置读取变量(转)

    postman提供了environment管理功能,想要在多个环境中测试,比如在测试环境.灰度环境.生产环境等,只需要用同样的接口,切换下环境即可,非常方便.具体步骤: 切换环境 1.点击界面右上角的 ...