随笔分类 - ACM
摘要:01背包问题:这是最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。用子问题定义状态:即c[i][v]表示前i件物品恰放入一个重量为m的背包可以获得的最大价值。则其状态转移方程便是:c[i][m]=max{c[i-1][m],c[i-1][m-w[i]]+p[i]}这个方程非常重要,基本上所有跟背包相关的问题的方程都是由它衍生出来的。所以有必要将它详细解释一下:“将前i件物品放入重量为m的背包中”这个子问题,若只考虑第i件物品的策略(放或不放),那么就可以转化为一个只牵扯前i-1件物品的问题。如果不放第i件物品,那么问题就转化为“前i-1件物品放入容量为v的背包中”,价值为c[i-
阅读全文
摘要:只需在最长上升子序列中多加一个判断条件即可View Code 1 #include <iostream> 2 using namespace std; 3 double missile[1001]; 4 int dp[1001]; 5 int main() 6 { 7 int n; 8 while(1) 9 {10 cin>>n;11 if(n==0)12 break;13 for(int i=1;i<=n;i++)14 cin>>missile[i];15 ...
阅读全文
摘要:最长上升子序列先dp,O(n^2)太慢了,超时。10000的规模需要1千万,不可接受,5000000可以接受。View Code 1 #include <stdio.h> 2 3 int signal[40001]; 4 int length[40001]; 5 int main() 6 { 7 int test,p; 8 scanf("%d",&test); 9 for(int t=1;t<=test;t++){10 scanf("%d",&p);11 for(int i=1;i<=p;i++)12 scan..
阅读全文
摘要:一维的dpView Code 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 string dict[600]; 5 int dp[301]; 6 #define min(a,b) (a<b)?a:b 7 int main() 8 { 9 int W,L;10 int pd,pm;11 string msg;12 cin>>W>>L;13 cin>>msg;14 for(int i=0;i<W;i++)15 cin>>dict
阅读全文
摘要:这种循环的倒是第一次见View Code 1 #include <stdio.h> 2 3 int necklace[200]; 4 int m[101][101]; 5 int dp(int c[],int n) 6 { 7 for(int i=1;i<=2*n;i++) m[i][i]=0; 8 for(int r=2;r<=n;r++) 9 for(int i=1;i<2*n-r+1;i++){ //2n no value10 int j=i+r-1;11 m[i][j]=m[i+1][j]+c...
阅读全文
摘要:几乎与1753一样。不过flip的时候,多加了一个判断,居然就超时了。。。。。用位运算的方法特别好,flip的操作特别快。不过没做View Code 1 #include <stdio.h> 2 bool handlers[5][5]; //false means closed, true means open 3 bool isflip[5][5]={false}; 4 int step; 5 void flip(int row,int col) 6 { 7 int i; 8 handlers[row][col]=!handlers[row][col]; 9 ...
阅读全文
摘要:看成是一棵深度为16的树,每一个格子有两个状态,flip或non-flip,构成一棵二叉树。深搜这棵树就可以得到结果最少的轮数,可以给深搜加一条件: flip step次的时候,检查是否完成,step从0~16,这样就可以得到最少的轮数了。感觉可以用宽搜View Code 1 #include <stdio.h> 2 bool chess[5][5]; 3 int step; 4 void flip(int row,int col) 5 { 6 chess[row][col]=!chess[row][col]; 7 if(row-1>0) 8 chess[ro...
阅读全文
摘要:真是不知道为什么,枚举的时候明明没有枚举全啊!选定一个最小bandwidth之后,只枚举了一种情况,然后就进入下一个bandwidth的循环了。比如最小带宽为80,后面有两个数据(85,100,2)和(86,50,2),这个时候循环就只会去枚举(85,100,2),不会去枚举(86,50,2),为啥这样也能通过呢。。。。。 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 struct Device 6 { 7 int band
阅读全文
摘要:几经波折虽然写出了递归式,但是一提交,超时。然后改成了不用递归,先算出所有值,存到数组里,结果WA。然后发现,n=100时数太大太大,要用大数来处理。不想写大数的程序,所以改用java写,终于ACC++的两个版本:View Code 1 #include <stdio.h> 2 #include <string.h> 3 int connection[201]; 4 int connect(int m,int n) 5 { 6 if(n==m+1) 7 return 1; 8 if(m>=n) 9 return 1;10 //n>m+1...
阅读全文
摘要:测试数据没有问题的,但是不知为什么超时先转为后缀表达式,把括号去掉;再转为中缀表达式,转化成输出形式转化过程,想清楚了其实很简单 1 #include <string> 2 #include <iostream> 3 using namespace std; 4 int grade(char ch) 5 { 6 switch(ch) 7 { 8 case '*': 9 case '/': 10 return 2; 11 case '+': 12 case '-': 13 ...
阅读全文
摘要:转自http://www.cnblogs.com/gaojun/archive/2010/09/11/1824016.htmlC++中string是标准库中一种容器,相当于保存元素类型为char的vector容器(自己理解),这个类提供了相当丰富的函数来完成对字符串操作,以及与C风格字符串之间转换,下面是对string一些总结<引用> 一,C语言的字符串在C语言里,对字符串的处理一项都是一件比较痛苦的事情,因为通常在实现字符串的操作的时候都会用到最不容易驾驭的类型——指针。比如下面这个例子://example 1:char str[12] = "Hello";c
阅读全文
摘要:1 #include <stdio.h> 2 #include <string.h> 3 4 char preorder[30]; 5 char inorder[30]; 6 char tree[1000000]; //tree index starts from 1 //树的深度问题,2^h数组大小,选得太小会AW 7 char postorder[30]; 8 int postindex; 9 int search(char ch,char array[],int start,int end)10 {11 for(int i=start;i<=end;i++)
阅读全文
摘要:注意corridor只有200个,第n个corridor对应room 2n-1和2n 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 5 using namespace std; 6 int corridor[201]; 7 bool compare(int a,int b) 8 { 9 return a>b;10 }11 int main()12 {13 int cases;14 int n;15 int from,to;16 int start,end;17 .
阅读全文
摘要:求出素数表之后暴力搜索 1 #include <stdio.h> 2 #include <string.h> 3 int prime[10000]; 4 bool number[10001]; 5 int constructPrime(int n) 6 { 7 int i,j; 8 int pc=0; 9 memset(number,false,sizeof(number));10 for(i=2;i<=n;i++)11 if(!number[i])12 {13 prime[pc++]=i;14 ...
阅读全文
摘要:如果n是素数,则2n,3n,4n....都是合数1 void GenPrimes (int n){2 int i , j , k; pc = 0;3 memset (mk , false , n +1);4 for(i=2; i <=n; i++)5 if (! mk[i]){6 primes [pc ++] = i;7 for(j=i+i; j <=n; j+=i) mk[j] = true ;8 }9 }
阅读全文
摘要:测试数据没有问题,但是不知道为什么老得到Wrong Answer 1 #include <stdio.h> 2 #include <string.h> 3 #define MAXLEN 1000 4 struct decimal 5 { 6 int num[MAXLEN]; 7 int len; 8 int frac; 9 }; 10 11 void Product(decimal d1, decimal d2,decimal& d3) 12 { 13 int i,j; //i points to d1,j points to d2...
阅读全文

浙公网安备 33010602011771号