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\n",ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/s1124yy/p/6096371.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值