03 2022 档案
摘要:题目链接 https://www.acwing.com/problem/content/3/ 题目思路 01背包进阶版,每种物品有无限个 所以就是将每种物品的体积和价值存下来,然后遍历每种物品,在当前体积的限制下求可拿物品的价值最优解 题目代码 #include <iostream> #includ
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/2/ 题目思路 梦开始的地方,01背包的每个物品只可以选一次 DP思想:“最后” 每次考虑的是最后是否为最优解,从最后一步的最优解向前推,推出每一步的最优解。 01背包就是判断每步是拿当前物品和不拿当前物品的价值
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1048 题目思路 经典01背包问题,每一步的状态为拿当前物品和不拿当前物品的价值最大值 #include <iostream> #include <algorithm> using namespace std; cons
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/1017/ 题目思路 本题是经典的线性DP 如上图所示,每一步的最优解即为上一步的最大值加当前地方花生数 题目代码 #include <iostream> #include <algorithm> using n
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1162 题目思路 主要是找到被1围着的0,但是刚开始找的最后一个,然后WA两个点,最后找到第一个0的位置,然后宽搜成功AC 题目代码 #include <iostream> #include <algorithm> #i
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/1472/ 题目思路 没有技巧,全是感情,bfs经典模板题 题目代码 #include <iostream> #include <algorithm> #include <cstring> #include <q
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1141 题目思路 一开始觉着是个宽搜就兴冲冲地背一了波模板,然后很高兴的TLE三个 所以这题需要优化,不能每个点都跑一边bfs,所以应该将连通块染色,相当于把一条路上的元素都标记成一样的,最后通过一个数组来存每个连通块的
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1451 题目思路 只需要搜索在四个方向且不为0的数字块即可 dfs和bfs都可 题目代码 bfs #include <iostream> #include <algorithm> #include <cstring> #
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1332 题目思路 还是bfs题,但初始加入队列的有多个元素,所以我就没写成函数形式 题目代码 #include <iostream> #include <algorithm> #include <cstring> #in
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P2298 题目思路 依旧是bfs,记得这个有 无答案 情况 题目代码 #include <iostream> #include <algorithm> #include <cstring> #include <queue>
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1746 题目思路 今天全是bfs题,这道题记住只有0可以走 题目代码 #include <iostream> #include <algorithm> #include <cstring> #include <queue
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1747 题目思路 还是经典的bfs模板题,只是多了四个方向 题目代码 #include <iostream> #include <algorithm> #include <cstring> #include <queue
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1443 题目思路 经典宽搜问题,记住马走日! 题目代码 #include <iostream> #include <algorithm> #include <cstring> #include <queue> using
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/4321/ 题目思路 最开始做的时候只考虑了有无环和走重复路径 但是少考虑了不能走相邻路径 所以只需要将每次走过路径的上下左右四个方向记录一下,只要有一块被重复走过即为不合法路径 题目代码 #include <i
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1434 题目思路 这里借用一下小呆呆大佬(https://www.acwing.com/user/myspace/index/7416 )的图 如果用暴搜会有大量重复搜索,所以可以通过空间换时间。 DP思想:确定当前位置
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1219 题目思路 dfs搜索,枚举每一行,查找合法的位置 关键:正对角线用一维表示为 \(u + i\),反对角线为了不超范围表示为 \(n - u + i\) 题目代码 #include <iostream> #inc
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P3397 题目思路 直接暴力求解,枚举$x_1 \sim x_2$,\(y_1 \sim y_2\) 或者二维差分 题目代码 暴力 #include <iostream> #include <algorithm> usin
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/solution/P4995 题目思路 就是一高一低来回跳,由于数据大,所以答案用long long(用int只a一半) 题目代码 #include <iostream> #include <algorithm> using
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P2678 题目思路 记着存起点和终点。。 二分 mid 为两点之差 当前点和上一点的距离 >= mid时,更新上一点为当前点,当 < mid时cnt++相当于移除这块石头 当移除石头 ⇐ m 时 l 更新 mid 增大,>
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/description/1661/ 题目思路 最开始想暴力,没暴出来。之后看了大佬的二分题解豁然开朗 因为是找最近距离的最大值,所以先找到最小的距离作为右边界 l, r表示待放奶牛的位置与上一奶牛的位置差 che
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1024 题目思路 二分 :根据题意,两根乘积 < 0,两根之间必有根,并且两根之差 >= 1,所以以1为大区间,若两根乘积为负则二分寻找 暴力 :因为保留两位小数,则每次缩小区间0.01,直接寻找乘积为0的两根,其平均数
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/description/3748/ 题目思路 将问题转换成,有一个数组$a[N]$,找到一个特殊的数idx,满足至少有idx个数 >= idx a[i] >= i (idx求最大).并且给定一个距离l,使得这个区
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/3373/ 题目思路 用哈希表存储生肖对应年, 读入字符串并过滤掉多余内容, 通过判断 previous和 next 求与Bessie的相对年龄差 需要用 (j % 12 + 12) % 12 来处理负数情况.因
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/3361/ 题目思路 唱完字母歌 = 农夫听到的字母按所给顺序全部出现.所以遍历匹配字符串,当所听到的字母是顺序中的字母,模式串后移,当所听到的字母不属于当前顺序后的字母,则ans ++ 题目代码 #includ
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1923 题目思路 利用快排 + 二分来实现分治,从而求得第k小的数 题目代码 快排 + 二分 #include<bits/stdc++.h> using namespace std; int q[5000005], k;
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1098 题目思路 模拟,注意记录偏移量来去掉 ''-'' 题目代码 最傻的模拟 #include <iostream> #include <algorithm> using namespace std; const in
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1518 题目思路 模拟农夫和奶牛行动路线,为了避免死循环,当ans到一定值时跳出 题目代码 #include <iostream> #include <algorithm> using namespace std; ch
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/4316/ 题目思路 贪心 从最底层向高层遍历,同一分支的两个结点比较,将大值赋给父节点, ans += 最大值 + 最小值 #include <iostream> #include <algorithm> #i
阅读全文
摘要:题目链接 https://www.acwing.com/problem/content/4315/ 题目思路 KMP算法,再通过查询子串来计算出现次数 题目代码 #include <iostream> #include <algorithm> using namespace std; const i
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1328 题目思路 用二维数组存每种情况的得分情况,然后枚举来计算分数 #include <iostream> using namespace std; int main() { int vs[5][5] = {{0, 0
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P4924 题目思路 顺时针转是第$i$行第$j$列的数字转移到第$j$行倒数第$i$列 \(temp[n - m + j][n + m - i] = g[i][j]\) 逆时针转是第$i$行第$j$列的数字转移到倒数第$j
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1009 题目思路 本题为高精度乘法 + 高精度加法 题目代码 #include <iostream> #include <algorithm> #include <cstring> using namespace std
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1303 题目思路 模板题 题目代码 #include <iostream> #include <algorithm> #include <vector> using namespace std; vector<int>
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1601 题目思路 模板 题目代码 #include <iostream> #include <algorithm> #include <vector> using namespace std; vector<int> a
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P1563 题目思路 当谜题方向与小人朝向相同时是逆时针,相反时是顺时针,求最终小人位置 题目代码 #include <iostream> #include <algorithm> using namespace std;
阅读全文
摘要:题目链接 https://www.luogu.com.cn/problem/P2670 题目思路 模拟当前点的八个方向,扫描是否有雷 题目代码 #include <iostream> #include <algorithm> using namespace std; const int N = 11
阅读全文