算法
Dejavu1z
做题,总结,不断的学习
展开
-
2020ICPC·小米 网络选拔赛热身赛
B题意描述思路对于每个数字,我们求出它的贡献。以1,2,1,31,2,1,31,2,1,3为例。a[1]a[1]a[1]在[1,1],[1,2],[1,3],[1,4][1,1],[1,2],[1,3],[1,4][1,1],[1,2],[1,3],[1,4]中出现,且出现了444次,贡献为1∗41*41∗4;a[2]a[2]a[2]在[2,2],[2,3],[2,4],[1,2],[1,3],[1,4][2,2],[2,3],[2,4],[1,2],[1,3],[1,4][2,2],[2,3原创 2020-11-06 19:02:59 · 373 阅读 · 0 评论 -
一些基础数论的知识和证明
算术基本定理N=pα1∗pα2∗...∗pαkN=p^{\alpha _{1}}*p^{\alpha _{2}}*...*p^{\alpha _{k}}N=pα1∗pα2∗...∗pαk约数个数(α1+1)∗(α2+1)...∗(αk+1)(\alpha _{1}+1)*(\alpha _{2}+1)...*(\alpha _{k}+1)(α1+1)∗(α2+1)...∗(αk+1)证明:已知N=pα1∗pα2∗...∗pαkN=p^{\alpha _{1}}*p^{\alpha _{原创 2020-10-07 17:38:26 · 659 阅读 · 4 评论 -
codeforces 1417C(思维)
题意描述思路使用一个vector数组来存储每个数字对应的下标,然后对111到nnn进行遍历,统计该数字相邻位置的最大距离,该距离即为子段长度为kkk时共用的数字。发现,长度为nnn的子段一定包含长度为n−1n-1n−1的子段,所以最后再遍历一遍,使res[i]=min(res[i],res[i−1])res[i]=min(res[i],res[i-1])res[i]=min(res[i],res[i−1])。AC代码#include<bits/stdc++.h>#define x f原创 2020-09-28 11:45:28 · 277 阅读 · 0 评论 -
codeforces 1420C1
题意描述思路我们定义dp[i[[0]dp[i[[0]dp[i[[0]为选择偶数的最大和,即最后一步是加法的最大和;dp[i][1]dp[i][1]dp[i][1]为选择奇数的最大和,即最后一步是减法的最大和。得出转移方程:dp[i][0]=max(dp[i−1][0],dp[i−1][1]+a[i])dp[i][0]=max(dp[i-1][0],dp[i-1][1]+a[i])dp[i][0]=max(dp[i−1][0],dp[i−1][1]+a[i])dp[i][1]=max(dp[i−1]原创 2020-09-27 17:41:41 · 234 阅读 · 0 评论 -
codeforces 1335E1+E2(思维)
题意描述思路+代码easy版本:我们只需要枚举[1,i],[i+1,j],[j+1,n][1,i],[i+1,j],[j+1,n][1,i],[i+1,j],[j+1,n]三个区间即可,设lenlrlenlrlenlr为第一个和第三个区间中出现最多的数字的次数,lenmidlenmidlenmid为第二个区间中出现最多数字的次数,则ans=max(ans,lenlr∗2+lenmid)ans=max(ans,lenlr*2+lenmid)ans=max(ans,lenlr∗2+lenmid)。我们可原创 2020-09-25 22:11:04 · 218 阅读 · 0 评论 -
codeforces 455A(dp)
题意描述Alex doesn’t like boredom. That’s why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.Given a sequence a consisting of n integers. The player can make several steps. In a single st原创 2020-09-24 08:19:14 · 230 阅读 · 0 评论 -
codeforces 1203D2(贪心)
题意描述t是s的子串,求能够删除的最大区间,使得t仍是s的子串思路我们可以从左向右求出t的第i个字母在哪个位置 1=<i<=t1=<i<=t1=<i<=t,然后再同理从右向左求出right数组。最后遍历一遍,取right和left对应字母之间的最大值即可。AC代码#include<bits/stdc++.h>#define x first#define y second#define PB push_back#define mst(x,a)原创 2020-09-18 17:54:30 · 126 阅读 · 0 评论 -
codeforces 1328D(思维)
题意描述The round carousel consists of n figures of animals. Figures are numbered from 1 to n in order of the carousel moving. Thus, after the n-th figure the figure with the number 1 follows. Each figure has its own type — the type of the animal correspondin原创 2020-09-16 08:09:19 · 190 阅读 · 0 评论 -
codeforces 1311D(暴力)
题意描述You are given three integers a≤b≤c.In one move, you can add +1 or −1 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation seve原创 2020-09-15 21:47:28 · 155 阅读 · 0 评论 -
codeforces 1367D(思维)
题意描述Polycarp wrote on the board a string s containing only lowercase Latin letters (‘a’-‘z’). This string is known for you and given in the input.After that, he erased some letters from the string s, and he rewrote the remaining letters in any order. As原创 2020-09-15 20:47:57 · 145 阅读 · 0 评论 -
codeforces1294D(思维+暴力)
题意描述Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples:for the array [0,0,1,0,2] MEX equals to 3 because numbers 0,1 and 2 are presented in the array and 3 is the minimum non-negative integer not pre原创 2020-09-15 16:04:19 · 190 阅读 · 0 评论 -
codeforces1385D (递归+分治)
题意描述You are given a string s[1…n] consisting of lowercase Latin letters. It is guaranteed that n=2k for some integer k≥0.The string s[1…n] is called c-good if at least one of the following three conditions is satisfied:The length of s is 1, and it consi原创 2020-09-14 16:15:10 · 276 阅读 · 0 评论 -
codeforces 1343D(差分数组)
题意描述You are given an array a consisting of n integers (it is guaranteed that n is even, i.e. divisible by 2). All ai does not exceed some integer k.Your task is to replace the minimum number of elements (replacement is the following operation: choose som原创 2020-09-14 15:58:25 · 241 阅读 · 0 评论 -
codeforces 1353D(优先队列)
题意描述You are given an array a of length n consisting of zeros. You perform n actions with this array: during the i-th action, the following sequence of operations appears:Choose the maximum by length subarray (continuous subsegment) consisting only of zer原创 2020-09-13 11:40:16 · 295 阅读 · 0 评论 -
codeforces 1296D(贪心)
题意描述有n个怪物,a和b轮流对怪物造成a和b伤害,有k次超能力,超能力可以跳过对方的回合,如果a击败了怪物,则a获得一分,反之亦然。求a能够获得的最大点数思路要想获得最大的点数,那么要最大限度的使用k次超能力。使怪物的血量对(a+b)(a+b)(a+b)求余,使怪物的血量在1~a+b之间,如果求余后为0,则使血量回滚到之前的状态。然后计算每个血量使用的k的次数,容易想到次数为⌈h/a⌉−1\left \lceil h/a \right \rceil-1⌈h/a⌉−1,最后排序后计算答案即可。AC代原创 2020-09-12 21:59:20 · 208 阅读 · 0 评论 -
codeforces 1216D(数学)
题意描述There were n types of swords in the theater basement which had been used during the plays. Moreover there were exactly x swords of each type. y people have broken into the theater basement and each of them has taken exactly z swords of some single typ原创 2020-09-08 16:07:45 · 253 阅读 · 0 评论 -
codeforces 1405B(思维)
题意描述You’re given an array a of n integers, such that a1+a2+⋯+an=0.In one operation, you can choose two different indices i and j (1≤i,j≤n), decrement ai by one and increment aj by one. If i<j this operation is free, otherwise it costs one coin.How ma原创 2020-09-07 14:52:10 · 281 阅读 · 0 评论 -
codeforces 1234C(思维)
题意描述一个管道有6种类型,两种直管四种弯管,可以将管道任意旋转,给定t个询问,回答每次是否可以从(0,0)到哪(1,n)。思路通过观察可以发现,如果是直管的话,只能向右移动。如果是弯管的话,需要保证同一位置的另一排也是弯管,否则移动失败。AC代码#include<bits/stdc++.h>#define x first#define y second#define PB push_back#define mst(x,a) memset(x,a,sizeof(x))#def原创 2020-09-06 15:19:00 · 291 阅读 · 0 评论 -
codeforces 1256C (贪心+构造)
题意描述There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n+1 (more formally, the river can be represented as a sequence of n+2 cells numbered from 0 to n+1). There are also m wooden platforms on a river, the i-th pl原创 2020-09-05 17:46:12 · 384 阅读 · 0 评论 -
codeforces 1399D
题意描述You are given a binary string s consisting of n zeros and ones.Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence loo原创 2020-08-07 08:37:45 · 265 阅读 · 0 评论 -
codeforces1322A(括号匹配)
题意描述A bracketed sequence is called correct (regular) if by inserting “+” and “1” you can get a well-formed mathematical expression from it. For example, sequences “(())()”, “()” and “(()(()))” are correct, while “)(”, “(()” and “(()))(” are not.The teach原创 2020-08-03 20:51:39 · 192 阅读 · 0 评论 -
codeforces 1040B(思维)
题意描述有n个煎饼,每次可以反转会反转左右k个,求最小反转次数思路如果k为0的话,每个煎饼一定需要反转。如果nnn能够整除2∗k+12*k+12∗k+1的话,那么每次都加2∗k+12*k+12∗k+1一定不会重叠,否则的话,我们可以先对余数的情况进行输出,然后就转换成了整除的状态。AC代码#include<bits/stdc++.h>#define x first#define y second#define PB push_back#define mst(x,a) memse原创 2020-08-02 21:14:11 · 173 阅读 · 0 评论 -
codeforces 1005D(数学)
题意描述给定一个字符串s,询问有s最多能够分成几部分可以被3整除思路我们发现,如果连续三位的数都不能被3整除,那么这三位加起来一定可以被整除。所以我们遍历一遍即可。AC代码#include<bits/stdc++.h>#define x first#define y second#define PB push_back#define mst(x,a) memset(x,a,sizeof(x))#define all(a) begin(a),end(a)#define rep原创 2020-08-02 13:46:36 · 213 阅读 · 0 评论 -
codeforces 1364B(数学)
题意描述Given a permutation p of length n, find its subsequence s1, s2, …, sk of length at least 2 such that:|s1−s2|+|s2−s3|+…+|sk−1−sk| is as big as possible over all subsequences of p with length at least 2.Among all such subsequences, choose the one whos原创 2020-08-01 16:41:16 · 238 阅读 · 0 评论 -
codeforces 1366B(线段相交)
题意描述You are given an array consisting of n integers a1, a2, …, an. Initially ax=1, all other elements are equal to 0.You have to perform m operations. During the i-th operation, you choose two indices c and d such that li≤c,d≤ri, and swap ac and ad.Calc原创 2020-08-01 15:13:30 · 176 阅读 · 0 评论 -
codeforces 1373C (数学)
题意描述给定一段伪代码,求程序结束后res的值res = 0for init = 0 to inf cur = init ok = true for i = 1 to |s| res = res + 1 if s[i] == '+' cur = cur + 1 else cur = cur - 1 if cur < 0 ok = fals原创 2020-07-31 16:15:46 · 318 阅读 · 0 评论 -
HDOJ 2112(最短路)
题意描述经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那原创 2020-07-29 16:10:30 · 190 阅读 · 0 评论 -
codeforces 1144D(思维)
题意描述You are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):Choose a pair of indices (i,j) such that |i−j|=1 (indices i and j are adjacent) and set ai:=ai+|ai−aj|;Choose a pa原创 2020-07-28 16:17:14 · 392 阅读 · 0 评论 -
codeforces 722C(带权并查集+反向思维)
题意描述You are given an array consisting of n non-negative integers a 1, a 2, …, a n.You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroye原创 2020-07-28 15:01:37 · 332 阅读 · 0 评论 -
POJ 2493 (map)
题意描述给定有n个词的字典和m个句子,每个词语除首位和末位的字母可以任意移动,求能够构成m个句子的总共不同情况有多少思路由于每个词语的中间位置可以任意改变,所以我们对每个词语中间的位置进行排序,并用map来记录排序后的词语出现的次数。对于m个句子,由于有空格的存在,所以我们可以使用getline来读取一行并用istringstream来对空格进行分割。AC代码#include<iostream>#include<cstdio>#include<sstream>原创 2020-07-27 19:33:41 · 135 阅读 · 0 评论 -
codeforces 982C (dfs)
题意描述You’re given a tree with n vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.给定一棵树,询问最多删除多少条边可以使每个连通块的数量都是偶数思路这道题的even最初理解成了相同的原创 2020-07-27 14:13:28 · 216 阅读 · 0 评论 -
codeforces 1282B(dp)
题目描述身上有p块钱,可以连续购买k件物品只需要付其中最贵的价钱,求出能够购买物品的最大数量思路我们用f[i]f[i]f[i]表示购买前i件商品的最小花费,因为可以连续购买k件物品,所以当i大于k时,得到转移方程f[i]=min(f[i−1],f[i−k])+a[i]f[i]=min(f[i-1],f[i-k])+a[i]f[i]=min(f[i−1],f[i−k])+a[i]。AC代码#include<bits/stdc++.h>#define x first#define y原创 2020-07-25 16:32:57 · 191 阅读 · 0 评论 -
POJ2955(区间dp)
题意描述求最长括号匹配的子序列长度思路区间dp的经典题目,我们用f[i][j]f[i][j]f[i][j]来表示从i到j的括号序列,如果s[i]s[i]s[i]和s[j]s[j]s[j]处的字符可以构成字符,那么就有f[i][j]=f[i+1][j−1]+2f[i][j]=f[i+1][j-1]+2f[i][j]=f[i+1][j−1]+2,然后就枚举k来求最值。转移方程如下:f[i][j]=f[i+1][j−1]+2f[i][j]=f[i+1][j-1]+2f[i][j]=f[i+1][j−1]+原创 2020-07-25 14:54:14 · 285 阅读 · 0 评论 -
codeforces 1272D(dp)
题意描述You are given an array a consisting of n integers.You can remove at most one element from this array. Thus, the final length of the array is n−1 or n.Your task is to calculate the maximum possible length of the strictly increasing contiguous subarra原创 2020-07-23 15:33:41 · 200 阅读 · 0 评论 -
codeforces 628B(数学)
题意描述Max wants to buy a new skateboard. He has calculated the amount of money that is needed to buy a new skateboard. He left a calculator on the floor and went to ask some money from his parents. Meanwhile his little brother Yusuf came and started to pres原创 2020-07-23 15:15:01 · 182 阅读 · 0 评论 -
codeforces 940B(贪心)
题意描述Right now she actually isn’t. But she will be, if you don’t solve this problem.You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:Subtract 1 from x. This opera原创 2020-07-23 15:12:08 · 191 阅读 · 0 评论 -
codeforces 545C(贪心)
题意描述小灵每天睡觉都要听故事。今天是关于伐木工的一个故事。故事有点无聊,于是她开始神游,想象着伐木工伐木的过程。一条路上有 n 棵树,1棵树占据1个坐标点 x1, x2, …, xn。每棵树有不同的高度 hi。伐木工砍到树后可以让树往右倒或者往左倒,倒下的树就会占据一个路段区间 [xi - hi, xi] 或者 [xi;xi + hi]。没有被砍的树就只占据一个坐标点 xi。被砍倒的树占据的路段区间内不能包含另一棵树的坐标点。伐木工希望尽可能多地砍倒树,请帮小灵计算一下伐木工最多能砍倒多少棵树。思路原创 2020-07-23 15:06:31 · 340 阅读 · 0 评论 -
codeforces 1382C1(思维)
题目描述This is the easy version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved.There are two binary strings a and b of lengt原创 2020-07-22 09:59:30 · 424 阅读 · 0 评论 -
codeforces 544C(完全背包求方案数)
题意描述Programmers working on a large project have just received a task to write exactly m lines of code. There are n programmers working on a project, the i-th of them makes exactly a i bugs in every line of code that he writes.Let’s call a sequence of non原创 2020-07-21 15:56:43 · 780 阅读 · 0 评论 -
codeforces 349B(贪心)
题意描述Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya’s house. Igor thinks that the larger the number is, the more chance to win Tanya’s heart he has.Unfortunately, Igor could only g原创 2020-07-21 15:31:20 · 207 阅读 · 0 评论