刷题
各OJ上的题目
随意凯
这个作者很懒,什么都没留下…
展开
-
leetcode 缺失的第一个正数——巧妙数学思维
给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。示例 1:输入: [1,2,0]输出: 3示例 2:输入: [3,4,-1,1]输出: 2示例 3:输入: [7,8,9,11,12]输出: 1提示:你的算法的时间复杂度应为O(n),并且只能使用常数级别的额外空间。分析:这道题之所以为难题,是因为这个限制条件的约束。这道题解法巧妙在数组下表隐藏的数字1、2、3、4、5.。。。。的排序,这也就满足了题目中额外空间开销为常熟级别的要求。具体分析:如果num[i]>原创 2020-06-27 17:10:47 · 183 阅读 · 0 评论 -
leetcode 87. 扰乱字符串——动态规划或者递归
这道题被列为困难,确实难度大。我起初根本没想到能用动态规划解决,但是递归还是比较好理解的。方法一:递归借的一张图片情况一表示不交换顺序,看s1是否等于t1&&s2是否等于t2情况二表示交换顺序,看之前的s1是否等于s2&&s2是否等于s1(图中没有按照之前的标出,那就靠颜色区分吧)然后不断递归即可注意:如果不剪枝,会超时剪枝:1. 如果字符串为一个字符,直接判断即可2. 可以通过map统计字符个数来判断代码class Solution {publ原创 2020-06-30 10:07:53 · 229 阅读 · 0 评论 -
单调栈的应用
**单调栈:**故名思意,栈中元素是单调递增(不减)或者是单调递减(不增)。leetcode 85——最大矩形 是一个应用单调栈的好题目。题目分析:要找到一个最大矩形,当然可以暴力解法,这个思路简单,这里就不多说了。该如何使用单调栈解决问题呢?可以先尝试地做leetcode 84——柱状图中的最大矩形这个是85题的一个简化版。height数组保留的是柱状图中的高度,left【i】表示的是第i跟柱状图最左边的坐标-1,right【i】表示的是第i根柱状图最右边的距离+1.(所以最后计算矩形时方原创 2020-06-29 18:12:37 · 2529 阅读 · 0 评论 -
Leetcode 最短回文串
题目链接解法一:马拉车算法只是在马发车算法基础上改动了最后的处理方式,因为这道题限制在字符串前面添加字符,所以必须回文子串的起始位置在s字符串的开头才能。class Solution {public: string shortestPalindrome(string s) { //const int M=INT_MAX; int n=s.length(); if(n<=1){ return s;原创 2020-08-29 12:15:19 · 247 阅读 · 0 评论 -
AOJ 0525 Osenbei——DFS解决穷竭搜索问题
利用DFS和位数组来快速解决这道题原创 2020-04-29 17:35:57 · 131 阅读 · 0 评论 -
猴子吃包子——蓝桥杯算法训练
介绍两种方法来解决蓝桥杯算法训练题——猴子吃包子原创 2020-04-19 12:47:38 · 1225 阅读 · 0 评论 -
Cylinder——蓝桥杯提高题
题目: Cylinder时间限制: 1Sec 内存限制: 128MB题目描述Using a sheet of paper and scissors, you can cut out two faces to form a cylinder in the following way:Cut the paper horizontally (parallel to the shorter sid...原创 2020-04-20 19:51:55 · 325 阅读 · 0 评论 -
2^k进制数——蓝桥杯提升题
进制与组合数的问题原创 2020-04-20 18:22:20 · 494 阅读 · 0 评论 -
K-进制数——蓝桥杯提升题
K-进制数这道题利用了一点小技巧,思路简单!原创 2020-04-27 16:20:09 · 334 阅读 · 0 评论 -
杨辉三角形——简单的动态规划
这篇博客不仅介绍杨辉三角形,我还要说明的是二维数组里面的一些技巧和错误形式原创 2020-04-17 22:28:05 · 488 阅读 · 0 评论 -
蓝桥杯历年试题——小计算器
蓝桥杯往年试题——小计算器,考察基础的题目。原创 2020-04-15 09:48:50 · 2467 阅读 · 0 评论 -
蓝桥杯题目——小数第n位
阅读很多博客,我总结以下蓝桥杯的题目——小数第n位的解法原创 2020-04-16 12:25:43 · 369 阅读 · 0 评论 -
AOJ 2200 Mr. Rito Post Office——floyd+动态dp
这道题做了一天,踩了不少坑。。呜呜呜题目大意快递到了:你是某个岛国(ACM-ICPC Japan)上的一个苦逼程序员,你有一个当邮递员的好基友利腾桑遇到麻烦了:全岛有一些镇子通过水路和旱路相连,走水路必须要用船,在X处下船了船就停在X处。而且岛上只有一条船,下次想走水路还是得回到X处才行;两个镇子之间可能有两条以上的水路或旱路;邮递员必须按照清单上的镇子顺序送快递(镇子可能重复,并且对于重复的镇子不允许一次性处理,比如ABCB的话B一定要按顺序走两次才行)。测试数据有多组:N Mx1 y1 t1原创 2020-05-28 18:32:49 · 415 阅读 · 1 评论 -
对局匹配————动态规划
蓝桥杯历年题目——对局匹配,是一道动态规划类型的题目。原创 2020-04-17 16:37:06 · 169 阅读 · 0 评论 -
十六进制转八进制——进制转换
如果不用技巧,这种简单的进制转换要么超时、要么超容量原创 2020-04-17 17:40:16 · 5553 阅读 · 1 评论 -
分考场——无向图着色问题
以蓝桥杯往年例题为基础,解释无向图着色问题。原创 2020-04-15 20:37:23 · 983 阅读 · 0 评论 -
牛客 Shopee的办公室(二)——简单的动态规划思想
题目描述shopee的办公室非常大,小虾同学的位置坐落在右上角,而大门却在左下角,可以把所有位置抽象为一个网格(门口的坐标为0,0),小虾同学很聪明,每次只向上,或者向右走,因为这样最容易接近目的地,但是小虾同学不想让自己的boss们看到自己经常在他们面前出没,或者迟到被发现。他决定研究一下如果他不通过boss们的位置,他可以有多少种走法?输入描述:第一行 x,y,n (0<x<=30, 0<y<=30, 0<=n<= 20) 表示x,y小虾的座位坐标,n 表示bo原创 2020-06-20 21:08:58 · 309 阅读 · 0 评论 -
POJ 3134
DescriptionStarting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:x2 = x × x, x3 = x2 × x, x4 = x3 × x, …, x31 = x30 × x.The operation of squaring can be appreciably shorten the sequence of multiplications. The f原创 2020-08-28 20:30:05 · 206 阅读 · 0 评论 -
poj 1011 一起成为剪枝er吧
DescriptionGeorge took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Pl原创 2020-08-28 12:14:41 · 143 阅读 · 0 评论 -
POJ 2441 状态压缩+动态规划(好题)
DescriptionFarmer Johnson’s Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because they believe that the others are all very weak. Farmer Johnson has N cows (we number the cows from 1 to N) and原创 2020-08-08 12:19:18 · 247 阅读 · 0 评论 -
POJ 1759 详解二分可以解决的原因
DescriptionThe New Year garland consists of N lamps attached to a common wire that hangs down on the ends to which outermost lamps are affixed. The wire sags under the weight of lamp in a particular way: each lamp is hanging at the height that is 1 millim原创 2020-07-27 09:42:14 · 240 阅读 · 0 评论 -
POJ Telephone Lines 二分搜索+Dijkstra方法=最小化第k个值
DescriptionFarmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.There are N (1 ≤ N ≤ 1,000) forlorn telepho原创 2020-07-25 21:15:23 · 135 阅读 · 0 评论 -
POJ 3685 双重二分法解题
DescriptionGiven a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i^2 + 100000 × i + j^2 - 100000 × j + i × j, you are to find the M-th smallest element in the matrix.InputThe first line of input is the number原创 2020-07-24 20:53:28 · 302 阅读 · 0 评论 -
POJ 3292 Semi-prime H-numbers——暴力和素数筛两种方法
DescriptionThis problem is based on an exercise of David Hilbert, who pedagogically suggested that one study the theory of 4n+1 numbers. Here, we do only a bit of that.An H-number is a positive number which is one more than a multiple of four: 1, 5, 9, 1原创 2020-06-17 20:37:42 · 137 阅读 · 0 评论 -
POJ 3268 Silver Cow Party——两次Dijkstra求解单源往返的最短路径问题
DescriptionOne cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1…N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires原创 2020-05-28 09:39:30 · 223 阅读 · 0 评论 -
poj 3259 Wormholes——弗洛伊德判断是否存在负环
DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of F原创 2020-05-27 22:16:37 · 1006 阅读 · 0 评论 -
poj 1703 Find them, Catch them——种类并查集的最简单例题
DescriptionThe police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present quest原创 2020-05-27 11:29:57 · 222 阅读 · 0 评论 -
poj 2010 Moo University - Financial Aid——优先队列+贪心(详细注释)
DescriptionBessie noted that although humans have many universities they can attend, cows have none. To remedy this problem, she and her fellow cows formed a new university called The University of Wisconsin-Farmside,“Moo U” for short.Not wishing to admi原创 2020-05-26 18:47:16 · 189 阅读 · 0 评论 -
POJ 3614 Sunscreen——贪心与优先队列的完美结合
DescriptionTo avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they’re at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work.原创 2020-05-25 11:22:16 · 212 阅读 · 0 评论 -
POJ 2148 Cow Exhibition——01背包方法遇到负状态的处理方法
首先简单说一下dfs的方法,然后重点介绍dp处理这道题的方法原创 2020-05-09 22:27:45 · 194 阅读 · 0 评论 -
POJ 3666 Making the Grade——有难度的动态规划
这道题目是运用动态规划解决的,我看很多博客都没有解释透彻,在这里我将详细地解释这道题的原理及做法原创 2020-05-09 09:51:51 · 228 阅读 · 0 评论 -
POJ 1631——LIS最长上升子序列的三种解法
这道题说白了就是考察怎么实现最长上升子序列快速实现,这里介绍三种方法原创 2020-05-08 11:46:09 · 191 阅读 · 0 评论 -
POJ 3181 Dollar Dayz——两种方法实现划分数问题
这道题是划分数的裸题,可以实现分别使用一维数组和二维数组来实现划分数的问题原创 2020-05-08 09:16:20 · 298 阅读 · 0 评论 -
POJ 2229 Sumsets——动态规划
此题目采用两种方式解决,一种是完全背包解法,另一种是数学方法原创 2020-05-05 22:17:44 · 153 阅读 · 0 评论 -
POJ 3040 Allowance——经典贪心算法题目
本题是一道经典的纯运用贪心算法思想的题目原创 2020-05-04 12:15:56 · 972 阅读 · 0 评论 -
POJ1017 Packets——理解贪心的经典题目
通过这道题理解贪心的思想原创 2020-05-03 23:51:22 · 185 阅读 · 0 评论 -
POJ Stall Reservations——贪心区间+优先队列
利用优先队列来完成贪心算法,极大的减小了时间复杂度和代码实现难度原创 2020-05-01 09:59:59 · 133 阅读 · 0 评论 -
POJ Radar Installation——贪心算法(区间)
我直接说这道题题目重点:x轴是雷达所在位置,x轴以上是海,以下是陆地,海上有岛屿,雷达半径为r,问最少需要多少雷达可实现全覆盖岛屿。错误的思路这是我第一次想的方法,就是对每一个岛屿找到当前岛屿所能对应的雷达的最大值,再看有多少岛屿可以被覆盖,下一次从未被覆盖的第一个岛屿开始继续贪心。但这种方法无法解决下面这个例子:2 5-5 3-3 5会发现第一个对应的最大雷达位置是-1,但是(-...原创 2020-04-30 21:45:25 · 161 阅读 · 0 评论 -
POJ Smallest Difference的几种解法
介绍几种解法,但只有全排列一个可以AC,dfs可以算出正确答案,但是会超时原创 2020-04-30 11:54:08 · 205 阅读 · 0 评论 -
POJ 3046 Ant Counting——多重集组合数+滚动数组
这道题是多重集组合数的裸体,但必须使用滚动数组才可以通过,否则内存超出限制原创 2020-05-07 15:58:21 · 208 阅读 · 0 评论