【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.
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.
第2到N+1行:每一行仅包含一个正整数,即某个砝码的质量.保证这些砝码的质量是一个不下降序列
Output
* Line 1: A single integer that is the largest mass that can be accurately and safely measured.
一个正整数,表示用所给的砝码能称出的不压坏天平的最大质量.
Sample Input
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
HINT
约翰有3个砝码,质量分别为1,10,20个单位.他的天平最多只能承受质量为15个单位的物体.用质量为1和10的两个砝码可以称出质量为11的牛.这3个砝码所能组成的其他的质量不是比11小就是会压坏天平
Source
【BZOJ】1673: [Usaco2005 Dec]Scales 天平(dfs背包)的更多相关文章
- BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1673 题意: 有n个砝码(n <= 1000),重量为w[i]. 你要从中选择一些砝 ...
- bzoj 1673: [Usaco2005 Dec]Scales 天平【dfs】
真是神奇 根据斐波那契数列,这个a[i]<=c的最大的i<=45,所以直接搜索即可 #include<iostream> #include<cstdio> usin ...
- bzoj:1673 [Usaco2005 Dec]Scales 天平
Description Farmer John has a balance for weighing the cows. He also has a set of N (1 <= N <= ...
- bzoj1673[Usaco2005 Dec]Scales 天平*
bzoj1673[Usaco2005 Dec]Scales 天平 题意: n个砝码,每个砝码重量大于前两个砝码质量和,天平承重为c,求天平上最多可放多种的砝码.n≤1000,c≤2^30. 题解: 斐 ...
- [Usaco2005 Dec]Scales 天平
题目描述 约翰有一架用来称牛的体重的天平.与之配套的是N(1≤N≤1000)个已知质量的砝码(所有砝码质量的数值都在31位二进制内).每次称牛时,他都把某头奶牛安置在天平的某一边,然后往天平另一边加砝 ...
- BZOJ 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
题目 1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MB Description Farm ...
- bzoj 1625: [Usaco2007 Dec]宝石手镯【背包】
裸的01背包 #include<iostream> #include<cstdio> using namespace std; int c,n,w,v,f[20001]; in ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- BZOJ 1729: [Usaco2005 dec]Cow Patterns 牛的模式匹配
Description 约翰的N(1≤N≤100000)只奶牛中出现了K(1≤K≤25000)只爱惹麻烦的坏蛋.奶牛们按一定的顺序排队的时候,这些坏蛋总会站在一起.为了找出这些坏蛋,约翰让他的奶牛排好 ...
随机推荐
- SpriteKit改变Node锚点其物理对象位置不对的解决
在创建Node的物理对象后,默认情况下物理对象和Node的实际边界相应的非常好,由于此时Node的默认锚点是当中心位置即(0.5,0.5),只是假设我们改变了Node的锚点,就会发现其物理边界还是保持 ...
- Be Happy.——我的ACM退役贴
一个月的忙碌后,最终能静下心来写一些什么. 该结束的最终都要结束了.考试,课程设计,所剩寥寥无几的大学时光,ACM. 看过不少大牛的退役贴,自嘲成银牌狗铜牌狗.写一写碎碎念,大抵如此,每一个人都无法落 ...
- html <br/>引起的”血案“
图片一: 图片二: 图片三: 图片四:
- 一招破解混淆后的JavaScript代码
http://www.cnblogs.com/zjyuan/archive/2011/12/14/2287647.html JavaScript不是很给力,想怎么破解就怎么破解!此文章教你的不仅仅是破 ...
- 使用C#开发ActiveX控件[new]
文章出处:http://www.cnblogs.com/yilin/p/csharp-activex.html 前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以 ...
- ASP.NET MVC之Html.RenderAction(无操作方法 传参数)
WEB窗体模式开发惯了,切入MVC模式,好多东西都不懂,每一步都要查资料. 初步得来的一些知识点体会是: _Layout.cshtml就相当于母版页 然后partical视图(部分视图)就是用户控件. ...
- Python zfill() 方法
描述 Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0. 语法 zfill()方法语法: S.zfill(width) 参数 width -- 指定字符串的长度.原字 ...
- SQL Server2005 两台服务器上的数据库互相同步(转载)
1.1测试环境 Item 发布机 A 订阅机 B OS Windows 2003 Server Windows 2003 Server SQL SQL Server 2005 企业版 SQL Serv ...
- 开发avr单片机网络资源
1.avr用的c语言标准库 http://www.nongnu.org/avr-libc/ 2.avr的下载上传器 http://www.nongnu.org/avrdude/ 3.编程环境platf ...
- Atitit.故障排除系列---NoClassDefFoundError NoClassDefFoundError
Atitit.故障排除系列---NoClassDefFoundError NoClassDefFoundError java.lang.ClassNotFoundException找不到类异常.当应 ...