Interesting Calculator(dp)

3 篇文章 0 订阅

描述

There is an interesting calculator. It has 3 rows of buttons.
Row 1: button 0, 1, 2, 3, …, 9. Pressing each button appends that digit to the end of the display.
Row 2: button +0, +1, +2, +3, …, +9. Pressing each button adds that digit to the display.
Row 3: button *0, *1, *2, *3, …, *9. Pressing each button multiplies that digit to the display.
Note that it never displays leading zeros, so if the current display is 0, pressing 5 makes it 5 instead of 05. If the current display is 12, you can press button 3, +5, *2 to get 256. Similarly, to change the display from 0 to 1, you can press 1 or +1 (but not both!).
Each button has a positive cost, your task is to change the display from x to y with minimum cost. If there are multiple ways to do so, the number of presses should be minimized.

输入

There will be at most 30 test cases. The first line of each test case contains two integers x and y(0<=x<=y<=105). Each of the 3 lines contains 10 positive integers (not greater than 105), i.e. the costs of each button.

输出

For each test case, print the minimal cost and the number of presses.

样例输入

12 256
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
12 256
100 100 100 1 100 100 100 100 100 100
100 100 100 100 100 1 100 100 100 100
100 100 10 100 100 100 100 100 100 100

样例输出

Case 1: 2 2
Case 2: 12 3

题意:
有3*9的矩阵按键,第一排是再数字末尾加一个数(0 ->1可以这样),第二排是数字加上一个数,第三排为乘上一个数,数为列号,给出x,y (x<=y)和每个按键的费用,求最小费用和最小费用时的最小操作按键数。

思路:
搭积木,对于一个数x它可以由 x/10 + x%10 得到 或者 x-j (x-j>=0)+j 再或者 x/j (x%j==0 ) * j 得到,由于 x肯定大于被操作前的数(乘 0 除外),所以对于一个数 x它一定由比他小的数操作获得。所以对 [1,y]内进行操作枚举和记录每个数的最优结果即可(dp),dp[0] 的次数为1,它的值为cost[0][2],因为y的最优解可能由先将x置零组成。

#include<bits/stdc++.h>
using namespace std;
const int Ms=1e5+5;
struct node
{
    int val,cnt;
    bool operator <(const node &o)const
    {
        if(val!=o.val)return val<o.val;
        return cnt<o.cnt;
    }
}dp[Ms];
int cost[10][3];
int main()
{
    int x,y,ca=1;
    node tmp;
    while(~scanf("%d%d",&x,&y))
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<10;j++)
            {
                scanf("%d",&cost[j][i]);
            }
        }
        memset(dp,0x3f,sizeof dp);
        dp[0].cnt=1;dp[0].val=cost[0][2];
        dp[x].cnt=dp[x].val=0;
        for(int i=1;i<=y;i++)
        {
            tmp.val=dp[i/10].val+cost[i%10][0];
            tmp.cnt=dp[i/10].cnt+1;
            dp[i]=min(dp[i],tmp);
            for(int j=1;j<10;j++)
            {
                if(i-j>=0)
                {
                    tmp.val=dp[i-j].val+cost[j][1];
                    tmp.cnt=dp[i-j].cnt+1;
                    dp[i]=min(dp[i],tmp);
                }
                if(i%j==0)
                {
                    tmp.val=dp[i/j].val+cost[j][2];
                    tmp.cnt=dp[i/j].cnt+1;
                    dp[i]=min(dp[i],tmp);
                }
            }
        }
        //for(int i=x;i<=y;i++)printf("%d %d %d\n",i,dp[i].val,dp[i].cnt);
        printf("Case %d: %d %d\n",ca++,dp[y].val,dp[y].cnt);
    }
    return 0;
}

若有什么错误,欢迎指正^ _ ^ 。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值