CCF201809-4 再卖菜 记忆化搜索

本文探讨了使用C++进行复杂序列化操作的方法,包括如何定义和使用自定义的数据结构,以及如何通过深度优先搜索(DFS)算法解决特定问题。通过对算法的详细解释和代码实现,展示了如何有效地处理数据序列化和算法优化。
/*
					_ooOoo_
					o8888888o
					88" . "88
					(| -_- |)
					O\  =  /O
				____/`---'\____
				.'  \\|     |//  `.
			/  \\|||  :  |||//  \
			/  _||||| -:- |||||-  \
			|   | \\\  -  /// |   |
			| \_|  ''\---/''  |   |
			\  .-\__  `-`  ___/-. /
			___`. .'  /--.--\  `. . __
		."" '<  `.___\_<|>_/___.'  >'"".
		| | :  `- \`.;`\ _ /`;.`/ - ` : | |
		\  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
					`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
			佛祖保佑       永无BUG
*/
#include <bits/stdc++.h>
#define MAX 305
using namespace std;

int n, a[MAX], b[MAX];
int m[MAX][MAX][MAX];
int flag;

void init()
{
	flag = 0;
	memset(m, 0, sizeof(m));
}

void print()
{
	for (int i = 1; i <= n; i++) {
		if (i != 1) cout << " ";
		cout << b[i];
	}
	cout << endl;
}

void dfs(int index)
{
	if (m[index][b[index - 1]][b[index - 2]]) return;
	print();
	m[index][b[index - 1]][b[index - 2]] = 1;

	if (index == n) {
		int x = 3 * a[index - 1] - b[index - 1] - b[index - 2];
		for (int i = 0; i < 3; i++) {
			if (x + i <= 0) continue;
			b[n] = x + i;
			if ((b[n] + b[n - 1] + b[n - 2]) / 3 == a[n - 1]) {
				if ((b[n] + b[n - 1]) / 2 == a[n]) {
					print();
					exit(0);
				}
			}
		}
		return;
	}

	int x = 3 * a[index - 1] - b[index - 1] - b[index - 2];
	
	for (int i = 0; i < 3; i++) {
		if (x + i <= 0) continue;
		b[index] = x + i;
		if ((b[index] + b[index - 1] + b[index - 2]) / 3 == a[index - 1]) dfs(index + 1);
	}
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= 2 * a[1]; i++) {
		b[1] = i;
		b[2] = 2 * a[1] - b[1];
		if ((b[1] + b[2]) / 2 == a[1]) dfs(3);
		b[2] = 2 * a[1] - b[1] + 1;
		if ((b[1] + b[2]) / 2 == a[1]) dfs(3);
	}
	return 0;
}

 

### CCF CSP 2018年9月 Python真题及答案解析 #### 题目概述 CCF CSP 2018年9月的第一题涉及卖菜问题,第二题则围绕买菜展开。这两道题目均考察了基本的数据处理能力和逻辑思维能力。对于初学者来说,理解输入输出的要求以及边界条件尤为重要[^2]。 #### 卖菜 (201809-1) ##### 描述 给定一个长度为 n 的整数数组 a 和两个参数 p 和 q,计算满足以下条件的子区间数量: - 子区间的最小值大于等于 p; - 子区间的最大值小于等于 q。 ##### 解析 该问题可以通过滑动窗口算法高效解决。具体而言,维护当前窗口内的最小值和最大值,并动态调整窗口大小以统计符合条件的子区间数目。以下是基于此思路的一个实现: ```python def count_subarrays(n, p, q, array): from collections import deque min_deque = deque() max_deque = deque() result = 0 left = 0 for right in range(n): while min_deque and array[right] < array[min_deque[-1]]: min_deque.pop() min_deque.append(right) while max_deque and array[right] > array[max_deque[-1]]: max_deque.pop() max_deque.append(right) while array[min_deque[0]] < p or array[max_deque[0]] > q: if min_deque[0] == left: min_deque.popleft() if max_deque[0] == left: max_deque.popleft() left += 1 result += right - left + 1 return result # 输入部分 n, p, q = map(int, input().split()) array = list(map(int, input().split())) print(count_subarrays(n, p, q, array)) ``` 上述代码实现了双端队列来追踪当前窗口的最大值与最小值,从而快速判断是否满足约束条件并更新计数值。 --- #### 买菜 (201809-2) ##### 描述 某人每天可以购买一定量的商品,每种商品的价格不同。已知未来几天各商品价格变化情况,请设计一种策略使得总花费最少。 ##### 解析 这是一个典型的贪心算法应用案例。核心思想在于优先选择单位成本最低的商品进行采购,直到达到每日需求上限为止。下面展示了一种可能的解决方案: ```python from heapq import heappop, heappush def minimal_cost(days, daily_needs, prices_per_day): total_cost = 0 available_items = [] for day_index in range(len(daily_needs)): current_price = prices_per_day[day_index] # 将当天所有可选物品加入候选列表 for item_id, price in enumerate(current_price): heappush(available_items, (price, item_id)) need_count = daily_needs[day_index] # 贪婪选取最便宜的选项直至满足当日需求 while need_count > 0 and available_items: cheapest_item = heappop(available_items)[0] total_cost += cheapest_item need_count -= 1 return total_cost # 示例数据读取方式 days = int(input()) daily_needs = list(map(int, input().strip().split())) prices_per_day = [] for _ in range(days): row_prices = tuple(map(float, input().strip().split())) prices_per_day.append(row_prices) result = minimal_cost(days, daily_needs, prices_per_day) print(result) ``` 这里利用堆结构保持全局最优的选择顺序,在每次循环中不断补充新的备选项至堆内以便后续操作。 --- ### 总结 以上两道题目分别代表了不同的编程技巧——前者侧重于窗口管理技术的应用;后者体现了如何通过适当的数据结构优化贪婪决策过程。两者共同强调了对细节的关注以及对标准输入/输出格式严格遵循的重要性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水能zai舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值