08 2011 档案
Colored Sticks(彩色棒)
摘要:poj 2513题目大意:给一些彩色棒,看能不能组成一个解决:trie+无向图欧拉路(偶数结点度数为0或者2)clude <iostream> #include <cstring>#include <cstdio>using namespace std;struct node{ int next[26]; int wordNo;};int top;int num;node tree[2000000];short adj[510010];int data[510010];void init(){ top=1; num=0; memset(data,-1,si.
阅读全文
Keywords Search(查找关键字)
摘要:hdoj 2222题目大意:给出一些字符串 ,再给出一段文字,问文字中出现多少个单词解决:AC自动机#include <iostream>#include <cstring>#include <queue>#include <cstdio>using namespace std;#define s scanfint top;struct node{ int cnt; int fail; int next[26];};node tree[400000];char mai[1000005];void init(){ top=1; memset(tre
阅读全文
Oulipo
摘要:poj 3461题目大意:啥都不说了,就是一个模式匹配解决:KMP 刚开始一直wa,后来经同学提醒突然想到了溢出了,改了之后,一次就过了#include <iostream>#include <cstdio>#include <cstring>using namespace std;char s[1000005],t[10005];int next[10005],tlen,slen;void getnext(){ int i=1,j=0; next[1]=0; while(i<=tlen) {//本来的模式匹配 i < tlen 就结束了,但是为
阅读全文
Maze(迷宫)
摘要:poj 2157题目大意:S是起点,G是 终点,"."是可走的路,“X”是不可走的路解决:BFS 本题有些难度,因为若为钥匙,将钥匙吃了之后,将这个点变为“.”,若为门,判断是否对应该门的钥匙都拿到手了,若都拿到手了,可以将这个门打开,即变为“.”,否则,等这个点周围的所有点都进队列后,若队列为空说明路都走过了,一定无法通过,若队列非空,说明还有其他的路可以走,将这个点入队列,一会等着把钥匙都得到了,在出队列判断。#include <iostream>#include <cstdio>#include <queue>using names
阅读全文
搜索理论
摘要:宽度优先搜索bfs一、搜索的bfs,宽度优先搜索,一般用于求最短的得到到目的地的距离,有个起始点,先把这个起始点入队列,不要忘记将这个起始点标记为已经利用,不然会走回来的,然后是与这个起始点的周围的点的监测,若可以行的通,我们一一检测这些扩展出来的点,若是目的地就结束了,若不是目的地,将改点标记为已经利用,并将该点入队列。二、搜索的双向bfs,双向的bfs一般来说可以用单向解决,但是单向的效率上还是差了点,适合双向bfs的题目有一些共同特点:给出起始和最终状态,让求出一条从初始到末状态的最短路。双向bfs的实现过程: 从初始结点开始扩展,每扩展一层,在从目标节点按照产生系统相反的办法来扩展结点
阅读全文
hdoj分类
摘要:基础题:1000、1001、1004、1005、1008、1012、1013、1014、1017、1019、1021、1028、1029、1032、1037、1040、1048、1056、1058、1061、1070、1076、1089、1090、1091、1092、1093、1094、1095、1096、1097、1098、1106、1108、1157、1163、1164、1170、1194、1196、1197、1201、1202、1205、1219、1234、1235、1236、1248、1266、1279、1282、1283、1302、1303、1323、1326、1330、1334、1
阅读全文
Tempter of the Bone(诱惑的骨头)
摘要:hdoj 1010题目大意:给一个地图,在规定时间内刚好找到出口,不早也不能晚解决:dfs+剪枝#include <iostream>#include <cstdio>#include <cmath>using namespace std;#define s scanf#define p printf#define d "%d"char map[8][8];int n,m,step;bool escape;int sx,sy,ex,ey;int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};void dfs(in
阅读全文
Ultra-QuickSort(超快速排序)
摘要:题目大意:给出一些数,用最小的交换相邻两个数,使得为正序,求最小交换次数解决:可以用归并排序,由于在士兵杀敌系列中已经用过了,所以为了练练何为树状数组中的离散化,再用树状数组做了一遍/*本题思路,首先将范围在0 ≤ a[i] ≤ 999,999,999之间的点压缩到0- 500,000之间,然后统计每个数之前所有比这个数小的*/#include <iostream> #include <cstdio>#include <algorithm>#include <cstring>using namespace std;int n;struct nod
阅读全文
Matrix(矩阵)
摘要:poj 2155题目大意:第一行给出测试用例的次数,第二行第一个 给出矩阵的size,第二个给出命令的个数C表示更新数据,将范围在x1<=x<=x2,和y1<=y<=y2区域内的数每个都加上1,Q表示询问某一个点的大小解决:二维树状数组,与poj1195 mobile phones刚好相反,是插入区域,问点#include <iostream> #include <cstdio>#include <cstring>using namespace std;const int N=1010;int c[N][N];void init(){
阅读全文
Mobile phones(电话簿)
摘要:poj 1195题目大意:给出0时初始化,给出1时将矩阵中坐标为x,y的增加A,给出2时 查询区域为l<=x<=r, b<=r<=t范围内的总和解决:二维树状数组,只要知道一维中如何插点问段,这个便顺利写出#include <iostream>#include <cstdio>using namespace std;const int N=1200;int c[N][N];int n;void init(){ memset(c,0,sizeof(c));}int lowbit(int x){ return x&(-x);}void upd
阅读全文
Stars (星星)
摘要:poj 2352题目大意:解决:树状数组,关键是要将x轴的下标从一开始可以将x都加1#include <iostream>#include <cstdio>using namespace std;const int N=32005;int n;int c[N];int lev[N];int lowbit(int x){ return x&(-x);}void updata(int pos,int inc){ for(int i=pos;i<N;i+=lowbit(i)) c[i]+=inc;}int getsum(int pos){ int sum=0;
阅读全文
poj 搜索题目
摘要:简单搜索(1)深度优先搜索 (poj2488,poj3009,poj1321) (2)广度优先搜索 (poj3278,poj1426,poj3126,poj3087.poj3414,poj2251,poj3083)(3)简单搜索技巧和剪枝(poj2531,poj1416,poj2676,1129)搜索、回溯、遍历1022 1111 1118 1129 1190 1562 1564 1573 1655 2184 2225 2243 2312 2362 2378 23861010,1011,1018,1020,1054,1062,1256,1321,1363,1501,1650,...
阅读全文
Wormholes(虫洞)
摘要:poj3259题目大意:给出一个F代表农场的个数,其实就是测试样例组数,给出个N代表每个农场包含N个厂区,M代表N个厂区之间之间的路径条数,W表示有W个虫洞题目上说路径是双向的,虫洞是单向的,意味着是有向图,并且说虫洞从一个顶点到另一个顶点让时间倒流,意味着路的权值是负值,接下来的M行是路径的条数,并且题目已经说明是一个厂区到另一个厂区不一定只有一条路(Two fields might be connected by more than one path.),接下来的W行是虫洞的起始和结束位置,还有让时间倒退的值。解决:首先是建图,我用邻接矩阵,建图的时候要注意两个厂区之间不一定只有一条路,要
阅读全文
Currency Exchange(货币交换)
摘要:poj 1860题目大意:给出n货币种类数,从1....n,给出m表示交换货币的地点,s表示源点的编号,v表示起始总钱数,接下来m行,每行6个分别是从a到b和a到b的利率,a到b的费用,b到a的利率,b到a的费用,可以建图了。解决:spfa直接就出来了#include <iostream>#include <queue>using namespace std;struct node{ int v; double w,c; int next;};node e[100000];const int N=110;int pos;int head[N];int n,m,s;dou
阅读全文
转载:风云无际
摘要:ACM学习资料ACM基本算法分类、推荐学习资料和配套pku习题一.动态规划 参考资料: 刘汝佳《算法艺术与信息学竞赛》《算法导论》 推荐题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=1141 简单http://acm.pku.edu.cn/JudgeOnline/problem?id=2288 中等,经典TSP问题http://acm.pku.edu.cn/JudgeOnline/problem?id=2411 中等,状态压缩DPhttp://acm.pku.edu.cn/JudgeOnline/problem?id=1112 中等http:/
阅读全文
King(国王)
摘要:poj1364题目大意:给你n个数,组成一个集合s={a1,a2....an},再给你m个条件满足a[si]+a[si+1]...+a[si+ni] 后边一个关系,比如第一个条件是 a[1]+a[2]+a[3]>0然后转化为s[3]-s[0]>0>=1第二个条件是a[2]+a[3]+a[4]<2;转化为 s[4]-s[1]<2<=1这是做的第一道差分约束题,问了好几个同志,最终差不多了,就过了差分约束只是对于>= 或者<=使用,如果说求最大值的话,限制条件肯定是<=关系,然后建立图,求最短路径,找负环。相反的不说了。引用discuss里几句
阅读全文
Til the Cows Come Home(赶牛回家)
摘要:poj 2387题目大意:求出图中最短的路径解决:迪杰斯特拉 或者 spfa 在此处用spfa(队列)#include <iostream>#include <cstdio>#include <queue>using namespace std;const int N=1005;const int MAX=0x3f3f3f3f;int cost[N][N];int dist[N];bool inq[N];int n;void init(int v0){ for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) c
阅读全文
求逆序数
摘要:nyist 129题目大意:解决:归并排序#include <iostream> #include <cstdio>using namespace std;int num[1000005];int tmp[1000005];long long total;void merge(int beg,int mid,int end){ int i=beg,j=mid+1,k=0; while(i<=mid && j<=end) {//最关键的是下边这句话total+= mid-i+1;意思是前边比后边这个数大的个数 if(num[i]>num[
阅读全文
Arbitrage(套利)
摘要:poj 2240题目大意:给出不同的货币,然后是一种对另一种的兑换比率,若从一种货币,经过若干次兑换又换回本身,原来是1元,回来时比一元多,就输出Yes 否则输出No解决:floyd算法#include <iostream>#include <string>using namespace std;const int N=35;double cost[N][N];int n;void init(){ for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(i==j)cost[i][j]=1.0; else cost[i][j]
阅读全文
奥运排序问题
摘要:hdoj 3789题目大意:解决:用sort函数中不同的cmp15MS1212K#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N=1000000;struct node{int id; int gold;int total;double goldper;double totalper;};node nation[N];int num[N];bool mark[N];int rank[N]
阅读全文
Stockbroker Grapevine(投资经纪人)
摘要:poj1125题目大意:哎,本题读题就读了三遍才搞懂输入的究竟是什么东西,其实只要理解input和output就行了input是这样的,第一行输出经济人的个数,并且编号为1....n, 接下来n行每行第一个数为与n联系的人的个数m,接下来有m对,没对第一个表示与n联系的人的编号,第二个数表示两人取得联系的时间,toutput是这样的,输出从哪个人发出信息取得的总时间最短,并求出这个从该人向其他人发出信息最长时间解决:floyd算法求出各个点到其他点的最短时间,,并查集判断是否是连通的 ,代码比较简单就不加注释了#include <iostream>#include <cstd
阅读全文
Domino Effect(多米诺效应)
摘要:poj1135题目大意:多米诺的关键点从1......n,把1推倒,求出最后倒下的是哪个,或者是两点中间倒下解决:迪杰斯特拉求出从1到各个顶点的最短代价(此处为时间),找出最短代价中用时最长的那个,然后再枚举出在边中间到下的最长时间是哪两个点比较下最长的即为所求的结果#include <iostream>#include <cmath>#include <cstdio>using namespace std;#define MAX 0x3f3f3f3fconst int N=505;int cost[N][N];bool vis[N];int dist[N]
阅读全文
最短路
摘要:hdoj2544题目大意:给出两点和两点之间的时间代价,求出从起点到终点的最小时间解决:与hdoj 1874一模一样,地杰斯特拉算法求距离#include <iostream>using namespace std;int n;#define N 100#define MAX 0x3f3f3f3fint cost[N+10][N+10];int vis[N+10];int dist[N+10];void init(){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cost[i][j]=MAX;}void dijikstra()
阅读全文
畅通工程续
摘要:hdoj 1874题目大意:给出一个有向图,求从某点到另一点的最短路径解决:迪杰斯特拉求単源点到其它点的最短路径#include <iostream>using namespace std;#define N 200#define MAX 0xffffffint cost[N+10][N+10];int vis[N+10];int dist[N+10];int n;void init(){ for(int i=0;i<n;i++) for(int j=0;j<n;j++) cost[i][j]=MAX;}int dijistra(int start,int end){
阅读全文
欧拉路与欧拉回路
摘要:对无向图:定义:给定无孤立结点图G,若存在一条路,经过图中每条边一次且仅仅一次,该条路称欧拉路,若存在一条回路,经过图中每边一次且仅仅一次,该回路称为欧拉回路。具有欧拉回路的图称为欧拉图,不是柏拉图。定理:无向图G具有一条欧拉路,当且仅当G是连通的,且有0个或者是两个奇数度得结点。推论:无向图G具有一条欧拉回路,当且仅当G是连通的,并且所有结点的度数均为偶数。一笔画问题就是典型的这类问题:要判定一个图G是否可一笔画出,有两种情况, 从图中某一个结点出发,经过图G中每个边一次再回到该结点,或者是从G中某一个结点出发,经过G中每边一次且仅一次到达另一个结点,分别对应着欧拉回路和欧拉路的问题对有向图
阅读全文
统计难题
摘要:hdoj1251题目大意: 求出以某字符串为前缀的单词的数量解决:trie树#include <iostream>#include <cstring>using namespace std;struct node{ int cnt; int next[26];};node trie[3000000];int top=0;void init(){//初始化字典树,0号为根节点,只需将next值置为0就行了 memset(trie[0].next,0,sizeof(trie[0].next)); top=1;}void insert(char *str){ int i=0,
阅读全文
小希的迷宫
摘要:hdoj 1272题目大意:给出一个图,注意这是个有向图(小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)),由于这个错误调试了一上午解决:与poj 1308 一模一样,用并查集判断图的连通性,统计结点和边的个数来判断是否有回路#include <iostream>#include <set>#include <utility>using namespace std;int num[100005];bool mark[100005];int Max;set<pair<int,int> > s;int find(int x
阅读全文
Least Common Multiple(最小公倍数)
摘要:hdoj1019题目大意:给多个数,求这几个数的最小公倍数解决:递归#include <iostream>using namespace std;int gcd(int a,int b){ if(b==0)return a; else return gcd(b,a%b);}__int64 lcm(int num[],int n){ if(n==1)return num[0]; else { __int64 ret=lcm(num,n-1); return ret* num[n-1] /gcd(ret,num[n-1]); }}int main(){ int icase; cin&g
阅读全文
maze(迷宫)
摘要:poj 3984题目大意:解决:bfs,关键是对路径的保存#include <iostream>#include <cstring>#include <vector>#include <queue>using namespace std;int map[5][5];struct node{ int x,y;};//此处定义了一个存放上一个坐标的路径结构体,采用递归输出node path[5][5];queue<node> q;int sx=1,sy=1;int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};i
阅读全文
Sum It Up(加和)
摘要:zoj1711题目大意:给出一个和,求出所给数相加等于这个和的所有不同情况解决:The numbers in each list appear in nonincreasing order, and there may be repetitions.有序是前提,由于有4 6 4 3 2 2 1 1这样的数据存在,我们必须判断重复的等式6=3+2+1(第一个1)或者是6=3+2+1(第二个1)如何判断呢见代码//#include <iostream>#include <cstdio>#include <cstring>using namespace std;i
阅读全文
Additive equations(和式)
摘要:zoj 1204题目大意:给出一些数字,求出某几个数字之和等于给出的某一个数字 ,并按照要求输出解决: 排序,dfs#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int num[35];bool flag;int res[35],n;void dfs(int start,int dest_deep,int current_deep,int current_sum){ if(current_deep==des
阅读全文
sort漫谈
摘要:转载自:http://www.cppblog.com/changshoumeng/articles/114379.html#_Toc260776773详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握1 STL提供的Sort 算法 --------------------------------------------------------------------------------1.1 所有sort算法介绍1.2 sort 中的比较函数1.3 sort 的稳定性1.4 全排序1.5 局部排序1.6 nth_element 指定元素排序1.7 partition
阅读全文
phone list(电话簿)
摘要:poj3630题目大意:给若干字符串,判断是否是前缀解决:trie树的应用,由于字符串的长度不一插入是要注意#include <iostream>#include <cstdio>#include <string>#include <cstring>using namespace std;struct node{ bool isword; int next[10];};node tree[100000];int top;bool flag;void init(){ tree[0].isword=false; memset(tree[0].next
阅读全文
posted @ 2011-08-08 10:26
猿类的进化史
st算法介绍
摘要:实现过程及原理:首先是预处理用DP解决。设a[i]是要求区间最值的数列,f[i][j]表示从第i个数起连续2^j个数的最大值。例如数列3 2 4 5 6 8 1 2 9 7 ,f[1][0]表示从第一个数起,长度为2^0个数的最大值,其实就是第一个元素3本身。f[1][2]=5,f[1][3]=8…….从这里可以看出f[i][0]实际就是a[i]。这样dP的初始值就有了,剩下的就是状态转移方程。我们把f[i][j]平均分成两段(因为f[i][j]的值一定是个偶数0),从i到i+2^(j-1)-1为一段,i+2^(j-1)到i+2^j-1为一段。用上例说明,当i=1,j=3时就是3 2 4 5和
阅读全文
士兵杀敌(四)
摘要:题目解决方案一:线段树 1916ms 47100B C/C++可以看到用线段树解决非常的浪费空间#include <iostream>#include <cstdio>using namespace std;#define L(x) (x<<1)#define R(x) ((x<<1)+1)#define M(x,y) ((x+y)>>1)const int N=1000005;struct node{ int l,r,score;};node tree[4*N];/*此处建立的线段树为[a,b][b+1,c]类型,由于询问的是点的大
阅读全文
士兵杀敌(三)
摘要:题目描述解决方案一:用线段树解决,因为查询的是某区间的最值时间1396 空间 5304代码长度75#include <iostream>#include <algorithm>#include <cstdio>using namespace std;struct node{ int l,r,min,max;};const int N=100005;int num[N];#define L(x) (x<<1)#define R(x) ((x<<1)+1)#define M(x,y) ((x+y)>>1)node tree[4
阅读全文
士兵杀敌(二)
摘要:题目解决:(树状数组)本题是树状数组的基本应用符合两个特征,1、求区间和 2、修改的是单个元素 #include <iostream> #include <cstdio>using namespace std;const int N=1000005;int c[N];int n;//该函数功能是求出n二进制中最右边0的个数的2次幂,也等于c[n]包含的元素个数num[n-lowbit(n)+1]+...+num[n]int lowbit(int n){ return n&(-n);}//更新数组中pos的值使它加上inc,更新首先更新c[pos]一直更新该点的父
阅读全文
A Simple Problem with Integers(关于整数的问题)
摘要:poj 3468题目大意:给定Q(1 ≤ Q≤ 100,000)个数A1,A2… AQ,,以及可能多次进行的两个操作:1)对某个区间Ai … Aj的个数都加n(n可变)2) 求某个区间Ai … Aj的数的和2) 求某个区间Ai … Aj的数的和解决:线段树/*线段树的区间有两种形式,第一种是[1,2][2,3],第二种是[1,1],[2,2],在这里,由于我们查询的区间和是不能相交的,所以只能采用第二中形式,*/#include <iostream>#include <cstdio>using namespace std;typedef long long intt;s
阅读全文
Expressions(表达式)
摘要:NKoj1751题目大意:给出后缀表达式序列,求出层次遍历的结果解决:还原树的形状,bfs之后就是结果,先直接开辟树结点数组,将数据导入结点,然后寻找节点间父子关系,从而完成建树过程ac代码: 1 #include <iostream> 2 #include <cctype> 3 #include <queue> 4 #include <string> 5 #include <vector> 6 using namespace std; 7 string str; 8 struct node 9 {10 char data;11 bo
阅读全文
Tree Recovery(将树复原)
摘要:poj 2255题目大意:给出先序遍历和中序遍历树的结果,求出后续遍历的结果解决:将树还原,根据先序和中序的结果还原树的形状,然后再后续遍历出结果 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 struct node 5 { 6 char data; 7 node *lch,*rch; 8 node(){lch=rch=NULL;} 9 };10 node *root=NULL;11 //创建树的过程12 /*13 pre代表先序序列,in代表中序序列,此处一定要用引用14 */15
阅读全文
浙公网安备 33010602011771号