TOJ3976 Change 完全背包 || 找规律
来源:互联网 发布:电子宠物机多少钱知乎 编辑:程序博客网 时间:2024/06/06 01:57
After Shawn sees the following picture, he decides to give up his career in IT and turn to sell fruits. Since Shawn is a lazy guy, he doesn’t like to do any extra work. When he starts selling fruits, he finds that he always needs to take changes for customers. He wants you to write a program to give the least number of changes. Shawn is also a strange guy, because he takes changes by 6,5,3,1.
Input
The first line of input is an integer T which indicates the sum of test case. Each of the following T lines contains an integer n(1≤n≤105), which is the total money Shawn need to give to the customer.
Output
For each test case, output "Case i: Result" in one line where i is the case number andResult is the least number of changes Shawn need to give customer.
Sample Input
341019
Sample output
Case 1: 2Case 2: 2Case 3: 4
Hint
Case 1: 4=3+1Case 2: 10=5+5Case 3: 19=6+6+6+1
第一反应是完全背包,无奈写炸TLE:
f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i],f[i][v]|0<=k*c[i]<=v}
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ int T,V; int w[4] = {6,5,3,1}; int dp[100005]; cin >> T; for (int num = 1;num <= T;num++){ cin >> V; for (int i = 0;i <= V;i++) dp[i] = i; for (int i = 0;i < 4;i++){ for (int k = 0;k * w[i] <= V;k++){ for (int j = V;j >= k * w[i];j--){ dp[j] = min(dp[j],dp[j - k * w[i]]+ k); } } } printf("Case %d: %d\n",num,dp[V]); } return 0;}
查题解发现可以直接套规律......
#include <stdio.h>int a[12]={1,2,1,2,1,1,2,2,2,2,2,2};int main(){int t,n,sum;scanf("%d",&t);for(int i=0;i<t;i++){scanf("%d",&n);sum=0;while(n>12){n-=6;sum++;}sum+=a[n-1];printf("Case %d: %d\n",i+1,sum); }}
后来仔细研读完全背包,发现应该优化为01背包.....看来本菜真的是忘完了(背包详情讲解看另一篇《背包九讲》)
procedure CompletePack(cost,weight)
forv=cost..V
f[v]=max{f[v],f[v-cost]+weight}
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>using namespace std;int main(){ int T,V; int w[4] = {6,5,3,1}; int dp[100005]; cin >> T; for (int num = 1;num <= T;num++){ cin >> V; for (int i = 0;i <= V;i++) dp[i] = i; for (int i = 0;i < 4;i++){ for (int j = w[i];j <= V;j++){ dp[j] = min(dp[j],dp[j-w[i]]+1); } } printf("Case %d: %d\n",num,dp[V]); } return 0;}
阅读全文
0 0
- TOJ3976 Change 完全背包 || 找规律
- 找规律:String Change
- hdoj 2277 Change the ball 【找规律】
- hdu 2277 Change the ball(找规律)
- hdoj 2277 Change the ball 【找规律】
- hdoj 2277 Change the ball 【找规律】
- poj2229【完全背包-规律Orz...】
- Uva 674 Coin Change 完全背包
- poj1787 Charlie's Change 完全背包
- uva 674Coin Change(完全背包)
- uva 674 Coin Change(完全背包)
- UVaOJ-674 Coin Change (完全背包)
- POJ 1787 Charlie's Change / 完全背包
- uva674 - Coin Change(完全背包)
- UVA 674 - Coin Change(完全背包)
- UVA 674 Coin Change(完全背包)
- UVA 674 Coin Change(完全背包)
- LightOJ 1232 - Coin Change (II) 【完全背包】
- Java中Map类的基本实现
- L2-013. 红色警报-PAT团体程序设计天梯赛(图的连通分量个数统计)
- 请编程设计一个登陆界面,要求输入账号和密码(不考虑事件)。欢迎评价与点赞
- Stellar Phoenix Video Repair(视频修复工具)官方破解版V3.0.0.0下载 | Stellar Phoenix视频修复工具
- 数据结构实验之排序七:选课名单
- TOJ3976 Change 完全背包 || 找规律
- 关于coding.net命令行创建项目
- 数据结构课程设计
- 561. Array Partition I
- 查资料所得
- 项目学习(三)
- numpy ndarray初始化
- 最全的数据结构解析与归纳
- 将多个属性值的属性中的某些属性值整合成一个属性值