摘要:// N个物品 放进容量为C的背包里面 要求价值最大// 一看 第一反应是0 1背包 不过 N=100000 C=10000// 注意到 v,c在 10以内// 那么 最多就100种组合了 然后就转化为 多重背包了// dp#include #include #include #include #include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 10010int dp[maxn];int mp[110][110]; // v,cchar str[.
阅读全文
摘要:// v给出N种硬币和个数,问可以取到1->M中的多少个值。// 背包 完全背包 或多 重背包(二进制优化)都可以做// #include #include #include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 100010int dp[maxn],use[maxn];int val[110],num[110];int main(){ int n,m; dp[0]=1; while(scanf("%d %d",&n,
阅读全文
摘要:题意: N 个点 N-1条边 每条边有权值 求每个点所能达的最长距离// 树形dp 2次dfs 一次转成有根树、并求出每个子树(以i为根)到叶子节点的最大距离 还有就是从与i相连的个子节点j遍历下去的最大距离 // 其实 每个子树(以i为根)到叶子节点的最大距离 就是 max(child[i][j]);#include #include #include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 10010vector E[maxn],V[maxn],c.
阅读全文
摘要:http://poj.org/problem?id=2955// 求最长合法子序列// 区间 dp// dp[i][j] 表示区间 i,j 的最长合法子序列#include #include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 110int dp[maxn][maxn];char a[110];bool cmp(char c1 ,char c2){ if(c1=='('&&c2==')') return
阅读全文
摘要:// 转载自http://blog.163.com/benz_/blog/static/18684203020115721917109/算法不难看出,就是一个无限背包+多重背包。问题在于背包的范围。设John出了X元,则需要找零X-T元。证明X不超过T+v_max^2:假设超过了,则找零超过v_max^2,则找零的货币数定超过v_max,根据抽屉原理,必然有若干个货币组合起来是v_max的倍数,那么这些货币肯定可以在给钱的时候少给一些,从而推出这样的方案肯定不是最优方案。复杂度:O(n*(T+vmax^2))// 我自己做时 是把所有 v[i]相加 得到的 复杂度为 O(n*(T+sum{v[
阅读全文
摘要:// 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n#include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 200010int dp[maxn];int S[110],F[110];int main(){ int N; while(scanf("%d",&N)!=EOF){ int i,j,k; ...
阅读全文
摘要:题意:给定一个数p,要求用K种币值分别为1,2,3...K的硬币组成p,问方案数,1,2,2和2,2,1算一种方案即与顺序无关,n #include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 1010int dp[maxn][40];void Add(int n,int m){ int i; for(i=0;i=10) dp[n][i]%=10,dp[n][i+1]++; }}int main(){ int N,K; whi...
阅读全文
摘要:// 题意 给定一个数p,要求用四种币值为1,5,10,25的硬币拼成p,并且硬币数要最多,如果无解输出"Charlie cannot buy coffee.",1dp[j]&&dp[j-mon[i]]&&used[j-mon[i]]+1#include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 10010int dp[maxn];struct node{ int from; int kind; int
阅读全文
摘要:题意 :给定n种积木,每种积木都有一个高度hi,一个数量ci,还有一个限制条件,这个积木所在的位置不能高于ai,问能叠起的最大高度// 这里有个问题需要注意 就是ai小的要先进行背包 不然 在 ai高度下 L1L2 和 L2L1是不一样的 // 然后就是简单的0-1背包了#include #include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 40010int dp[maxn];struct node{ int h; int a; int...
阅读全文
摘要:// 题意 :夫妻两洗衣服,衣服有m种颜色,每种颜色又有若干件,每件衣服洗完需要特定的时间,要求每种颜色放在一起洗,洗完才能洗其他衣服。最后问洗完需要的最少时间// 将衣服按颜色分类 然后求出每种颜色衣服需要的最少时间,然后累加// 求每种颜色衣服的最少时间就是把洗衣服的时间尽量分成靠近的两部分 就用总时间的一半作为背包的容量 ,然后求该容量的最大值 每件的空间消耗和价值看成是一样的// 然后就是 0-1背包了#include #include #include #include #include #include using namespace std;#define MOD 1000000
阅读全文
摘要:// 混合背包// xiaoA想尽量多花时间做ACM,但老板要求他在T时间内做完n堆工作,每个工作耗时ac[i][j],// 幸福感ag[i][j],每堆工作有m[i]个工作,每堆工作都有一个性质,// 0表示至少要做里面的1个工作,// 1表示最多做里面的1个工作,// 2表示随意,做或不做都行。最后问在符合老板要求的情况下的最大幸福感,怎么都不符合要求就输出-1.// 0 可以看 hdu 3033 I love sneakers!// 1,2 情况就是普通背包了#include #include #include #include #include #include using names
阅读全文
摘要:题意:题目给定N部电影,每部电影有时长和价值,要求看M部电影,并且时间控制在L以内,转化为背包问题,让我们在N件物品中找正好M件物品塞进容量L的包中,求最大的价值。// dp[i][j] 表示在容量为 i 的空间里面看 j部电影的最大收获价值// 背包#include #include #include #include #include #include using namespace std;#define MOD 1000000007#define maxn 110int dp[1010][maxn];int t[maxn],va[maxn];int main(){ int N,M...
阅读全文
摘要:题意:给定一个初始资金capital,然后给定d种投资方案,每种投资方案中有投资额value[i](是1000的倍数)和利息interest[i],每年的投资就可以拿到全部利息,然后累加起来继续投资利滚利。问经过year年后最多拥有多少资金?// 一年年的求最大利润就可以了// 每一年内都是完全背包// 这题的关键是value[i]都是1000的倍数 就可以把空间除以1000 了不然时间复杂度就高了好多#include #include #include #include #include #include using namespace std;#define MOD 1000000007#
阅读全文
摘要:Cash MachineTime Limit: 1000MSMemory Limit: 10000KTotal Submissions: 23136Accepted: 8093DescriptionA Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=
阅读全文
摘要:Dividing the PathTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 2858Accepted: 1064DescriptionFarmer John's cows have discovered that the clover growing along the ridge of the hill in his field is particularly good. To keep the clover watered, Farmer John is installing water sprinklers
阅读全文
摘要:Bridging signalsTime Limit: 1000MSMemory Limit: 10000KTotal Submissions: 9178Accepted: 5006Description'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. Once more the routing designers have screwed up completely, making the signals on the chip connect
阅读全文
摘要:跑跑卡丁车Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1806Accepted Submission(s): 589 Problem Description跑跑卡丁车是时下一款流行的网络休闲游戏,你可以在这虚拟的世界里体验驾驶的乐趣。这款游戏的特别之处是你可以通过漂移来获得一种 加速卡,用这种加速卡可以在有限的时间里提高你的速度。为了使问题简单化,我们假设一个赛道分为L段,并且给你通过每段赛道的普通耗时Ai和用加速卡的耗时Bi。加速卡的获
阅读全文
摘要:PolygonTime Limit: 1000MSMemory Limit: 10000KTotal Submissions: 4464Accepted: 1859DescriptionPolygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with either the symbol + (add
阅读全文
摘要:Multiplication PuzzleTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 5095Accepted: 3042DescriptionThe multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to
阅读全文
摘要:1137: 石子合并问题Time Limit:1500 msMemory Limit:10000 kB Judge type: Multi-cases Total Submit :1315(282 users)Accepted Submit :300(202 users)Page View : 10921Font Style:AaAaAa在一个圆形操场的四周摆放着n 堆石子。现要将石子有次序地合并成一堆。规定每次只能选相邻的2 堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分。试设计一个算法,计算出将n堆石子合并成一堆的最小得分和最大得分。编程任务: 对于给定n堆石子,编程计算合并成
阅读全文