02 2021 档案

摘要:题目链接 \(有人得知自已的消息等价于找到最小环\) \(有两种做法:\) \((1)栈:每过一个点将其放入栈中,因为存在环,必然会有走到某个点但是\\其已经在栈中的情况,这个时候进行pop操作,直到弹出该点,总共弹出的点数\\就是环的长度\) \((2)并查集:维护每个点到根节点的距离,如果发现有 阅读全文
posted @ 2021-02-28 15:36 phr2000 阅读(127) 评论(0) 推荐(0)
摘要:题目链接 题意 \(题目让我们找到一个最大值k,使得每删去一个点v,都使得删去点的边权和不超过k.\) 思路 \(容易想到一定是优先删去边权和最小的最好\) \(所以这里要始终能找到一个最小边权之和的点,用set就很方便\) \(我们先把所有点放入set,每次拿出边权和最小的点,用它去更新其他点的边 阅读全文
posted @ 2021-02-28 12:46 phr2000 阅读(90) 评论(0) 推荐(0)
摘要:家族树 \(拓扑排序模板\) \(拓扑排序基于bfs\) #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0); inline int 阅读全文
posted @ 2021-02-25 17:12 phr2000 阅读(63) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/submission/code_detail/4121247/ 知识点 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_std 阅读全文
posted @ 2021-02-24 21:04 phr2000 阅读(62) 评论(0) 推荐(0)
摘要:Maximum width $如何在m个点中找到最大的间隔? $ \(预处理l[],r[]分别表示第i个点最左可以在哪和最右可以在拿\) \(ans=max(ans,r[i+1]-l[i])\) #include <bits/stdc++.h> using namespace std; #defin 阅读全文
posted @ 2021-02-23 23:23 phr2000 阅读(81) 评论(0) 推荐(0)
摘要:Max Median \(题意:给定长度为n的序列,找到一个子序列长度\ge k的a[l..r],使中位数最大\) \(二分答案\) \(将大于等于x的数标记为1,否者标记为-1,求一遍前缀和可快速确定x能否为这一段的中位数\) \(因为长度不确定所以再记录前缀的最小值,那么若满足s[i]-ms[i 阅读全文
posted @ 2021-02-23 13:28 phr2000 阅读(122) 评论(0) 推荐(0)
摘要:Glass Half Spilled \(dp\) \(f[k][j]表示选k个杯子,总体积为j的条件下装水的最大值\) \(转移方程:f[k][j]=max(f[k][j],f[k-1][j-v[i]]+w[i])\) \(注意初始化,很多方案是不合法的\) \(对于每一个f[k][j],它的答案 阅读全文
posted @ 2021-02-22 23:25 phr2000 阅读(65) 评论(0) 推荐(0)
摘要:Row GCD \(gcd\) \(gcd(a,b)=gcd(a-b,b)\) \(gcd(a_1+b_j,…,a_n+b_j)=gcd(a_i-a_{i-1},a[1]+b_j)\) #include <bits/stdc++.h> using namespace std; #define IO 阅读全文
posted @ 2021-02-22 21:24 phr2000 阅读(59) 评论(0) 推荐(0)
摘要:捉迷藏 最小路径重复点覆盖 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) inline int lowbit(int x) { 阅读全文
posted @ 2021-02-22 17:11 phr2000 阅读(122) 评论(0) 推荐(0)
摘要:骑士放置 最大独立集 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) inline int lowbit(int x) { ret 阅读全文
posted @ 2021-02-22 17:09 phr2000 阅读(75) 评论(0) 推荐(0)
摘要:机器任务 最小点覆盖 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) inline int lowbit(int x) { ret 阅读全文
posted @ 2021-02-22 17:07 phr2000 阅读(63) 评论(0) 推荐(0)
摘要:棋盘覆盖 匈牙利算法 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) inline int lowbit(int x) { ret 阅读全文
posted @ 2021-02-22 17:05 phr2000 阅读(68) 评论(0) 推荐(0)
摘要:关押罪犯 #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) inline int lowbit(int x) { return x 阅读全文
posted @ 2021-02-22 17:03 phr2000 阅读(91) 评论(0) 推荐(0)
摘要:矿场搭建 \(\href{https://www.acwing.com/solution/content/24931/}{点连通分量的缩点}\) #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio 阅读全文
posted @ 2021-02-21 20:55 phr2000 阅读(59) 评论(0) 推荐(0)
摘要:电力 \(\href{https://www.acwing.com/solution/content/20702/}{点连通分量}\) #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(fals 阅读全文
posted @ 2021-02-21 20:53 phr2000 阅读(69) 评论(0) 推荐(0)
摘要:冗余路径 \(\href{https://www.acwing.com/solution/content/20697/}{边连通分量}\) \(本题是等价于加入最少边是整个图变成边连通分量(没有桥)\) #include <bits/stdc++.h> using namespace std; #d 阅读全文
posted @ 2021-02-21 20:46 phr2000 阅读(56) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1177/ \(强连通必然半连通,将强连通图转化为拓补图,从起点出发,能走的所有\color{Red}{最长的路径即为最大半连通子图}\) \(问题就转化为在拓补图上做dp的问题\) \(注意缩点后,每个点间只用保留一条边 阅读全文
posted @ 2021-02-20 01:50 phr2000 阅读(80) 评论(0) 推荐(0)
摘要:受欢迎的牛 \(tarjan将强连通分量缩点转化成拓扑图的原理看\)这里 \(最受欢迎的牛必然是拓补图的终点,且若图中不止一个终点,必然没有某个牛被所有牛欢迎\) #include <bits/stdc++.h> using namespace std; #define IO ios::sync_w 阅读全文
posted @ 2021-02-20 01:42 phr2000 阅读(49) 评论(0) 推荐(0)
摘要:闇の連鎖 \(\color{Red}{本题的实质是看一条边属于几个环}\) \(如图, 如何使标记的边的覆盖次数+c ?\) \(我们把边权看成是其子树下所有点权之和,如果我需要增加x到p与y到p之间的边权,p=lca(x,y),\\那么只需要将x标记为x+c,y标记为y+c,p标记为p-2c\) 阅读全文
posted @ 2021-02-19 16:31 phr2000 阅读(87) 评论(0) 推荐(0)
摘要:\(Tarjan\ O(m + n)\) \(Tarjan为离线算法\) \(在线做法:边读边做\) \(离线做法:先读完,再全部处理,最后全部输出\) \(Tarjan本质上是对向上标记法的优化,首先任取一个点当成根节点向下做dfs,并将所有节点分为三部分\) \(已经遍历并完成了回溯的点标记为2 阅读全文
posted @ 2021-02-18 21:45 phr2000 阅读(99) 评论(0) 推荐(0)
摘要:最近公共祖先 \((1)向上标记法\ O(n)\) \((2)倍增:fa[i,j]表示从i开始,向上走2^j步所能走到的节点,0 \le j\le log_2n,depth[i]表示深度\) \(步骤\) \(先将两个点跳到同一层\) \(让两个点同时往上跳,一直跳到它们的最近公共祖先的下一层\) 阅读全文
posted @ 2021-02-18 14:12 phr2000 阅读(79) 评论(0) 推荐(0)
摘要:\((1)求不等式的可行解\\ 源点需要满足的条件: \color{Red}{从源点出发,一定可以走到所有的边.}\) 步骤 \(先将每个不等式x_i\le x_j + c_k,转化为一条从x_j走到x_i,长度为c的一条路径.\) \(找一个虚拟源点,使得该源点一定可以遍历所有边.\) \(从源点 阅读全文
posted @ 2021-02-18 00:53 phr2000 阅读(52) 评论(0) 推荐(0)
摘要:雇佣收银员 分析 \(本题令s_i为0\sim i点需要的人数和\) \(\color{Red}{s_i作为图中的点}\) \(为了方便使用前缀和,r_i统一往后错一位,于是r_i表示i-1\sim i中需要的人数\) \(因为24小时是一个环的形式,这里的前缀需要注意:\) \(0\sim7小时间 阅读全文
posted @ 2021-02-18 00:51 phr2000 阅读(46) 评论(0) 推荐(0)
摘要:排队布局 分析 \(第一问问是否存在负环,将n个点全放进队列即可\) \(如何判断1到N的距离可以无限大?\) \(即判断1到N是否连通.\) 约束关系 \(x_b\le x_a+L\) \(x_b-x_a\ge D\Rightarrow x_a\le x_b-D\) \(x_i\le x_{i+1 阅读全文
posted @ 2021-02-17 22:45 phr2000 阅读(68) 评论(0) 推荐(0)
摘要:区间 分析 \(前缀和 + 差分约束\) \(令S_i为1\sim i中被选出的数.\) 约束关系 \(S_i \ge S_{i-1}\) \(S_i-S_{i-1}\le 1\Rightarrow S_{i-1}\ge S_i-1\) \(S_a-S_b\ge c\) 代码 #include <b 阅读全文
posted @ 2021-02-17 20:30 phr2000 阅读(71) 评论(0) 推荐(0)
摘要:糖果 分析 \(要求糖果的最小值,应该把不等式处理成x_i \ge x_j+c的形式,求最长路.\) 约束关系 \(x_i \ge 1\) \(x_i\ge x_0+1\) \(x_0=0\) \(当负环导致TLE,可以尝试将queue换成stack.\) #include <bits/stdc++ 阅读全文
posted @ 2021-02-17 17:09 phr2000 阅读(64) 评论(0) 推荐(0)
摘要:观光奶牛 \(求负环 + 01分数规划\) 思路 \(将点权算入边权:\) \[ \begin{aligned} &\frac{\sum{f_i}}{\sum{w_i}}>mid\\ \Rightarrow\ &\sum{f_i}-\sum{w_i}*mid>0\\ \Rightarrow\ &\s 阅读全文
posted @ 2021-02-16 21:08 phr2000 阅读(68) 评论(0) 推荐(0)
摘要:虫洞 \(本题为裸题\) 求负环 \(基于spfa\) \((1)统计每个点入队的次数,如果某个点入队n次,则说明存在负环.\) $(2)统计每个点的最短路中所包含的边数,如果某点的最短路所包含的边数大于等于n,\则也说明存在负环.\$ \(对于求负环是否存在,dist的初始化是无所谓的.\) #i 阅读全文
posted @ 2021-02-16 20:47 phr2000 阅读(104) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1148/ #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie 阅读全文
posted @ 2021-02-15 17:32 phr2000 阅读(70) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1150/ 次小生成树 \(定义:给一个带权的图,把图的所有生成树按权值从小到大排序,第二小的称为次小生成树.\) 求法 \(方法1:先求最小生成树,再枚举删去最小生成树的边求解,时间复杂度\ O(mlogm + nm). 阅读全文
posted @ 2021-02-15 17:07 phr2000 阅读(66) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1145/ 思路 \(把已有的边加上,会形成各个连通块,等价于把每个连通块当成一个点去做Kruskal算法.\) #include <bits/stdc++.h> using namespace std; #define 阅读全文
posted @ 2021-02-14 23:04 phr2000 阅读(48) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1144/ #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie 阅读全文
posted @ 2021-02-14 23:01 phr2000 阅读(68) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/861/ \(裸题\) #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N = 1000 阅读全文
posted @ 2021-02-14 22:59 phr2000 阅读(36) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1142/ \(一道裸题\) \(最小生成树有两种算法:Prim \ 和\ Kruskal.\) \(Prim:O(n^2)\) \(Kruskal:O(mlogm)\) \(如何证明算法正确(如何证明当前这条边一定可以被 阅读全文
posted @ 2021-02-14 22:56 phr2000 阅读(64) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/346/ #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie( 阅读全文
posted @ 2021-02-14 15:33 phr2000 阅读(53) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/345/ #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie( 阅读全文
posted @ 2021-02-14 15:30 phr2000 阅读(90) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1127/ #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie 阅读全文
posted @ 2021-02-14 15:27 phr2000 阅读(74) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/347 \(求从起点S到终点E恰好经过N条边(可以重复经过)的最短路.\) \(即求出原距离矩阵的N次方,可以用类似qmi()的方式优化.\) #include <bits/stdc++.h> using namespac 阅读全文
posted @ 2021-02-14 15:19 phr2000 阅读(63) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/description/385/ \(最短路与次短路\) #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(fals 阅读全文
posted @ 2021-02-10 17:04 phr2000 阅读(67) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1136/ #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie 阅读全文
posted @ 2021-02-10 17:02 phr2000 阅读(71) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1133/ #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie 阅读全文
posted @ 2021-02-10 17:01 phr2000 阅读(52) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1139/ #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie 阅读全文
posted @ 2021-02-10 17:00 phr2000 阅读(63) 评论(0) 推荐(0)
摘要:http://codeforces.com/contest/1467/problem/B 题意 \(一个数列里有波峰波谷\) \(波谷:a[i]<min(a[i+1],a[i-1]])\) \(波峰:a[i]>max(a[i+1],a[i-1])\) \(sum = 波峰数+波谷数\) \(若可以任 阅读全文
posted @ 2021-02-08 23:10 phr2000 阅读(70) 评论(0) 推荐(0)
摘要:http://codeforces.com/contest/1481/problem/C 题意 \(给n个数,要变成另外n个数,问依次做出m次变换能否成功.\) 思路 \(对于这m个数\) \(如果c[i]不在b中:\) \(如果是最后一个,一定不能成功.\) \(不是最后一个,要考虑它被后面的数覆 阅读全文
posted @ 2021-02-06 22:55 phr2000 阅读(158) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1093/ \(有一个 a×b 的整数组成的矩阵,现请你从中找出一个 n×n 的正方形区域,使得该区域所有数中的最大值和最小值的差最小。\) 思路 \(分别得到每个n*n正方形中的最小值和最大值\) \(对于每一行做一遍单 阅读全文
posted @ 2021-02-04 23:29 phr2000 阅读(96) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/description/1089/ 思路 \(单调对列\ + \ dp\) \(不能选超过m个连续的数求最大等价于在每m+1个数中选出最小的一个.\) \(单调队列中维护的f[i-1,\ i-m-1]区间的最小值\) \( 阅读全文
posted @ 2021-02-04 20:55 phr2000 阅读(80) 评论(0) 推荐(0)
摘要:#include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) inline int lowbit(int x) { return x & (-x 阅读全文
posted @ 2021-02-04 19:41 phr2000 阅读(55) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1092/ \(二分\ + \ 单调队列dp\) #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio(false);c 阅读全文
posted @ 2021-02-04 01:58 phr2000 阅读(150) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1091/ 状态表示 \(f[i]:点燃第i个且满足且每m个至少点燃一个的全体集合\) \(属性:min\) 状态转移 $f[i] = f[q[hh]]+f[i]\$ #include <bits/stdc++.h> us 阅读全文
posted @ 2021-02-04 01:56 phr2000 阅读(67) 评论(0) 推荐(0)
摘要:https://www.acwing.com/activity/content/problem/content/1458/1/ \(用单调队列得到[i-m,\ i-1]区间中最小的是s[j],\ s[i]-s[j]即为所求\) #include <bits/stdc++.h> using names 阅读全文
posted @ 2021-02-04 01:51 phr2000 阅读(54) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1086/ \(cpp负数的取模:(-a)\%b (a\%b)\) 状态表示 \(f[i][j][x]:有i位最高位位j且所有位之和对p取模的值为x\) 状态计算 \(f[i][j][x]+=f[i-1][k][mod(x 阅读全文
posted @ 2021-02-02 20:34 phr2000 阅读(72) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1085/ \(此题需要注意的是要去前导零, 前面2道题可以直接把不足n位的数看成补上前导零的数, 这样做处理起来很方便, 但是此题不行.\) $本题如果也把不足n位的数看成补上前导零去算, last的更新会出现问题导致循 阅读全文
posted @ 2021-02-02 16:02 phr2000 阅读(69) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1084/ \(f[i][j]:有i位且最高位为j的不增数.\) #include <bits/stdc++.h> using namespace std; #define IO ios::sync_with_stdio( 阅读全文
posted @ 2021-02-02 02:10 phr2000 阅读(52) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1083/ 思路 这个题相当于把一个数字转化为$b$进制, 然后去看小于等于它的数中有多少个在$b$进制下, 有$k$个$1$且其他位全部为0. \(对于每一位x, 左边的分支为该位取0到x-1,首先该位可取0,然后有两种 阅读全文
posted @ 2021-02-02 01:17 phr2000 阅读(104) 评论(0) 推荐(0)
摘要:https://www.acwing.com/problem/content/1079/ 树形dp + 状态机 此题于战略游戏类似, 但是由于要看的是节点而不是边, 所有两个状态不够. 状态表示 \(f[i][0]:点i被父节点看到\) \(f[i][1]:点i被子节点看到\) \(f[i][2]: 阅读全文
posted @ 2021-02-01 21:16 phr2000 阅读(55) 评论(0) 推荐(0)
摘要:https://codeforces.com/contest/1476/problem/D 题意 给 \(n + 1\) 个点和它们之间连接关系, 两点之间是单向的, 每走过一条边, 所有的边反向, 问从某个点开始出发, 能走过的最长的路径是多少 思路 一开始以为要建图, 实际上算是线性dp \(用 阅读全文
posted @ 2021-02-01 00:19 phr2000 阅读(69) 评论(0) 推荐(0)