【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)只爱惹麻烦的坏蛋.奶牛们按一定的顺序排队的时候,这些坏蛋总会站在一起.为了找出这些坏蛋,约翰让他的奶牛排好 ...
随机推荐
- canvas的api
Canvas API(画布)用于在网页实时生成图像,并且可以操作图像内容,基本上它是一个可以用JavaScript操作的位图(bitmap).使用前,首先需要新建一个canvas网页元素. 1 2 3 ...
- 用sql语句查出和sql相关的性能计数器
一台服务器上,用性能监视器死活显示不出来一部分计数器,没办法,用sql语句查了 --所有和sql相关的计数器 select * from sys.dm_os_performance_counters ...
- windows在与time.windows.com进行同步时出错
windows在与time.windows.com进行同步时出错 CreateTime--2017年6月29日10:28:16Author:Marydon 参考地址:http://www.jb51 ...
- 揭秘ThreadLocal(转)
转载自:掘金大闲人柴毛毛博客. ThreadLocal是开发中最常用的技术之一,也是面试重要的考点.本文将由浅入深,介绍ThreadLocal的使用方式.实现原理.内存泄漏问题以及使用场景. Thre ...
- JVM-01-转载博客
JVM——Java虚拟机架构 地址:http://blog.csdn.net/seu_calvin/article/details/51404589 JVM——内存管理和垃圾回收 地址:http:// ...
- unity模型任意无限切割插件
概述 3d模型的任意切割一直是游戏开发里的一个很大的问题,模型切割的关键点就只有生成横切面的新顶点以及切口纹理的缝合,理论上解决了这两点,就近乎可以做到以假乱真的程度了.本篇文章就这两点进行描述 详细 ...
- spring4.3+mybatis3.4+freemark+log4j2+fastjson整合
2017-7-1 更新 spring 版本 4.3.9 更新mybatis 为3.4.3 0.先写下文件结构防止配置放错地方 1.首先发下maven配置 <properties> < ...
- poj 1236 Network of Schools 【Tarjan】
题目链接:http://poj.org/problem?id=1236 题意: 本题为有向图. 需解决两个问题: 1 须要给多少个点,才干传遍全部点. 2 加多少条边,使得整个图变得强连通. 使用Ta ...
- 使用maven开发OSGI样例
一:创建maven项目,在pom.xml里面增加例如以下依赖 <dependency> <groupId>org.osgi</groupId> <artifa ...
- Winform 关闭Form而不销毁Form的内存
在winform程序中有的时候需要暂时关闭窗口并在需要的时候再次调出原来关闭的这个窗口(即关闭的时候不销毁该窗口的内存)实现方法如下: Form.Designer.cs中有如下方法 /// <s ...