bzoj1673: [Usaco2005 Dec]Scales 天平
来源:互联网 发布:it管培生 编辑:程序博客网 时间:2024/05/19 10:10
1673: [Usaco2005 Dec]Scales 天平
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 407 Solved: 162
[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.
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
Silver
题解:第一眼 看过去是dp,然后确实也是dp,但巧妙地应用了dfs,好思路,赞一个。
代码:
<span style="font-size:18px;">#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#define ll long longusing namespace std;int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}int n,m,a[1010];ll ans=-1,sum[1010];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(){ n=read();m=read(); for(int i=1;i<=n;i++)a[i]=read(),sum[i]=sum[i-1]+a[i]; dfs(n+1,0); cout<<ans<<endl;}</span>
- bzoj1673: [Usaco2005 Dec]Scales 天平
- 【bzoj1673】[Usaco2005 Dec]Scales 天平
- [bzoj1673][Usaco2005 Dec]Scales 天平
- BZOJ1673: [Usaco2005 Dec] Scales 天平 【DFS】
- 1673: [Usaco2005 Dec]Scales 天平
- 1673: [Usaco2005 Dec]Scales 天平
- bzoj 1673: [Usaco2005 Dec]Scales 天平
- bzoj 1673: [Usaco2005 Dec]Scales 天平(DFS)
- BZOJ 1673: [Usaco2005 Dec]Scales 天平 启发式搜索
- BZOJ 1673 USACO 2005 Dec Scales 天平
- bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
- bzoj 1731: [Usaco2005 dec]Layout 排队布局
- bzoj1671: [Usaco2005 Dec]Knights of Ni 骑士
- bzoj1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
- bzoj1731/poj3169[Usaco2005 dec]Layout 排队布局
- 1671: [Usaco2005 Dec]Knights of Ni 骑士
- BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
- 【bzoj1731】[Usaco2005 dec]Layout 排队布局
- LeetCode---206reverse-linked-list
- Linux战地日记—系统管理
- Leetcode118: Longest Palindromic Substring
- UICollectionViewFlowLayout has cached frame mismatch for index path
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- bzoj1673: [Usaco2005 Dec]Scales 天平
- android layout的一些注意事项
- 为什么要用new来建立一个对象
- C++ Primer Week1
- [ARM7--LPC2478]Power及Clock
- Python常见异常问题
- SDWebImage 涉及知识点位移枚举的操作解析
- 物理地址通过什么协议转换为ip地址
- 匈牙利算法示例