状态压缩DP具体可参考周伟的《状态压缩》。状态方程可列为dp[i][s]=dp[i-1][s']+dp[i-1][s] (sum[s]+1<=i)或者dp[i][s]=dp[i-1][s'] sum[s]==i其中i表示第几行 s表示状态 s'表示s的子状态#include<cstdio>#include<iostream>#include<string.h>using namespace std;const int maxn=1<<11;int state[maxn],sum[maxn];int dp[11][ma Read More
posted @ 2013-04-24 00:54 longlongago Views(212) Comments(0) Diggs(0) Edit
首先一个别人总结的模版,我觉得很好就引用一下/**高精度模版*/#include <stdio.h>#include <string.h>#include <iostream>using namespace std;const int MAXL = 500;struct BigNum{ int num[MAXL]; int len;};//高精度比较 a > b return 1, a == b return 0; a < b return -1;int Comp(BigNum &a, BigNum &b){ int i; if( Read More
posted @ 2013-03-18 22:30 longlongago Views(171) Comments(0) Diggs(0) Edit
/***假设有两条直线分别为m、n。直线m的直线方程为F(x,y)=a1*x+b1*y+c1=0。设直线m的两端点为(x1,y1)、(x2,y2),代入方程得a1*x1+b1*y1+c1=0,a1*x2+b1*y2+c1=0,联立解可得 a1=y1-y2,b1=x2-x1,c1=x1*y2-x2*y1。设直线n的两端点分别为(x3,y3)、(x4,y4),同理可得a2=y3-y4,b2=x4-x3,c2=x3*y4-x4*y3。又因为两直线相交即 a1*x+b1*y+c1=a2*x+b2*y+c2可解得x=c2*b1-b2*c1/D,y=c1*a2-c2*a1/D (D=a1*b2-a2*b1 Read More
posted @ 2013-03-15 21:37 longlongago Views(686) Comments(0) Diggs(0) Edit
题意大概为有一个箱子被格成n+1部分,往箱子里随机放m个玩具,问各个部分最终各有几个玩具。#include<iostream>#include<cstdio>#include<string.h>using namespace std;const int maxn=1<<13;struct LINE{ int x_1,y_1,x_2,y_2;}line[maxn];int point[maxn][2];bool judge(int x1,int y1,int x0,int y0,int x2,int y2){ int p=(x1-x0)*(y2-y Read More
posted @ 2013-03-12 13:31 longlongago Views(116) Comments(0) Diggs(0) Edit
线段树的基本题。题意大概是:人们依次来排队并插队,按人到来的顺序给出每个人插队的位置(插在第几个人后面),并告知每个人的id号,输出最终的情况。与poj2182有点像,只不过将数字的大小改成插入的先后顺序。#include<iostream>#include<stdio.h>#define MAXD 200001using namespace std;int N,P[MAXD],V[MAXD],f[MAXD],sum[MAXD*3],D,re[MAXD];void init(){ for(D=1;D<N+2;D<<=1); int i; for(i=1 Read More
posted @ 2013-02-03 19:31 longlongago Views(123) Comments(0) Diggs(0) Edit
线段树的基本题。询问给定区间内最大值与最小值的差。分别建个最大堆和最小堆即可。#include<stdio.h>#include<string.h>#include<iostream>#define MAXD 1000001using namespace std;int N,Q,maxt[2*MAXD],mint[2*MAXD],a[MAXD],D;int great(int a,int b){ return a>b?a:b;}int small(int a,int b){ return a<b?a:b;}int Qmax(int x,int y Read More
posted @ 2013-02-01 18:09 longlongago Views(125) Comments(0) Diggs(0) Edit
KMP的模版题。#include<iostream>#include<stdio.h>#include<string.h>#define MAXD 10001using namespace std;int N,q[MAXD],RES;char T[MAXD*100],P[MAXD];void PREFIX(){ int m=strlen(P); q[0]=-1; int k=-1; int i; for(i=1;i<m;i++) { while(k>=0&&P[k+1]!=P[i]) k=q[k]; if(P[k+1... Read More
posted @ 2013-01-29 11:37 longlongago Views(113) Comments(0) Diggs(0) Edit
字符串的基本题,暴力也可以过。第一个#之前是字典中的字符串,第二个#之前是要check的字符串。逐个讨论,相等的话就直接输出correct。字符串长度相同或相差一的就看是否只有一个字符不同。#include<string.h>#include<stdio.h>#include<iostream>using namespace std;char dic[10001][16],check[51][16];int main(){ //freopen("test.txt","r",stdin); int flag=0; cha Read More
posted @ 2013-01-28 18:46 longlongago Views(148) Comments(0) Diggs(0) Edit
最基本的二分匹配,用匈牙利算法DFS实现。#include<iostream>#include<string.h>#include<stdio.h>#define MAXD 301#define INF 0x3f3f3f3fusing namespace std;int P,N,res,cx[MAXD],cy[MAXD];bool edge[MAXD][MAXD],vis[MAXD];int path(int u){ int i; for(i=1;i<=N;i++) { if(edge[u][i]&&!vis[i]) { vi... Read More
posted @ 2013-01-24 09:48 longlongago Views(117) Comments(0) Diggs(0) Edit
BFS+prim在prim之前很暴力地求出各点之间距离。要注意只有在A和S处可以分开,题目有描述。读取x和y后要用gets读取换行符。#include<iostream>#include<stdio.h>#include<string.h>#include<queue>#define MAXD 51#define INF 0x3f3f3f3fusing namespace std;int N,graph[MAXD][MAXD],dis[MAXD][MAXD],a[MAXD*2][2],x,y,cost[MAXD*2][MAXD*2],e;char Read More
posted @ 2013-01-22 18:36 longlongago Views(157) Comments(0) Diggs(0) Edit