摘要: 题解:完全背包问题(背包要装满)#include using namespace std; int e,b,n; int f[10005]; int p[505],w[505]; int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d%d",&e,&b); e=b-e; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d%d",&p[i],&w[i]); for( 阅读全文
posted @ 2014-01-22 13:49 forever97 阅读(168) 评论(0) 推荐(0) 编辑
摘要: 题解:简单动态规划#include #include using namespace std;int f[21][1001];int max(int a,int b){return(a>b?a:b);}int main(){ int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) scanf("%d",&f[i][j 阅读全文
posted @ 2014-01-22 13:06 forever97 阅读(162) 评论(0) 推荐(0) 编辑
摘要: 题解:我们把每一秒可以走到的地方标注起来 5 4 5 6 3 4 5 6 7 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 90 1 2 3 4 5 6 7 8 9 10很显然,这是一座数塔,f[i,j]只可以从f[i+1,j],f[i+1,j-1],f[i+1,j+1]中来,那么就是简单的DP了。#include #include using namespace std;int f[100001][13];int max(int a,int b){return(a>b?a:b);}int main(){ int n; while(scanf("%... 阅读全文
posted @ 2014-01-22 10:44 forever97 阅读(134) 评论(0) 推荐(0) 编辑
摘要: 题解:经典数塔问题#include #include using namespace std;#define rep(i,m,n) for(int i=m;ib?a:b);}int main(){ int t; scanf("%d",&t); while(t--) { memset(f,0,sizeof(f)); int n; scanf("%d",&n); rep(i,1,n)rep(j,1,i)scanf("%d",&f[i][j]); for(int i=n-1; i; i--)rep(j,1,... 阅读全文
posted @ 2014-01-22 09:56 forever97 阅读(124) 评论(0) 推荐(0) 编辑
摘要: 题解:用优先队列,建立小根堆,然后每次取出最小的,将其乘上题目要求的数字再加入优先队列,至于重复的问题,用映射来解决#include #include #include #include using namespace std; #define MAXI 5843 priority_queue, greater > Q; long long hn[MAXI], l; map exi; void init() { long long tmp; l = 1; Q.push(1); while (l<MAXI) ... 阅读全文
posted @ 2014-01-22 09:15 forever97 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 题解:最长上升子序列的应用,按w升序,s降序排列后进行DP找最长序列,用pre[]数组记录该点的前驱,从而打印路径#include #include using namespace std; struct mouse{ int w,s,id; bool operatorm.s); } }m[1005]; int d[1005],pre[1005],ms=1,ans[1005]; void solve(){ d[0]=-1; int head=0; for(int i=1;im[j].w&&m[i].sd[t]... 阅读全文
posted @ 2014-01-22 08:09 forever97 阅读(170) 评论(0) 推荐(0) 编辑
摘要: 题解:最长上升子序列的扩展应用,不过,加上的是当前值,而不是1/*最长上升子序列扩展应用*/#include using namespace std;int n,d[1050],a[1050];int dp(){ int rs=0; for(int i=1;ia[j]&&a[i]+d[j]>d[i])d[i]=a[i]+d[j]; if(d[i]>rs)rs=d[i]; } return rs;}int main(){ while(scanf("%d",&n),n) { for(int i=1;i<=n;i++)scanf(&quo 阅读全文
posted @ 2014-01-22 08:04 forever97 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 题解:裸的LCS/*LCS*/#include #include using namespace std;int f[1000][1000]={0};int main(){ string a,b; while(cin>>a>>b) { int m,n; n=a.length(); m=b.length(); memset(f,0,sizeof(f)); for (int i=0; if[i+1][j])?f[i+1][j+1]=f[i][j+1]:f[i+1][j+1]=f[i+1][j]; ... 阅读全文
posted @ 2014-01-22 07:59 forever97 阅读(106) 评论(0) 推荐(0) 编辑
摘要: 题目大意:求出数列的最大子段和,并且说明是从第几项至第几项。题解1:简单贪心。#include #define rep(i,n) for(int i=1;iusing namespace std;int a[100005],s[100005],num[100005];int T,n,m,st,ed... 阅读全文
posted @ 2014-01-22 07:54 forever97 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 题解:构建Trie图 由图可知,设f(n)为字符串长度为n时复合条件的字符串个数,以字符串最后一个字符为分界点,当最后一个字符为m时前n-1个字符没有限制,即为f(n-1);当最后一个字符为f时就必须去除最后3个字符是fmf和fff的情况,在考虑最后两个字符为mf和ff的情况,显然不行;最后3个字符为fmf、mmf和fff、mff时只有当最后3个字符为mmf时前n-3个字符没有限制,即为f(n-3),当为mff时第n-3个字符可能为f因而对前n-3个字符串有限制;最后4个字符为fmff和mmff时mmff可行。得到公式f(n)=f(n-1)+f(n-3)+f(n-4) 然后,构建矩阵进行幂.. 阅读全文
posted @ 2014-01-22 07:50 forever97 阅读(204) 评论(0) 推荐(0) 编辑
摘要: 题解:考虑长为n的串,以s[i]表示i位的字符。1.若前n-1位组成的串合法,则由于首尾不同,再添加一位时,只有1种方法;即s[n] = s[n-1]2.若前n-1位组成的串不合法,再添加一位后合法,即因为首尾相同而引起的不合法,那么前n-2位组成的串必定合法。此时第n位有2种添加方法。即s[n] = 2*s[n-2]3.边界条件:f(1)=3;f(2)=6;f(3)=6#include using namespace std;long long f[51];int main(){ int n; f[1]=3; f[2]=6; f[3]=6; for(int i=4; i>n... 阅读全文
posted @ 2014-01-21 13:25 forever97 阅读(114) 评论(0) 推荐(0) 编辑
摘要: 递推式:f[n]=2*f[n-2]+f[n-1]#include #include using namespace std;long long f[51];int main(){ int i,n; f[1]=1; f[2]=3; for(int i=3; i>n; while(cin>>n) cout<<f[n]<<endl; return 0;} 阅读全文
posted @ 2014-01-21 11:09 forever97 阅读(98) 评论(0) 推荐(0) 编辑
摘要: 题解:http://www.cnblogs.com/forever97/p/3522238.html#include int main(){ int n; scanf("%d",&n); while(scanf("%d",&n)!=EOF) printf("%d\n",2*n*n-n+1); return 0;} 阅读全文
posted @ 2014-01-21 10:13 forever97 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 递推式:f[n]=f[n-1]+f[n-3]#include #include using namespace std;long long f[56];int main(){ int i,n; f[0]=1; f[1]=1; f[2]=2; for(int i=3; i>n,n!=0) cout<<f[n]<<endl; return 0;} 阅读全文
posted @ 2014-01-21 10:07 forever97 阅读(150) 评论(0) 推荐(0) 编辑
摘要: 题解:简单的斐波那契数列#include #include using namespace std;long long f[51];int main(){ int i,n; f[1]=1; f[2]=2; for(int i=3; i>n) cout<<f[n]<<endl; return 0;} 阅读全文
posted @ 2014-01-21 09:59 forever97 阅读(98) 评论(0) 推荐(0) 编辑
摘要: 题解:将m与n相减,又回到超级楼梯这道题。#include #include using namespace std;long long f[50];int main(){ int i,t; f[1]=1; f[2]=2; for(int i=3; i<50; i++) f[i]=f[i-1]+f[i-2]; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); n=m-n; cout<<f[n]<<endl; } ret... 阅读全文
posted @ 2014-01-21 09:49 forever97 阅读(112) 评论(0) 推荐(0) 编辑
摘要: 递推式:f[n]=f[n-1]+f[n-2]#include int f[41];int main(){ int i,t; f[1]=1; f[2]=1; for(int i=3; i<41; i++) f[i]=f[i-1]+f[i-2]; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); printf("%d\n",f[n]); } return 0;} 阅读全文
posted @ 2014-01-21 09:38 forever97 阅读(107) 评论(0) 推荐(0) 编辑
摘要: 题解:卡特兰数的几何意义,所以答案就是卡特兰数的两倍#include #include using namespace std;#define base 10000#define len 100void multiply(int a[],int max,int b){ int i,array=... 阅读全文
posted @ 2014-01-21 09:28 forever97 阅读(180) 评论(0) 推荐(0) 编辑
摘要: 题解:利用卡特兰数的几何意义,题目就可以转化为一个棋盘格,可以向下走或是向右走,但是不可以逾越对角线,就可以了。#include #include using namespace std;long long f[21][21];int main(){ int m,n; for(int i=1; i>m>>n) cout<<f[m][n]<<endl; return 0;} 阅读全文
posted @ 2014-01-21 09:17 forever97 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 卡特兰数再乘上n的阶乘#include#includeusing namespace std;#define base 10000#define len 100void multiply(int a[],int max,int b){ int i,array=0; for(i=max-1;i>=0;i--) { array+=b*a[i]; a[i]=array%base; array/=base; }}void divide(int a[],int max,int b){ int i,div=0; for(... 阅读全文
posted @ 2014-01-21 08:27 forever97 阅读(112) 评论(0) 推荐(0) 编辑