当前位置:网站首页>洛谷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;
}
转载请说明出处
边栏推荐
- Fabric. JS document
- Toast UI editor (editor allows you to edit your markup document using text or WYSIWYG, with syntax highlighting, scrolling synchronization, real-time preview and chart functions.)
- TS code automatically generates JS
- SSH访问控制,多次失败登录即封掉IP,防止暴力破解
- 添加Zabbix计算类型项目Calculated items
- Global event bus
- Find specified characters
- Statistical capital consonants
- Exercise 10-6 recursively find Fabonacci sequence
- Print. JS -- web page file printing
猜你喜欢
Redis: operation command of string type data
Exercise 6-6 use a function to output an integer in reverse order
[Jilin University] information sharing of postgraduate entrance examination and re examination
GRPC的四种数据流以及案例
Generate directories from web content
Interface for querying IP home
泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
Exercise 7-6 count capital consonants
Exercise 9-3 plane vector addition
一文了解微分段应用场景与实现机制
随机推荐
Zabbix添加Calculated items后保存页面成空白
JVM class loading
Leetcode(4)——尋找兩個正序數組的中比特數
7-2 and then what time (15 minutes)
Convert string to decimal integer
Eight sorts
Canvas utility library fabric JS user manual
Why is this error reported when modifying records in the database
JVM runtime data area
7-10 calculate salary
Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
6-9 statistics of single digits (15 points)
js 2023. String pair equal to the target string after connection
Exercise 6-2 using functions to sum special A-string sequences
Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
MySQL multi table query subquery
Leetcode (4) -- find the median of two positively ordered arrays
Doris学习笔记之数据表的创建
全文检索引擎Solr系列—–全文检索基本原理
Find the sum of the elements of each row of the matrix