POJ--3172 Scales (DFS 大容量背包 C++)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3148 | Accepted: 851 |
Description
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
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
Sample Input
3 15
1
10
20
Sample Output
11
Hint
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.
#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++)的更多相关文章
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- POJ 3172 Scales (01背包暴力)
题意:给定 n 个数,保证下一个数比上一个数和前一个数之和大,然后给定一个背包,问你最多放多少容积. 析:应该是很明显的01背包,但是可惜的是,数组开不出来,那就得考虑暴力,因为数不多,才几十而已,要 ...
- poj 3172 Scales 搜索
其实这个题目要是注意到了题目的一点关键性的描述就会变得很简单,题意是给出的砝码是至少是前两个的和的,有了这一点,那么砝码的数量应该就在几十左右,这样的话适当剪枝的搜索是应该可以过的. #include ...
- 【bzoj4182】Shopping 树的点分治+dfs序+背包dp
题目描述 给出一棵 $n$ 个点的树,每个点有物品重量 $w$ .体积 $c$ 和数目 $d$ .要求选出一个连通子图,使得总体积不超过背包容量 $m$ ,且总重量最大.求这个最大总重量. 输入 输入 ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 3181 Dollar Dayz(全然背包+简单高精度加法)
POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...
- POJ 3211 Washing Clothes(01背包)
POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...
- POJ 2184 Cow Exhibition (01背包变形)(或者搜索)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10342 Accepted: 4048 D ...
- POJ 3628 Bookshelf 2【背包型DFS/选or不选】
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11105 Accepted: 4928 Desc ...
随机推荐
- Ajax 学习笔记
什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味 ...
- 安装 CentOS 时, BIOS 设置界面,找不到虚拟镜像
安装 CentOS 时, 遇到 BIOS 设置界面,找不到虚拟镜像 1. 启动电脑或重启电脑,当电脑还没有进入window图标界面,按F2或DEL 2. 左下角有一个 Advanced Mode(F ...
- cvpr2017:RSVP
1.简单介绍 这个框架主要应用场景是更智能的视频监控.主要贡献是利用long term和short term的时序信息来预测当前帧.框架分割的主要对象是人,将图像中的人物分割成头发.脸.大衣.裤子.包 ...
- HttpServlet源码分析
1.HttpServlet的用法 提供了创建Http Servlet的抽象类,通过实现此类定义自己的Servlet 2.HttpServlet是否是线程安全 先说结论:HttpServlet不是线程安 ...
- JavaScript看书笔记01
JavaScript看书笔记... ------------------- JavaScript允许var定义语句出现在函数内部的任意位置.JS中使用Var来显示的给变量声明 JavaScript是一 ...
- mySQl数据库的学习笔记
mySQl数据库的学习笔记... ------------------ Dos命令--先在记事本中写.然后再粘贴到Dos中去 -------------------------------- mySQ ...
- 程序猿表白练级之Hello World
程序猿表白练级之Hello World ... 推荐一个优秀的编程网站:Hello World http://www.he11oworld.com/ The Hello World Collectio ...
- 进入子shell的各种情况分析
子shell的概念贯穿整个shell,写shell脚本时更是不可不知.所谓子shell,即从当前shell环境新开一个shell环境,这个新开的shell环境就称为子shell(subshell),而 ...
- 转载>>>Jpgraph图表
一.开启GD库 Jpgraph需要GD库的支持,所以在调式JpGraph之前,确保GD库已开启,这很重要,不然后面的工作就没办法展开了.GD库在PHP5中是被默认安装的,我们只需开启GD库就可以了. ...
- python中的collections.namedtuple
简介 collections.namedtuple是一个工厂方法,它可以动态的创建一个继承tuple的子类.跟tuple相比,返回的子类可以使用名称来访问元素. 使用方法 用一个例子来介绍: > ...