andre_joy

导航

随笔分类 -  POJ

poj 1741
摘要:地址:http://poj.org/problem?id=1741题意:给一棵无根树,求任意两点距离小于等于k的点对数量。mark:男人八题。。http://wenku.baidu.com/view/e087065f804d2b160b4ec0b5.html### 上面网址是讲解思路的论文,具体实现没说。我来写写我的实现过程。 首先是要找到重心,那么先递归求出每个点拆掉后能够得到的最大的点的个数,这个点的个数最小的肯定是重心。 然后就按照论文讲解的方法求点对数量,先要求出每个点到重心的距离。代码:#include <cstdio>#include <cstring>#i 阅读全文

posted @ 2012-10-12 12:08 andre_joy 阅读(1297) 评论(0) 推荐(0)

poj 2486
摘要:地址:http://poj.org/problem?id=2486题意:有一颗苹果树,每个节点上面有很多苹果,从一个节点到另外一个可以到达的节点花费1步,求k步最多能吃到多少苹果。mark:这是典型的回溯型树状dp。dp[i][j][0]代表以i为根节点的子树最多j步后回到i能吃到的最多的苹果,dp[i][j][1]代表以i为根节点的子树最多j步后不回到i节点最多能吃到的子树。那么状态转移就分三步了。(1)dp[i][j+2][0] = max(dp[i][j+2][0], dp[i][j-k][0]+dp[son][k][0]);(2)dp[i][j+1][1] = max(dp[i][j+ 阅读全文

posted @ 2012-10-10 22:49 andre_joy 阅读(756) 评论(0) 推荐(0)

poj 1947
摘要:地址:http://poj.org/problem?id=1947题意:给你一棵树,求最少剪掉几条边使能够得到一个p个节点的树。mark:因为节点个数不多,我们可以把节点个数当成一个状态,那么dp[i][j]代表以i为根的子树要变成j个节点需要剪掉的边数。 这里我们考虑状态转移的时候要注意,,我们考虑子节点的时候不用考虑它与父节点的那条边,就当不存在,那么最后求的的最小边数加1就ok了~ 考虑一个节点时,有两种选择,要么剪掉跟子节点相连的边,则dp[i][j] = dp[i][j]+1; 要么不剪掉,则d[i][j] = max(dp[i][j],... 阅读全文

posted @ 2012-10-09 22:41 andre_joy 阅读(631) 评论(2) 推荐(0)

poj 1155
摘要:地址:http://poj.org/problem?id=1155题意:电视台发送信号给很多用户,每个用户有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号。mark:树状dp。由于求的是最多多少用户,那么我们可以把用户个数当成一个状态。dp[i][j]代表i节点为根节点的子树j个用户的时候最大剩余费用。 则dp[i][j] = max(dp[i][j], dp[i][k]+dp[son][j-k]-w[i][son]); 注意两点,第一点是上面式子中的dp[i][k]必须先用一个tem[MAX]数组提取出来,因为在计算的过程中会相互影响。第二... 阅读全文

posted @ 2012-10-09 22:18 andre_joy 阅读(502) 评论(6) 推荐(0)

poj 1837
摘要:地址:http://poj.org/problem?id=1837题意:一个天枰,左边右边总共有n个钩子,总共有m个砝码,要求钩子可以不全用,砝码必须全用的所有方案数。mark:这题可以这样想,每一个砝码挂在每一个位置有一个权值,那么就组成了一个m*n的矩阵,然后需要在每一行里面选出一个使得最后权值和为0,则转移方程为dp[i][pos[j]*w[i]+k] = dp[i-1][k]。因为这里面权值可能有负数,而最大可能是-15*25*20 = -7500,所以数组可以把7500当0用。代码:#include <stdio.h>#include <string.h>#i 阅读全文

posted @ 2012-09-26 19:36 andre_joy 阅读(142) 评论(0) 推荐(0)

poj 3260
摘要:地址:http://poj.org/problem?id=3260题意:有Vi种硬币,每个硬币有Ci个,要付的价钱T,付多了,老板会找钱,用已有的硬币种类找,求给的和找的硬币数量最少的方案。mark:多重背包,可以参照我的另外一篇日志http://www.cnblogs.com/andre0506/archive/2012/09/22/2697788.html来优化,效率是O(NV)。 这种方法必须排序!代码:#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct{ int 阅读全文

posted @ 2012-09-24 12:14 andre_joy 阅读(366) 评论(0) 推荐(0)

poj 1787
摘要:地址:http://poj.org/problem?id=1787题意:分硬币,有1,5,10,25四种硬币,给定每种硬币的数量,给定要组合成的价值,问刚好达到价值时用的硬币最多的情况。mark:多重背包!本题给出两种方法,特别注意下面一个方法!!!代码:需要回溯,于是就加一个path[]存放父亲,dp[]代表个数。由于该题有5,10这两个硬币,多重背包二进制优化的时候可能出现5*2,然后本题要区分开来,所以记录一下就行。#include <stdio.h>#include <string.h>#include <stdlib.h>const int N = 阅读全文

posted @ 2012-09-22 10:12 andre_joy 阅读(556) 评论(0) 推荐(0)

