![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
codeforces
文章平均质量分 75
tomjobs
别慌,慌也没用
展开
-
Codeforces F. Imbalance Value of a Tree(并查集加排序,树)
题意:求出树中所有路径最大值减去最小值之和思路:美团笔试遇到了这题,这里再学习一下。首先考虑简单版本,求出所有路径最大值之和。可以先将所有点按照值排序,然后依次取,每次只考虑所有取出的点,当前的点就是所有点中的最大值点。通过并查集统计点的数目再计数。最小值和也是一样的,将值取负就可以了。具体统计的细节可以看代码,挺好懂的,一个计数问题。#include <cstdio>#include <cstring>#include <algorithm>#incl原创 2021-10-11 12:22:06 · 239 阅读 · 0 评论 -
Codeforces-1496 F. BFS Trees(生成树,最短路)
We define a spanning tree of a graph to be a BFS tree rooted at vertex ???? if and only if for every node ???? the shortest distance between ???? and ???? in the graph is equal to the shortest distance between ???? and ???? in the spanning tree.Given a gr原创 2021-03-13 13:25:29 · 250 阅读 · 0 评论 -
Codeforces-1494 C. 1D Sokoban(推箱子,模拟)
You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.You start on a position 0. There are ???? boxes, the ????-th box is on a position ????????. All positions of the原创 2021-03-12 15:03:01 · 378 阅读 · 1 评论 -
Codeforces-1494 D. Dogeforces(递归,建树)
The Dogeforces company has ???? employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The h原创 2021-03-12 14:48:54 · 280 阅读 · 0 评论 -
Codeforces-1494 E. A-Z Graph(思维,回文)
You are given a directed graph consisting of ???? vertices. Each directed edge (or arc) labeled with a single character. Initially, the graph is empty.You should process ???? queries with it. Each query is one of three types:“+ ???? ???? ????” — add arc原创 2021-03-12 14:43:25 · 241 阅读 · 0 评论 -
Codeforces-1496 D. Let‘s Go Hiking(思维)
On a weekend, Qingshan suggests that she and her friend Daniel go hiking. Unfortunately, they are busy high school students, so they can only go hiking on scratch paper.A permutation ???? is written from left to right on the paper. First Qingshan chooses原创 2021-03-10 23:42:59 · 321 阅读 · 1 评论 -
Codeforces-1496 C. Diamond Miner
Diamond Miner is a game that is similar to Gold Miner, but there are ???? miners instead of 1 in this game.The mining area can be described as a plane. The ???? miners can be regarded as ???? points on the y-axis. There are ???? diamond mines in the minin原创 2021-03-10 23:31:12 · 339 阅读 · 0 评论 -
Codeforces-1496 B. Max and Mex
You are given a multiset ???? initially consisting of ???? distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.You will perform the following operation ???? times:Add the element ⌈????+????2⌉ (rounded up) in原创 2021-03-10 23:28:29 · 383 阅读 · 0 评论 -
Codeforces-1496 A. Split it!
Kawashiro Nitori is a girl who loves competitive programming.One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.Given a string ???? and a parameter ????, you need to check if there exist ????+1 non-原创 2021-03-10 23:26:51 · 363 阅读 · 0 评论 -
Codeforces-1491 C. Pekora and Trampoline(思维)
There is a trampoline park with ???? trampolines in a line. The ????-th of which has strength ????????.Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice.If at the moment Pekora jumps on tram原创 2021-03-01 14:32:02 · 449 阅读 · 0 评论 -
Codeforces-1491 D. Zookeeper and The Infinite Zoo(二进制,思维)
There is a new attraction in Singapore Zoo: The Infinite Zoo.The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled 1,2,3,…. There is a directed edge from vertex ???? to vertex ????+???? if and only if ????&????=???原创 2021-03-01 14:24:49 · 345 阅读 · 1 评论 -
Codeforces-1491 E. Fib-tree(斐波那契数列,树)
Let ???????? denote the ????-th term of Fibonacci sequence, defined as below:????0=????1=1for any integer ????≥0, ????????+2=????????+1+????????You are given a tree with ???? vertices. Recall that a tree is a connected undirected graph without cycles.W原创 2021-03-01 14:08:27 · 774 阅读 · 0 评论 -
Codeforces 1492B - Card Deck
思路:因为根据等比数列求和公式,nk>n1+n2+n3+…+nk−1n^k>n^1+n^2+n^3+…+n^{k-1}nk>n1+n2+n3+…+nk−1,所以从后往前,考虑当前最大没有选择过的数连着后面的所有部分全部移走。#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>#include <vector>using.原创 2021-02-24 00:13:00 · 320 阅读 · 0 评论 -
Codeforces-1492 C. Maximum width(构造)
思路:存下sss字符串中每个字母的位置;从前往后考虑,对于ttt字符串的每个位置依次寻找这个字母最近的大于上一次的位置。这样得到一个可行的ppp序列。我们要求最大间隔,所以可以从后往前考虑,找到ttt中每个位置最后一个小于下一次的位置,然后算出当前和之前的差值,就是对于iii这个位置能得到的p[i]−p[i−1]p[i]-p[i-1]p[i]−p[i−1]最大值。#include <cstdio>#include <algorithm>#include <cstr.原创 2021-02-24 00:10:46 · 379 阅读 · 0 评论 -
Codeforces-1492 D. Genius‘s Gambit(二进制,构造)
思路:特殊的情况是k=0k=0k=0的时候,此时直接输出即可。其他情况,很明显最后构造成这样:1xxxxx1xxx01xxxxx0xxx1要求k<a+b−2k<a+b-2k<a+b−2,并且a≥1,b≥2a≥1,b≥2a≥1,b≥2#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>#include <vector>.原创 2021-02-24 00:06:53 · 239 阅读 · 0 评论 -
Codeforces-1492 E. Almost Fault-Tolerant Database(思维)
You are storing an integer array of length ???? in a database. To maintain internal integrity and protect data, the database stores ???? copies of this array.Unfortunately, the recent incident may have altered the stored information in every copy in the d原创 2021-02-24 00:00:46 · 330 阅读 · 1 评论 -
Codeforces-1486 F. Pairs of Paths(计数,dfs序树状数组,LCA)
题意:一棵树,给你mmm条路径,问有多少对路径满足只有一个交点。思路:设1为根,然后找出每个路径两个端点的lcalcalca。可以得到路径间仅一个交点只有两种情况:两个路径lcalcalca相同,一个路径的lcalcalca在另外一个路径上。先对每个路径进行一下处理,除了记录两个端点x,yx,yx,y记录其lcalcalca,还要保存两个端点对应在lcalcalca子树中位置a,ba,ba,b。并且要保证a<ba<ba<b(交换实现)。对于xxx或者yyy本身就是lcalcal.原创 2021-02-20 16:22:30 · 269 阅读 · 0 评论 -
Codeforces-1486 E. Paired Payment(多维最短路)
题意:无向图,但是一次至少走两步,权值为两个边边权和的平方。求1到其他每个点的最短距离。思路:直观的想法是重构图,将所有的两步变成一个边。但是这样很容易想到,如果某些点存在很多出边,就可以构成很多的两步边,这样复杂度不可接受。观察可知,这里的边权很小,很适合作为一维状态。因为要两条边才能计算一次距离,所以定义dis[x][w][odd]dis[x][w][odd]dis[x][w][odd]代表起点走到xxx,上一条边权值为www,已经走过边数的是否为奇数oddoddodd。这样当从奇数步数转移.原创 2021-02-19 22:37:40 · 548 阅读 · 0 评论 -
Codeforces-1486 D. Max Median(中位数,二分)
题意:求子区间大小不小于kkk能得到的最大中位数。这里的中位数定义为排序后第(n+1)2\frac{(n+1)}{2}2(n+1)的数。思路:二分这个这个最大的中位数midmidmid,将数组中大于midmidmid的位置设置为1,否则设置为-1。这样问题就变成了能否找到大于kkk的和大于0的区间,这个用前缀和维护一下就好了。#include <cstdio>#include <cstring>#include <algorithm>#include &.原创 2021-02-19 22:33:12 · 388 阅读 · 0 评论 -
Codeforces-1486 C2. Guessing the Greatest (hard version)(交互,二分)
题意:一个排列,1e5的范围,至多20次询问,每次可以询问一个子段的次大值位置。求最大值的下标位置。思路:通过第一次询问找出次大值位置pospospos,然后询问(1,pos)(1,pos)(1,pos)确定,如果结果等于pospospos,那么最大值是在pospospos左边,否则在pospospos右边。这样就得到了一个确定了最大值所在的区间,且这个区间左边界(或右边界)为次大值。对于左边界为次大值的情况,假设最大值位置为xxx,那么对于i<xi<xi<x的所有的询问(pos原创 2021-02-19 22:29:32 · 184 阅读 · 0 评论 -
Codeforces-1487 E. Cheap Dinner(线段树优化DP)
思路:定义dp[i][j]dp[i][j]dp[i][j]代表第iii次用餐中,选到了第jjj个物品能得到的最小价值和。就是每一层维护的dp数组用线段树表示,那么限制就是这一层的每个数与之前一层有一些数不能匹配,那么限制就是这一层的每个数与之前一层有一些数不能匹配。不能匹配的数将数组划分为了很多区间,转移就是你在这些区间中之间取最小值加上当前这个数的权值#include <cstdio>#include <cstring>#include <algorithm&g.原创 2021-02-16 16:03:07 · 419 阅读 · 0 评论 -
Codeforces-1487 C. Minimum Ties(构造)
思路:赛中写的复杂些(也过了),这个代码我是参考了别人的,但都没想通证明。不过很明显的是,当nnn为奇数的时候,可以没有平局,只要每个人赢n2\frac{n}{2}2n场就好了。当nnn为偶数的时候,每个人赢n2−1\frac{n}{2}-12n−1场,剩下的场次平局。PS:感觉CF的C题怎么越来越难了,就是那种,可以写,但是很乱搞证明不了。#include <cstdio>#include <cstring>#include <algorithm>#.原创 2021-02-16 16:00:52 · 448 阅读 · 0 评论 -
Codeforces-1487 D. Pythagorean Triples(数学)
思路:数学题,打表可以找出规律,不过我们也可以推一下。c2=a2+b2c^2 = a^2 + b^2c2=a2+b2c=a2−bc = a^2 - bc=a2−b⇒\Rightarrow⇒c2=b2+c+bc^2 = b^2 + c + bc2=b2+c+b⇒\Rightarrow⇒c(c−1)=b(b+1)c(c-1)=b(b+1)c(c−1)=b(b+1)因为c,c−1c,c-1c,c−1互质,b,b+1b,b+1b,b+1互质,所以要么c=bc=bc=b,要么c=b+1c=b+1c=b+.原创 2021-02-16 15:55:43 · 364 阅读 · 0 评论 -
Codeforces-1485 F. Copy or Prefix Sum(DP)
思路:可以想到,结果每个位置由两种可能的值,所以理想结果应该是2n−12^{n-1}2n−1。但是不一定,原因是∑1i−1ai=0∑^{i-1}_1a_i=0∑1i−1ai=0,导致两种取法得到的结果一样。所以我们得用动态规划的方法去重。直观的动态规划思路肯定是dp[i][j]dp[i][j]dp[i][j]代表前缀iii,前缀和为jjj时候能得到的hybridhybridhybrid数组数目。转移就是dp[i][j]+=dp[i−1][j−b[i]],dp[i][b[i]]+=dp[i][j].原创 2021-02-15 18:30:12 · 249 阅读 · 0 评论 -
Codeforces-1485 E. Move and Swap(树上DP)
题意:一棵树,所有边权都是1,1为根节点,叶子与根的距离都为ddd。有红蓝两个物品,初始在根节点,有操作有三个步骤。红物走到一个儿子节点蓝物走到一个节点jjj,满足dis(1,i)+1=dis(1,j)dis(1,i)+1=dis(1,j)dis(1,i)+1=dis(1,j),iii为蓝物当前节点可以交换红蓝两个物品的位置每轮操作完的价值为abs(a[i]−a[j])abs(a[i]-a[j])abs(a[i]−a[j]),i,ji,ji,j为两者位置,当两个物品都在叶子的时候不再继续操.原创 2021-02-15 17:53:30 · 202 阅读 · 0 评论 -
Codeforces-1485 D. Multiples and Power Differences(构造)
思路:纯纯的构造题~~只需要找出1到161到161到16所有数的最小公倍数lcmlcmlcm,将满足(i+j)(i+j)(i+j)为奇数的位置赋值为lcm+a[i][j]4lcm+a[i][j]^4lcm+a[i][j]4,其他位置赋值为lcmlcmlcm。#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;con.原创 2021-02-15 17:39:47 · 211 阅读 · 0 评论 -
Codeforces-1485 C. Floor and Mod(数论,除法分块)
思路:设ab=k\frac{a}{b}=kba=k,那么有k=a−k∗bk=a-k*bk=a−k∗bk=ab+1k=\frac{a}{b+1}k=b+1a很明显,k≤xk≤\sqrt{x}k≤x。因为b>kb>kb>k,所以如果k>xk>\sqrt{x}k>x,那么b+1>xb+1>\sqrt{x}b+1>x,k=ab+1<ax<xk=\frac{a}{b+1}<\frac{a}{\sqrt{x}}<\sqr.原创 2021-02-15 17:34:45 · 271 阅读 · 0 评论 -
Codeforces-1474 A. Puzzle From the Future
In the 2022 year, Mike found two binary integers ???? and ???? of length ???? (both of them are written only by digits 0 and 1) that can have leading zeroes. In order not to forget them, he wanted to construct integer ???? in the following way:he creates原创 2021-01-20 16:41:58 · 326 阅读 · 0 评论 -
Codeforces-1474 B. Different Divisors
Positive integer ???? is called divisor of positive integer ????, if ???? is divisible by ???? without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.We gave you an integer ???? and asked you to find the smallest positive integer ??原创 2021-01-20 16:40:04 · 1051 阅读 · 0 评论 -
Codeforces-1474 C. Array Destruction
You found a useless array ???? of 2???? positive integers. You have realized that you actually don’t need this array, so you decided to throw out all elements of ????.It could have been an easy task, but it turned out that you should follow some rules:In原创 2021-01-20 16:36:58 · 291 阅读 · 0 评论 -
Codeforces-1474 D. Cleaning(前缀和)
During cleaning the coast, Alice found ???? piles of stones. The ????-th pile has ???????? stones.Piles ???? and ????+1 are neighbouring for all 1≤????≤????−1. If pile ???? becomes empty, piles ????−1 and ????+1 doesn’t become neighbouring.Alice is too l原创 2021-01-20 16:32:01 · 377 阅读 · 0 评论 -
Codeforces-1474 E. What Is It?(思维,排列)
Lunar rover finally reached planet X. After landing, he met an obstacle, that contains permutation ???? of length ????. Scientists found out, that to overcome an obstacle, the robot should make ???? an identity permutation (make ????????=???? for all ????)原创 2021-01-20 16:13:39 · 482 阅读 · 1 评论 -
Codeforces-1473 F. Strange Housing(贪心,染色)
Students of Winter Informatics School are going to live in a set of houses connected by underground passages. Teachers are also going to live in some of these houses, but they can not be accommodated randomly. For safety reasons, the following must hold:A原创 2021-01-17 18:09:17 · 311 阅读 · 1 评论 -
Codeforces-1473 E - Strange Shuffle(交互,巧妙)
This is an interactive problem.???? people sitting in a circle are trying to shuffle a deck of cards. The players are numbered from 1 to ????, so that players ???? and ????+1 are neighbours (as well as players 1 and ????). Each of them has exactly ???? ca原创 2021-01-17 18:01:23 · 278 阅读 · 0 评论 -
Codeforces-1471 D. Strange Definition(哈希,质因数分解)
Let us call two integers ???? and ???? adjacent if ????????????(????,????)????????????(????,????) is a perfect square. For example, 3 and 12 are adjacent, but 6 and 9 are not.Here ????????????(????,????) denotes the greatest common divisor (GCD) of intege原创 2021-01-17 17:38:10 · 288 阅读 · 0 评论 -
Codeforces-1473 C - Strange Birthday Party
Petya organized a strange birthday party. He invited ???? friends and assigned an integer ???????? to the ????-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are ???? unique presents available, the ????-th pres原创 2021-01-17 17:27:49 · 216 阅读 · 0 评论 -
Codeforces-1471 B. Strange List
You have given an array ???? of length ???? and an integer ???? to a brand new robot. What the robot does is the following: it iterates over the elements of the array, let the current element be ????. If ???? is divisible by ????, the robot adds ???? copie原创 2021-01-17 17:23:42 · 238 阅读 · 0 评论 -
Codeforces-1471 A. Strange Partition
You are given an array ???? of length ????, and an integer ????. You can perform the following operation as many times as you would like (possibly zero): replace two adjacent elements of the array by their sum. For example, if the initial array was [3,6,9]原创 2021-01-17 17:21:07 · 346 阅读 · 0 评论 -
Codeforces-1473 E. Minimum Path(分层图(多维)最短路)
题意:一条路径的花费为求1点到其他所有点路径的最小花费。思路:每个路径可以分出两种操作:减掉一条边,加上一条边。那么定义dis[i][0/1][0/1]dis[i][0/1][0/1]dis[i][0/1][0/1]代表1到iii路径的最小花费,以及包括第一次操作的使用情况,第二次操作的使用情况。然后就dijdijdij转移就好了。#include <cstdio>#include <cstring>#include <algorithm>#inclu原创 2021-01-16 20:08:39 · 272 阅读 · 0 评论 -
Codeforces-1473 D. Program(前缀和)
You are given a program that consists of ???? instructions. Initially a single variable ???? is assigned to 0. Afterwards, the instructions are of two types:increase ???? by 1;decrease ???? by 1.You are given ???? queries of the following format:query原创 2021-01-16 20:04:31 · 482 阅读 · 0 评论