题意:一样是求钱的转换方案数,但是这次单位下降到分,但给的是元为单位的,所以是浮点的,但是固定有两位小数. 思路:数据都放大100倍来计算,去除精度问题,转成整型时要注意精度.即使给的是0.02,乘以100后的结果不一定是2,而是2左右,所以再加上一个很小的数再转即可,比如0.0001: #include <iostream> #include <cstdio> #define LL long long using namespace std; ; , , , , , , , ,…
首先是 Uva 147:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 细心看完这题后发现还是完全背包,只不过需要对浮点数处理一下.即把所有硬币的面值都乘以100,化为整数,对输入的数据也作同样的处理,然后就是套完全背包的模板了,在输出时还要用格式和精度来卡一卡你……一开始我没想到用printf可以的,于是百度了cout的输出格式控制,…
题目连接:147 - Dollars 题目大意:有11种硬币, 现在输入一个金额, 输出有多少种组成方案. 解题思路:uva 674 的升级版,思路完全一样, 只要处理一下数值就可以了. #include <stdio.h> #include <string.h> const int N = 30005; const int val[11] = {5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000}; long long n,…
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 147 - Dollars Time limit: 3.000 seconds Dollars New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and…
https://vjudge.net/problem/UVA-147 题意: 换零钱,计算方案数. 思路: 完全背包,UVa674的加强版. #include<iostream> #include<string> #include<cstring> #include<algorithm> using namespace std; + ; , , , , , , , , , , }; long long d[maxn]; int main() { //freo…
题目大意:给出五种硬币,价值分别为 1,5,10,25,50,.当给出一个价值时,求出能够组合的种数(每种硬币可以用无限次). 思路:完全背包, dp[i][j]表示总数 i 能够被表示的种数.状态转移方程为 dp[i][j] = dp[i-k*v[j]][j-1] (k = 0,1,2,3...). C++ 代码如下: #include<iostream> #include<cstdio> #include<algorithm> #include<cmath&g…
/* * UVA_147.cpp * * Created on: 2013年10月12日 * Author: Administrator */ #include <iostream> #include <cstdio> using namespace std; const int maxn = 6005; int b[11] = {1,2,4,10,20,40,100,200,400,1000,2000}; long long a[maxn]; int main(){ double…
*注 虽然没什么人看我的博客但我还是要认认真真写给自己看 背包问题应用场景给定 n 种物品和一个背包.物品 i 的重量是 w i ,其价值为 v i ,背包的容量为C.应该如何选择装入背包中的物品,使得装入背包的总价值最大?*01 背包 *01 背包特点: 给定 n 种物品和一个背包 ( 每个物品只能选取一个).物品 i 的重量是w[i],其价值为v[i],背包的容量为C.应该如何选择装入背包中的物品,使得装入背包中的物品的总价值最大?*状态: dp[i][j] 表示在只能从 1-i 个物品中选…
#include<bits/stdc++.h> using namespace std; int main() { unsigned ]; memset(dp,,sizeof(dp)); dp[]=; ;i<=;i++) ;j++) dp[j]=dp[j]+dp[j-i]; int n; while((scanf("%d",&n))!=EOF) { printf("%I64d\n",dp[n]); } ; ; } 这两道题基本思路是一样的,…
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态转移方程 dp[j+c[i]] = dp[j] + dp[j+c[i]] 代码总览 /* Title:UVA.674 Author:pengwill Date:2017-2-16 */ #include <iostream> #include <cstdio> #include <…
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141 题意: 给定区间,求区间内所有整数a,f(a) = a * a + a - 1为质数的概率. 分析: 卡精度卡的蛋疼.. 最后要加个eps处理四舍五入问题..不是很懂.. 代码: #include<cstdio> const int maxm = 1e4 +…
题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum,   A,B其中必有一人获得的钱小于等于sum/2,另一人获得的钱大于等于sum/2.   因此用sum/2作为背包容量对n个硬币做01背包处理,   所能得到的最大容量即为其中一人获得的钱数. #include <iostream> #include <cstdio> #include <cstring> #includ…
Problem C: Homer Simpson Time Limit: 3 seconds Memory Limit: 32 MB Homer Simpson, a very smart guy, likes eating Krusty-burgers. It takes Homer m minutes to eat a Krusty- burger. However, there�s a new type of burger in Apu�s Kwik-e-Mart. Homer likes…
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great lengt…
题目连接:10404 - Bachet's Game 题目大意:由一堆石子, 给出石子的总数, 接下来由stan和ollie两个人玩游戏,给出n, 在给出n种取石子的方法(即为每次可取走石子的数量),由stan先,两人轮流取走石子,最后一个将石子全部去完的人胜利,问, 给出的一堆石子, 两人均按最好的方案游戏, 最后将会是谁胜 ? 解题思路:问题可以看做是一个完全背包的变形, dp[i]只有0 和1两种状态, 1 是代表当前i个石子先取者为必胜, 0 带表当前n个石子先取者为必败.转态转移方程i…
https://vjudge.net/problem/UVA-674 题意: 计算兑换零钱的方法共有几种. 思路: 完全背包基础题. #include<iostream> #include<string> #include<cstring> #include<algorithm> using namespace std; ]; ] = { , , , , }; int main() { //freopen("D:\\txt.txt", &…
https://vjudge.net/problem/UVA-12563 题意: 在一定的时间内连续唱歌,最后一首唱11分钟18秒的劲歌金曲,问最多能长多长时间. 思路: 0-1背包问题,背包容量为t-1,因为至少还要留1秒钟来放劲歌金曲.还需要注意的是题目要求的是在唱最多首歌的情况下所能唱的最长时间,所以这里我们来限制一下时间,对于j时刻,我们要求正好唱完这首歌,那么这样唱的歌肯定是最多的. #include<iostream> #include<algorithm> #incl…
 Dollars New Zealand currency consists of $100, $50, $20, $10, and $5 notes and $2, $1, 50c, 20c, 10c and 5c coins. Write a program that will determine, for any given amount, in how many ways that amount may be made up. Changing the order of listing…
