![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
POJ
无敌大饺子
这个作者很懒,什么都没留下…
展开
-
POJ 1041 - John's trip
第一次写欧拉回路,哭了#include #include #include #include #include using namespace std; #define MAXSTREET 2000 #define MAXJUNC 50 int path[MAXSTREET],dg[MAXJUNC],pNum,maxj; struct Edge{ int v,num,deleted;原创 2013-03-29 11:09:49 · 571 阅读 · 0 评论 -
POJ 3628 Bookshelf 2(背包)
刚开始看到题目发现数据竟然这么大,然后一直在找解决方法,最后竟然不用考虑,poj的数据很小. 太坑爹了. #include #include #include #include using namespace std; const int maxn=20000001; int dp[maxn],t[21],n,b; int main(){ while (scanf("%d%d",&n原创 2013-04-22 19:49:17 · 573 阅读 · 0 评论 -
POJ 3253 Fence Repair(哈夫曼)
题目有点坑竟然只有1个数据,并且要用long long. 每次都选优先队列中的最小的两个,并把相加的结果压回队列直到队列中只剩一个元素 #include #include #include #include #include using namespace std; int n; int main(){ scanf("%d",&n); priority_queue,great原创 2013-04-17 18:15:36 · 422 阅读 · 0 评论 -
POJ 3264 Balanced Lineup (RMQ)
RMQ(Range Minimum/Maximum Query)问题: RMQ问题是求给定区间中的最值问题。当然,最简单的算法是O(n)的,但是对于查询次数很多(设置多大100万次),O(n)的算法效率不够。可以用线段树将算法优化到O(logn)(在线段树中保存线段的最值)。不过,Sparse_Table算法才是最好的:它可以在O(nlogn)的预处理以后实现O(1)的查询效率。下面把Spa原创 2013-04-26 10:31:50 · 510 阅读 · 0 评论 -
ZOJ 1655 Transport Goods(dijkstra最短路)
首先按照求n到各个点的最短距离,松弛条件是dis[v] 然后像拓扑排序一样对最短路上每一个点设置一个入度,只有点的入度为0的时候,才把这个点上的东西送到路径上的下一个点 #include #include #include #include #include using namespace std; #define pdi pair const int maxn=110; doub原创 2013-04-19 16:19:57 · 458 阅读 · 0 评论 -
POJ 2777 Count Color(线段树+位运算)
更新的时候要用延迟标记,用位运算保存儿子结点合并的结果,强大啊. #include #include #include using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=100010; int segs[maxn*3],L,T,O; bool f[maxn*3]; ch原创 2013-04-29 10:21:00 · 495 阅读 · 0 评论 -
POJ 1273 Drainage Ditches(最大流入门题)
里面可能有重边 可以把重边全部加在一起就可以了,用的是EK算法. #include #include #include #include using namespace std; #define INF 0X50505050 const int maxn = 205; int g[maxn][maxn], fa[maxn], n, m; bool vis[maxn]; bool b原创 2013-05-02 15:35:18 · 552 阅读 · 0 评论 -
POJ 2250 Compromise(LCS问题)
最长公共子序列打印序列. 用一个path数组记录路径. #include #include #include using namespace std; const int MAX = 101; short dp[MAX][MAX]; short path[MAX][MAX]; char pro1[MAX][31], pro2[MAX][31]; void print_path(int原创 2014-03-14 16:38:47 · 387 阅读 · 0 评论 -
POJ 1958 Strange Towers of Hanoi
求4条柱子的汉诺塔问题所需要的移动次数. 题目中已经给出了算法,实现就可以了. 代码里把k值打表了. #include #include using namespace std; int ans = 0; const int Ok[13] = {0, 0, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4}; int memo[13]; int tower_3(int原创 2014-03-14 15:58:06 · 543 阅读 · 0 评论 -
POJ2192 | ZOJ 2401 Zipper
题意:给出3个字符串A,B,C 判断第3个字符串是否可以通过前2个串穿插变成. 设dp[i][j]表示A的前i个字符串和B的前j个字符串是否能够构成C的前i + j个字符. 则dp[i][j] = max(dp[i - 1][j] * (A[i - 1] == C[i + j - 1]), dp[i][j - 1] * (B[j - 1] == C[i + j - 1])).这里字符串索引从0原创 2014-03-15 14:16:05 · 456 阅读 · 0 评论 -
POJ 3356 AGTC(经典DP Edit Distance)
题意:给出2个字符串x, y,长度为n,m,允许使用3种操作,求把x变成y需要的最小步骤,也就是要求两个字符串的编辑距离. 插入:在x的任意位置插入一个字符. 删除:在x的任意位置删除一个字符. 替换:在x的任意位置替换一个字符成任意字符. 设dp[i][j]为x后缀[i:]变成y的后缀[j:]所需要的最小步骤([i:]代表从i位置开始一直到尾部的字符串) 则: 如果x[i] == y原创 2014-03-15 16:36:34 · 861 阅读 · 0 评论 -
POJ 1157 LITTLE SHOP OF FLOWERS
题意:给出F朵花,V个花瓶,每朵花插入每个花瓶都有一个美观值,要求第i朵花所在的花瓶号小于第j朵花所在的花瓶号(i 设dp[i][j]为前i朵花插入到前j个花瓶能得到的最大美观值(i 则 如果i == 1,dp[i][j] = max(dp[i][j - 1], val[i][j]) 否则如果j > V - F + i(i能插的地方只能在[i, V - F + i]号花瓶里,所以):d原创 2014-03-15 20:21:24 · 552 阅读 · 0 评论 -
POJ 1887 Testing the CATCHER
最长递减序列问题. #include #include #include using namespace std; const int MAX = 5000; int dp[MAX], g[MAX], A[MAX]; int main(int argc, char const *argv[]){ int h, caseno = 1; while(scanf("%d", &h) &&原创 2014-03-15 17:04:10 · 320 阅读 · 0 评论 -
POJ 1745 Divisibility
题意:给出N数字,在N个数字中放入N - 1个+,-号,判断是否存在一种放置方案,使得结果能整除K, 设dp[i][j]为前i个数字除K的余数j是否存在,那么答案就是dp[N][0]是否为true. 因为(a + b) % c == (a % c + b % c ) % c, (a - b) % c == (a % c - b % c) % c 那么dp[i + 1][(j + val[原创 2014-03-30 12:58:57 · 811 阅读 · 0 评论 -
POJ 2948 Martian Mining(DP)
题意:(摘自刘汝佳的训练指南)给出n*m网格中每个格子的A矿和B矿的数量,A矿必须由右向左运输,B矿必须由下向上运输,管子不能拐弯或间断(即往上和往左的路线不能有交集)。要求使收集到的A,B矿总量尽量大。 思路:考虑到管子不能转弯和间断,可以设dp[i][j]为以i,j为左下角的最大矿总量. 开两个辅助数组A[i][j]为在i,j位置的时候建A管子的时候从i,j到i, 1的总和, B[i][j原创 2014-04-21 09:44:31 · 906 阅读 · 0 评论 -
POJ 1093 Formatting Text(DP)
题意:给出一段文章,要求将里面的单词按照n个字符一行来分,不够一行的可以在单词间插入空格来得到,一行有一个basness值,为一个每个gap之间的空格数-1的平方,求badness总和最少的分隔方式. 经典DP,设dp[i]为从第i开始到第n单词的最少badness和分隔方式,那么dp[i] = min(dp[j + 1] + badness(i, j)).badness函数计算第i个单词到第j原创 2014-04-05 18:35:23 · 931 阅读 · 0 评论 -
POJ 2965 The Pilots Brothers' refrigerator(BFS+二进制判重)
很奇怪,提交时候用C++就超时而G++则不会. 875MS过的 #include #include #include #include const int maxn=65538; using namespace std; struct state{ int data; int cp; }; bool vis[maxn]; state q[maxn]; int fa[maxn];原创 2013-04-13 10:10:20 · 459 阅读 · 0 评论 -
POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (筛选法素数打表)
#include #include using namespace std; const int maxn=1000100; bool isP[maxn]; void init(){ for (int i=2;i<maxn;++i){ isP[i]=1; } for (int i=2;i<maxn;++i){ if(isP[i]){ for (int j=2;j*i<max原创 2013-04-13 11:27:21 · 550 阅读 · 0 评论 -
POJ 3468 A Simple Problem with Integers
#include #include #include using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=100000; long long f[maxn<<2],sum[maxn<<2],N,Q; void pushUp(int rt){ sum[rt]=sum[rt原创 2013-04-02 12:32:36 · 470 阅读 · 0 评论 -
POJ 2488 A Knight's Journey
经典的骑士巡游问题,注意字典序最小,应该从上到下,从左到右遍历 #include #include #include using namespace std; const int maxn=30; int di[]={-1,1,-2,2,-2,2,-1,1},dj[]={-2,-2,-1,-1,1,1,2,2},n,m; bool vis[maxn][maxn]; vectorsta原创 2013-04-04 21:45:12 · 802 阅读 · 0 评论 -
ZOJ 1403 Safecracker
简单的回溯搜索题,枚举每5个字母的120中组合 #include #include #include #include #include #include #include using namespace std; vectorstak,ans; int len,target; char str[15]; void backtracking(int curi){ if(sta原创 2013-04-03 21:05:38 · 600 阅读 · 0 评论 -
POJ 1321 棋盘问题
找到第一个有#的行开始回溯就可以了 #include #include using namespace std; const int maxn=9; char board[maxn][maxn]; bool C[maxn]; int ans,n,k; void backtracking(int curi,int cnt){ if(cnt==0){ ans++; return ;原创 2013-04-05 15:40:27 · 450 阅读 · 0 评论 -
POJ 3278 Catch That Cow
注意x等于0的时候就不要减一了,同样x*2大于10W也不行 #include #include #include #include using namespace std; const int maxn=100010; bool vis[maxn]; int x,k; struct state { int x,t; state(int xx=0,int tt=0):x(xx),t(t原创 2013-04-05 16:45:37 · 448 阅读 · 0 评论 -
POJ 3009 Curling 2.0
刚开始因为是广搜,果断超时超内存,其实是深搜回溯 #include #include #include using namespace std; const int maxn=21; char maze[maxn][maxn]; int di[]={-1,1,0,0},dj[]={0,0,-1,1},n,m,ans; void dfs(int ci,int cj,int cnt){原创 2013-04-05 14:21:56 · 498 阅读 · 0 评论 -
POJ 2251 Dungeon Master
三维的迷宫问题 //2013年1月3日 11:08:23 //author:zxj #include #include #include using namespace std; struct point{ int i,j,z; point(int ii=0,int jj=0,int zz=0):i(ii),j(jj),z(zz){} }; int L,R,C; int tl,tr原创 2013-04-05 16:06:44 · 451 阅读 · 0 评论 -
POJ 1426/ ZOJ 1530 Find The Multiple
直接用BFS打表了,STL的queue速度比较慢,198和99串比较长就手动了,蛋疼 #include #include #include using namespace std; const int maxn=201; long long table[201]; int n; void init(){ for (int i=1;i<maxn;++i) { if(i==198原创 2013-04-05 17:26:47 · 740 阅读 · 0 评论 -
POJ 3126 Prime Path
素数打表,普通广搜 #include #include #include using namespace std; struct state { int num,cost; state(int nnum=0,int ccost=0):num(nnum),cost(ccost){} }; const int maxn=10010; bool table[10010],vis[maxn原创 2013-04-05 21:34:21 · 391 阅读 · 0 评论 -
POJ 1988 Cube Stacking
可以用并查集来做,不进行路径压缩的话直接TLE 用一个cnt数组来表示在X下面的方块个数,每次count的时候进行路径压缩而不是在find时候压缩: cnt[X]=cnt[id[X];//将X到其父亲之间的方块个数加上父亲到爷爷之间的方块个数 id[X]=id[id[x]];//然后把X当前的父亲改成爷爷 在find的时候按照路径找上去 #include #include usi原创 2013-04-06 14:16:23 · 439 阅读 · 0 评论 -
POJ 2236 Wireless Network
普通的并查集操作 #include #include #include #include using namespace std; const int maxn=1010; int id[maxn],size[maxn],x[maxn],y[maxn],n,d; vectorg[maxn]; bool isRepaired[maxn]; double dis(int x1,int y1原创 2013-04-06 16:53:16 · 390 阅读 · 0 评论 -
POJ 1703 Find them, Catch them
并查集操作 查: 1.如果两人的祖先相等,则两人属于同一个帮派 2.如果一人的祖先等于对方祖先的敌对方,则属于不同的帮派 3.否则就是不确定 并: 1.如果某人的敌对方还没有,就把对方的祖先作为自己的敌对方 2.将自己和对方祖先的敌对方合并 #include #include using namespace std; const int maxn=100010; int id[原创 2013-04-06 11:43:57 · 415 阅读 · 0 评论 -
POJ 2492 A Bug's Life
并查集操作,用一个oid数组来存放虫子的对立面 如果两只虫子a,b的祖先相同那就是同性恋了. 每次合并a,b把a和b的对立面合并,把b和a的对立面合并 #include #include using namespace std; const int maxn=2010; int id[maxn],oid[maxn],size[maxn],n,m; void clear(){ for原创 2013-04-06 17:48:50 · 408 阅读 · 0 评论 -
POJ 2299 Ultra-QuickSort(树状数组+离散化)
第一道树状数组+离散化 的题目,纪念下 #include #include #include using namespace std; const int maxn=500010; int temp[maxn],tree[maxn],n; struct node{ int n,id; bool operator<(const node &rhs)const{ return n<r原创 2013-04-09 17:48:38 · 431 阅读 · 0 评论 -
POJ 2499 Binary Tree
刚开始天真的用模拟的从1,1到目标结点,结果当然是暴栈了 然后发现可以从目标结点往1,1走: 1.当前结点a>b说明是父节点的左儿子(fa+fb,fb),就左计数器自增,然后a=a-b; 2.当前结点a 然高高兴兴的提交上去结果TLE了. 主要是因为减速度还是太差应付不了这种情况 目标结点100000000,1 最后发现可以用取模快速的到达转折点来求: 1.当前结点a>b说明是父原创 2013-04-13 11:00:32 · 496 阅读 · 0 评论 -
POJ 1179 Polygon(环形DP 矩阵连乘)
题意:给出一个环 环上的每个点有一个值 点和点之间用一条边相连,边上有一个运算符,要求删除一条边,然后对剩下的线两两顶点合并运算.求通过运算能得到的最大值.并求出删除某条边后能经过运算得到这个最大值的这些边. 由于N 要注意的是最大值可能有最小值相乘得到(负数),所以也要记录最小值. 设dp1[i][j]为从i到j的最大运算结果,dp2为最小值 那么 dp1[i][j] = max(dp原创 2014-03-23 10:40:53 · 947 阅读 · 0 评论