
codeforces
文章平均质量分 66
cf题解
Sankkl1
这个作者很懒,什么都没留下…
展开
-
【2021ICPC上海站】H-Life is a Game(kruskal重构树)
题目链接:https://ac.nowcoder.com/acm/contest/24872/H分析利用kruskal重构树,倍增法向上找到能够走到的最远点,再维护一个子树和即可。代码#include <bits/stdc++.h>#define INF 0x3f3f3f3ftypedef long long ll;const int maxn = 2e5+5;const int mod = 1e9+7;using namespace std;ll sz[maxn], va原创 2021-11-30 16:59:48 · 7302 阅读 · 2 评论 -
【CF1611】E. Escape The Maze(easy+hard)
E1题目链接:https://codeforces.com/contest/1611/problem/E1E2题目链接:https://codeforces.com/contest/1611/problem/E2分析我们定义一个点叫做安全点,即走到这个点上即可到达安全的叶子节点(不会被朋友逮到);朋友初始站的点叫朋友点,否则叫非朋友点;deep[i]deep[i]deep[i]表示第iii号点的深度,md[i]md[i]md[i]表示在第iii号点下面距离iii最近的朋友点的深度。很明显若一个点是叶原创 2021-11-26 11:43:32 · 812 阅读 · 2 评论 -
【CF1610】D. Not Quite Lee(lowbit)
题目链接:https://codeforces.com/contest/1610/problem/D分析所有情况减去不符合要求的情况。可以发现,一个奇数是必定可以的,因为可以以000为中点对称,同理,一些数的和为奇数也是可以满足要求的。那么我们就不必考虑奇数以及和为奇数的情况。经过推断可以发现,若两个数的lowbitlowbitlowbit是不同的,那么他们一定不可以满足要求(例如{2,4}\{2,4\}{2,4})。两个相同lowbitlowbitlowbit的数可以加成一个高一位的lowbit原创 2021-11-25 00:11:05 · 626 阅读 · 0 评论 -
【CF1612】D. X-Magic Pair(数学)
题目链接:https://codeforces.com/contest/1612/problem/D分析可以发现只需要一直将两个数中较大的那个数变小就可以得到所有能得到的数字,需要注意的是,每次都需要把小的那个数化为最小(例如a=6,b=8a=6,b=8a=6,b=8,需要先将aaa化为222),这样才能得到所有情况。代码#include<bits/stdc++.h>using namespace std;typedef long long ll;typedef unsigned原创 2021-11-22 20:08:39 · 1130 阅读 · 0 评论 -
【CF1612】E. Messages(排序)
题目链接:https://codeforces.com/contest/1612/problem/E分析根据每个位置kkk值的和降序排序,因为kkk最大只有202020,所以最多设置前202020个位置。把202020种情况都跑一遍算一遍期望取最大就是答案。代码#include<bits/stdc++.h>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<原创 2021-11-22 19:57:17 · 1213 阅读 · 4 评论 -
【CF1201】D. Treasure Hunting(dp动态规划)
题目链接:https://codeforces.com/problemset/problem/1201/D分析可以发现,若一行中所有的宝箱刚好都被拿完之后,只可能停在最左边或者最右边的宝箱。那么该行的最左边的宝箱点可以从上一行的最左边或最右边的宝箱点的状态转移过来。我们设dp[i][0]dp[i][0]dp[i][0]为刚好拿完前iii行宝箱后停留在第iii行最左端的宝箱的状态的最少步数,dp[i][1]dp[i][1]dp[i][1]则为刚好拿完前iii行宝箱后停留在第iii行最右端的宝箱的状态的原创 2021-11-15 21:24:45 · 549 阅读 · 0 评论 -
【CF1605】D. Treelabeling(二分图)
题目链接:https://codeforces.com/contest/1605/problem/D分析根据题意,我们需要尽量让两个相连的点之间不能到达,于是想到二分图。我们发现,两个数若最高位不同,那么必定不能到达,于是可以根据最高位将n个数划分为若干个不同的集合,集合之间的数不能互相到达。(例如n=9时,我们可以划分为4个集合{1},{2,3},{4,5,6,7},{8,9},集合size分别是1,2,4,2)那么我们只需要根据二分图不同颜色的数量将这些集合组合成两个集合,再将答案填入即可。根原创 2021-11-13 12:26:47 · 697 阅读 · 0 评论 -
【CF1280】C. Jeremy Bearimy(贪心)
题目链接:https://codeforc.es/contest/1280/problem/C分析**最小值:**根据贪心策略,我们每一条边用的次数要尽量少,如果一条边的两边都有偶数个点,那么这条边肯定不用;相反的,如果一条边两边都有奇数个点,那么这条边不得不用,因为奇数个点不足以都凑成对,必须跨边去借点。**最大值:**类似于最小值的思路,每一条边我们要用尽量多的次数,如果一条边的两边分别有 n ,2 * k - n 个点,那么这条边最多可以被用到 min(n, 2 * k - n) 次,这样我们就原创 2021-10-24 10:05:35 · 1513 阅读 · 0 评论 -
【CF1389】E. Calendar Ambiguity(数论)
题目链接:https://codeforces.com/problemset/problem/1389/E分析根据所给条件,可以写出:(x−1)d+y≡(y−1)d+x (mod w)(x-1)d+y\equiv (y-1)d+x \ (mod \ w)(x−1)d+y≡(y−1)d+x (mod w)进一步推出:(x−y)(d−1)≡0 (mod w)(x-y)(d-1)\equiv 0\ (mod\ w)(x−y)(d−1)≡0 原创 2021-10-23 21:53:10 · 231 阅读 · 0 评论 -
【CF1391】D. 505(思维+dp)
题目链接:https://codeforces.com/problemset/problem/1391/D分析可以发现情况为4×44\times44×4以及更大时,不存在符合要求的矩阵,所以只需要判断n=1,2,3n=1, 2, 3n=1,2,3三种情况:n==1n==1n==1:不存在长度是偶数的方阵,答案即000n==2n==2n==2:每列111的数量须是奇偶交错,两种情况试一遍即可n==3n==3n==3:定义dp[i][a][b][c]dp[i][a][b][c]dp[i][a][b]原创 2021-10-23 00:00:59 · 167 阅读 · 0 评论 -
【CF1398】E. Two Types of Spells(set)
题目链接:https://codeforces.com/problemset/problem/1398/E分析不难发现,每次我们要让能被双倍使用的咒语尽量大,能被双倍使用的咒语数量就是1类咒语的数量。而能被双倍使用的咒语即是除了第一次使用的1类咒语外的所有咒语。根据贪心的思想,第一次使用的1类咒语肯定是1类咒语种最小的,其他咒语根据大小排序之后选择最大的几个即可。可以用set来维护,具体实现看代码。代码#include<bits/stdc++.h>using namespace s原创 2021-10-22 11:56:37 · 221 阅读 · 0 评论 -
【CF1393】D. Rarity and New Dress(dp)
题目链接:https://codeforces.com/problemset/problem/1393/D分析如果仅考虑菱形的左上角那块三角,可以发现这是可以用dpdpdp来算出最大长度的,如果(i−1,j)(i - 1, j)(i−1,j)以及(i,j−1)(i, j - 1)(i,j−1)的颜色与(i,j)(i, j)(i,j)颜色相同,那么转移方程就为dp[i][j]=min(dp[i−1][j],dp[i][j−1])+1dp[i][j]=min(dp[i-1][j], dp[i][j-1])+原创 2021-10-22 11:49:17 · 169 阅读 · 0 评论 -
【CF1392】E. Omkar and Duck(构造)
题目链接:https://codeforces.com/problemset/problem/1392/E分析要构造除每一条路径上的和都不同的矩阵只需要根据类似字典序的构造方法即可代码#include<bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<int, int> pii;#define fi first#define se second#define lson (k &原创 2021-10-22 11:43:53 · 172 阅读 · 0 评论 -
【CF1437】E. Make It Increasing(最长上升子序列好题)
题目链接:https://codeforces.com/problemset/problem/1437/E分析简单地说,就是求被几个确定的数分成的每个区间内的最长上升子序列,我们可以对每个区间求一遍最长上升子序列。题目中会有约束:区间的两边是已经确定的数。可以先去除已经在这两个数范围之外的数,因为这样的数是一定要进行操作来修改的。那么如何求最长上升子序列呢,可以参考文章,时间复杂度为O(nlogn)O(nlogn)O(nlogn)。但是要将一个数列修改成严格单调是不能仅仅看最长上升子序列的,例如[原创 2021-10-09 14:04:54 · 205 阅读 · 0 评论 -
【CF1430】E. String Reversal(树状数组区间修改)
题目链接:https://codeforces.com/problemset/problem/1430/E分析先将字符串reverse一下,然后从后往前遍历,每次遍历一个字符,找到这个字符在原串中的最后一个位置,将其移动过来即可。简化一下,相当于每次将某个字符的最右边一个移动到最右边(用过以后要去掉),先记录每个字符的位置pospospos,然后记录这个字符前面有xxx个字符已经被移走了,答案就加上n−pos−xn-pos-xn−pos−x。每个位置的xxx值可以用树状数组来维护,维护区间修改。代原创 2021-10-01 20:12:24 · 204 阅读 · 0 评论 -
【CF1566】E. Buds Re-hanging(思维+dfs)
题目链接:https://codeforces.com/contest/1566/problem/E分析对于一棵树,我们可以将所有的bud都分离开来,分成许多最大深度为2的块:然后可以将其尽量往一条链上连:可以发现如果一个bud点有xxx个叶结点,那么其对答案的贡献为x−1x-1x−1。递归即可,具体看代码代码#include<bits/stdc++.h>using namespace std;typedef long long ll;#define fi first#原创 2021-09-13 10:50:09 · 1811 阅读 · 1 评论 -
【CF1569】D. Inconvenient Pairs(思维+二分)
题目链接:https://codeforces.com/contest/1569/problem/D分析横线和竖线分开来考虑,可以发现不方便的点对只会出现在相邻两条直线之间(不包含线上的点),那么我们只需要知道相邻两条直线之间有多少点就行了,注意要减去选同一直线上点的情况。代码#include<bits/stdc++.h>using namespace std;typedef long long ll;#define fi first#define se second#defi原创 2021-09-09 16:06:55 · 560 阅读 · 0 评论 -
【CF1567】D. Expression Evaluation Error
题目链接:https://codeforces.com/contest/1567/problem/D分析先想一下如果我们将101010拆分成了9+19+19+1,那么最后损失了111,因为10(11)=1110_{(11)} = 1110(11)=11而(9+1)11=10(9 + 1)_{11} = 10(9+1)11=10。如何拆分一个数不会造成损失:将每位上的数单独拆分出来,例如1023=1000+20+31023=1000+20+31023=1000+20+3;在一个位数上进行拆分(原创 2021-09-06 10:35:42 · 500 阅读 · 0 评论 -
【CF1557】D. Ezzat and Grid(离散化+线段树)
题目链接:https://codeforces.com/contest/1557/problem/D分析设dp[i]dp[i]dp[i]表示从111到iii行最多连几行(取第iii行),pre[i]pre[i]pre[i]表示取了iii并且前面是最优取法情况下的前一行。于是我们得到了pre[i]pre[i]pre[i]数组即可得到答案。可以用线段树来维护,每个区间有一个{value,last}\{value, last\}{value,last},表示到该区间的最大取的行数,以及取到这个区间的最新的行原创 2021-08-11 13:22:14 · 209 阅读 · 0 评论 -
【CF1550】C. Manhattan Subarrays(思维)
题目链接:https://codeforces.com/contest/1550/problem/CSuppose you have two points p=(xp,yp) and q=(xq,yq). Let’s denote the Manhattan distance between them as d(p,q)=|xp−xq|+|yp−yq|.Let’s say that three points p, q, r form a bad triple if d(p,r)=d(p,q)+d(q,r原创 2021-07-15 00:35:02 · 598 阅读 · 0 评论 -
【CF1529】C题(树形dp)
题目链接:https://codeforces.com/contest/1529/problem/CC.Parsa’s Humongous TreeParsa has a humongous tree on n vertices.On each vertex v he has written two integers lv and rv.To make Parsa’s tree look even more majestic, Nima wants to assign a number av (lv原创 2021-05-25 23:48:04 · 243 阅读 · 0 评论 -
Codeforces Round #720 (Div. 2) C.Nastia and a Hidden Permutation(思维)
题目链接:https://codeforces.com/contest/1521/problem/CThis is an interactive problem!Nastia has a hidden permutation p of length n consisting of integers from 1 to n. You, for some reason, want to figure out the permutation. To do that, you can give her an i原创 2021-05-08 19:22:58 · 269 阅读 · 1 评论 -
Codeforces Round #716 (Div. 2) C.Product 1 Modulo N(裴蜀定理)
题目链接:http://codeforces.com/contest/1514Now you get Baby Ehab’s first words: “Given an integer nn, find the longest subsequence of [1,2,…,n−1] whose product is 11 modulo nn.” Please solve the problem.A sequence bb is a subsequence of an array aa if bb can原创 2021-04-26 19:08:55 · 224 阅读 · 0 评论 -
Contest 2050 and Codeforces Round #718 D. Explorer Space(dp动态规划)
题目连接:https://codeforces.com/contest/1517/problem/DYou are wandering in the explorer space of the 2050 Conference.The explorer space can be viewed as an undirected weighted grid graph with size n×m. The set of vertices is {(i,j)|1≤i≤n,1≤j≤m}. Two vertices原创 2021-04-24 23:10:40 · 312 阅读 · 0 评论 -
Contest 2050 and Codeforces Round #718 C. Fillomino 2(思维)
题目连接:https://codeforces.com/contest/1517/problem/CFillomino is a classic logic puzzle. (You do not need to know Fillomino in order to solve this problem.) In one classroom in Yunqi town, some volunteers are playing a board game variant of it:Consider an原创 2021-04-24 22:30:30 · 417 阅读 · 0 评论 -
Contest 2050 and Codeforces Round #718 B. Morning Jogging
题目连接:https://codeforces.com/contest/1517/problem/BThe 2050 volunteers are organizing the “Run! Chase the Rising Sun” activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.There are n+1 checkpoints on the trail原创 2021-04-24 22:27:16 · 533 阅读 · 0 评论 -
Educational Codeforces Round 106 (Div. 2) C. Minimum Grid Path(思维 枚举 贪心)
题目链接:https://codeforces.com/contest/1499/problem/CLet’s say you are standing on the XY-plane at point (0,0) and you want to reach point (n,n).You can move only in two directions:to the right, i. e. horizontally and in the direction that increase your x原创 2021-03-19 01:09:25 · 324 阅读 · 0 评论 -
Codeforces Round #705 (Div. 2) C. K-beautiful Strings(思维 枚举)
题目链接:https://codeforces.com/contest/1493/problem/CYou are given a string ss consisting of lowercase English letters and a number kk. Let’s call a string consisting of lowercase English letters beautiful if the number of occurrences of each letter in that原创 2021-03-15 20:24:39 · 345 阅读 · 0 评论 -
Codeforces Round #707 (Div. 2) C. Going Home(思维 暴力)
题目链接:https://codeforces.com/contest/1501/problem/CIt was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya’s friend presented h原创 2021-03-13 20:19:32 · 566 阅读 · 1 评论 -
Educational Codeforces Round 105 (Rated for Div. 2) C. 1D Sokoban(思维 二分)
题目链接:https://codeforc.es/contest/1494/problem/CYou 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 n boxes, the i-th box is on原创 2021-03-03 00:45:28 · 2805 阅读 · 8 评论 -
Codeforces Round #703 (Div. 2) C1. Guessing the Greatest(二分)
题目链接:https://codeforces.com/contest/1486/problem/C1There is an array a of n different numbers. In one query you can ask the position of the second maximum element in a subsegment a[l…r]. Find the position of the maximum element in the array in no more tha原创 2021-03-02 19:25:36 · 161 阅读 · 0 评论 -
Codeforces Round #703 (Div. 2) B. Eastern Exhibition(思维)
题目链接:https://codeforces.com/contest/1486/problem/BYou and your friends live in n houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking y原创 2021-03-02 17:07:49 · 226 阅读 · 0 评论 -
Codeforces Round #704 (Div. 2) C. Maximum width(思维)
题目链接:http://codeforces.com/contest/1492/problem/CYour classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: s of length n and t of length m.A sequence p1,p2,…,pm, where 1≤p1<p2<…<pm≤n, is原创 2021-02-24 17:30:43 · 358 阅读 · 0 评论 -
Educational Codeforces Round 103 (Rated for Div. 2) B. Inflation(思维)
题目链接:http://codeforces.com/contest/1476/problem/BYou have a statistic of price changes for one product represented as an array of n positive integers p0,p1,…,pn−1, where p0 is the initial price of the product and pi is how the price was increased during t原创 2021-01-30 00:37:43 · 1356 阅读 · 1 评论 -
Codeforces Round #698 (Div. 2) B. Nezzar and Lucky Number(思维)
题目链接:http://codeforces.com/contest/1478/problem/BNezzar’s favorite digit among 1,…,9 is d. He calls a positive integer lucky if d occurs at least once in its decimal representation.Given q integers a1,a2,…,aq, for each 1≤i≤q Nezzar would like to know if原创 2021-01-29 14:11:11 · 341 阅读 · 0 评论 -
Codeforces Round #698 (Div. 2) C. Nezzar and Symmetric Array(思维)
题目链接:http://codeforces.com/contest/1478/problem/CLong time ago there was a symmetric array a1,a2,…,a2n consisting of 2n distinct integers. Array a1,a2,…,a2n is called symmetric if for each integer 1≤i≤2n, there exists an integer 1≤j≤2n such that ai=−aj.F原创 2021-01-29 02:00:22 · 457 阅读 · 0 评论 -
Good Bye 2020 E. Apollo versus Pan(按位简化计算)
题目链接:https://codeforc.es/contest/1466/problem/EOnly a few know that Pan and Apollo weren’t only battling for the title of the GOAT musician. A few millenniums later, they also challenged each other in math (or rather in fast calculations). The task they g原创 2021-01-26 16:58:54 · 218 阅读 · 0 评论 -
Codeforces Round #696 (Div. 2) C. Array Destruction
题目连接:https://codeforc.es/contest/1474/problem/CYou found a useless array a of 2n positive integers. You have realized that you actually don’t need this array, so you decided to throw out all elements of a.It could have been an easy task, but it turned ou原创 2021-01-20 02:21:57 · 368 阅读 · 0 评论 -
Codeforces Round #696 (Div. 2) B. Different Divisors(素数筛)
题目连接:https://codeforc.es/contest/1474/problem/BPositive integer x is called divisor of positive integer y, if y is divisible by x without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.We gave you an integer d and asked you to find原创 2021-01-20 01:00:57 · 542 阅读 · 1 评论 -
codeforces Good Bye 2020 D. 13th Labour of Heracles
题目链接:https://codeforc.es/contest/1466/problem/Doutputstandard outputYou’ve probably heard about the twelve labors of Heracles, but do you have any idea about the thirteenth? It is commonly assumed it took him a dozen years to complete the twelve feats, s原创 2020-12-31 23:02:05 · 358 阅读 · 0 评论