07 2012 档案

摘要:题意:求组成数n的组合数分析:动态规划,完全背包#include <stdio.h>#include <string.h>#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)#define re2(i,l,r) for(int i=l;i<r;i++)#define re3(i,l,r) for(int i=l;i<=r;i++)#define clr(x,y) memset(x,y,sizeof(x))int dp[200] , n;int main 阅读全文
posted @ 2012-07-12 22:22 lenohoo 阅读(208) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2012-07-12 15:56 lenohoo 阅读(87) 评论(0) 推荐(0)
摘要:时间复杂度:n*log nView Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 50005;int dp[maxn] , a[maxn];int Lis(int n) { int len = 1 , left , right , mid; dp[1] = a[1]; for(int i=2;i<=n;i++) { left = 1; right = len; while(left <= r... 阅读全文
posted @ 2012-07-11 13:14 lenohoo 阅读(157) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2012-07-10 18:24 lenohoo 阅读(162) 评论(0) 推荐(0)
摘要:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1200题意:两种提问方式: P:输入:对于每一个数1,2……n他之前有几个数比他大 输出:输出n个数的排列方式 I:输入:输入n个数的排列方式 输出:对于每一个数1,2……n他之前有几个数比他大分析:模拟View Code #include <cstdio>#include <cstring>#include <iostream>#include <vector>using namespace std;#define re(i,n) for 阅读全文
posted @ 2012-07-10 12:51 lenohoo 阅读(171) 评论(0) 推荐(0)
摘要:题意:类似 套汇,问货币轮流转移一圈会不会有盈利分析:有spfa算法,这里因为数据量比较小所以用了floydView Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)#define clr(x,y) memset(x,y,sizeof(x))#define inf (1<<29)c 阅读全文
posted @ 2012-07-10 05:47 lenohoo 阅读(253) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2012-07-08 20:29 lenohoo 阅读(2285) 评论(0) 推荐(0)
摘要:流体阻力:F=-kv 阅读全文
posted @ 2012-07-08 10:42 lenohoo 阅读(227) 评论(0) 推荐(0)
摘要:题意:一开始有n个点m条边,后来每次加一条边,求加上这一条边后途中有几座“桥”。分析:双连通分量View Code #include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define maxn 100010struct Edge{ int u,v,next; int vis;}edge[1000005];int head[maxn];int E=0;void addedge(int u,int v){ edge[E].u 阅读全文
posted @ 2012-07-08 03:10 lenohoo 阅读(241) 评论(0) 推荐(0)
摘要:题意:给你n个城市的信息,包括两个城市之间的火车费 和 城市里的公交车费,现在给你两座城市,求从一个城市的火车站出发到达另一座城市的火车站最少花费。分析:floyd 。 注意记录路径,以P[i][j] 表示 i城市 到 j城市 的 最短路经上的 直接与i相邻下一个火车站,每次floyd,如果V[i][j] >V[i][k]+V[k][j],则直接把两条路径叠加到一起,如果V[i][j] == V[i][k] + V[k][j],则对i-->j 和 i-->k-->j的两条路径比较字典序,取小者View Code #include <cstdio>#inclu 阅读全文
posted @ 2012-07-07 00:55 lenohoo 阅读(259) 评论(0) 推荐(0)
摘要:DP的思想:从i到i+2^(j-1)-1为一段,i+2^(j-1)到i+2^j-1为一段(长度都为2^(j-1)),状态转移方程为: F[i,j]=max(F[i,j-1],F[i+2^(j-i),j-1])最大值 f[i,j]=max(f[i,j-1],f[i+2^(j-1),j-1]);最小值 f[i,j]=min(f[i,j-1],f[i+2^(j-1),j-1]);View Code #include<cstdio>#include<cstring>#include<iostream>#include<cmath>using namesp 阅读全文
posted @ 2012-07-06 21:02 lenohoo 阅读(196) 评论(0) 推荐(0)
摘要:题意:给你n个数和m个大小关系,问给出了这些大小关系之后还有多少对数的大小关系不知道。分析:根据给出的大小关系建有向边,每次对每个点延有向边搜索到底,确定该边的所有连边关系,最后统计没有关系的边对数。View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)const int maxn = 2 阅读全文
posted @ 2012-07-05 20:27 lenohoo 阅读(215) 评论(0) 推荐(0)
摘要:题意:给你C个字母,输出由L个字母组成并且包含至少一个元音字母和两个辅音字母的所有由L个字母组成的排列。分析:dfsView Code #include <cstdio>#include <cstring>#include <iostream>#include <vector>#include <algorithm>using namespace std;char pan[30] , ss[5];int L , C;vector<char> V;bool cmp(char a , char b) { return a &l 阅读全文
posted @ 2012-07-05 19:24 lenohoo 阅读(305) 评论(0) 推荐(0)
摘要:题意:给你一些边,判断这些边能否构成一棵树。分析:并查集,但是得注意: 1.可能存在自环 2.每个数据开始输入为0 0 的时候也算一棵树View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)const int maxn = 110;int p[maxn];int flag[maxn];in 阅读全文
posted @ 2012-07-05 17:15 lenohoo 阅读(218) 评论(0) 推荐(0)
摘要:题意:求其试点发出的到其他各点的最短路的最大值。分析:最短路View Code #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)#define inf (1<<29)const int maxn = 110;int map[maxn][ma 阅读全文
posted @ 2012-07-05 15:45 lenohoo 阅读(255) 评论(0) 推荐(0)
摘要:题意:有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都出列(总人数不足S个时将循环报数),求小孩出列的顺序。分析:约瑟夫问题View Code #include <cstdio>#include <cstring>#include <iostream>#include <string>using namespace std;const int maxn =65;string name[maxn];int w , s , n;in 阅读全文
posted @ 2012-07-05 15:26 lenohoo 阅读(188) 评论(0) 推荐(0)
摘要:题意:输入一对字符串,按1999取数,判断最后剩下的数,是'?'输出“Yes” ,是' '输出“No”,不然输出“No comments”。分析:约瑟夫环问题。View Code #include <cstdio>#include <cstring>#include <iostream>#include <string>using namespace std;string s;int N = 1999;char c;int main() { while((c = getchar()) != EOF) { if(c 阅读全文
posted @ 2012-07-05 14:58 lenohoo 阅读(178) 评论(0) 推荐(0)
摘要:题意:给你n张牌,让你变一个魔术:第1次把上面的1张牌放到底部,然后最上面的牌就是1,然后拿走1。第2次把上面的2张牌依次放到底部,然后最上面的牌就是2,然后拿走2....重复这个过程,直到所有的牌都被拿走。问一开始的牌应该从上到下怎么放,才能完成这个魔术。分析:逆向思维,从后向前模拟。View Code #include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;queue<int> Q;void output() 阅读全文
posted @ 2012-07-05 12:54 lenohoo 阅读(143) 评论(0) 推荐(0)
摘要:题意:给你n个学生和m对关系,每对关系表示这两个学生有同一个宗教信仰,求学校里最多有几种不同的宗教信仰。分析:并查集,初始有n个几何,每次关系都把两个元素所在的集合合到一起。View Code #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)#defi 阅读全文
posted @ 2012-07-05 04:19 lenohoo 阅读(135) 评论(0) 推荐(0)
摘要:题意:给你一个序列,问是否能够通过入栈出栈从1到n的顺序序列得到该序列分析:模拟栈View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)const int maxn =1010;int a[maxn] , sta[maxn];bool pan(int a[],int n) { int to 阅读全文
posted @ 2012-07-05 03:52 lenohoo 阅读(186) 评论(0) 推荐(0)
摘要:题意:N的虫里面有M对情侣关系,判断其中存不存在“基情”,即在之前确定的异性关系中出现了同性的情侣。分析:我们可以假设异性在不同的并查集里,那么假设有一对情侣a和b是异性关系,那么a和b的异性是同性关系,b和a的异性也是同性关系,即a和b的异性 以及 b和a的异性 在同一个并查集里。为了模拟这个过程,我们假设对于任意一个元素c , c+n是他的默认异性,接下来每一次合并之前判断有没有矛盾就行了。View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std 阅读全文
posted @ 2012-07-05 03:28 lenohoo 阅读(204) 评论(0) 推荐(0)
摘要:题意:给出一个0-1矩阵,判断是否能从其中选择几行使得选出的行中每一列都有且仅有一个1。分析:dfs , 此题也可以用Dancing Links 优化,因为是一道精确覆盖的题普通搜索:View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 18 , maxm = 303;int n , m;int a[maxn][maxm];bool vis[maxm];int ok;bool pan(int i) { for(i 阅读全文
posted @ 2012-07-05 02:49 lenohoo 阅读(294) 评论(0) 推荐(0)
摘要:题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个终点 ,问最少进行几次 切换操作 能够 使 火车 完成这个历程 , 如果开不到,输出“-1”。分析:设默认路径边权为0,备选路径边权为1,求单源最短路即可。View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re(i,n) for( 阅读全文
posted @ 2012-07-05 00:05 lenohoo 阅读(283) 评论(0) 推荐(0)
摘要:题意:有N个客户,M个仓库,和K种货物。已知每个客户需要每种货物的数量,每个仓库存储每种货物的数量,每个仓库运输各种货物去各个客户的单位费用。判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用。分析:首先判断每个仓库的库存是否满足N个客户的需要,在这个条件下,因为有K种货物,而K种货物又是从不同的仓库发出的,所以对于每一种货物,我们可以设想起其为一种费用,对每种货物进行一次 最小费用最大流即可。View Code #include <cstdio>#include <cstring>#include <iostream>#include & 阅读全文
posted @ 2012-07-04 18:33 lenohoo 阅读(138) 评论(0) 推荐(0)
摘要:题意:给出20个国家的连接情况,然后求出连接2个目标国家的最少边数。算法:转化为最短路问题。将每条边的权值设为1,“最少边数”即等价为“最短路”View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re1(i,n) for(int i=1;i<=n;i++)const int maxn = 21;int map[maxn][maxn];int main() { int n , m; int cas = 1; while(~s 阅读全文
posted @ 2012-07-04 04:28 lenohoo 阅读(186) 评论(0) 推荐(0)
摘要:题意:有一迷宫,求从入口到出口的最短路、左优先最短路、右优先最短路。算法:bfs+dfsView Code #include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int maxn = 50;bool vis[maxn][maxn];char map[maxn][maxn];int ldir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };int rdir[4][2] = { {-1, 阅读全文
posted @ 2012-07-04 02:54 lenohoo 阅读(150) 评论(0) 推荐(0)
摘要:题意:已知起点s,求从s出发,依次到达n个点,然后再回到起点s所需的最短路径。分析:dfs,从第一个点开始,枚举所有的边,遍历到头,当枚举完所有的点。View Code #include <cstdio>#include <cstring>#include <iostream>#include <cmath>#include <cstdlib>using namespace std;int x[500] , y[500];bool vis[500];int n;int GetDis(int a, int b) { return abs 阅读全文
posted @ 2012-07-03 20:56 lenohoo 阅读(179) 评论(0) 推荐(0)
摘要:题意:给你n个数,求出所有6个数的组合数分析:dfsView Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;int b[30];int a[10];int n;void Output() { printf("%d",b[a[0]]); for(int i=1;i<6;i++) printf(" %d",b[a[i]]); printf("\n");}void dfs(int dep, 阅读全文
posted @ 2012-07-03 15:08 lenohoo 阅读(216) 评论(0) 推荐(0)
摘要:题意: n个人 玩k张牌,你给你上家开始发牌,每发一张,你得把牌首的p张牌放到牌尾去,输出你最终得到的牌的初始位置。分析:模拟View Code #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn =100100;bool b[maxn];int a[maxn];/ 当前已经发到第i张牌,返回下一张牌的编号int GetNextNumber(int i , int p ,int k) 阅读全文
posted @ 2012-07-03 14:18 lenohoo 阅读(570) 评论(0) 推荐(0)
摘要:题意:从点(0,0)出发,目的地为(x,y),其中有一些点不能通过,求最短距离。分析:bfs View Code #include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int maxn = 1010;bool vis[maxn][maxn];int dir[4][2] = { {0,-1},{0,1},{-1,0},{1,0} };struct Node { int x,y,step; };int X ,Y , 阅读全文
posted @ 2012-07-03 02:18 lenohoo 阅读(253) 评论(0) 推荐(0)
摘要:题意:求最大环的边数,给出一个无向图,图中每条边的长度都是1,求图中最长环的长度是多少。分析:dfs , 开一个数组pan作访问标记View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;const int maxn = 5000 , maxm = 500050;int pan[maxn];bool vis[maxn];struct Edge{ int v , next; }edge[maxm];int E , head[maxn];int n , 阅读全文
posted @ 2012-07-03 01:44 lenohoo 阅读(200) 评论(0) 推荐(0)
摘要:题意:给出若干个矩形的上下边,如定义的边界,求大于这个边界 且 长和宽递增的最长矩阵序列 并 输出分析:对宽排序 , 对长进行DP 最长上升子序列 处理#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define re(i,n) for(int i=0;i<n;i++)const int maxn = 100100;int dp[maxn];int pre[maxn];struct Pan { int 阅读全文
posted @ 2012-07-02 23:43 lenohoo 阅读(235) 评论(0) 推荐(0)
摘要:题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h。现在知道了出发地和终点的坐标,以及这些地铁线路每个 站点的坐标,你的步行速度为10km/h,且你到了地铁的任意一个站之后就刚好有地铁出发。问你从出发点到终点最少需要多少时间。分析:按“车能开的地方坐车,车不能开的地方走路”的想法建一个图,结果就是求最短路,因为数据范围很小,所以用floyd算法,没有使用较复杂的单源最短路算法View Code #include <cstdio>#include <cstring>#include <iostream>#incl 阅读全文
posted @ 2012-07-02 13:05 lenohoo 阅读(342) 评论(0) 推荐(0)
摘要:题意:有n个城市,m条连接两个城市的道路(双向边),每条道路有自己的最大复载量。现在问从城市a到城市b,车上的最大载重能为多少。即求从a到b的路径中权值最小边的最大值分析:贪心的思想,我们每次找 道路载重量最大的路加入集合中,其思想于最短路思想基本上是一样的,所以用最短路的思想解题,dist值改为路径上最大上限(相当于最大流,这里用最短路来解)View Code #include <cstdio>#include <cstring>#include <iostream>#include <string>#include <map>us 阅读全文
posted @ 2012-07-02 04:41 lenohoo 阅读(215) 评论(0) 推荐(0)
摘要:题意:给你一个*n的有向图,其中第i行第j列元素表示i—>j的最短路,求途中最少有几条边?分析: 假设一开始是个完全图,就有n*(n-1)条边,然后考虑的时候再删除重边; 如果出现i—>k +k—>j < i—>j 的情况,因为是最短路所以出错,输出"impossible"; 不然,如果存在 i—>k +k—>j == i—>j , 那么直接的边 i —> j 就可以删去,原图中就少了一个点View Code #include <cstdio>#include <cstring>#include 阅读全文
posted @ 2012-07-02 03:03 lenohoo 阅读(183) 评论(0) 推荐(0)
摘要:题意:有k头牛n个农场面条有向边(拓扑有序,不存在环),告诉你所有奶牛的初始位置,求所有奶牛能够到达的公共点。记录每头奶牛能够到达的位子,最后判断一下就可以了。View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)const int maxn = 1010 , maxm = 10010;i 阅读全文
posted @ 2012-07-02 02:47 lenohoo 阅读(131) 评论(0) 推荐(0)
摘要:题意:在一个n*m的格子里有b个障碍物,每个障碍物都有一个标记表示他的周围(上下左右)必须有且只有几个灯泡,想在让你方灯泡,要求灯泡彼此之间不能被照到,而且每个空白区域一定要被照到,求最少的灯泡数分析:dfs + 剪枝View Code #include <cstdio>#include <cstring>#include <iostream>using namespace std;#define re(i,n) for(int i=0;i<n;i++)#define re1(i,n) for(int i=1;i<=n;i++)#define i 阅读全文
posted @ 2012-07-01 23:27 lenohoo 阅读(435) 评论(0) 推荐(0)
摘要:题意:要建一棵圣诞树,使得总的花费最小。具体规则是:圣诞树是一颗无向树形图,其中,编号为1的节点为根节点,原始图中每条边具有边权:材料的单位价值;每个点也有一个权:点的重量。生成树中,各条边的花费是该边权* 该边的子树中所有点的重量和,总的花费则是生成树中所有边的花费之和。题目要求是这样一棵树,每条边的权值是该边和所有子节点的点权和的乘积,那么,将我们的角度从边转移到点,对于每一个点,他所涉及的权值就是他延一条边到达源点1的距离*它本身的权值,对每个点都是如此;所以求出点1到每个点的单源最短路,每个点的距离乘点权和就是答案。View Code #include <cstdio>#i 阅读全文
posted @ 2012-07-01 01:01 lenohoo 阅读(253) 评论(0) 推荐(0)