01 2013 档案
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1421DPdfs记忆化搜索写的,没空间优化 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define N 2013 5 6 int n, k, a[N], dp[N][N]; 7 8 int min(int x, int y) 9 {10 return x<y? x: y;11 }12 13 int sq(int x)14 {15 return x*x;16 }17 18 int dfs(int i, int j)19 {20
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1058DP#include <stdio.h>#define N 5848int h[N];int a[4];int p[4] = {1, 1, 1, 1};int min(){ int i, flag = 0; for(i=1; i<4; i++) { if(a[i] < a[flag]) { flag = i; } } for(i=0; i<4; i++) { if(a[i] == a[fla...
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1024DP最大m个不重叠连续子段和 1 #include <stdio.h> 2 3 #define N 1000100 4 5 int a[N], dp1[N], dp2[N]; 6 7 int max(int x, int y) 8 { 9 return x>y? x: y;10 }11 12 int main()13 {14 int i, j, n, m;15 while(~scanf("%d%d", &m, &n))16 {17 for(i=1;
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1160DP,LIS 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct Point 5 { 6 int x, y, num; 7 }p[1234]; 8 9 int cmp(const void *a, const void *b)10 {11 return ((struct Point *)b)->x - ((struct Point *)a)->x;12 }13 14 int n, dp[1234], pre[12
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2602DP,0-1背包 1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 1234 5 6 int n, m; 7 int dp[N], v[N], w[N]; 8 9 int main()10 {11 int t, i, j;12 scanf("%d", &t);13 while(t-- && scanf("%d%d", &n, &m))14
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1114DP,完全背包 1 #include <stdio.h> 2 3 int main() 4 { 5 int t, i, j, n, m, m1, v, w; 6 int dp[10010]; 7 scanf("%d", &t); 8 while(t-- && scanf("%d%d%d", &m1, &m, &n)) 9 {10 m -= m1;11 dp[0] = 0;12 for(i=1; i<
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1203DP,0-1背包和0-1背包稍有差别:状态转移方程:dp[j] = max(dp[j], dp[j-w[i]]*v[i])P(至少一份offer) = 1 - P(一个offer也没有)P(一个offer也没有) = Mul(1 - P(拿到 i 学校的offer)) (其中i为选择的学校)#include <stdio.h>#define N 10010int n, m;float dp[N], v[N];int w[N];int main(){ int i, j; while(scanf
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2844DP,部分背包耗费:钱币的面值,价值:全为1,数量:每种钱币的数量,求恰好装满的dp值,初始化dp[0]=0,dp[1~m] = “负无穷”,最后遍历dp[1~m],如果能恰好装满(dp值为正)就算一种,统计一共有多少种 1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 100100 5 6 int n, m; 7 int dp[N], w[123], num[123]; 8 const int minint = -
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2191DP,部分背包 1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 123 5 6 int n, m; 7 int dp[N], w[N], v[N], num[N]; 8 9 void pack01(int wi, int vi)10 {11 int j;12 for(j=m; j>=wi; j--)13 {14 if(dp[j-wi]+vi > dp[j])15 {16 ...
阅读全文
摘要:http://poj.org/problem?id=3273二分查找一个数组大小为n,分成连续的m部分,求和最大的那个部分把n分成m部分,由于弱菜代码能力比较差,处理下标问题很晕。。。改用“栈”模拟,不用考虑下标问题#include <stdio.h>#include <stack>#define N 100100using namespace std;int n, a[N];int need(int x){ int i, temp; stack<int> s; for(i=1; i<=n; i++) { if(s.empty() || s.top()
阅读全文
摘要:http://poj.org/problem?id=1496求组合数 1 #include <stdio.h> 2 #include <string.h> 3 4 int C(int x, int y) 5 { 6 return y? C(x-1, y-1)*x/y: 1; 7 } 8 9 int main()10 {11 char s[12] = "\0";12 int a[12] = {0};13 int i, j, sum, n, flag;14 while(~scanf("%s", s+1))15 {16 flag =..
阅读全文
摘要:http://poj.org/problem?id=1083哪个门前的走廊,被经过的次数最多,这个次数就是答案 1 #include <stdio.h> 2 3 int main() 4 { 5 int t, n, i, a, b, sum[432] = {0}, result; 6 scanf("%d", &t); 7 while(t-- && scanf("%d", &n)) 8 { 9 while(n-- && scanf("%d%d", &a, &b)
阅读全文
摘要:http://poj.org/problem?id=1189DP,“数塔” 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[56][56]; 5 long long dp[56][56]; 6 7 long long gcd(long long x, long long y) 8 { 9 return y? gcd(y, x%y): x;10 }11 12 13 int main()14 {15 int i, j, k, n, m;16 long long r;17 char s[1234] = "\.
阅读全文
摘要:http://poj.org/problem?id=1018DP,写的好蒙。。。 1 #include <stdio.h> 2 #include <map> 3 4 using namespace std; 5 6 #define N 110 7 8 int b[N][N], p[N][N]; 9 int dp[N][N*N];10 const int maxint = 1<<30;11 12 int main()13 {14 int i, j, t, n, m, x, y, k;15 float max1, temp1;16 map<int, int
阅读全文
摘要:http://poj.org/problem?id=1065DP,最长“上升”子序列 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct Sticks 5 { 6 int x, y; 7 }s[5432]; 8 9 int n, dp[5432];10 11 int cmp1(struct Sticks a, struct Sticks b)12 {13 return a.x < b.x || a.y < b.y;14 }15 16 int LIS()17 {18 int i, j, max1, max
阅读全文
摘要:http://poj.org/problem?id=3250单调队列,栈模拟有 n 头牛头朝东站成一列。每头牛有一定的高度,并且能看到其前面高度比它低的牛的头顶,直到被某头高度大于等于它的高度的牛所挡住。计算每头牛能看到的牛头顶的数量之和。 1 #include <stdio.h> 2 #include <stack> 3 4 using namespace std; 5 6 stack<pair<int, int> > s; 7 long long sum = 0; 8 9 void push(int a, int i)10 {11 while
阅读全文
摘要:http://poj.org/problem?id=2704基础DP 1 #include <stdio.h> 2 3 int main() 4 { 5 int i, j, k, n, map[36][36]; 6 long long dp[36][36]; 7 char c; 8 while(scanf("%d%*c", &n), ~n) 9 {10 //init11 for(i=1; i<=n; i++)12 {13 for(j=1; j<=n; j++)14 ...
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1829并查集,二分图识别如果A喜欢B,不能直接判断A,B的性别,但是可以知道,A喜欢的人和B是同性的(一个集合)。不直接合并A,B所在集合,而间接合并A喜欢的人所在集合 与 B所在集合,使问题得到简化。建立一个like数组,储存A喜欢的一个人为like[A](任意一个都可以),1.初始化:开始让每个人喜欢自己,like[i] = i;在执行合并操作(3)之前,如果A,B双方中还有喜欢自己的,则调整为喜欢对方,like[A] = B, like[B] = A;2.查询操作:如果A和B在一个集合中,则AB属于同性
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1856并查集,集合计数多加一个sum数组,统计集合元素个数,初始化为1 1 #include <stdio.h> 2 #define N 10001000 3 4 int n, f[N], sum[N], max = 1;//f-->father 5 6 int find(int x) 7 { 8 return x-f[x]? f[x]=find(f[x]): x; 9 }10 11 void merge(int x, int y)12 {13 int r1, r2;//r-->roo
阅读全文
摘要:http://poj.org/problem?id=3692二分图匹配,匈牙利算法(dfs+邻接矩阵),最大独立集最大独立集 == N - 最小点覆盖 == N - 最大匹配数把没有关系的两点连接,反向建图 1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 210 5 6 int n, m, map[N][N], girl[N], flag[N]; 7 8 int find(int x) 9 {10 int i;11 for(i=1; i<=m; i++)12 {13 if(!flag...
阅读全文
摘要:http://poj.org/problem?id=1274二分图匹配,匈牙利算法(dfs+二维数组模拟邻接链表)裸题,不用自己建图,用邻接链表试了一下 1 #include <stdio.h> 2 #include <string.h> 3 4 int n, m; 5 int list[210][210], girl[210], flag[210]; 6 7 int find(int x) 8 { 9 int i;10 for(i=1; i<=list[x][0]; i++)11 {12 if(!flag[list[x][i]])13 ...
阅读全文
摘要:http://poj.org/problem?id=3020二分图匹配,匈牙利算法(dfs+邻接矩阵)建图:相邻点双向连接,建无向图结果(最少需要的“圈”数) ==(总点数 - 最大匹配数)/*没有匹配成功的点需要自己单独一个“圈”*/ + (最大匹配数 / 2) /*匹配成功的点,2个点共用一个"圈"*/化简为:结果 == 总点数 - (最大匹配数 / 2) 1 #include <stdio.h> 2 #include <string.h> 3 #define N 432 4 5 int n, m; 6 int map[N][N], flag[N
阅读全文

浙公网安备 33010602011771号