_xiaobai_

导航

2011年8月18日

zoj3310 Unrequited Love (DP)

摘要: /*不错的题目:如果没有首尾相连的话,这个题目就很简单了: 动态方程:F( n ) = max( F[ n-1 ], F[ n-2 ] +D[ n ] ) 题目分析: 现在告诉我们首尾相连,为了无后效性,我们要把环拆开。 我们把起始时分为两种状况,取或不取,当取第一个元素时,我们DP到n-1位置;当不取第一个元素时,我们DP到n位置。 时间复杂性:O(n) */View Code 1 #include <stdio.h> 2 #include <string.h> 3 4 int D[ 1000002 ]; 5 int F[ 1000002 ]; 6 int G[ 10 阅读全文

posted @ 2011-08-18 12:50 _xiaobai_ 阅读(377) 评论(0) 推荐(0) 编辑

zoj2271 Chance to Encounter a Girl(DP)

摘要: /*概率计算:按时间为阶段,每个点由上一阶段周围的四个点来维护。注意事项:1.时间O(N^3*T),在问题的边缘时间,所以打表计算。 2.关于概率的求解,如果遇到就结束了,所以向后走就说明 之前没有碰到,所以不用前面的碰到的概率计算后面的值。 */View Code 1 #include <stdio.h> 2 #include <string.h> 3 4 double answ[ 51 ] = { 5 0.0000,0.6667,0.0000,0.4074,0.0000, 6 0.3361,0.0000,0.2928,0.0000,0.2629, 7 0.0000, 阅读全文

posted @ 2011-08-18 12:18 _xiaobai_ 阅读(568) 评论(0) 推荐(0) 编辑

zoj1093 Monkey and Banana(DP)

摘要: /* 经典模型:最大不下降子序列,将每个箱子旋转三次即可,用贪心可证,长边对齐原则 */View Code 1 #include <iostream> 2 #include <cstdlib> 3 4 using namespace std; 5 6 int P[ 6 ][ 3 ] = { 7 0,1,2,0,2,1, 8 1,0,2,1,2,0, 9 2,0,1,2,1,0};10 11 struct node{12 int D[ 3 ];13 }N[ 31 ],S[ 185 ];14 15 int DP[ 185 ];16 17 int cmp( const voi 阅读全文

posted @ 2011-08-18 02:11 _xiaobai_ 阅读(186) 评论(0) 推荐(0) 编辑

zoj1022 Parallel Expectations(DP)

