ACM
文章平均质量分 69
u小鬼
a melancholy programmer
展开
-
Codeforces 补题合集
对于一个长为 n 的数组 b,求一个 1-n 的排列 a 使得,bi=floor(iai)b_i=floor(\frac{i}{a_i})bi=floor(aii) .通过 bib_ibi 我们可以求出每个位置的取值区间:(1)当 bi=0,i+1≤ai≤nb_i=0, i+1{\leq}a_i{\leq}nbi=0,i+1≤ai≤n(2)当 bi>0,ibi+1+1≤ai≤ibib_i>0,\frac{i}{b_i+1}+1{\leq}a_i{\leq}\frac{i}{b_i}bi>0原创 2022-07-09 12:17:45 · 867 阅读 · 0 评论 -
状压DP/TSP问题
Boboniu and Bit Operations CodeForces 1395C题目链接:https://codeforces.com/problemset/problem/1395/C题目大意给定两个序列a,b,对于ai在序列b中任选一个作为mask,ans |= (ai & mask).思路首先注意序列大小范围<2^9,由于位运算性质最终答案<2^9.定义状态:f[i][j]:前i个数存在状态j(bool) f[i][j]: 前i个数存在状态j(bool) f[i原创 2022-05-02 22:17:49 · 961 阅读 · 0 评论 -
树状数组、线段树(未完待续)
树状数组基本概念1、lowbit(x):取x最低位的1lowbit(x) = x&(-x)eg : lowbit(1010) = 10lowbit(1000) = 10002、和原始数组不同,树状数组存储的不是原始数据,而是原始数组中下标在一个区间的数之和。对于树状数组a[pos],它存储的是下标在区间(pos-lowbit(pos), pos]的数之和。也正是因为这点,树状数组在查询和更新时使用和lowbit相关的操作,实现了查询和更新O(log n)的复杂度。单点更新and区原创 2021-09-19 23:32:26 · 537 阅读 · 0 评论 -
2021暑期排位赛补题合集~
第一场:https://blog.csdn.net/qq_23096319/article/details/119212725?spm=1001.2014.3001.5501C:并查集、启发式合并第五场:https://blog.csdn.net/qq_23096319/article/details/119698665?spm=1001.2014.3001.5501C:DFS、树上倍增I:DP、矩阵快速幂第六场:https://blog.csdn.net/qq_23096319/article/原创 2021-09-01 17:26:45 · 484 阅读 · 0 评论 -
排位赛(九)补题~
Problem B题目大意:给一个n*n的矩阵,由‘.’和‘#’构成,‘.’代表陆地,可以着陆,‘#’代表湖泊,不可以着陆。现有个人要从最左上角跳到最右下角,他一次最多跳k步,问最少步数,若没有,输出-1。思路:暴力bfs的话,复杂度是O(n^2*k),限时:2s,肯定是不行的,但是,我们可以把k优化成logk,通过单调队列维护当前每行每列的最小值,这个最小值指的是从原点跳到该点的最少步数。这样扫一遍矩阵答案就出来了。AC代码#include<bits/stdc++.h>usi原创 2021-08-26 17:31:51 · 529 阅读 · 0 评论 -
排位赛⑦补题
题目https://codeforces.com/gym/102411/problem/E题目大意给定一棵树及若干询问节点,问树上是否存在一点到若干询问节点的距离相等。(所有边权为1)思路多源BFS,注意只有要跳过去的点未被访问过,或者是同一时间访问的,才是合法的跳动。用dep[ ]数组记录节点深度也就是首次访问该节点的时间。AC代码#include<bits/stdc++.h>#define pb push_back#define lowbit(x) x&(-x)原创 2021-08-19 17:04:06 · 461 阅读 · 0 评论 -
DFS和BFS
两种重要的思维方法,搜索方法O(∩_∩)O哈哈~概念DFS:深度优先搜索。可以用递归实现,也可以用栈实现。不一定是在图中,有很多DFS暴力搜索解决的问题。经常用来解决无法确定该写几层循环的问题。要有一个可以被具体描述、存储的状态,状态可以作为递归函数的参数,如果难以传递后者传递的代价过高(比如是个数组),也可以设为全局变量,然后通过打可逆标记来改变状态。对于每个状态,我们要知道它能达到的后续状态有哪些,通过递归来搜索每一个后续状态。搜索完每一个状态之后,此时,我们需要清理搜索时打的标记,防止影响后原创 2021-08-17 16:21:59 · 481 阅读 · 0 评论 -
排位赛(6)继续“发愤”补题
A尝试拍一下纸质题面。题目题目大意思路想直接算的话,直接暴毙O(∩_∩)O哈哈~数据范围达到10^15。一个线性递推式,递推到很大的某一项,祭出:矩阵快速幂!AC代码#include<bits/stdc++.h>#define pb push_back#define lowbit(x) x&(-x)#define mst(a,b) memset(a,b,sizeof a)#define fi first#define se second#define原创 2021-08-14 20:33:50 · 540 阅读 · 0 评论 -
排位赛(5)“发愤”补题
C题目只有纸质题面,等比赛结束之后补上题目大意:给定一棵有n个节点的树,选择一个点作为首都,有q个询问,询问1是改变首都,询问2是问所有点到首都会有多少经过询问节点。数据范围:1 <= N <= 1e5, 1 <= Q <= 5e4思路:问题明显和一个点的子树大小有关。暴力的方法我们会想每更新一次首都都dfs一次。甚至乎比赛过程中,我还脑洞了一种方法:dfs一次把所有点的所有边连出去的树大小求出,询问时,dfs判断首都在询问节点(设为A)的哪一支(设为B),答案是n-s原创 2021-08-14 12:50:53 · 538 阅读 · 1 评论 -
启发式合并(DSU)Jet Trains、HNOI2009 梦幻布丁
启发式算法是基于人类直觉和思维习惯对算法进行优化、改进而得,启发式合并是其中的一个典型,且它的时间复杂度是有保证的。考虑一个基本问题:将 n 个集合,总共含有 m 个元素,合并成为一个大的集合。最糟糕的做法是将当前最大的集合往最小的集合里合并,假设1个集合包含全部 m 个元素,其余为空集,这样操作的复杂度就是O((n−1)∗m)=O(nm)O((n-1)*m) = O(nm)O((n−1)∗m)=O(nm)。我们想为什么不试试将小集合往大集合里合并?这样似乎可以达到使得集合数目减少同时又不费力(移动较原创 2021-07-29 15:56:38 · 777 阅读 · 0 评论 -
2021牛客多校3-B、J
比赛链接https://ac.nowcoder.com/acm/contest/11254B Black and white思路:1、十分巧妙,构造二分(部)图:对于位置(i,j),若其被染黑,则在Ai和Bj之间连一条边,即A是n个点组成的点集,B是m个点组成的点集;2、我们可以发现,魔法操作并不会改变该二分图的连通性(十分重要,画画图可以验证);3、实际上一开始染黑一些格再通过魔法操作使格子全部染黑,要求一开始的状态保证二分图联通即可;4、问题转换为求最小生成树,采用桶排序优化边的排序部分。原创 2021-07-25 10:09:24 · 276 阅读 · 0 评论 -
L. Spicy Restaurant(多源BFS+递推)
L. Spicy Restaurant题目链接https://codeforces.com/gym/103117/problem/L思路:1、考虑到1≤ai≤100 ,一开始直接使用BFS,对每个点进行100次BFS,得到d[i][j],表示最近的点到j点满足辣味<=i的距离,这样的时间复杂度为O(100n^2+q),q为询问个数,会超时。2、用多源BFS,得到dis[i][j],表示j点与最近的辣味等于i的点之间的距离,递推处理再更新一遍,得到dis[i][j]表示j点与最近的辣味<原创 2021-07-17 09:22:03 · 405 阅读 · 0 评论 -
POJ - 1330 Nearest Common Ancestors
Nearest Common AncestorsA rooted tree is a well-known data structure in computer science and engineering. An example is shown below:In the figure, each node is labeled with an integer from {1, 2,…,16}. Node 8 is the root of the tree. Node x is an ancesto原创 2021-03-05 20:40:20 · 99 阅读 · 0 评论 -
UVA - 10583 Ubiquitous Religions
There are so many different religions in the world todaythat it is difficult to keep track of them all. You areinterested in finding out how many different religionsstudents in your university believe in.You know that there are n students in your unive原创 2021-02-25 12:35:51 · 158 阅读 · 0 评论 -
UVA - 136 ugly numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …shows the first 11 ugly numbers. By convention, 1 is included.Write a program to find and print the 1500’th ugly number.InputThere is no原创 2021-02-25 12:28:24 · 69 阅读 · 0 评论 -
CodeForces - 1452B toy blocks
You are asked to watch your nephew who likes to play with toy blocks in a strange way.He has n boxes and the i-th box has ai blocks. His game consists of two steps:he chooses an arbitrary box i;he tries to move all blocks from the i-th box to other boxe原创 2021-02-25 12:03:29 · 190 阅读 · 0 评论 -
LibreOJ - 10015 扩散
题目描述一个点每过一个单位时间就会向 4 个方向扩散一个距离,如图所示:两个点 a 、b 连通,记作 e(a,b),当且仅当 a 、b 的扩散区域有公共部分。连通块的定义是块内的任意两个点 u、v 都必定存在路径 e(u,a0),e(a0,a1),…e(ak,v)。给定平面上的 n 个点,问最早什么时候它们形成一个连通块。输入格式第一行一个数 n ,以下 n 行,每行一个点坐标。输出格式输出仅一个数,表示最早的时刻所有点形成连通块。样例Input Output20 05 55数据范原创 2021-02-25 10:54:36 · 213 阅读 · 0 评论 -
POJ - 2255 tree recovery
POJ - 2255 tree recoveryLittle Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.This is an example of one of her creations:原创 2021-02-23 16:34:14 · 145 阅读 · 0 评论 -
POJ - 3278 Catch That Cow
POJ - 3278 Catch That CowFarmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. F原创 2021-02-23 16:05:31 · 59 阅读 · 0 评论 -
51Nod - 2060 全排列
51Nod - 2060 全排列输入一个整数n(n <= 9),输出1、2、3、······、n这n个数的全排列(按照字典序输出)。Input一个整数nOutput多行,每行表示一种排列,行内使用空格分隔相邻两数。Sample Input3Sample Output1 2 31 3 22 1 32 3 13 1 23 2 1思路:dfs,对于当前位置找之前未使用的数字,然后递归到下一位置代码:#include <iostream>using names原创 2021-02-23 15:45:37 · 150 阅读 · 0 评论 -
UVA514-Rails
UVA514-RailsThere is a famous railway station in PopPush City. Country there is incredibly hilly. The stationwas built in last century. Unfortunately, funds were extremely limited that time. It was possible toestablish only a surface track. Moreover, it原创 2021-02-23 15:28:44 · 115 阅读 · 0 评论