Codeforces Round #381 (Div. 2)A. Alyona and copybooks(dfs)

A. Alyona and copybooks

Problem Description:

Little girl Alyona is in a shop to buy some copybooks for school. She study four subjects so she wants to have equal number of copybooks for each of the subjects. There are three types of copybook's packs in the shop: it is possible to buy one copybook for a rubles, a pack of two copybooks for b rubles, and a pack of three copybooks for c rubles. Alyona already has n copybooks.

What is the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4? There are infinitely many packs of any type in the shop. Alyona can buy packs of different type in the same purchase.

Input:

The only line contains 4 integers n, a, b, c (1 ≤ n, a, b, c ≤ 109).

Output:

Print the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4.

Sample Input:

1 1 3 4

Sample Output:

3

【题目链接】A. Alyona and copybooks

【题目类型】dfs或暴力

&题意:

你有n本书,你要再买k本,使得n+k能被4整除,a是1本的价钱,b是2本的价钱,c是3本的价钱。问最小价钱是多少?

&题解:

这题刚做的时候想简单了,毕竟只是cf第一题嘛,但最后,发现还是有多种情况的,比如还差1本的时候,也有可能是5本。
这题暴力感觉好麻烦,我就仔细的想了一下,发现可以dfs,num代表本数,res代表多少钱,一定要超过3*n再停止,这样是为了搜索更多可能的情况。

&代码:

#include <bits/stdc++.h>
typedef long long ll;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
ll n, a, b, c, ans;
void dfs(ll num, ll res) {
	if (num % 4 == 0) ans = std::min(ans, res);
	if (num > 3 * n) return;
	dfs(num + 1, res + a);
	dfs(num + 2, res + b);
	dfs(num + 3, res + c);
}
int main() {
	while (~scanf("%d%d%d%d",&n,&a,&b,&c)) {
		ans = LINF;
		n %= 4;
		dfs(n, 0);
		printf("%d
",ans);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/s1124yy/p/6096371.html