It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great length and thus crea…
题目: 思路: dp[j][h]表示选取了j个向量,且高度为h,利用01背包来解决问题. 没选当前的向量:dp[j][h] = dp[j][h]; 选了当前的向量:dp[j][h] = dp[j-1][h-p[i].y]+2*p[i].x*(h-p[i].y)+p[i].area;(p[i].area = p[i].x+p[i].y) 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1000000000 #defi…
参考了https://www.cnblogs.com/dwtfukgv/p/5645446.html (1)直接二分答案.说实话我没有想到, 一开始以为是贪心, 以某种策略能得到最优解. 但是想了很久没想出来, 后来看了博客发现因为显然答案是单调的, 可以用二分来做. 看到最大, 最小, 可以考虑答案是否单调, 单调考虑用二分 (2)然后是小数化分数, 其实一开始我想模拟分数, 然后发现很麻烦, 之后博客里的方法技巧性很强. 其实这个方法默认了分母是在1到n之间的, 而好像题目并没有给出这个条件…
设置最后打开的是盒子1, 另外一个盒子剩下i个 那么在这之前打开了n + n - i次盒子 那么这个时候的概率是C(2 * n - i, n) p ^ (n+1) (1-p)^ (n - i) 那么反过来最后打开的是盒子2, 那么概率是C(2 * n - i, n) p ^ (n-i) (1-p)^ (n +1) 那么当前的概率就是两个加起来,然后乘以权值,即i就可以了 所以枚举所有的i加起来就好了. 但这样会损失很多精度, 所以我们可以用对数 也就是说算的时候先取对数来算,后来再取回去 不要忘…
  Dividing coins  It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great le…
一个简单的dp   面值是5的倍数  将面值都除5   因为输出问题wa .... #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; long long f[6020]; int coin[] = {1,2,4,10,20,40,100,200,400,1000,2000}; void dp(int n) { m…
题意:给n个分成两个组,保证每个组的人都相互认识,并且两组人数相差最少,给出一种方案. 析:首先我们可以知道如果某两个人不认识,那么他们肯定在不同的分组中,所以我们可以根据这个结论构造成一个图,如果两个不相互认识, 那么就加一条边,然后如果这个图是二分图,那么这分组是可以,否则就是不可能的.然后dp[i][j]表示那两个组相差人数为 j 是不是可以达到, 当然可能为负数,所以可以提前加上n,然后就是逆序输出答案即可. 代码如下: #pragma comment(linker, "/STACK:1…
解法 dp表示目前的种数,要全部装满所以f[0]=1其余为0的初始化是必不可少的 代码 #include <bits/stdc++.h> using namespace std; int t; int v[]={1,5,10,25,50}; int dp[10000]; void DP() { memset(dp,0,sizeof(dp)); dp[0]=1; for(int i=0;i<5;i++) for(int j=v[i];j<=t;j++) dp[j]+=dp[j-v[i…
 题意 霍默辛普森吃汉堡  有两种汉堡  一中吃一个须要m分钟  还有一种吃一个须要n分钟  他共同拥有t分钟时间    要我们输出他在尽量用掉全部时间的前提下最多能吃多少个汉堡  假设时间无法用完  输出他吃的汉堡数和剩余喝酒的时间 非常明显的全然背包问题  求两次  一次对个数  一次对时间即可了   时间用不完的情况下就输出时间的 d1为个数的  d2为时间的  dt保存时间 #include<cstdio> #include<cstring> #include<a…
Your task is to divide a number of persons into two teams, in such a way, that: everyone belongs to one of the teams; every team has at least one member; every person in the team knows every other person in his team; teams are as close in their sizes…
1.hdu 2126 Buy the souvenirs 题意:给出若干个纪念品的价格,求在能购买的纪念品的数目最大的情况下的购买方案. 思路:01背包+记录方案. #include<iostream> #include<cstdio> #include<cstring> using namespace std; ; ; int cnt[maxw]; int dp[maxw]; int p[maxn]; int main() { int t; scanf("%…
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) ACM总结(fennec) 其实在北京比赛完的时候,我就想写了,不过还是早了点,直到上海比赛结束,大家的心中都不是太好受.郭老师有句话:你们这样做也是对的,不成功就成仁.让我的心也能安慰了不少. 我是从大一下学期开始接触ACM的,那时候我们学校才刚刚起步,siyee,wjiang师兄可以说是我的领路人了…