当前位置:网站首页>洛谷P5194 [USACO05DEC]Scales S 题解
洛谷P5194 [USACO05DEC]Scales S 题解
2022-07-03 14:15:00 【q779】
洛谷P5194 [USACO05DEC]Scales S 题解
题目链接:P5194 [USACO05DEC]Scales S
题意:
约翰有一架用来称牛的体重的天平。与之配套的是 N N N ( 1 ≤ N ≤ 1000 1 \leq N \leq 1000 1≤N≤1000 )个已知质量的砝码(所有砝码质量的数值都在32位带符号整数范围内)。
每次称牛时,他都把某头奶牛安置在天平的某一边,然后往天平另一边加砝码,直到天平平衡,于是此时砝码的总质量就是牛的质量(约翰不能把砝码放到奶牛的那边,因为奶牛不喜欢称体重,每当约翰把砝码放到她的蹄子底下,她就会尝试把砝码踢到约翰脸上)。
天平能承受的物体的质量不是无限的,当天平某一边物体的质量大于 C C C ( 1 ≤ C ≤ 2 30 1 \leq C \leq 2^{30} 1≤C≤230 )时,天平就会被损坏。砝码按照它们质量的大小被排成一行。并且,这一行中从第3个砝码开始,每个砝码的质量至少等于前面两个砝码(也就是质量比它小的砝码中质量最大的两个)的质量的和。
约翰想知道,用他所拥有的这些砝码以及这架天平,能称出的质量最大是多少。由于天平的最大承重能力为 C C C ,他不能把所有砝码都放到天平上。
现在约翰告诉你每个砝码的质量,以及天平能承受的最大质量,你的任务是选出一些砝码,使它们的质量和在不压坏天平的前提下是所有组合中最大的。
显然暴搜+剪枝
我们可以反向搜索,也就是先选大的
不难发现这样更容易产生更大的答案
然后乱搞搞就过了
代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(55)
int sum[N],mx,a[N],n,c;
void dfs(int now,int u)
{
if(u<1)
{
mx=max(mx,now);
return;
}
if(now+sum[u]<=mx) return;
if(now+a[u]<=c)dfs(now+a[u],u-1);
dfs(now,u-1);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n >> c;
for(int i=1; i<=n; i++)
{
cin >> a[i];
if(a[i]>c){
n=i-1;break;}
sum[i]=sum[i-1]+a[i];
}
dfs(0,n);
cout << mx << '\n';
return 0;
}
转载请说明出处
边栏推荐
- Strategy, tactics (and OKR)
- Solution to failure or slow downloading of electron when electron uses electron builder to package
- Zabbix添加Calculated items后保存页面成空白
- 7-4 BCD decryption (10 points)
- Accelerating strategy learning using parallel differentiable simulation
- Mysql多表查询 #子查询
- 7-9 find a small ball with a balance
- Leetcode(4)——寻找两个正序数组的中位数
- Exercise 9-1 time conversion
- 泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
猜你喜欢
[Jilin University] information sharing of postgraduate entrance examination and re examination
编程语言:类型系统的本质
7-8 overspeed judgment
愉悦资本新双币基金近40亿元完成首次关账
Exercise 10-6 recursively find Fabonacci sequence
Programming language: the essence of type system
JS first summary
Jiuyi cloud black free encryption free version source code
Exercise 6-2 using functions to sum special A-string sequences
Exercise 10-1 judge the three digits that meet the conditions
随机推荐
Redis: operation command of string type data
7-6 mixed type data format input
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
Canvas utility library fabric JS user manual
Common mixins
Leetcode(4)——寻找两个正序数组的中位数
Strategy, tactics (and OKR)
Interface for querying IP home
Eight sorts
Exercise 8-8 moving letters
DDK for XP
simpleParallax. JS (create poor visual effects for website pictures)
Exercise 10-8 recursive implementation of sequential output of integers
Raft 协议
Solution to failure or slow downloading of electron when electron uses electron builder to package
Duet date picker (time plug-in that can manually enter the date)
Add ZABBIX calculation type itemcalculated items
The mail function of LNMP environment cannot send mail
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Too many files with unapproved license