P9234 [蓝桥杯 2023 省 A] 买瓜

题目链接:https://www.luogu.com.cn/problem/P9234

题目思路:
    每个瓜有三种选择,不要/劈开/要整个,显然整个决策做下来是树状的,于是我们考虑用dfs,每次能拿到相对重量的瓜时就统计劈开数量。如果直接写不优化,恭喜你喜提20分,于是我们考虑剪枝。
    最容易想到,当先择了的西瓜总量大于需要时可以剪掉。先给a排个序然后从大到小搜似乎也是个优化选项,恭喜您离100分又接近了一些。我们每次把当前劈开瓜的次数也记录下来,当当前次数大于等于之前搜出过的最小值,也剪掉。现在提交喜提52分,那么如何继续优化呢?当前选择的西瓜总量+剩下的西瓜总量小于所需时,剪掉,我们使用前缀和记录剩下的西瓜总量。注意存在买不到西瓜的可能。

#include<bits/stdc++.h>

using namespace std;

using ll = long long;
const ll maxn = 1e18;
const int MOD = 998244353;
ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}
int n, m;
ll a[35];
int ans = INT_MAX;
ll pre[35];

void dfs(int pos, ll sum, int t) {//当前层数,剩下的瓜总量,当前劈开次数
	if (sum == m) {
		ans = min(ans, t);
		return;
	}
	if (m > sum + pre[pos] || sum > m || t >= ans) { return; }

	if (pos < 1) return;

	dfs(pos - 1, sum + a[pos], t);
	dfs(pos - 1, sum + a[pos] / 2, t + 1);
	dfs(pos - 1, sum, t);
}

void solve() {
	cin >> n >> m;
	m *= 2;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] *= 2;
	}

	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; i++) {
		pre[i] += pre[i - 1] + a[i];
	}
	dfs(n, 0, 0);
	if (ans == INT_MAX)
		cout << -1 << '\n';
	else
		cout << ans << '\n';
}

signed main() {

	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);

	int t1 = 1;
	//cin >> t1;
	while (t1--)
		solve();

	return 0;
}

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
蓝桥杯2023年B组C++赛的题目包括了2个填空题和8个编程题。根据参赛者的博客\[1\],本届蓝桥杯的难度相较于上届有所增加,编程题的难度较大,而思维题的数量较少。参赛者在博客中提到了其中一个题目,即试题A:日期统计\[2\]。在博客中还提到了一种解题方法,即暴力枚举\[3\]。暴力枚举的代码如下: ```cpp #include <iostream> #include <cmath> #include <algorithm> using namespace std; const int total = 23333333; const double H = 11625907.5798; int main() { for (int i = 0; i < total / 2; i++) { double ans = 0; ans -= 1.0 * i * i / total * log2(1.0 * i / total); ans -= 1.0 * (total - i) * (total - i) / total * log2(1.0 * (total - i) / total); if (abs(ans - H) < 1e-4) { cout << i << endl; return 0; } } return 0; } ``` 以上是关于蓝桥杯2023年B组C++赛的一些信息。希望对你有所帮助! #### 引用[.reference_title] - *1* *3* [2023年第十四届蓝桥杯C++B组复盘](https://blog.csdn.net/m0_46326495/article/details/130043563)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [2023第十四届蓝桥杯C++B组题目回顾与参赛感想](https://blog.csdn.net/BinBinCome/article/details/130048888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值