- 博客(100)
- 收藏
- 关注
原创 牛客算法課 (算法入門班) 二分, 三分, 01分數規劃
我們二分答案, 有多少套牌.然後如果二分出來的是 x套牌, 我們檢查, 組成x套牌需要用到多少張joker, 如果joker的數量超過x, 說明同一個組合裡用到了超過1張的joker. 我們還要檢查使用的joker 有沒有超過題目限定的m.給定每天有多少個教室可以借. 和. 每天有多少份訂單(包括借出和反還時間 和 數量).暴力思路, 直接遍歷每天的訂單 同時 修改每天可以借出的教室, 用 O(n * m * (t - s)),這個顯然會超時.由於我們知道, 如果某一個天所有教室都借出去了, 然後有人再借那
2022-06-28 10:13:28
468
原创 C++ 多態筆記
多態就是字面意思, 多種型態。這裡我學到了一個非常有用的應用。就是當你有一個基類,想讓他的派生類進行比較排序的時候, 你會發現由於派生類的類別不一樣,你需要重載比較函數很多次,這樣寫下去會死人的。我們可以利用多態的特性。在基類中造出純虛函數,然後讓派生類和基類中的函數同名。如果我們讓兩個不同派生類的基類進行比較的時候,積類會直接去找他所指向的派生類中的同名函數, 這樣就可以減少我們的代碼量。比如: 你現在想比較很多個不同的圖形面積,裡面有矩形,圓形,三角形。如果你要寫cmp function, 你需要寫很多
2022-06-24 13:46:02
379
1
原创 1201. Ugly Number III && 264. Ugly Number ll
First, we can enumerate different numbers as a candidate answer as long as there are n - 1's ugly numbers before it.So, right now we have three conditions here, the first one is all the ugly numbers should be smaller than the answer. The second condition i
2022-06-23 19:11:34
215
原创 1488. Avoid Flood in The City
1. We first use a multiset to store all the index of the sunny day2. We second store the lake in a map using the index as a value3. When we find a lake that already existed on the map, meaning that it would flood, we try to find the first sunny day after t
2022-06-22 17:39:25
194
原创 33. Search in Rotated Sorted Array && 33. Search in Rotated Sorted Array
rotated sorted array. there are two parts. both are sorted, our target either in left to mid, or mid to right.So, basically, we need to find the if the left hand side or right hand side are sorted.if(nums[l]
2022-06-14 18:38:15
66
原创 牛客算法課 (算法入門班) 貪心與模擬(4)
給定長度為n的整數數列{Ai}, 找出兩個整數 ai 和 aj (i < j), 使得ai - aj盡量的大.暴力一定是固定一個點ai, 枚舉aj, 然後打擂台.你會發現如果我們把後綴最小的aj 提前算出來, 我們就可以省略一個nested loop去找最小aj了.又或者你固定aj, 去找前綴最大ai.思維講解 沒有代碼一開始直接有一個思路就是按照字典序排序, 然後從大到小合拼, 但是, 由於每個數字的長度不一樣, 所以有機會出錯, 我們可以考慮, 比較的時候, 讓a + b 和 b + a 去比字典序,
2022-06-13 23:31:56
251
原创 牛客算法課 (算法入門班) 貪心與模擬(3)
我們考慮使用二進制的表示整個矩陣, 然後透過位運算來實現按按鈕的變化.由於同一個按鈕, 按兩次就會等於沒有按過, 所以我們所有按鈕只會按一次. 還有每一個按鈕可以影響的區域為上下左右, 所以與隔離自己超過兩格的地方都沒有直接關係. 因此我們考慮, 我們每一次都會在第i行, 嘗試把第i - 1 行所有格子變為黑, 或全白.那麼剩下的難題就是第一行應該怎麼去改? 考慮所有可能性, 我們只有第一行要枚舉可能性, 剩下的行數就已經可以確認他們的狀態了, 這就跟掃地雷的那一題是一樣的原理.由於題目是問操作次數, 所以
2022-06-11 18:00:19
64
原创 牛客算法課 (算法入門班) 貪心與模擬(2)
目录[HNOI2003]激光炸弹帶權中位數的兩種解法NC107658 poj3061 SubsequenceNC18386 字符串NC207040 丢手绢NC20241 [SCOI2005]扫雷MINE糖糖别胡说,我真的不是签到题目数学考试(值得一做)Xorto(異或前綴和) NC19913[CQOI2009] 中位數圖「土」秘法地震 (垃圾題目, 知道原理就行不要做, 他媽的什麼狗屎輸入, 用cin 就沒辦法做)仓库选址 問區間和拓展至二維, 求矩形和。這個減是減掉重合矩陣dp[i][j] = dp[i]
2022-06-10 04:11:46
288
1
原创 CF797E
關於E題, 我本來想著 把所有數字取模後排序, 然後二分找答案, 但是因為每次選擇某個數字後的刪除操作 需要用O(n), 就不知道怎麼實現了. 而且還可以用二分去找匹配的剩餘價值.原來有multiset這個好工具, 基本能自動排序, 和支持刪除操作, stl大法好, 而且還支持lowerbound 和upperbound 操作,實在方便, erase 操作, 會一次性把所有等於那個數值的東西刪除, 我們只需要刪除用了那個數, 所以每次拿begin()的位置, 然後刪除begin()的位置, 拿了的數字
2022-06-08 14:28:22
99
原创 牛客算法課 (算法入門班) 貪心與模擬(1)
算法的特征:1. 有窮性: 總是在執行有限步後結束2. 確定性: 算法的每一步驟是有明確的定義3. 輸入: 算法可以有輸入可以沒有輸入4. 輸出: 至少有一個輸出5. 可行性: 每一步都是在有限時間內完成算法的評價:1. 正確性2. 可讀性3. 容錯性4. 時間複雜度5. 空間複雜度模擬是什麼?題目讓你幹什麼就幹什麼列題一看就知道 要刪前 後加, 還要查詢 在不在裡面.方法 set + dequedeque的size 大過 m, 就刪除 然後加入新數字[NOIP2011]铺地毯給n張地毯, 求某個
2022-06-07 06:13:59
134
原创 1749. Maximum Absolute Sum of Any Subarray(DP)
Normally, you would try a O(N^2) brute force.but actually, you know we are finding a interval sum and take a absolute value.interval sum i, j = prefixsum[j] - prefixsum[i - 1];so if we want the max interval sum, we need to find the minimum prefixSum and ma
2022-06-06 17:55:32
60
原创 1653. Minimum Deletions to Make String Balanced
Same problem with monotone fliping coin.the machanics is like this, if you encounter b in side some a, you need to flip it, so you store the times you encounter the b.but if you find out what if the whole array is b, or a, or a or b is monopoly in this arr
2022-06-06 06:00:27
89
原创 1105. Filling Bookcase Shelves(dp)
we set dp[i] as the min height of putting previous i's book.how can we update, we just need to take the previous book shelves book to put them in the new shelve (to ensure it wouldnt exceed the length of the book shelve), and we take the max value of book
2022-06-06 05:29:07
90
原创 1911. Maximum Alternating Subsequence Sum(DP)(bb)
First, you would notice this question is quite annoying because odd and even indices would change while you choose different subsequences.And you would notice that other indices could be even or odd in a subsequence except for the first index.Here we w
2022-06-06 05:12:46
87
原创 1372. Longest ZigZag Path in a Binary Tree(DFS/DP)
simple dfs problem, if we change the direction, we add 1 to path length, otherwise reset the path length.of course, we need to know the last node is left or right to judge we have adjusted the direction or not.The hardest thing of this question is to think
2022-06-06 05:02:36
100
原创 1690. Stone Game VII (prefixSum and dp)
because we always need to query the interval sum. So, we make this operation to become O(1) using prefixSum.[1, 2, 3, 4] ^ ^ i i+1if we pick up the left stone, dp[i + 1][j], if we pick up the right stone, dp[i][j - 1].as both players would maximize th
2022-06-06 04:58:09
99
原创 1035. Uncrossed Lines(LCS based dp)
we know the line would not cross, so we can just directly use LCS's templete.
2022-06-06 04:53:23
76
原创 518. Coin Change 2 (knapsack problem)
Classical complete knapsack problem.every value we can pick inifite time as long as it didnt exceed the size of our bag.we would use the 1d array to implement it.we define, dp[i] is using previous i's coin how many ways to make up a certain amount.the outt
2022-06-06 04:51:10
94
原创 2110. Number of Smooth Descent Periods of a Stock (counting dp)
counting ~!
2022-06-06 04:42:00
61
原创 树形问题(笔记)
如何找树的重心?1. dfs一次, 算出以每个点为跟的子树大小2. 记录以每个节点为根的最大子树大小3. 如果以当前节点作为根的最大子树比当前根要优就换POJ 1655树的直径dfs1. 从根找深度最深的节点2.再从最深节点找多一次最深的节点即刻向下更新方法2. 动态规划回溯更新公共祖先LCA 有没有想过为什么我们要这么无聊去找LCA?现在给你一个问题,让你找一颗树上两颗节点的最短距离, 你可以用dijistra, 或者 spfa来找, 但是这样很愚蠢, 因为不仅没有利用树的特性, 还带来
2022-06-06 04:14:17
96
原创 線段樹和樹狀數組 (筆記)
单点修改和区间求和的模板luogu P3374也可以用来求LCA.leetcode 236 luogu P3379 求LCA 整体区间修改.P3373这一题有乘法修改和加法修改, 要弄两个懒标签数组, 一个应对乘法, 一个应对加法.当有乘法的修改时, 我们要把加法的懒标签一并变大同等倍数, 但是加法操作的时候不需要帮乘法懒标签加上, 因为乘法标签只记录倍数, 而这个倍数对加法操作有影响.树状数组不用理解原理, 背了就行.单点修改, 区间求和权值线段树求逆序对什么是权值线段树呢?
2022-06-04 11:11:06
92
原创 P6146 [USACO20FEB]Help Yourself G (計數問題)
題目定義了兩個東西.第一個是 併, 併是指所有被至少一個線段覆蓋的點的集合.第二個是複雜度, 複雜度是指這些並所形成的連通塊數目.如何理解複雜度呢?你可以想象為 一開始有兩條不想交的線段, 他們明顯沒有形成一個連通塊, 而他們兩個自己就是連通塊, 所以開始複雜度為2, 如果這個時候加入一條線段, 他跟其中一個線段相交了, 那麼他們就屬於一個連通塊了. 所以總共還是只有兩個連通塊.那麼如何求出n條線段所有子集的複雜度之和呢?每個線段本身就是一個子集.我們把所有線段按左端點排個序, 這樣我們可以一個一個線段 慢
2022-06-03 16:07:53
52
原创 圖論算法(筆記)
存圖:鏈向星優化//no weight graphvector<int> edge[100];int main(){ for(int i = 1; i <= n; i++){ int uu, vv; cin >> uu >> vv; edge[uu].push_back(vv); //no direction edge[vv].push_back(uu);
2022-05-30 14:39:08
164
原创 926. Flip String to Monotone Increasing(DP)
This is a typical dp question.Monotonic increasing only has three patterns, 000111, 0000 and 1111.In brute force, we just need to calculate a prefix sum for 0 and 1.We use a mid-point to find the minimum flip, the left-hand side of the mid-point shou
2022-05-28 05:21:33
68
原创 131. Palindrome Partitioning(DFS/DP)
We would dfs on the search tree and find all the patterns, we would also write a check method to check if the string is palindrome or not, if it is we would add it to the subset.class Solution {public: bool check(int l, int r, string &s){ .
2022-05-28 03:03:03
55
原创 1493. Longest Subarray of 1‘s After Deleting One Element(DP/sliding window)
Pretty straightforward!We use sliding window and when the 0 appears in that window more than once, we would break the window, otherwise we would keep update the answer.class Solution {public: int longestSubarray(vector<int>& nums) {
2022-05-28 02:28:52
68
原创 1578. Minimum Time to Make Rope Colorful(DP)
Pretty straightforward!We want to make the array colorful which does not allow the same color to appear consecutively.If the same color appears consecutively, we only want to reserve the biggest cost one and deleteothers, because it can guarantee our
2022-05-28 02:25:09
178
原创 1947. Maximum Compatibility Score Sum(DFS/DP)
This question is quite straightforward, Just need to find all combinations and calculate the score using dfs.This is a brute force, and remember the dfs method we need to pass the value by reference otherwise it will use the extra time to copy the vector
2022-05-28 02:13:36
107
原创 516. Longest Palindromic Subsequence (DP)
Here we have two solutions.Both are DP.The first solution is quite smart and difficult to think of.Basically, you just need to reverse the string, and then find the longest common subsequence.The reason is a palindromic string is the same as it is
2022-05-28 01:55:55
77
原创 1031. Maximum Sum of Two Non-Overlapping Subarrays(sliding window + DP)
This is an implicit sliding window problem. It is almost the same as buying and selling the stock 3.The idea is that we gonna set up a middle line, and the line would keep moving forward. When is it moving, we would see if there is any better option for
2022-05-28 00:59:22
75
原创 712. Minimum ASCII Delete Sum for Two Strings(DP)
This is classical matching DP. We can directly use the longest common subsequence solution's idea to solve this question.The question asks us to delete some characters to make two strings the same. Actually, it is the same as finding the longest common s
2022-05-27 23:37:59
68
原创 CF189A (Complete Knapsack DP)
Polycarpus has a ribbon, its length isn. He wants to cut the ribbon in a way that fulfils the following two conditions:After the cutting each ribbon piece should have lengtha,borc. After the cutting the number of ribbon pieces should be maximum.H...
2022-05-20 11:16:08
134
原创 CF230B. T-primes (Number Theory)
We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integertТ-prime, ifthas exactly three distinct positive divisors.You are given an array ofnpositive integers. For e...
2022-05-20 10:17:41
237
原创 241. Different Ways to Add Parentheses(DPS + DP)
Given a stringexpressionof numbers and operators, returnall possible results from computing all the different possible ways to group numbers and operators. You may return the answer inany order.Adding parentheses is like choosing an operator and ca...
2022-05-20 07:14:43
108
原创 983. Minimum Cost For Tickets (DP)
You have planned some train traveling one year in advance. The days of the year in which you will travel are given as an integer arraydays. Each day is an integer from1to365.Train tickets are sold inthree different ways:a1-daypass is sold forc...
2022-05-19 06:15:57
74
原创 526. Beautiful Arrangement (DFS)
Just need to find all the permutations.In DFS we will pass the index of a specific permutation's number and a visited array to mark which number we have added to the permutation.Very simple combination and permutations problem.class Solution {publ.
2022-05-19 06:10:27
63
原创 1706. Where Will the Ball Fall (DP, IQ)
You have a 2-Dgridof sizem x nrepresenting a box, and you havenballs. The box is open on the top and bottom sides.Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.A b...
2022-05-19 02:21:57
206
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人