1.1 Competitive Programming
Total Page:16
File Type:pdf, Size:1020Kb
Contents Foreword vi Preface viii Authors’ Profiles xix List of Abbreviations xx List of Tables xxi List of Figures xxii 1 Introduction 1 1.1CompetitiveProgramming............................ 1 1.2TipstobeCompetitive.............................. 3 1.2.1 Tip1:TypeCodeFaster!........................ 3 1.2.2 Tip2:QuicklyIdentifyProblemTypes................. 4 1.2.3 Tip3:DoAlgorithmAnalysis...................... 6 1.2.4 Tip4:MasterProgrammingLanguages................. 10 1.2.5 Tip5:MastertheArtofTestingCode................. 13 1.2.6 Tip6:PracticeandMorePractice................... 15 1.2.7 Tip7:TeamWork(forICPC)...................... 16 1.3GettingStarted:TheEasyProblems...................... 16 1.3.1 AnatomyofaProgrammingContestProblem............. 16 1.3.2 Typical Input/Output Routines . .................... 17 1.3.3 TimetoStarttheJourney........................ 19 1.4TheAdHocProblems.............................. 21 1.5SolutionstoNon-StarredExercises....................... 27 1.6ChapterNotes................................... 32 2 Data Structures and Libraries 33 2.1OverviewandMotivation............................. 33 2.2LinearDSwithBuilt-inLibraries........................ 35 2.3Non-LinearDSwithBuilt-inLibraries..................... 43 2.4DataStructureswithOurOwnLibraries.................... 49 2.4.1 Graph................................... 49 2.4.2 Union-FindDisjointSets......................... 52 2.4.3 SegmentTree............................... 55 2.4.4 BinaryIndexed(Fenwick)Tree..................... 59 2.5SolutiontoNon-StarredExercises........................ 64 2.6ChapterNotes................................... 67 i CONTENTS c Steven & Felix 3 Problem Solving Paradigms 69 3.1OverviewandMotivation............................. 69 3.2CompleteSearch................................. 70 3.2.1 IterativeCompleteSearch........................ 71 3.2.2 RecursiveCompleteSearch........................ 74 3.2.3 Tips.................................... 76 3.3DivideandConquer............................... 84 3.3.1 InterestingUsagesofBinarySearch................... 84 3.4Greedy....................................... 89 3.4.1 Examples................................. 89 3.5DynamicProgramming.............................. 95 3.5.1 DPIllustration.............................. 95 3.5.2 ClassicalExamples............................ 103 3.5.3 Non-ClassicalExamples......................... 112 3.6SolutiontoNon-StarredExercises........................ 118 3.7ChapterNotes................................... 120 4Graph 121 4.1OverviewandMotivation............................. 121 4.2GraphTraversal.................................. 122 4.2.1 DepthFirstSearch(DFS)........................ 122 4.2.2 BreadthFirstSearch(BFS)....................... 123 4.2.3 FindingConnectedComponents(UndirectedGraph)......... 125 4.2.4 FloodFill-Labeling/ColoringtheConnectedComponents...... 125 4.2.5 TopologicalSort(DirectedAcyclicGraph)............... 126 4.2.6 BipartiteGraphCheck.......................... 128 4.2.7 GraphEdgesPropertyCheckviaDFSSpanningTree......... 128 4.2.8 FindingArticulationPointsandBridges(UndirectedGraph)..... 130 4.2.9 FindingStronglyConnectedComponents(DirectedGraph)...... 133 4.3MinimumSpanningTree............................. 138 4.3.1 OverviewandMotivation........................ 138 4.3.2 Kruskal’sAlgorithm........................... 138 4.3.3 Prim’sAlgorithm............................. 139 4.3.4 OtherApplications............................ 141 4.4Single-SourceShortestPaths........................... 146 4.4.1 OverviewandMotivation........................ 146 4.4.2 SSSP on Unweighted Graph . ...................... 146 4.4.3 SSSP on Weighted Graph . ...................... 148 4.4.4 SSSP on Graph with Negative Weight Cycle . ........... 151 4.5All-PairsShortestPaths............................. 155 4.5.1 OverviewandMotivation........................ 155 4.5.2 Explanation of Floyd Warshall’s DP Solution . ........... 156 4.5.3 OtherApplications............................ 158 4.6NetworkFlow................................... 163 4.6.1 OverviewandMotivation........................ 163 4.6.2 FordFulkerson’sMethod......................... 163 4.6.3 EdmondsKarp’sAlgorithm....................... 164 4.6.4 FlowGraphModeling-Part1...................... 166 4.6.5 OtherApplications............................ 167 4.6.6 FlowGraphModeling-Part2...................... 168 ii CONTENTS c Steven & Felix 4.7SpecialGraphs.................................. 171 4.7.1 DirectedAcyclicGraph.......................... 171 4.7.2 Tree.................................... 178 4.7.3 EulerianGraph.............................. 179 4.7.4 BipartiteGraph.............................. 180 4.8SolutiontoNon-StarredExercises........................ 187 4.9ChapterNotes................................... 190 5 Mathematics 191 5.1OverviewandMotivation............................. 191 5.2AdHocMathematicsProblems......................... 192 5.3JavaBigIntegerClass............................... 198 5.3.1 BasicFeatures............................... 198 5.3.2 BonusFeatures.............................. 199 5.4Combinatorics................................... 204 5.4.1 FibonacciNumbers............................ 204 5.4.2 BinomialCoefficients........................... 205 5.4.3 CatalanNumbers............................. 205 5.4.4 RemarksaboutCombinatoricsinProgrammingContests....... 206 5.5NumberTheory.................................. 210 5.5.1 PrimeNumbers.............................. 210 5.5.2 GreatestCommonDivisor&LeastCommonMultiple......... 211 5.5.3 Factorial.................................. 212 5.5.4 FindingPrimeFactorswithOptimizedTrialDivisions......... 212 5.5.5 WorkingwithPrimeFactors....................... 213 5.5.6 FunctionsInvolvingPrimeFactors................... 214 5.5.7 ModifiedSieve............................... 216 5.5.8 ModuloArithmetic............................ 216 5.5.9 ExtendedEuclid:SolvingLinearDiophantineEquation........ 217 5.5.10RemarksaboutNumberTheoryinProgrammingContests...... 217 5.6 Probability Theory . ............................... 221 5.7Cycle-Finding................................... 223 5.7.1 Solution(s)usingEfficientDataStructure............... 223 5.7.2 Floyd’sCycle-FindingAlgorithm.................... 223 5.8GameTheory................................... 226 5.8.1 DecisionTree............................... 226 5.8.2 MathematicalInsightstoSpeed-uptheSolution............ 227 5.8.3 NimGame................................. 228 5.9SolutiontoNon-StarredExercises........................ 229 5.10ChapterNotes................................... 231 6 String Processing 233 6.1OverviewandMotivation............................. 233 6.2 Basic String Processing Skills .......................... 234 6.3AdHocStringProcessingProblems....................... 236 6.4StringMatching.................................. 241 6.4.1 LibrarySolutions............................. 241 6.4.2 Knuth-Morris-Pratt’s(KMP)Algorithm................ 241 6.4.3 StringMatchingina2DGrid...................... 244 6.5StringProcessingwithDynamicProgramming................. 245 iii CONTENTS c Steven & Felix 6.5.1 StringAlignment(EditDistance).................... 245 6.5.2 Longest Common Subsequence ...................... 247 6.5.3 NonClassicalStringProcessingwithDP................ 247 6.6SuffixTrie/Tree/Array.............................. 249 6.6.1 SuffixTrieandApplications....................... 249 6.6.2 SuffixTree................................. 250 6.6.3 ApplicationsofSuffixTree........................ 251 6.6.4 SuffixArray................................ 253 6.6.5 ApplicationsofSuffixArray....................... 258 6.7SolutiontoNon-StarredExercises........................ 264 6.8ChapterNotes................................... 267 7 (Computational) Geometry 269 7.1OverviewandMotivation............................. 269 7.2BasicGeometryObjectswithLibraries..................... 271 7.2.1 0DObjects:Points............................ 271 7.2.2 1DObjects:Lines............................ 272 7.2.3 2DObjects:Circles............................ 276 7.2.4 2DObjects:Triangles.......................... 278 7.2.5 2DObjects:Quadrilaterals........................ 281 7.3AlgorithmonPolygonwithLibraries...................... 285 7.3.1 PolygonRepresentation......................... 285 7.3.2 PerimeterofaPolygon.......................... 285 7.3.3 AreaofaPolygon............................. 285 7.3.4 CheckingifaPolygonisConvex..................... 286 7.3.5 CheckingifaPointisInsideaPolygon................. 287 7.3.6 CuttingPolygonwithaStraightLine.................. 288 7.3.7 FindingtheConvexHullofaSetofPoints............... 289 7.4SolutiontoNon-StarredExercises........................ 294 7.5ChapterNotes................................... 297 8 More Advanced Topics 299 8.1OverviewandMotivation............................. 299 8.2MoreAdvancedSearchTechniques....................... 299 8.2.1 BacktrackingwithBitmask....................... 299 8.2.2 BacktrackingwithHeavyPruning.................... 304 8.2.3 State-SpaceSearchwithBFSorDijkstra’s............... 305 8.2.4 MeetintheMiddle(BidirectionalSearch)............... 306 8.2.5 InformedSearch:A*andIDA*..................... 308 8.3MoreAdvancedDPTechniques......................... 312 8.3.1 DPwithBitmask............................. 312 8.3.2 CompilationofCommon(DP)Parameters............... 313 8.3.3 HandlingNegativeParameterValueswithOffsetTechnique...... 313 8.3.4 MLE?ConsiderUsingBalancedBSTasMemoTable......... 315 8.3.5 MLE/TLE?UseBetterStateRepresentation.............