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 ...
随机推荐
- Tab选框
<html>代码 基本架构:一个大的div下面2个ul,ul下面各三个li <div class="big"> <ul class="con ...
- 关于JAVASCRIPT call 方法和 apply 方法性能对比
JavaScript 关于call 方法和 apply 方法常用形式 call obj.call(object, args , ....); apply obj.apply(object, [args ...
- 轻量级ORM框架 QX_Frame.Bantina(二、框架使用方式介绍)
轻量级ORM框架QX_Frame.Bantina系列讲解(开源) 一.框架简介 http://www.cnblogs.com/qixiaoyizhan/p/7417467.html 二.框架使用方式介 ...
- 第5天:js-系统时间对象
今天听了一小节,js中如何获取系统时间.最后做了一个轮播图效果,是在别人的代码上修改的,一开始做了好半天,都没做好,最后,在我的坚持下,终于做出来了.今天先到这儿,去休息了! 获取时间对象:new D ...
- Linux下自动备份MySQL数据库并上传到远程FTP服务器
Linux下自动备份MySQL数据库并上传到远程FTP服务器且删除指定日期前的备份Shell脚本 说明: 1.备份MySQL数据库存放目录/var/lib/mysql下面的xshelldata数据库 ...
- 简单table分页
html <table id="pagingTable"> <thead> <tr> <td> 第一列 </td> &l ...
- 【Tomcat】shell 部署配置 war包
使用shell 一次执行,将项目中的war包的配置全部修改 #!/bin/bash #----------------------------------------------- # FileNam ...
- EclipseIDE--使用整理
EclipseIDE--使用整理.. ---------------- /--------------------习惯上的字体使用,华文中宋GeorgiaCambria华文中宋/----------- ...
- Windows权限提升基础知识和命令
介绍 这篇文章是介绍window的权限提升,虽然不是一个全面的指南,但会试图覆盖主要的技术,常用的资源列表在文章底部,可供大家参考. window权限提升基础知识 初始信息收集 在开始提权之前,我们需 ...
- 用最简单的方法判断JavaScript中this的指向
目录 * 一个特例 * 开始判断 * 法则一:对象方法中的this指向对象本身(箭头函数形式的除外) * 法则二:多层嵌套函数中的this指向等同于包含该this的最近一个function的this ...