bzoj1673:天平

1673: [Usaco2005 Dec]Scales 天平

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 391  Solved: 157
[ Submit][ Status][ Discuss]

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个砝码,质量分别为11020个单位.他的天平最多只能承受质量为15个单位的物体.用质量为110的两个砝码可以称出质量为11的牛.这3个砝码所能组成的其他的质量不是比11小就是会压坏天平

Source

Silver

这道题看到了以为是背包dp;然后突然发现数据那么大Orz就跪了,遂找题解。。。原来还可以这样子搞啊。这怎么想到的呢。反正记住就好了。听说是有技巧的背包。。

------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
int n,m;
ll ans=-1;
const int nmax=1010;
int a[nmax];
ll sum[nmax];
void dfs(int cur,ll count){
 if(count>m) return;
 if(sum[cur-1]+count<=m){
  ans=max(ans,sum[cur-1]+count);
  return ;
 }
 ans=max(ans,count);
 for(int i=1;i<cur;i++){
  count+=a[i];
  dfs(i,count);
  count-=a[i];
 }
}
int main(){
 scanf("%d%d",&n,&m);
 for(int i=1;i<=n;i++){
  scanf("%d",&a[i]);
  sum[i]=sum[i-1]+a[i];
 }
 dfs(n+1,0);
 printf("%d\n",ans);
 return 0;
}

----------------------------------------------------------------------------------

 

 

转载于:https://www.cnblogs.com/20003238wzc--/p/4827193.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、据库操作、用户权限管理等关键技术。 据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的据库设计文件。这些文件通常包括据库结构图、据表设计文档,以及示例据SQL脚本。用户可以通过这些文件快速搭建项目所需的据库环境,并了解各个据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值