当前位置: 首页 > news >正文

【BZOJ】1673: [Usaco2005 Dec]Scales 天平(dfs背包)

http://www.lydsy.com/JudgeOnline/problem.php?id=1673

bzoj翻译过来的c<=230不忍吐槽。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

这题很奇葩。。

因为这些数像fib数一样递增,所以n<=45。。。。。。。。。。。。。。。。。。。。。。

。。。

dfs背包即可。。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=1005;
int n, m, ans=-1;
long long a[N], sum[N];
void dfs(int x, long long tot) {
	if(tot>m) return;
	if(sum[x-1]+tot<=m) {
		ans=max(ans, sum[x-1]+tot);
		return;
	}
	ans=max(ans, tot);
	for1(i, 1, x-1) {
		tot+=a[i];
		dfs(i, tot);
		tot-=a[i];
	}
}
int main() {
	read(n); read(m);
	for1(i, 1, n) read(a[i]), sum[i]=sum[i-1]+a[i];
	dfs(n+1, 0);
	printf("%d", ans);
	return 0;
}

 

 


 

 

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<230)时,天平就会被损 坏.    砝码按照它们质量的大小被排成一行.并且,这一行中从第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

INPUT DETAILS:

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.

Sample Output

11

HINT

   约翰有3个砝码,质量分别为1,10,20个单位.他的天平最多只能承受质量为15个单位的物体.用质量为1和10的两个砝码可以称出质量为11的牛.这3个砝码所能组成的其他的质量不是比11小就是会压坏天平

Source

Silver

相关文章:

  • BM和KMP字符串匹配算法学习
  • MySql数据库3【优化1】表的优化
  • PHP学习路线图
  • JavaScript Cookie
  • 大数据时代,统计学方法有多大的效果?
  • 第三章:推荐系统冷启动与CB
  • 再学 GDI+[29]: TGPPen - 自定义复合画笔 - SetCompoundArray
  • WinAPI: PolyBezierTo - 绘制贝塞尔线(更新当前位置)
  • Delphi 与 DirectX 之 DelphiX(44): TDIB.DoAddColorNoise();
  • MVC与MVP(转)
  • IDisposable资源释放接口
  • 多角度看.NET面试题
  • java/.net-常用工具下载地址常用学习网址快捷键
  • 財哥面京东dm的经历【帮財哥发的】
  • 基于数据访问的集合类型-领域驱动设计的又一种特定对象
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • 【刷算法】从上往下打印二叉树
  • 〔开发系列〕一次关于小程序开发的深度总结
  • Java多线程(4):使用线程池执行定时任务
  • Java反射-动态类加载和重新加载
  • java概述
  • jdbc就是这么简单
  • js
  • Making An Indicator With Pure CSS
  • PHP面试之三:MySQL数据库
  • Python打包系统简单入门
  • Redis 中的布隆过滤器
  • 初识MongoDB分片
  • 订阅Forge Viewer所有的事件
  • 回顾 Swift 多平台移植进度 #2
  • 检测对象或数组
  • 聚簇索引和非聚簇索引
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 前端技术周刊 2019-01-14:客户端存储
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 一些关于Rust在2019年的思考
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • 数据库巡检项
  • ​什么是bug?bug的源头在哪里?
  • #QT(智能家居界面-界面切换)
  • #Ubuntu(修改root信息)
  • (13):Silverlight 2 数据与通信之WebRequest
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (14)Hive调优——合并小文件
  • (42)STM32——LCD显示屏实验笔记
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (十一)JAVA springboot ssm b2b2c多用户商城系统源码:服务网关Zuul高级篇
  • (循环依赖问题)学习spring的第九天