poj 2184
摘要:地址:http://poj.org/problem?id=2184题意:有很多羊,每只羊有一个幽默度和智商,要选出一些羊,智商加幽默度总和最大,其中智商总和和幽默度总和都不能是负数。mark:变种的01背包,可以把幽默度看成体积,智商看成价值,那么就转换成求体积和价值都为正值的最大值的01背包了。 由于有负数,所以可以每个体积+1000,然后开一个数组记录用该体积得到最大值时用了多少个1000.代码:#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#incl 阅读全文

posted @ 2012-09-19 18:39 andre_joy 阅读(1259) 评论(0) 推荐(1)

poj 2823
摘要:地址:http://poj.org/problem?id=2823题意:n个数里面连续的k个数,找最大和最小的。mark:优先队列,不过要重载一下cmp函数。 也可以单调队列做。代码:#include <stdio.h>#include <string.h>#include <stdlib.h>int queue[1000010];int a[1000010];int m,n;void solve(int f){ int i,j,k; int fr,la; fr = la = 0; queue[la++] = 0; for(i = 1; i < m; 阅读全文

posted @ 2012-08-03 15:33 andre_joy 阅读(184) 评论(0) 推荐(0)

poj 1753
摘要:地址:http://poj.org/problem?id=1753题意:黑白棋,翻动一个格子,周围相连的格子都翻转,问最少几次使所有棋子颜色相同。mark:正解高斯消元,第一次写,写了两天,终于理解并ac了~代码:#include <stdio.h>#include <string.h>int g[20][20];int min(int a, int b) {return a < b ? a : b;}int find(int i){ int j; for(j = i; j < 17; j++) if(g[j][i]) return j; return 0; 阅读全文

posted @ 2012-08-02 22:43 andre_joy 阅读(132) 评论(0) 推荐(0)

poj 1258
摘要:地址:http://poj.org/problem?id=1258题意:把所有农场连起来的最小费用。mark:最简单的最小生成树。第一次写,代码比较乱~代码:#include <stdio.h>#include <string.h>const int N = 110;const int MAX = 100010;int vis[N],dis[N];int a[N][N];int min(int a, int b) {return a < b ? a : b;}int main(){ int n,ans,min1,f,sum; int i,j,k; while(~s 阅读全文

posted @ 2012-07-26 10:55 andre_joy 阅读(106) 评论(0) 推荐(0)

poj 3903
摘要:地址:http://poj.org/problem?id=3903题意:求最长上升子序列。mark:在网上学习一种O(nlgn)的算法,拿过来试验了一下。http://blog.sina.com.cn/s/blog_76344aef0100scyq.html代码:#include <stdio.h>int len;int a[100010];int d[100010];int find(int m){ int fr = 0, la = len-1, mid; while(fr <= la) { mid = (fr+la)/2; if(d[mid] ... 阅读全文

posted @ 2012-07-25 15:30 andre_joy 阅读(236) 评论(0) 推荐(0)

poj 1185
摘要:地址:http://poj.org/problem?id=1185题意:中文……mark:经典的状态压缩dp。第一次写,在网上看别人解题报告看了好多,百度一下就有很多。 推荐一个解释很详细的解题报告http://apps.hi.baidu.com/share/detail/14572384 wa了好多次。特别是match函数那,有很多a,b为0的时候的临界情况。 ps:别人的代码不一定是正确的,我找了一篇ac代码,然后比对数据,结果发现他的数据不对。囧……代码:#include <stdio.h>#include <string.h>#define N 61int dp 阅读全文

posted @ 2012-07-25 00:08 andre_joy 阅读(152) 评论(0) 推荐(0)

poj 2492
摘要:地址:http://poj.org/problem?id=2492题意:判断是否有同性恋。。。mark:解题报告主流思想是并查集,我用的bfs。 wa了很多次,都是一些不细心。而且这题居然每组数据后面都有一个空格。。。。代码:#include <stdio.h>#include <queue>#include <string.h>using namespace std;bool a[2010][2010];int b[2010];int f[2010];int m;int pd(){ int fr,la,i,j,p; queue<int> q; 阅读全文

posted @ 2012-07-23 12:28 andre_joy 阅读(228) 评论(0) 推荐(0)

poj 2442
摘要:地址:http://poj.org/problem?id=2442题意:m段序列,每个序列取一个数,求组成序列和最小的n个数。mark:最近刚接手的优先队列。代码:#include <stdio.h>#include <iostream>#include <stdlib.h>#include <queue>using namespace std;int a[2010], b[2010];int cmp(const void *a, const void *b){ return *(int *)a - *(int *)b;}int main(){ 阅读全文

posted @ 2012-07-22 10:13 andre_joy 阅读(116) 评论(0) 推荐(0)

POJ 1579
摘要:地址:http://poj.org/problem?id=1579题意:按照题目要求递归。mark:直接递归肯定是TLE的。记忆化深度搜索。代码:#include <stdio.h>int s[21][21][21];int w(int a, int b, int c){ if(a <= 0 || b <= 0 || c <= 0) return 1; if(a > 20 || b > 20 || c > 20) return s[20][20][20] = w(20, 20, 20); if(s[a][b][c]) return s[a][b] 阅读全文

posted @ 2012-07-12 18:50 andre_joy 阅读(168) 评论(0) 推荐(0)