02 2020 档案
摘要:题目链接: "https://www.luogu.com.cn/problem/P3378" 题目大意:维护一个小根堆。 堆的入门题。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2170" 解题思路: 先用并查集计算出所有的集合内元素个数(以下简述为集合大小)。 然后对每一个集合,以集合大小同时作为价值和体积做 01背包。 我们在计算中用 $dp[i]$ 表示体积为 $i$ 的背包所能够包含的
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1433" 题目大意 房间里放着 $n$ 块奶酪。一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 $(0,0)$ 点处。 输入格式 第一行一个正整数 $n$。 接下来每行 $2$ 个实数,表示第 $i$ 块
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2835" 解题思路: 如果A愿意借给B,则B的入度加一。 然后计算一下有多少个点的入度为0,这些是我们需要分配光盘的。 但是可能存在强联通分量,这种情况下,一个强联通分量里没有入读为0的点,但是我需要制定以下这个点。
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1991" 解题思路: 二分答案+并查集。 但是这道题目写的不是很明确,其实如果对于一个D大家都在一个连通块的时候,是不需要配备卫星通话线路的。 也就是说,如果我连通块的数量是 $cnt$ ,那么我只需要判断 $cnt
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P4185" 解题思路完全参考自 "cjx大佬的博客" 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P4145" 题目大意: 两种操作: 1. 区间求和; 2. 区间开方。 解题思路: 使用线段树解决这个问题。 但是区间更新(开方)需要懒惰标记。 但是这样也会TLE,所以还需要记录区间更新的次数的最小值,如果这个区间内
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2658" 解题思路: 这道题当D确定的情况下,其实就是一个连通块问题。 然后我们二分答案求最小的D即可。 实现代码如下: "洛谷P3073" 和这题一样。
阅读全文
摘要:题目链接:https://www.luogu.com.cn/problem/P1546 题目大意: 给你一个邻接矩阵,求它的最小生成树。 解题思路: 因为是邻接矩阵,用Prim算法求最小生成树写起来比较方便。 实现代码如下: #include <bits/stdc++.h> using namesp
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2330" 这道题其实就是求最小生成树的最长边。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P3144" 解题思路: 每次多去掉一个点,然后剩下的点进行并查集,并判断剩下的点是否属于同一个集合当中。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P3958" 解题思路: 并查集。 如果两个奶酪的距离 $\le 2 \cdot r$ ,则合并。 再开两个点:$n+1$ 表示底,$n+2$ 表示顶。 然后如果一个点的 $z_i \le r$,则将该点与 $n+1$
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1892" 题目大意: 告诉你一些朋友和敌人关系。 以及: 我朋友的朋友是我的朋友; 我敌人的敌人也是我的朋友。 求:最多有多少各团伙? 解题思路: 把每个点 $i$ 拆分成两个点 $i$ 和 $i+n$,其中 $i$
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1396" 解题思路: 贪心思想,按照边的拥挤度从小到大合并两个点,每次合并之后判断 $s$ 和 $t$ 是否在一个集合中, 如果不在,则继续;如果在,就输出这条边的拥挤度并结束。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2078" 解题思路: 执行两次并查集。 第一次计算和小明认识的男生数量 $cnt1$; 第二次计算和小红认识的女生数量 $cnt2$。 答案就是 $\min (cnt1, cnt2)$ 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1656" 解题思路: 枚举每一条边,假设这条边被删掉,然后用并查集判断剩下的 $m 1$ 条边能否让这个图连通。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1455" 解题思路: 这道题目就是首先用并查集把属于同一个集合的物品合到一起,然后对每一个集合的物品做01背包。 实现代码如下:
阅读全文
摘要:Power digit sum $2^{15} = 32768$ and the sum of its digits is $3 + 2 + 7 + 6 + 8 = 26$. What is the sum of the digits of the number $2^{1000}$? 幂的数字和
阅读全文
摘要:Lattice paths Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bott
阅读全文
摘要:Longest Collatz sequence The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using
阅读全文
摘要:Large sum Work out the first ten digits of the sum of the following one hundred 50 digit numbers. 大和 计算出以下一百个50位数的和的前十位数字。 解题思路 目前想到的就是用高精度加法模拟。 100个5
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1016" 解题思路(思路全部来自 "hongzy大神的博客" ): 思路: 1. 在起点加满油; 2. 到第i个加油站把油箱里价格 P[i]的油退了,换成价格为P[i]的油。 3. 每次烧油就找最便宜的油烧 实现:单
阅读全文
摘要:Highly divisible triangular number The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1
阅读全文
摘要:Largest product in a grid In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07
阅读全文
摘要:Summation of primes The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 素数的和 所有小于10的素数的和是2 + 3 + 5
阅读全文
摘要:Special Pythagorean triplet A Pythagorean triplet is a set of three natural numbers, $a using namespace std; int main() { for (int i = 1; i <= 1000/3;
阅读全文
摘要:Largest product in a series The four adjacent digits in the 1000 digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. Find the thirte
阅读全文
摘要:10001st prime By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number?
阅读全文
摘要:Sum square difference The sum of the squares of the first ten natural numbers is, $$1^2 + 2^2 + \cdots + 10^2 = 385$$ The square of the sum of the fir
阅读全文
摘要:Smallest multiple 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest posi
阅读全文
摘要:Largest palindrome product A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 digit numbers is 9009
阅读全文
摘要:Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? 最大质因数 13195的所有质因数为5
阅读全文
摘要:Even Fibonacci numbers Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 t
阅读全文
摘要:Multiples of 3 and 5 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P4017" 解题思路: 定义 $f[i]$ 为到点 $i$ 的方案数,则: $$f[i] = \sum_{j \to i}{f[j]}$$ 其中 $j \to i$ 表示存在一条从 $j$ 到 $i$ 的边。 实现代码
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1964" 对于第 $i$ 件物品: 如果 $a_i$ 能被 $c_i$ 整除,则将其转换成 $\frac{a_i}{c_i}$ 件体积为 $1$,价格为 ${c_i} \cdot b_i$ 的物品; 如果 $a_i$
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1536" 题目大意:告诉你一些点属于同一个集合,求最少连多少条边能够合成一个集合。 解题思路: 最少连边数 = 集合数 1。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1551" 解题思路 这道题是最基础的并查集入门题。 我们甚至可以不适用并查集的路径优化。 暴力的查找祖先节点的方式如下: 添加路径压缩后的查找函数如下: 递归写法如下: 完整实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1894" 题目大意:求二分图最大匹配。 解题思路:hungary算法。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1168" 解题思路: 开一个大根堆维护前 $\lfloor \frac{i}{2} \rfloor$ 个较小的元素; 开一个小根堆维护前 $\lceil \frac{i}{2} \rceil$ 个较大的元素。 然后每
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1182" 解题思路: 二分答案。 check(num)用于判断每段不超过num的情况是否存在。 然后二分答案。 2020 3 24 思路补充: 首先,我们可以编写一个 函数,该函数用于验证当每段的最大值为 len 时
阅读全文
摘要:思路来自 "百度百科" 中的一段话: 设 $n$ 个数的错排方案数为 $D_n$, 第一步,考虑第 $n$ 个元素,把它放在某一个位置,比如位置 $k$ ,一共有 $n 1$ 种放法; 第二步,考虑第 $k$ 个元素,这时有两种情况: (1)把它放到位置 $n$ ,那么对于除 $n$ 以外的 $n
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1144" 题目大意: 给你一个无向无权图,求点 $1$ 到所有点的最短路的方案数。 解题思路: 因为是无权图,所以可以使用广搜来求最短路,然后在广搜的过程中,定义: $dist[u]$ 表示点 $1$ 到点 $u$
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1714" 题目大意: 给你一个大小为 $n$ 的数组,求满足区间元素个数 $\le m$ 的连续子序列和的最大值。 解题思路: 假设数组中第 $i$ 个元素为 $a[i]$ ,我可以定义 $sum[i]$ 表示前 $
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P6040" 解题思路: 设状态 $f[i]$ 表示老师走到第 $i$ 个人的最小花费,则: $$f[i] = \min_{j \in [i x, i 1]}(f[j] + (i j 1) \times k + d)$$
阅读全文
摘要:题目链接: "http://poj.org/problem?id=2559" 题目大意: 直方图中的每一个柱子都是一个宽度为 $i$ 高度为 $h_i$ 的矩形,求最大矩形面积。 解题思路: 定义两个数组: $L[i]$ 表示第 $i$ 根柱子往左走碰到的第一根比它矮的柱子的坐标 $+1$(如果第
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2947" 题目大意: 约翰的 $N(1 \le N \le 10^5)$ 头奶牛站成一排,奶牛$i$的身高是$H_i(l \le H_i \le 1,000,000)$.现在,每只奶牛都在向右看齐.对于奶牛$i$,如
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1725" 解题思路: 这道题目是单调队列优化DP的入门题。 状态转移方程是: $$f[i] = \min_{j \in [i r,i l]} f[j] + a[i]$$ 可以使用单调队列优化。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1886" 题目大意: 给你一个长度为 $n$ 的数组 $a$ 以及 $k$,对于所有 $\ge k$ 的 $i$ ,求:区间 $[i k+1, i]$ 的最大值和最小值。 解题思路: 单调队列。 对于求区间最小值,维
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2952" 这道题目是一道双端队列入门题,我这里用STL容器提供的deque直接实现这个功能。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2627" 解题思路: 定义状态 $f[i]$ 表示:"前 $i$ 只奶牛,且当 $i t$ 时, $$f[i] = \max_{j \in [i k 1, i 1]}(f[j] + \sum_{x=j+2}^i e[
阅读全文
摘要:题目链接: "http://poj.org/problem?id=2373" 题目描述 农夫约翰的奶牛们发现了在牧场的山脊上长着味道非常不错的三叶草。为了让这些三叶草得到灌溉,农夫约翰正在山脊上安装洒水器。 为了让安装流程得到简化,每一个洒水器必须沿着山脊进行安装(我们可以将山脊按成一个一维的长度为
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1096" 解题思路: 递归。 推导公式: $f[1] = 2$ $f[i] = f[i 2] \cdot 2 + 2$ 因为数据比较大,需要用到高精度。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1090" 解题思路:使用优先队列来维护一个小根堆,每次取出2个堆顶元素,将它们的和加入答案中并且它们的和在此放入堆中。 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1281" 解题思路: 二分答案。 check(num) 用于判断每个人分配 num 也的方案是否可行,然后二分答案。 solve(num) 函数,用于确保尽可能让前面的人少抄写。其实现给予贪心思想是:尽可能地让后面的
阅读全文
摘要:题目链接: "https://www.vijos.org/d/newbzoj/p/590c9934d3d8a13210993a72" 题目大意: 有一排n棵树,第i棵树的高度是Di。 MHY要从第一棵树到第n棵树去找他的妹子玩。 如果MHY在第i棵树,那么他可以跳到第i+1,i+2,...,i+k棵
阅读全文
摘要:题目描述 输入一个长度为n的整数序列,从中找出一段不超过m的连续子序列,使得整个序列的和最大。 输入格式 第一行两个数n,m(n,msum[j2],那么j1可以直接抛弃,也就是在这个j的序列里,必须是单调递增的,所以我们可以用一个单调队列来维护这一关系 实现代码如下:
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P1365" 题解地址: "https://www.luogu.com.cn/blog/five20/solution p1365" 实现代码如下:
阅读全文
摘要:题目连接: "https://www.luogu.com.cn/problem/P3802" 题目大意: 有 $a_1$ 个 $1$,$a_2$ 个 $2$,$\cdots$,$a_7$ 个 $7$,现在要将这些数拼成一个序列,问:序列中出现连续的 $7$ 个数都不同的期望次数? 解题思路(参考自
阅读全文
摘要:题目大意: 求:由字符串 s 通过下列三种操作 1. 插入一个字符; 2. 删除一个字符; 3. 改变一个字符 变换到字符串 t 所需要的最少操作次数(亦即最短编辑距离问题) 解题思路: 定义状态 $f[i][j]$ 表示 $s[0..i]$ 和 $t[0..j]$ 合并所需的最小花费,则可得状态转
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2014" 题目大意: 大学里有 $n$ 门课,这些课之间有依赖关系(先修课)。每门课有学分 $s[i]$ ,求选择 $m$ 课最多能获得的学分? 解题思路: 树形DP。 我们可以假设先修课和当前这节课之间是父子关系(
阅读全文
摘要:题目链接: "http://poj.org/problem?id=1679" 题目大意: 给你一个简单连通图,判断他的最小生成树是否唯一。 解题思路: 首先(我这里用Kruskal算法)求出它的最小生成树(以下简称MST)以及对应的边,然后构造出这棵MST。 然后我们枚举图上每一条不在此MST上的边
阅读全文
摘要:题目链接: "https://www.luogu.com.cn/problem/P2072" 题目大意: 已知一个地方有M种宗教(编号为1—M),有N个教徒(编号为1—N),每个教徒信且只信一种宗教。现在要按顺序把这N个教徒分成一些集体,每个集体的危险值定义为这个集体中的宗教种数,且一个集体的宗教种
阅读全文
浙公网安备 33010602011771号