摘要: /* 这道题算不上难题,不过算得上的麻烦题了。设状态 T[ i ][ j ] 为程序1执行i条指令,程序2执行j条指令后的变量平均值,P1为程序1指令i的概率,P2为程序2指令j的概率则容易推出,T[ i ][ j ] = (T[ i-1 ][ j ]*P1+T[ i ][ j-1 ]*P2)/(P1+P2) 不过需要注意几点: 1.理解题意很重要,这个题很容易误解 。如果认为每种指令执行的情况是一样的话,就会求错方程:(设N[ i ][ j ]为状态T[ i ][ j ]数量 ) P1 = N[ i-1 ][ j ]/N[ i ][ j ]P2 = N[ i ][ j-1 ]/N[ i ][ 阅读全文

posted @ 2011-08-18 02:08 _xiaobai_ 阅读(674) 评论(1) 推荐(0) 编辑

zoj1986 Bridging Signals(DP)

摘要: /* 单调队列优化的最大上升子序列 O(NlogN)算法*/View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int data[ 40000 ]; 6 int Queu[ 40000 ]; 7 8 int Search( int h, int key ) 9 {10 if ( key <= Queu[ 0 ] ) return 0;11 int m,l = 0;12 while ( l < h ) {13 m = (h+l)>> 阅读全文

posted @ 2011-08-18 02:05 _xiaobai_ 阅读(200) 评论(0) 推荐(0) 编辑

zoj1642 Match for Bonus(DP)

摘要: /*简单DP:裸的最大公共子序列 */View Code 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 5 using namespace std; 6 7 int CtoI( char ch ) 8 { 9 if ( ch >= 'a' && ch <= 'z' )10 return ch - 'a';11 else return ch - 'A' + 26;12 }13 14 i 阅读全文

posted @ 2011-08-18 02:02 _xiaobai_ 阅读(206) 评论(0) 推荐(0) 编辑

zoj1245 Triangles(DP)

摘要: /*动态三角形:每次DP时考虑的是两个子三角形的高度即可注意:三角形可以是倒置的。 */View Code 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <stdio.h> 5 6 using namespace std; 7 8 char Tri[ 101 ][ 201 ]; 9 short Val[ 101 ][ 201 ];10 11 int main()12 {13 int n,cases = 1;14 while ( scanf(&q 阅读全文

posted @ 2011-08-18 02:01 _xiaobai_ 阅读(227) 评论(0) 推荐(0) 编辑

zoj1163 The Staircases(DP)

摘要: /*01背包:整数拆分问题 */View Code 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 5 using namespace std; 6 7 long long dp[ 501 ]; 8 9 int main()10 {11 memset( dp, 0, sizeof( dp ) );12 dp[ 0 ]= 1;13 for ( int i = 1 ; i <= 500 ; ++ i )14 for ( int j = 500 ; j >= i ; - 阅读全文

posted @ 2011-08-18 01:58 _xiaobai_ 阅读(183) 评论(0) 推荐(0) 编辑

zoj1134 Strategic Game(DP/图论)

摘要: /*简单的树形DP:利用搜索正向求解。 0的孩子必须是1,1的孩子任意,取最小的。 */View Code 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define min(x,y) ((x)<(y)?(x):(y)) 6 7 typedef struct node 8 { 9 int Count;10 int Value;11 int Next[ 10 ];12 }node;13 node Node[ 1510 ];14 int Root;15 16 ty 阅读全文

posted @ 2011-08-18 01:55 _xiaobai_ 阅读(211) 评论(0) 推荐(0) 编辑

zoj1094 Matrix Chain Multiplication(模拟)

摘要: /*简单的模拟:可以用栈,也可以用递归实现*/View Code 1 #include <iostream> 2 #include <cstring> 3 4 using namespace std; 5 6 struct node 7 { 8 int L,R; 9 long long V;10 }Matrix[ 27 ],Error;11 12 bool Flag;13 14 node Calculate( char* Data, int s, int e )15 {16 int count = 0;17 node Save[ 2 ];18 for ( int i = 阅读全文

posted @ 2011-08-18 01:48 _xiaobai_ 阅读(269) 评论(0) 推荐(0) 编辑

zoj1027 Human Gene Functions(DP)

摘要: /*经典模型:最长公共子序列 */View Code 1 #include <iostream> 2 #include <cstdlib> 3 4 using namespace std; 5 6 int Geno[ 210 ][ 210 ]; 7 int Matr[ 5 ][ 5 ] = { 8 { 5,-1,-2,-1,-3}, 9 {-1, 5,-3,-2,-4},10 {-2,-3, 5,-2,-2},11 {-1,-2,-2, 5,-1},12 {-3,-4,-2,-1, 0}};13 14 int V( char C )15 {16 switch( C ) 阅读全文

posted @ 2011-08-18 01:46 _xiaobai_ 阅读(177) 评论(0) 推荐(0) 编辑

zoj1558 Euro Efficiency(DP)

摘要: /*背包变形:如果按照完全背包计算会有后效性,所以用01背包计算注意两点:1.数据的范围,因为要做差,所以数据要超过100 2.背包时物品的顺序,本题的解会受到顺序影响, 所以先对所有数据01背包,然后再次01背包。补充:忽然发现bfs是DP,按广度为阶段,并且每个值最求一次#24*/View Code 1 #include <stdio.h> 2 #include <iostream> 3 4 using namespace std; 5 6 int F[ 205 ]; 7 int C[ 7 ]; 8 9 int main()10 {11 int T;12 while 阅读全文

posted @ 2011-08-18 01:06 _xiaobai_ 阅读(320) 评论(0) 推荐(0) 编辑