UVA 10626 - Buying Coke
【摘要】 题目链接~~>
做题感悟:做小白书后面的题目感觉大部分动态规划都可以用记忆化搜索去解决,记忆化搜索也很强大啊!
解题思路:记忆化搜索
开三维的dp[ i ] [ j ] [ k ] 代表硬币分别还剩 i , j , k 的最优解.
这里投放硬币一定要注...
做题感悟:做小白书后面的题目感觉大部分动态规划都可以用记忆化搜索去解决,记忆化搜索也很强大啊!
解题思路:记忆化搜索
开三维的dp[ i ] [ j ] [ k ] 代表硬币分别还剩 i , j , k 的最优解.
这里投放硬币一定要注意:
(1) 、 投放 一个 10 找回 两 个 1
(2) 、 投放一个 10 三个 1 找回一个 5
(3)、 投放两个 5 找回两个 1
(4)、 投放一个 5 三个 1
(5)、 投放八个 1
代码:
-
#include<iostream>
-
#include<fstream>
-
#include<iomanip>
-
#include<ctime>
-
#include<fstream>
-
#include<sstream>
-
#include<stack>
-
#include<cstring>
-
#include<cmath>
-
#include<map>
-
#include<queue>
-
#include<vector>
-
#include<cstdio>
-
#include<algorithm>
-
#define INT __int64
-
using namespace std ;
-
const double esp = 0.00000001 ;
-
const int INF = 0x3f3f3f3f ;
-
const int mod = 1e9 + 7 ;
-
const int MY = 10 + 10 ;
-
const int MX = 59049+ 10 ;
-
int n ,x ,y ,z ;
-
int dp[710][160][60] ;
-
int DP(int n ,int x ,int y ,int z)
-
{
-
if(dp[x][y][z] != -1)
-
return dp[x][y][z] ;
-
if(!n) return 0 ;
-
if(!y && !z) // 花费八个1
-
return dp[x][y][z] = n*8 ;
-
int& ans = dp[x][y][z] ;
-
ans = INF ;
-
if(z >= 1)
-
{
-
ans = min(ans ,DP(n-1 ,x+2 ,y ,z-1) + 1) ; // 花费一个 10
-
if(x >= 3)
-
ans = min(ans ,DP(n-1 ,x-3 ,y+1 ,z-1) + 4) ; // 花费 1 个 10 3 个 1
-
}
-
if(y >= 2)
-
ans = min(ans ,DP(n-1 ,x+2 ,y-2 ,z) + 2) ; // 花费 2 个 5
-
if(y >= 1 && x >= 3)
-
ans = min(ans ,DP(n-1 ,x-3 ,y-1 ,z) + 4) ; //花费 1 个 5 三个 1
-
return ans ;
-
}
-
int main()
-
{
-
int Tx ;
-
scanf("%d" ,&Tx) ;
-
while(Tx--)
-
{
-
scanf("%d%d%d%d" ,&n ,&x ,&y ,&z) ;
-
memset(dp ,-1 ,sizeof(dp)) ;
-
cout<<DP(n ,x ,y ,z)<<endl ;
-
}
-
return 0 ;
-
}
-
文章来源: blog.csdn.net,作者:Linux猿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/nyist_zxp/article/details/38842837
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)