From 489cb2ba51b6b1c9fca6c6be5da5c9d0af749916 Mon Sep 17 00:00:00 2001 From: Segcolt <9hmbzr275@mozmail.com> Date: Fri, 12 Sep 2025 14:50:25 -0300 Subject: [PATCH] Add a bunch of other problems. Some are not finished... --- .../G. Maximum Product.cpp | 144 ++++++++ .../A. Attack on Alpha-Zet.cpp | 78 ++++ .../B. Marbles.cpp | 134 +++++++ .../C. Pizza Cutter.cpp | 156 ++++++++ .../D. Unraveling Monty Hall.cpp | 89 +++++ .../E. Enigma.cpp | 97 +++++ .../F. Music Festival.cpp | 81 +++++ .../I. Switches.cpp | 120 ++++++ .../L. Subway Lines.cpp | 159 ++++++++ .../B. Buffoon.cpp | 95 +++++ .../D. Denouncing Mafia.cpp | 129 +++++++ .../H. Hour for a Run.cpp | 91 +++++ .../J. Jar of Water Game.cpp | 204 +++++++++++ .../L. Less Coin Tosses.cpp | 80 ++++ .../M. Maratona Brasileira de Popcorn.cpp | 125 +++++++ .../B. Battleship.cpp | 117 ++++++ .../E. Party Company.cpp | 189 ++++++++++ .../F. Fastminton.cpp | 122 +++++++ .../G. Game Show!.cpp | 96 +++++ .../C. Creating Multiples.cpp | 125 +++++++ .../E. Escalator.cpp | 128 +++++++ .../G. Getting in Shape.cpp | 133 +++++++ .../H. Handling the Blocks.cpp | 111 ++++++ .../K. Kathmandu.cpp | 100 +++++ .../M. Monarchy in Vertigo.cpp | 130 +++++++ .../N. No Luck.cpp | 132 +++++++ .../F. Failing Flagship.cpp | 170 +++++++++ .../J. Jagged Skyline.cpp | 124 +++++++ .../K. Kiosk Construction.cpp | 182 +++++++++ 2022 USP Try-outs/B. Tuk-Tuk Express.cpp | 196 ++++++++++ ... Finding Maximal Non-Trivial Monotones.cpp | 96 +++++ .../D. Displacing Particles.cpp | 108 ++++++ .../E. Eliminating Ballons.cpp | 99 +++++ .../F. Multidimensional Hangman.cpp | 150 ++++++++ .../H. Helping the Transit.cpp | 185 ++++++++++ .../I. Intercepting Information.cpp | 88 +++++ .../J. Playing 23.cpp | 142 ++++++++ .../K. Sam-Oh, the funny coach.cpp | 139 +++++++ .../A. Assessment Disruption.cpp | 83 +++++ .../F. Fast Forward.cpp | 157 ++++++++ .../K. Kernel Scheduler.cpp | 108 ++++++ .../L. Last One Standing.cpp | 116 ++++++ .../M. Mini-Tetris 3023.cpp | 91 +++++ .../N. Naming Wine Bottles.cpp | 131 +++++++ .../J. Journey of the Robber.cpp | 153 ++++++++ .../A. Amusement Park Adventure.cpp | 91 +++++ .../E. Extracting Pollen.cpp | 109 ++++++ .../F. Fatigue-Fighting Vacation.cpp | 101 +++++ .../G. Great Treaty of Byteland.cpp | 314 ++++++++++++++++ .../H. Honest Worker.cpp | 114 ++++++ .../I. Investigating Zeroes and Ones.cpp | 113 ++++++ .../L. Lexicographical Challenge.cpp | 97 +++++ .../M. Maximizing Flight Efficiency.cpp | 121 ++++++ .../I. Neuvillette Circling.cpp | 193 ++++++++++ .../L. Campus.cpp | 231 ++++++++++++ .../I. Nested Circles.cpp | 152 ++++++++ .../E. Enter the Museum.cpp | 140 +++++++ .../B. Bacterial Sampling.cpp | 182 +++++++++ .../D. Dueling Digits.cpp | 117 ++++++ .../E. Egotistical Command Chain.cpp | 111 ++++++ .../J. Japanese Samurai Fight.cpp | 124 +++++++ .../A. Amalgram.cpp | 99 +++++ .../D. Drone Control.cpp | 123 +++++++ .../F. Finding Suspicious Proteins.cpp | 140 +++++++ .../A. Attention to the Meeting.cpp | 81 +++++ .../B. Bacon Number.cpp | 239 ++++++++++++ .../E. Enigma of the Jewelry Case.cpp | 117 ++++++ ...F. Fractions are better when continued.cpp | 89 +++++ .../B. Block sum array.cpp | 141 +++++++ .../E. Simple Skewness.cpp | 201 ++++++++++ .../B. Hamiiid, Haaamid... Hamid?.cpp | 108 ++++++ Codeforces Beta Round 19/E. Fairy.cpp | 89 +++++ .../E. Bertown roads.cpp | 141 +++++++ .../D. Come a Little Closer.cpp | 201 ++++++++++ .../E. Kirei Attacks the Estate.cpp | 149 ++++++++ .../A. Yes-Yes?.cpp | 119 ++++++ .../B. Lost Permutation.cpp | 101 +++++ .../C. Thermostat.cpp | 116 ++++++ .../D. Make It Round.cpp | 122 +++++++ .../A. Skibidus and Amog'u.cpp | 93 +++++ .../B. Skibidus and Ohio.cpp | 93 +++++ ... Skibidus and Fanum Tax (easy version).cpp | 118 ++++++ ... Skibidus and Fanum Tax (hard version).cpp | 130 +++++++ .../D. Skibidus and Sigma.cpp | 105 ++++++ .../E. Skibidus and Rizz.cpp | 127 +++++++ .../F. Skibidus and Slay.cpp | 132 +++++++ .../G. Skibidus and Capping.cpp | 139 +++++++ .../A. New World, New Me, New Array.cpp | 90 +++++ ...er in the Past, I Help Goblins Deceive.cpp | 92 +++++ ... for StORages Has Become My Main Skill.cpp | 109 ++++++ ...Exam Is Easy, but I Couldn't Handle It.cpp | 123 +++++++ ... and His Two-Hit Multi-Target Attacks?.cpp | 117 ++++++ .../F. Goodbye, Banker Life.cpp | 92 +++++ .../C. XOR and Triangle.cpp | 91 +++++ .../A. Olympiad Date.cpp | 108 ++++++ .../B. Team Training.cpp | 113 ++++++ .../C. Combination Lock.cpp | 97 +++++ .../D. Place of the Olympiad.cpp | 96 +++++ .../E. Interesting Ratio.cpp | 104 ++++++ .../F. Igor and Mountain.cpp | 82 +++++ .../G. Gleb and Boating.cpp | 96 +++++ .../A. Ideal Generator.cpp | 89 +++++ .../B. Expensive Number.cpp | 107 ++++++ .../C. Simple Repetition.cpp | 123 +++++++ .../D. Skibidi Table.cpp | 217 +++++++++++ .../E. Min Max MEX.cpp | 137 +++++++ .../F. Hackers and Neural Networks.cpp | 118 ++++++ .../G. Shorten the Array.cpp | 171 +++++++++ .../A. Trippi Troppi.cpp | 85 +++++ .../B. Bobritto Bandito.cpp | 95 +++++ .../C. Brr Brrr Patapim.cpp | 109 ++++++ .../D. Tung Tung Sahur.cpp | 150 ++++++++ .../E. Boneca Ambalabu.cpp | 108 ++++++ .../F. Trulimero Trulicina.cpp | 134 +++++++ .../G. Chimpanzini Bananini.cpp | 131 +++++++ .../H. La Vaca Saturno Saturnita.cpp | 157 ++++++++ Codeforces Round 1020 (Div. 3)/A. Dr. TC.cpp | 103 ++++++ .../B. St. Chroma.cpp | 92 +++++ .../C. Cherry Bomb.cpp | 134 +++++++ .../D. Flower Boy.cpp | 136 +++++++ Codeforces Round 1020 (Div. 3)/E. Wolf.cpp | 181 +++++++++ Codeforces Round 1020 (Div. 3)/F. Goblin.cpp | 104 ++++++ .../G1. Baudelaire (easy version).cpp | 135 +++++++ .../A. Permutation Warm-Up.cpp | 99 +++++ .../B. SUMdamental Decomposition.cpp | 123 +++++++ .../C. Neo's Escape.cpp | 124 +++++++ .../A. LRC and VIP.cpp | 140 +++++++ .../B. Apples in Boxes.cpp | 108 ++++++ .../C. Maximum Subarray Sum.cpp | 173 +++++++++ .../D. Apple Tree Traversing.cpp | 344 ++++++++++++++++++ .../A. Square Year.cpp | 92 +++++ .../B. Not Quite a Palindromic String.cpp | 105 ++++++ .../C. Need More Arrays.cpp | 98 +++++ .../F. Small Operations.cpp | 165 +++++++++ .../A. False Alarm.cpp | 106 ++++++ Codeforces Round 1029 (Div. 3)/B. Shrink.cpp | 89 +++++ .../C. Cool Partition.cpp | 123 +++++++ .../D. Retaliation.cpp | 130 +++++++ .../E. Lost Soul.cpp | 123 +++++++ .../A. Letter Home.cpp | 98 +++++ .../B. Above the Clouds.cpp | 102 ++++++ .../C. Those Who Are With Us.cpp | 140 +++++++ Codeforces Round 1032 (Div. 3)/D. 1709.cpp | 118 ++++++ .../E. Sponsor of Your Problems.cpp | 107 ++++++ .../F. Yamakasi.cpp | 118 ++++++ .../A. Blackboard Game.cpp | 89 +++++ .../B. Tournament.cpp | 111 ++++++ .../C. Prefix Min and Suffix Max.cpp | 153 ++++++++ .../D. Binary String Battle.cpp | 98 +++++ .../E. MEX Count.cpp | 128 +++++++ .../F. Minimize Fixed Points.cpp | 126 +++++++ .../A. Only One Digit.cpp | 97 +++++ .../B. No Casino in the Mountains.cpp | 118 ++++++ .../C. I Will Definitely Make It.cpp | 113 ++++++ .../D. This Is the Last Time.cpp | 106 ++++++ .../E. G-C-D, Unlucky!.cpp | 122 +++++++ .../F. 1-1-1, Free Tree!.cpp | 153 ++++++++ .../A. Recycling Center.cpp | 107 ++++++ .../B. Deque Process.cpp | 123 +++++++ .../C. Leftmost Below.cpp | 107 ++++++ .../D. Sum of LDS.cpp | 102 ++++++ .../B. Stay or Mirror.cpp | 113 ++++++ .../A. Submission is All You Need.cpp | 112 ++++++ Codeforces Round 1042 (Div. 3)/A. Lever.cpp | 117 ++++++ .../B. Alternating Series.cpp | 105 ++++++ .../C. Make it Equal.cpp | 120 ++++++ .../D. Arboris Contractio.cpp | 154 ++++++++ .../E. Adjacent XOR.cpp | 118 ++++++ .../F. Unjust Binary Life.cpp | 135 +++++++ Codeforces Round 1042 (Div. 3)/G. Wafu!.cpp | 146 ++++++++ .../A. Homework.cpp | 116 ++++++ .../B. The Secret Number.cpp | 110 ++++++ .../C1. The Cunning Seller (easy version).cpp | 110 ++++++ .../C2. The Cunning Seller (hard version).cpp | 145 ++++++++ .../D. From 1 to Infinity.cpp | 120 ++++++ .../E. Arithmetics Competition.cpp | 147 ++++++++ .../F. Rada and the Chamomile Valley.cpp | 223 ++++++++++++ .../A. Redstone?.cpp | 105 ++++++ .../B. Villagers.cpp | 104 ++++++ .../C. The Nether.cpp | 149 ++++++++ .../D. Chicken Jockey.cpp | 116 ++++++ .../E. I Yearned For The Mines.cpp | 188 ++++++++++ .../A. Painting With Two Colors.cpp | 96 +++++ .../B. Add 0 or K.cpp | 112 ++++++ .../C. Even Larger.cpp | 116 ++++++ .../D. Sliding Tree.cpp | 168 +++++++++ .../A. In the Dream.cpp | 98 +++++ .../B. Like the Bitset.cpp | 132 +++++++ .../C. Against the Difference.cpp | 155 ++++++++ .../D. For the Champion.cpp | 134 +++++++ .../E. By the Assignment.cpp | 122 +++++++ .../A. Collatz Conjecture.cpp | 95 +++++ .../B. Fun Permutation.cpp | 109 ++++++ .../C. Maximum Even Sum.cpp | 101 +++++ .../D. Replace with Occurrences.cpp | 134 +++++++ .../E. Mexification.cpp | 164 +++++++++ .../F. Prefix Maximum Invariance.cpp | 143 ++++++++ .../A. Maple and Multiplication.cpp | 102 ++++++ .../A. Shift Sort.cpp | 108 ++++++ .../B. Another Divisibility Problem.cpp | 91 +++++ .../C. Ultimate Value.cpp | 208 +++++++++++ .../E. Build String.cpp | 207 +++++++++++ .../E. Xenia and Tree.cpp | 119 ++++++ .../E. Valera and Queries.cpp | 195 ++++++++++ .../C. Watto and Mechanism.cpp | 147 ++++++++ .../E. Soldier and Traveling.cpp | 213 +++++++++++ .../B. Mike and Feet.cpp | 109 ++++++ .../C. Kefa and Park.cpp | 128 +++++++ .../D. Vasiliy's Multiset.cpp | 158 ++++++++ .../B. Mahmoud and a Triangle.cpp | 90 +++++ .../B. Karen and Coffee.cpp | 139 +++++++ Codeforces Round 447 (Div. 2)/A. QAQ.cpp | 102 ++++++ .../D. Substring.cpp | 133 +++++++ .../E. Cyclic Components.cpp | 128 +++++++ .../F. Xor-Paths.cpp | 140 +++++++ .../D. Almost All Divisors.cpp | 158 ++++++++ .../E. By Elevator or Stairs?.cpp | 97 +++++ .../C. Skyline Photo.cpp | 237 ++++++++++++ .../H. Maximal AND.cpp | 105 ++++++ .../A. Number Transformation.cpp | 89 +++++ .../B. Dictionary.cpp | 93 +++++ .../C. Infinite Replacement.cpp | 97 +++++ .../D. A-B-C Sort.cpp | 118 ++++++ .../E. Breaking the Wall.cpp | 113 ++++++ .../F. Desktop Rearrangement.cpp | 133 +++++++ .../B. Stone Age Problem.cpp | 121 ++++++ .../B. Playing in a Casino.cpp | 110 ++++++ .../D. Running Miles.cpp | 107 ++++++ .../A. Love Story.cpp | 93 +++++ .../B. Blank Space.cpp | 100 +++++ .../C. Mr. Perfectly Fine.cpp | 108 ++++++ .../D. Gold Rush.cpp | 99 +++++ .../E. The Lakes.cpp | 123 +++++++ .../F. Forever Winter.cpp | 112 ++++++ .../G. Hits Different.cpp | 113 ++++++ .../H. Don't Blame Me.cpp | 106 ++++++ .../A. How Much Does Daytona Cost?.cpp | 94 +++++ .../B. Aleksa and Stack.cpp | 93 +++++ .../C. Vasilije in Cacak.cpp | 92 +++++ .../E. Iva & Pav.cpp | 128 +++++++ Codeforces Round 905 (Div. 3)/A. Morning.cpp | 101 +++++ .../B. Chemistry.cpp | 114 ++++++ .../C. Raspberries.cpp | 121 ++++++ Codeforces Round 905 (Div. 3)/D. In Love.cpp | 158 ++++++++ .../E. Look Back.cpp | 99 +++++ .../G1. Dances (Easy version).cpp | 121 ++++++ .../G2. Dances (Hard Version).cpp | 126 +++++++ .../F. Greetings.cpp | 106 ++++++ Codeforces Round 918 (Div. 4)/G. Bicycles.cpp | 130 +++++++ .../D. Divisible Pairs.cpp | 101 +++++ .../D. Binary Cut.cpp | 101 +++++ Codeforces Round 944 (Div. 4)/G. XOUR.cpp | 120 ++++++ .../C. Tree Pruning.cpp | 150 ++++++++ .../A. Line Breaks.cpp | 101 +++++ .../B. Transfusion.cpp | 106 ++++++ .../C. Uninteresting Number.cpp | 115 ++++++ .../D. Digital string maximization.cpp | 101 +++++ .../E. Three Strings.cpp | 162 +++++++++ .../F. Maximum modulo equality.cpp | 106 ++++++ .../A. Preparing for the Olympiad.cpp | 97 +++++ Codeforces Round 995 (Div. 3)/B. Journey.cpp | 98 +++++ .../C. Preparing for the Exam.cpp | 112 ++++++ .../D. Counting Pairs.cpp | 138 +++++++ .../E. Best Price.cpp | 139 +++++++ .../A. Fibonacciness.cpp | 99 +++++ .../B. Farmer John's Card Game.cpp | 130 +++++++ .../C. Game of Mathletes.cpp | 113 ++++++ .../D. Subtract Min Sort.cpp | 100 +++++ .../E. Graph Composition.cpp | 158 ++++++++ .../F. Four Divisors.cpp | 111 ++++++ .../D. Array Collapse.cpp | 155 ++++++++ .../E. Level Up.cpp | 107 ++++++ .../A. FizzBuzz Remixed.cpp | 90 +++++ .../B. Robot Program.cpp | 122 +++++++ .../C. Limited Repainting.cpp | 78 ++++ .../E. Changing the String.cpp | 149 ++++++++ .../D. Segments Covering.cpp | 146 ++++++++ .../E. Maximum Subsequence.cpp | 132 +++++++ .../A. Chess For Three.cpp | 105 ++++++ .../B. Beautiful Divisors.cpp | 114 ++++++ .../C. Rumor.cpp | 127 +++++++ .../D. Credit Card.cpp | 155 ++++++++ .../E. Counting Arrays.cpp | 149 ++++++++ .../A. Hungry Student Problem.cpp | 106 ++++++ .../B. The Modcrab.cpp | 114 ++++++ .../C. Boxes Packing.cpp | 114 ++++++ .../D. Almost Difference.cpp | 170 +++++++++ .../E. Swapping Characters.cpp | 188 ++++++++++ .../A. Nearest Minimums.cpp | 101 +++++ .../B. Two Cakes.cpp | 95 +++++ .../C. Three Garlands.cpp | 111 ++++++ .../D. Inversion Counting.cpp | 129 +++++++ .../E. Stack Sorting.cpp | 142 ++++++++ .../A. Garden.cpp | 98 +++++ .../B. Browser.cpp | 117 ++++++ .../C. Permute Digits.cpp | 153 ++++++++ .../D. Almost Acyclic Graph.cpp | 191 ++++++++++ .../E. Physical Education Lessons.cpp | 128 +++++++ .../A. Water The Garden.cpp | 112 ++++++ .../B. Tea Queue.cpp | 115 ++++++ .../C. Swap Adjacent Elements.cpp | 124 +++++++ .../D. Tanks.cpp | 244 +++++++++++++ .../E. Connected Components?.cpp | 134 +++++++ .../F. SUM and REPLACE.cpp | 183 ++++++++++ .../G. List Of Integers.cpp | 162 +++++++++ .../A. Word Correction.cpp | 107 ++++++ .../B. Run For Your Prize.cpp | 103 ++++++ .../C. Constructing Tests.cpp | 131 +++++++ .../D. Buy a Ticket.cpp | 129 +++++++ .../E. Max History.cpp | 83 +++++ .../A. Partition.cpp | 96 +++++ .../B. Weird Subtraction Process.cpp | 103 ++++++ .../C. String Transformation.cpp | 111 ++++++ .../D. Timetable.cpp | 130 +++++++ .../E. Largest Beautiful Number.cpp | 93 +++++ .../F. Fibonacci String Subsequences.cpp | 84 +++++ .../F. Fatorial.cpp | 224 ++++++++++++ .../M. Mangabas.cpp | 160 ++++++++ .../N. Não é permitido erros!.cpp | 121 ++++++ .../A. Binary Search.cpp | 103 ++++++ .../B. Closest to the Left.cpp | 97 +++++ .../C. Closest to the Right.cpp | 97 +++++ .../D. Fast search.cpp | 108 ++++++ .../A. Packing Rectangles.cpp | 102 ++++++ .../B. Ropes.cpp | 117 ++++++ .../E. Equation.cpp | 102 ++++++ .../G. Student Councils.cpp | 123 +++++++ .../H. Hamburgers.cpp | 125 +++++++ .../A. Get together.cpp | 118 ++++++ .../B. Splitting an Array.cpp | 125 +++++++ .../C. Cows in Stalls.cpp | 116 ++++++ .../D. Minimum maximum on the Path.cpp | 163 +++++++++ .../A. Maximum Average Segment.cpp | 141 +++++++ .../B. Minimum Average Path.cpp | 159 ++++++++ .../C. Pair Selection.cpp | 121 ++++++ .... K-th Number in the Union of Segments.cpp | 116 ++++++ .../B. Multiplication Table.cpp | 108 ++++++ .../C. K-th Sum.cpp | 117 ++++++ .../C. Experience.cpp | 150 ++++++++ .../A. Segment Tree for the Sum.cpp | 143 ++++++++ .../B. Segment Tree for the Minimum.cpp | 144 ++++++++ .../C. Number of Minimums on a Segment.cpp | 153 ++++++++ .../B. K-th one.cpp | 148 ++++++++ .../C. First element at least X.cpp | 148 ++++++++ .../D. First element at least X - 2.cpp | 149 ++++++++ .../A. Inversions.cpp | 125 +++++++ .../B. Inversions 2.cpp | 144 ++++++++ .../C. Nested Segments.cpp | 137 +++++++ .../D. Intersecting Segments.cpp | 152 ++++++++ .../E. Addition to Segment.cpp | 137 +++++++ .../A. Sign alternation.cpp | 162 +++++++++ .../B. Cryptography.cpp | 164 +++++++++ .../C. Number of Inversions on Segment.cpp | 171 +++++++++ .../D. Number of Different on Segment.cpp | 166 +++++++++ .../E. Earthquakes.cpp | 139 +++++++ .../A. Addition to Segment.cpp | 163 +++++++++ .../B. Applying MAX to Segment.cpp | 163 +++++++++ .../C. Assignment to Segment.cpp | 166 +++++++++ .../A. Addition and Minimum.cpp | 167 +++++++++ .../B. Multiplication and Sum.cpp | 167 +++++++++ .../C. Bitwise OR and AND.cpp | 163 +++++++++ .../D. Addition and Sum.cpp | 163 +++++++++ .../E. Assignment and Minimum.cpp | 163 +++++++++ .../F. Assignment and Sum.cpp | 162 +++++++++ .../A. Assignment and Maximal Segment.cpp | 190 ++++++++++ .../B. Inverse and K-th one.cpp | 165 +++++++++ ... Addition and First element at least X.cpp | 176 +++++++++ .../A. Assignment, Addition, and Sum.cpp | 234 ++++++++++++ ... Add Arithmetic Progression On Segment.cpp | 84 +++++ .../J. Journey To Stringland.cpp | 113 ++++++ Manthan, Codefest 16/C. Spy Syndrome 2.cpp | 156 ++++++++ OBI IV - 2024/J. A Viagem de Wevton.cpp | 108 ++++++ OBI V - 2024/F. Sanduíche.cpp | 131 +++++++ OBI VI - 2024/H. Retângulo.cpp | 138 +++++++ .../D. Frequency of String.cpp | 172 +++++++++ Trie - Hash/C - Watto and Mechanism.cpp | 78 ++++ .../B. Boundless Deck.cpp | 99 +++++ .../D. Delirium at Unballoon.cpp | 91 +++++ .../E. Elementary Magical School of Words.cpp | 147 ++++++++ VI UnBalloon Contest Mirror/F. Fibonacci.cpp | 195 ++++++++++ .../G. Graph of Love.cpp | 147 ++++++++ .../J. Wasserman, Rei das Águas.cpp | 197 ++++++++++ .../B. Bares.cpp | 160 ++++++++ .../F. Fome de Queijo.cpp | 213 +++++++++++ .../I. Interativo.cpp | 114 ++++++ 385 files changed, 49442 insertions(+) create mode 100644 2015-2016 Petrozavodsk Winter Training Camp, Saratov SU Contest/G. Maximum Product.cpp create mode 100644 2018 German Collegiate Programming Contest (GCPC 18)/A. Attack on Alpha-Zet.cpp create mode 100644 2018-2019 ACM-ICPC Brazil Subregional Programming Contest/B. Marbles.cpp create mode 100644 2018-2019 ACM-ICPC Brazil Subregional Programming Contest/C. Pizza Cutter.cpp create mode 100644 2018-2019 ACM-ICPC Brazil Subregional Programming Contest/D. Unraveling Monty Hall.cpp create mode 100644 2018-2019 ACM-ICPC Brazil Subregional Programming Contest/E. Enigma.cpp create mode 100644 2018-2019 ACM-ICPC Brazil Subregional Programming Contest/F. Music Festival.cpp create mode 100644 2018-2019 ACM-ICPC Brazil Subregional Programming Contest/I. Switches.cpp create mode 100644 2018-2019 ACM-ICPC Brazil Subregional Programming Contest/L. Subway Lines.cpp create mode 100644 2019-2020 ACM-ICPC Brazil Subregional Programming Contest/B. Buffoon.cpp create mode 100644 2019-2020 ACM-ICPC Brazil Subregional Programming Contest/D. Denouncing Mafia.cpp create mode 100644 2019-2020 ACM-ICPC Brazil Subregional Programming Contest/H. Hour for a Run.cpp create mode 100644 2019-2020 ACM-ICPC Brazil Subregional Programming Contest/J. Jar of Water Game.cpp create mode 100644 2019-2020 ACM-ICPC Brazil Subregional Programming Contest/L. Less Coin Tosses.cpp create mode 100644 2019-2020 ACM-ICPC Brazil Subregional Programming Contest/M. Maratona Brasileira de Popcorn.cpp create mode 100644 2020-2021 ACM-ICPC Brazil Subregional Programming Contest/B. Battleship.cpp create mode 100644 2020-2021 ACM-ICPC Brazil Subregional Programming Contest/E. Party Company.cpp create mode 100644 2020-2021 ACM-ICPC Brazil Subregional Programming Contest/F. Fastminton.cpp create mode 100644 2020-2021 ACM-ICPC Brazil Subregional Programming Contest/G. Game Show!.cpp create mode 100644 2021-2022 ACM-ICPC Brazil Subregional Programming Contest/C. Creating Multiples.cpp create mode 100644 2021-2022 ACM-ICPC Brazil Subregional Programming Contest/E. Escalator.cpp create mode 100644 2021-2022 ACM-ICPC Brazil Subregional Programming Contest/G. Getting in Shape.cpp create mode 100644 2021-2022 ACM-ICPC Brazil Subregional Programming Contest/H. Handling the Blocks.cpp create mode 100644 2021-2022 ACM-ICPC Brazil Subregional Programming Contest/K. Kathmandu.cpp create mode 100644 2021-2022 ACM-ICPC Brazil Subregional Programming Contest/M. Monarchy in Vertigo.cpp create mode 100644 2021-2022 ACM-ICPC Brazil Subregional Programming Contest/N. No Luck.cpp create mode 100644 2022 Benelux Algorithm Programming Contest (BAPC 22)/F. Failing Flagship.cpp create mode 100644 2022 Benelux Algorithm Programming Contest (BAPC 22)/J. Jagged Skyline.cpp create mode 100644 2022 Benelux Algorithm Programming Contest (BAPC 22)/K. Kiosk Construction.cpp create mode 100644 2022 USP Try-outs/B. Tuk-Tuk Express.cpp create mode 100644 2022-2023 ICPC Brazil Subregional Programming Contest/A. Finding Maximal Non-Trivial Monotones.cpp create mode 100644 2022-2023 ICPC Brazil Subregional Programming Contest/D. Displacing Particles.cpp create mode 100644 2022-2023 ICPC Brazil Subregional Programming Contest/E. Eliminating Ballons.cpp create mode 100644 2022-2023 ICPC Brazil Subregional Programming Contest/F. Multidimensional Hangman.cpp create mode 100644 2022-2023 ICPC Brazil Subregional Programming Contest/H. Helping the Transit.cpp create mode 100644 2022-2023 ICPC Brazil Subregional Programming Contest/I. Intercepting Information.cpp create mode 100644 2022-2023 ICPC Brazil Subregional Programming Contest/J. Playing 23.cpp create mode 100644 2023 ICPC HIAST Collegiate Programming Contest/K. Sam-Oh, the funny coach.cpp create mode 100644 2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/A. Assessment Disruption.cpp create mode 100644 2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/F. Fast Forward.cpp create mode 100644 2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/K. Kernel Scheduler.cpp create mode 100644 2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/L. Last One Standing.cpp create mode 100644 2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/M. Mini-Tetris 3023.cpp create mode 100644 2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/N. Naming Wine Bottles.cpp create mode 100644 2023-2024 ACM-ICPC Latin American Regional Programming Contest/J. Journey of the Robber.cpp create mode 100644 2023-2024 ICPC Brazil Subregional Programming Contest/A. Amusement Park Adventure.cpp create mode 100644 2023-2024 ICPC Brazil Subregional Programming Contest/E. Extracting Pollen.cpp create mode 100644 2023-2024 ICPC Brazil Subregional Programming Contest/F. Fatigue-Fighting Vacation.cpp create mode 100644 2023-2024 ICPC Brazil Subregional Programming Contest/G. Great Treaty of Byteland.cpp create mode 100644 2023-2024 ICPC Brazil Subregional Programming Contest/H. Honest Worker.cpp create mode 100644 2023-2024 ICPC Brazil Subregional Programming Contest/I. Investigating Zeroes and Ones.cpp create mode 100644 2023-2024 ICPC Brazil Subregional Programming Contest/L. Lexicographical Challenge.cpp create mode 100644 2023-2024 ICPC Brazil Subregional Programming Contest/M. Maximizing Flight Efficiency.cpp create mode 100644 2024 (ICPC) Jiangxi Provincial Contest -- Official Contest/I. Neuvillette Circling.cpp create mode 100644 2024 (ICPC) Jiangxi Provincial Contest -- Official Contest/L. Campus.cpp create mode 100644 2024 Aleppo Collegiate Programming Contest/I. Nested Circles.cpp create mode 100644 2024 ICPC Belarus Regional Contest/E. Enter the Museum.cpp create mode 100644 2024 ICPC Gran Premio de Mexico 1ra Fecha/B. Bacterial Sampling.cpp create mode 100644 2024 ICPC Gran Premio de Mexico 2da Fecha/D. Dueling Digits.cpp create mode 100644 2024 ICPC Gran Premio de Mexico 2da Fecha/E. Egotistical Command Chain.cpp create mode 100644 2024 ICPC Gran Premio de Mexico 2da Fecha/J. Japanese Samurai Fight.cpp create mode 100644 2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/A. Amalgram.cpp create mode 100644 2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/D. Drone Control.cpp create mode 100644 2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/F. Finding Suspicious Proteins.cpp create mode 100644 2024-2025 ICPC Brazil Subregional Programming Contest/A. Attention to the Meeting.cpp create mode 100644 2024-2025 ICPC Brazil Subregional Programming Contest/B. Bacon Number.cpp create mode 100644 2024-2025 ICPC Brazil Subregional Programming Contest/E. Enigma of the Jewelry Case.cpp create mode 100644 2024-2025 ICPC Brazil Subregional Programming Contest/F. Fractions are better when continued.cpp create mode 100644 2025 Argentinian Programming Tournament (TAP)/B. Block sum array.cpp create mode 100644 8VC Venture Cup 2016 - Elimination Round/E. Simple Skewness.cpp create mode 100644 Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)/B. Hamiiid, Haaamid... Hamid?.cpp create mode 100644 Codeforces Beta Round 19/E. Fairy.cpp create mode 100644 Codeforces Beta Round 89 (Div. 2)/E. Bertown roads.cpp create mode 100644 Codeforces Round 1027 (Div. 3)/D. Come a Little Closer.cpp create mode 100644 Codeforces Round 1027 (Div. 3)/E. Kirei Attacks the Estate.cpp create mode 100644 Codeforces Round 834 (Div. 3)/A. Yes-Yes?.cpp create mode 100644 Codeforces Round 834 (Div. 3)/B. Lost Permutation.cpp create mode 100644 Codeforces Round 834 (Div. 3)/C. Thermostat.cpp create mode 100644 Codeforces Round 834 (Div. 3)/D. Make It Round.cpp create mode 100644 Codeforces Round 1003 (Div. 4)/A. Skibidus and Amog'u.cpp create mode 100644 Codeforces Round 1003 (Div. 4)/B. Skibidus and Ohio.cpp create mode 100644 Codeforces Round 1003 (Div. 4)/C1. Skibidus and Fanum Tax (easy version).cpp create mode 100644 Codeforces Round 1003 (Div. 4)/C2. Skibidus and Fanum Tax (hard version).cpp create mode 100644 Codeforces Round 1003 (Div. 4)/D. Skibidus and Sigma.cpp create mode 100644 Codeforces Round 1003 (Div. 4)/E. Skibidus and Rizz.cpp create mode 100644 Codeforces Round 1003 (Div. 4)/F. Skibidus and Slay.cpp create mode 100644 Codeforces Round 1003 (Div. 4)/G. Skibidus and Capping.cpp create mode 100644 Codeforces Round 1006 (Div. 3)/A. New World, New Me, New Array.cpp create mode 100644 Codeforces Round 1006 (Div. 3)/B. Having Been a Treasurer in the Past, I Help Goblins Deceive.cpp create mode 100644 Codeforces Round 1006 (Div. 3)/C. Creating Keys for StORages Has Become My Main Skill.cpp create mode 100644 Codeforces Round 1006 (Div. 3)/D. For Wizards, the Exam Is Easy, but I Couldn't Handle It.cpp create mode 100644 Codeforces Round 1006 (Div. 3)/E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?.cpp create mode 100644 Codeforces Round 1006 (Div. 3)/F. Goodbye, Banker Life.cpp create mode 100644 Codeforces Round 1009 (Div. 3)/C. XOR and Triangle.cpp create mode 100644 Codeforces Round 1013 (Div. 3)/A. Olympiad Date.cpp create mode 100644 Codeforces Round 1013 (Div. 3)/B. Team Training.cpp create mode 100644 Codeforces Round 1013 (Div. 3)/C. Combination Lock.cpp create mode 100644 Codeforces Round 1013 (Div. 3)/D. Place of the Olympiad.cpp create mode 100644 Codeforces Round 1013 (Div. 3)/E. Interesting Ratio.cpp create mode 100644 Codeforces Round 1013 (Div. 3)/F. Igor and Mountain.cpp create mode 100644 Codeforces Round 1013 (Div. 3)/G. Gleb and Boating.cpp create mode 100644 Codeforces Round 1016 (Div. 3)/A. Ideal Generator.cpp create mode 100644 Codeforces Round 1016 (Div. 3)/B. Expensive Number.cpp create mode 100644 Codeforces Round 1016 (Div. 3)/C. Simple Repetition.cpp create mode 100644 Codeforces Round 1016 (Div. 3)/D. Skibidi Table.cpp create mode 100644 Codeforces Round 1016 (Div. 3)/E. Min Max MEX.cpp create mode 100644 Codeforces Round 1016 (Div. 3)/F. Hackers and Neural Networks.cpp create mode 100644 Codeforces Round 1016 (Div. 3)/G. Shorten the Array.cpp create mode 100644 Codeforces Round 1017 (Div. 4)/A. Trippi Troppi.cpp create mode 100644 Codeforces Round 1017 (Div. 4)/B. Bobritto Bandito.cpp create mode 100644 Codeforces Round 1017 (Div. 4)/C. Brr Brrr Patapim.cpp create mode 100644 Codeforces Round 1017 (Div. 4)/D. Tung Tung Sahur.cpp create mode 100644 Codeforces Round 1017 (Div. 4)/E. Boneca Ambalabu.cpp create mode 100644 Codeforces Round 1017 (Div. 4)/F. Trulimero Trulicina.cpp create mode 100644 Codeforces Round 1017 (Div. 4)/G. Chimpanzini Bananini.cpp create mode 100644 Codeforces Round 1017 (Div. 4)/H. La Vaca Saturno Saturnita.cpp create mode 100644 Codeforces Round 1020 (Div. 3)/A. Dr. TC.cpp create mode 100644 Codeforces Round 1020 (Div. 3)/B. St. Chroma.cpp create mode 100644 Codeforces Round 1020 (Div. 3)/C. Cherry Bomb.cpp create mode 100644 Codeforces Round 1020 (Div. 3)/D. Flower Boy.cpp create mode 100644 Codeforces Round 1020 (Div. 3)/E. Wolf.cpp create mode 100644 Codeforces Round 1020 (Div. 3)/F. Goblin.cpp create mode 100644 Codeforces Round 1020 (Div. 3)/G1. Baudelaire (easy version).cpp create mode 100644 Codeforces Round 1022 (Div. 2)/A. Permutation Warm-Up.cpp create mode 100644 Codeforces Round 1022 (Div. 2)/B. SUMdamental Decomposition.cpp create mode 100644 Codeforces Round 1022 (Div. 2)/C. Neo's Escape.cpp create mode 100644 Codeforces Round 1023 (Div. 2)/A. LRC and VIP.cpp create mode 100644 Codeforces Round 1023 (Div. 2)/B. Apples in Boxes.cpp create mode 100644 Codeforces Round 1023 (Div. 2)/C. Maximum Subarray Sum.cpp create mode 100644 Codeforces Round 1023 (Div. 2)/D. Apple Tree Traversing.cpp create mode 100644 Codeforces Round 1027 (Div. 3)/A. Square Year.cpp create mode 100644 Codeforces Round 1027 (Div. 3)/B. Not Quite a Palindromic String.cpp create mode 100644 Codeforces Round 1027 (Div. 3)/C. Need More Arrays.cpp create mode 100644 Codeforces Round 1027 (Div. 3)/F. Small Operations.cpp create mode 100644 Codeforces Round 1029 (Div. 3)/A. False Alarm.cpp create mode 100644 Codeforces Round 1029 (Div. 3)/B. Shrink.cpp create mode 100644 Codeforces Round 1029 (Div. 3)/C. Cool Partition.cpp create mode 100644 Codeforces Round 1029 (Div. 3)/D. Retaliation.cpp create mode 100644 Codeforces Round 1029 (Div. 3)/E. Lost Soul.cpp create mode 100644 Codeforces Round 1032 (Div. 3)/A. Letter Home.cpp create mode 100644 Codeforces Round 1032 (Div. 3)/B. Above the Clouds.cpp create mode 100644 Codeforces Round 1032 (Div. 3)/C. Those Who Are With Us.cpp create mode 100644 Codeforces Round 1032 (Div. 3)/D. 1709.cpp create mode 100644 Codeforces Round 1032 (Div. 3)/E. Sponsor of Your Problems.cpp create mode 100644 Codeforces Round 1032 (Div. 3)/F. Yamakasi.cpp create mode 100644 Codeforces Round 1034 (Div. 3)/A. Blackboard Game.cpp create mode 100644 Codeforces Round 1034 (Div. 3)/B. Tournament.cpp create mode 100644 Codeforces Round 1034 (Div. 3)/C. Prefix Min and Suffix Max.cpp create mode 100644 Codeforces Round 1034 (Div. 3)/D. Binary String Battle.cpp create mode 100644 Codeforces Round 1034 (Div. 3)/E. MEX Count.cpp create mode 100644 Codeforces Round 1034 (Div. 3)/F. Minimize Fixed Points.cpp create mode 100644 Codeforces Round 1037 (Div. 3)/A. Only One Digit.cpp create mode 100644 Codeforces Round 1037 (Div. 3)/B. No Casino in the Mountains.cpp create mode 100644 Codeforces Round 1037 (Div. 3)/C. I Will Definitely Make It.cpp create mode 100644 Codeforces Round 1037 (Div. 3)/D. This Is the Last Time.cpp create mode 100644 Codeforces Round 1037 (Div. 3)/E. G-C-D, Unlucky!.cpp create mode 100644 Codeforces Round 1037 (Div. 3)/F. 1-1-1, Free Tree!.cpp create mode 100644 Codeforces Round 1039 (Div. 2)/A. Recycling Center.cpp create mode 100644 Codeforces Round 1039 (Div. 2)/B. Deque Process.cpp create mode 100644 Codeforces Round 1039 (Div. 2)/C. Leftmost Below.cpp create mode 100644 Codeforces Round 1039 (Div. 2)/D. Sum of LDS.cpp create mode 100644 Codeforces Round 1040 (Div. 1)/B. Stay or Mirror.cpp create mode 100644 Codeforces Round 1040 (Div. 2)/A. Submission is All You Need.cpp create mode 100644 Codeforces Round 1042 (Div. 3)/A. Lever.cpp create mode 100644 Codeforces Round 1042 (Div. 3)/B. Alternating Series.cpp create mode 100644 Codeforces Round 1042 (Div. 3)/C. Make it Equal.cpp create mode 100644 Codeforces Round 1042 (Div. 3)/D. Arboris Contractio.cpp create mode 100644 Codeforces Round 1042 (Div. 3)/E. Adjacent XOR.cpp create mode 100644 Codeforces Round 1042 (Div. 3)/F. Unjust Binary Life.cpp create mode 100644 Codeforces Round 1042 (Div. 3)/G. Wafu!.cpp create mode 100644 Codeforces Round 1043 (Div. 3)/A. Homework.cpp create mode 100644 Codeforces Round 1043 (Div. 3)/B. The Secret Number.cpp create mode 100644 Codeforces Round 1043 (Div. 3)/C1. The Cunning Seller (easy version).cpp create mode 100644 Codeforces Round 1043 (Div. 3)/C2. The Cunning Seller (hard version).cpp create mode 100644 Codeforces Round 1043 (Div. 3)/D. From 1 to Infinity.cpp create mode 100644 Codeforces Round 1043 (Div. 3)/E. Arithmetics Competition.cpp create mode 100644 Codeforces Round 1043 (Div. 3)/F. Rada and the Chamomile Valley.cpp create mode 100644 Codeforces Round 1044 (Div. 2)/A. Redstone?.cpp create mode 100644 Codeforces Round 1044 (Div. 2)/B. Villagers.cpp create mode 100644 Codeforces Round 1044 (Div. 2)/C. The Nether.cpp create mode 100644 Codeforces Round 1044 (Div. 2)/D. Chicken Jockey.cpp create mode 100644 Codeforces Round 1044 (Div. 2)/E. I Yearned For The Mines.cpp create mode 100644 Codeforces Round 1045 (Div. 2)/A. Painting With Two Colors.cpp create mode 100644 Codeforces Round 1045 (Div. 2)/B. Add 0 or K.cpp create mode 100644 Codeforces Round 1045 (Div. 2)/C. Even Larger.cpp create mode 100644 Codeforces Round 1045 (Div. 2)/D. Sliding Tree.cpp create mode 100644 Codeforces Round 1046 (Div. 2)/A. In the Dream.cpp create mode 100644 Codeforces Round 1046 (Div. 2)/B. Like the Bitset.cpp create mode 100644 Codeforces Round 1046 (Div. 2)/C. Against the Difference.cpp create mode 100644 Codeforces Round 1046 (Div. 2)/D. For the Champion.cpp create mode 100644 Codeforces Round 1046 (Div. 2)/E. By the Assignment.cpp create mode 100644 Codeforces Round 1047 (Div. 3)/A. Collatz Conjecture.cpp create mode 100644 Codeforces Round 1047 (Div. 3)/B. Fun Permutation.cpp create mode 100644 Codeforces Round 1047 (Div. 3)/C. Maximum Even Sum.cpp create mode 100644 Codeforces Round 1047 (Div. 3)/D. Replace with Occurrences.cpp create mode 100644 Codeforces Round 1047 (Div. 3)/E. Mexification.cpp create mode 100644 Codeforces Round 1047 (Div. 3)/F. Prefix Maximum Invariance.cpp create mode 100644 Codeforces Round 1048 (Div. 2)/A. Maple and Multiplication.cpp create mode 100644 Codeforces Round 1049 (Div. 2)/A. Shift Sort.cpp create mode 100644 Codeforces Round 1049 (Div. 2)/B. Another Divisibility Problem.cpp create mode 100644 Codeforces Round 1049 (Div. 2)/C. Ultimate Value.cpp create mode 100644 Codeforces Round 147 (Div. 2)/E. Build String.cpp create mode 100644 Codeforces Round 199 (Div. 2)/E. Xenia and Tree.cpp create mode 100644 Codeforces Round 216 (Div. 2)/E. Valera and Queries.cpp create mode 100644 Codeforces Round 291 (Div. 2)/C. Watto and Mechanism.cpp create mode 100644 Codeforces Round 304 (Div. 2)/E. Soldier and Traveling.cpp create mode 100644 Codeforces Round 305 (Div. 1)/B. Mike and Feet.cpp create mode 100644 Codeforces Round 321 (Div. 2)/C. Kefa and Park.cpp create mode 100644 Codeforces Round 367 (Div. 2)/D. Vasiliy's Multiset.cpp create mode 100644 Codeforces Round 396 (Div. 2)/B. Mahmoud and a Triangle.cpp create mode 100644 Codeforces Round 419 (Div. 2)/B. Karen and Coffee.cpp create mode 100644 Codeforces Round 447 (Div. 2)/A. QAQ.cpp create mode 100644 Codeforces Round 460 (Div. 2)/D. Substring.cpp create mode 100644 Codeforces Round 479 (Div. 3)/E. Cyclic Components.cpp create mode 100644 Codeforces Round 498 (Div. 3)/F. Xor-Paths.cpp create mode 100644 Codeforces Round 560 (Div. 3)/D. Almost All Divisors.cpp create mode 100644 Codeforces Round 595 (Div. 3)/E. By Elevator or Stairs?.cpp create mode 100644 Codeforces Round 709 (Div. 1, based on Technocup 2021 Final Round)/C. Skyline Photo.cpp create mode 100644 Codeforces Round 784 (Div. 4)/H. Maximal AND.cpp create mode 100644 Codeforces Round 786 (Div. 3)/A. Number Transformation.cpp create mode 100644 Codeforces Round 786 (Div. 3)/B. Dictionary.cpp create mode 100644 Codeforces Round 786 (Div. 3)/C. Infinite Replacement.cpp create mode 100644 Codeforces Round 786 (Div. 3)/D. A-B-C Sort.cpp create mode 100644 Codeforces Round 786 (Div. 3)/E. Breaking the Wall.cpp create mode 100644 Codeforces Round 786 (Div. 3)/F. Desktop Rearrangement.cpp create mode 100644 Codeforces Round 791 (Div. 2)/B. Stone Age Problem.cpp create mode 100644 Codeforces Round 861 (Div. 2)/B. Playing in a Casino.cpp create mode 100644 Codeforces Round 870 (Div. 2)/D. Running Miles.cpp create mode 100644 Codeforces Round 871 (Div. 4)/A. Love Story.cpp create mode 100644 Codeforces Round 871 (Div. 4)/B. Blank Space.cpp create mode 100644 Codeforces Round 871 (Div. 4)/C. Mr. Perfectly Fine.cpp create mode 100644 Codeforces Round 871 (Div. 4)/D. Gold Rush.cpp create mode 100644 Codeforces Round 871 (Div. 4)/E. The Lakes.cpp create mode 100644 Codeforces Round 871 (Div. 4)/F. Forever Winter.cpp create mode 100644 Codeforces Round 871 (Div. 4)/G. Hits Different.cpp create mode 100644 Codeforces Round 871 (Div. 4)/H. Don't Blame Me.cpp create mode 100644 Codeforces Round 900 (Div. 3)/A. How Much Does Daytona Cost?.cpp create mode 100644 Codeforces Round 900 (Div. 3)/B. Aleksa and Stack.cpp create mode 100644 Codeforces Round 900 (Div. 3)/C. Vasilije in Cacak.cpp create mode 100644 Codeforces Round 900 (Div. 3)/E. Iva & Pav.cpp create mode 100644 Codeforces Round 905 (Div. 3)/A. Morning.cpp create mode 100644 Codeforces Round 905 (Div. 3)/B. Chemistry.cpp create mode 100644 Codeforces Round 905 (Div. 3)/C. Raspberries.cpp create mode 100644 Codeforces Round 905 (Div. 3)/D. In Love.cpp create mode 100644 Codeforces Round 905 (Div. 3)/E. Look Back.cpp create mode 100644 Codeforces Round 905 (Div. 3)/G1. Dances (Easy version).cpp create mode 100644 Codeforces Round 905 (Div. 3)/G2. Dances (Hard Version).cpp create mode 100644 Codeforces Round 918 (Div. 4)/F. Greetings.cpp create mode 100644 Codeforces Round 918 (Div. 4)/G. Bicycles.cpp create mode 100644 Codeforces Round 925 (Div. 3)/D. Divisible Pairs.cpp create mode 100644 Codeforces Round 944 (Div. 4)/D. Binary Cut.cpp create mode 100644 Codeforces Round 944 (Div. 4)/G. XOUR.cpp create mode 100644 Codeforces Round 975 (Div. 1)/C. Tree Pruning.cpp create mode 100644 Codeforces Round 991 (Div. 3)/A. Line Breaks.cpp create mode 100644 Codeforces Round 991 (Div. 3)/B. Transfusion.cpp create mode 100644 Codeforces Round 991 (Div. 3)/C. Uninteresting Number.cpp create mode 100644 Codeforces Round 991 (Div. 3)/D. Digital string maximization.cpp create mode 100644 Codeforces Round 991 (Div. 3)/E. Three Strings.cpp create mode 100644 Codeforces Round 991 (Div. 3)/F. Maximum modulo equality.cpp create mode 100644 Codeforces Round 995 (Div. 3)/A. Preparing for the Olympiad.cpp create mode 100644 Codeforces Round 995 (Div. 3)/B. Journey.cpp create mode 100644 Codeforces Round 995 (Div. 3)/C. Preparing for the Exam.cpp create mode 100644 Codeforces Round 995 (Div. 3)/D. Counting Pairs.cpp create mode 100644 Codeforces Round 995 (Div. 3)/E. Best Price.cpp create mode 100644 Codeforces Round 998 (Div. 3)/A. Fibonacciness.cpp create mode 100644 Codeforces Round 998 (Div. 3)/B. Farmer John's Card Game.cpp create mode 100644 Codeforces Round 998 (Div. 3)/C. Game of Mathletes.cpp create mode 100644 Codeforces Round 998 (Div. 3)/D. Subtract Min Sort.cpp create mode 100644 Codeforces Round 998 (Div. 3)/E. Graph Composition.cpp create mode 100644 Educational Codeforces Round 12/F. Four Divisors.cpp create mode 100644 Educational Codeforces Round 160 (Rated for Div. 2)/D. Array Collapse.cpp create mode 100644 Educational Codeforces Round 168 (Rated for Div. 2)/E. Level Up.cpp create mode 100644 Educational Codeforces Round 175 (Rated for Div. 2)/A. FizzBuzz Remixed.cpp create mode 100644 Educational Codeforces Round 175 (Rated for Div. 2)/B. Robot Program.cpp create mode 100644 Educational Codeforces Round 175 (Rated for Div. 2)/C. Limited Repainting.cpp create mode 100644 Educational Codeforces Round 179 (Rated for Div. 2)/E. Changing the String.cpp create mode 100644 Educational Codeforces Round 181 (Rated for Div. 2)/D. Segments Covering.cpp create mode 100644 Educational Codeforces Round 32/E. Maximum Subsequence.cpp create mode 100644 Educational Codeforces Round 33 (Rated for Div. 2)/A. Chess For Three.cpp create mode 100644 Educational Codeforces Round 33 (Rated for Div. 2)/B. Beautiful Divisors.cpp create mode 100644 Educational Codeforces Round 33 (Rated for Div. 2)/C. Rumor.cpp create mode 100644 Educational Codeforces Round 33 (Rated for Div. 2)/D. Credit Card.cpp create mode 100644 Educational Codeforces Round 33 (Rated for Div. 2)/E. Counting Arrays.cpp create mode 100644 Educational Codeforces Round 34 (Rated for Div. 2)/A. Hungry Student Problem.cpp create mode 100644 Educational Codeforces Round 34 (Rated for Div. 2)/B. The Modcrab.cpp create mode 100644 Educational Codeforces Round 34 (Rated for Div. 2)/C. Boxes Packing.cpp create mode 100644 Educational Codeforces Round 34 (Rated for Div. 2)/D. Almost Difference.cpp create mode 100644 Educational Codeforces Round 34 (Rated for Div. 2)/E. Swapping Characters.cpp create mode 100644 Educational Codeforces Round 35 (Rated for Div. 2)/A. Nearest Minimums.cpp create mode 100644 Educational Codeforces Round 35 (Rated for Div. 2)/B. Two Cakes.cpp create mode 100644 Educational Codeforces Round 35 (Rated for Div. 2)/C. Three Garlands.cpp create mode 100644 Educational Codeforces Round 35 (Rated for Div. 2)/D. Inversion Counting.cpp create mode 100644 Educational Codeforces Round 35 (Rated for Div. 2)/E. Stack Sorting.cpp create mode 100644 Educational Codeforces Round 36 (Rated for Div. 2)/A. Garden.cpp create mode 100644 Educational Codeforces Round 36 (Rated for Div. 2)/B. Browser.cpp create mode 100644 Educational Codeforces Round 36 (Rated for Div. 2)/C. Permute Digits.cpp create mode 100644 Educational Codeforces Round 36 (Rated for Div. 2)/D. Almost Acyclic Graph.cpp create mode 100644 Educational Codeforces Round 36 (Rated for Div. 2)/E. Physical Education Lessons.cpp create mode 100644 Educational Codeforces Round 37 (Rated for Div. 2)/A. Water The Garden.cpp create mode 100644 Educational Codeforces Round 37 (Rated for Div. 2)/B. Tea Queue.cpp create mode 100644 Educational Codeforces Round 37 (Rated for Div. 2)/C. Swap Adjacent Elements.cpp create mode 100644 Educational Codeforces Round 37 (Rated for Div. 2)/D. Tanks.cpp create mode 100644 Educational Codeforces Round 37 (Rated for Div. 2)/E. Connected Components?.cpp create mode 100644 Educational Codeforces Round 37 (Rated for Div. 2)/F. SUM and REPLACE.cpp create mode 100644 Educational Codeforces Round 37 (Rated for Div. 2)/G. List Of Integers.cpp create mode 100644 Educational Codeforces Round 38 (Rated for Div. 2)/A. Word Correction.cpp create mode 100644 Educational Codeforces Round 38 (Rated for Div. 2)/B. Run For Your Prize.cpp create mode 100644 Educational Codeforces Round 38 (Rated for Div. 2)/C. Constructing Tests.cpp create mode 100644 Educational Codeforces Round 38 (Rated for Div. 2)/D. Buy a Ticket.cpp create mode 100644 Educational Codeforces Round 38 (Rated for Div. 2)/E. Max History.cpp create mode 100644 Educational Codeforces Round 39 (Rated for Div. 2)/A. Partition.cpp create mode 100644 Educational Codeforces Round 39 (Rated for Div. 2)/B. Weird Subtraction Process.cpp create mode 100644 Educational Codeforces Round 39 (Rated for Div. 2)/C. String Transformation.cpp create mode 100644 Educational Codeforces Round 39 (Rated for Div. 2)/D. Timetable.cpp create mode 100644 Educational Codeforces Round 39 (Rated for Div. 2)/E. Largest Beautiful Number.cpp create mode 100644 Educational Codeforces Round 39 (Rated for Div. 2)/F. Fibonacci String Subsequences.cpp create mode 100644 I Maratona do Cerrado (mirror)/F. Fatorial.cpp create mode 100644 I Maratona do Cerrado (mirror)/M. Mangabas.cpp create mode 100644 I Maratona do Cerrado (mirror)/N. Não é permitido erros!.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 1/A. Binary Search.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 1/B. Closest to the Left.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 1/C. Closest to the Right.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 1/D. Fast search.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 2/A. Packing Rectangles.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 2/B. Ropes.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 2/E. Equation.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 2/G. Student Councils.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 2/H. Hamburgers.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 3/A. Get together.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 3/B. Splitting an Array.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 3/C. Cows in Stalls.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 3/D. Minimum maximum on the Path.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 4/A. Maximum Average Segment.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 4/B. Minimum Average Path.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 4/C. Pair Selection.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 5/A. K-th Number in the Union of Segments.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 5/B. Multiplication Table.cpp create mode 100644 ITMO Academy: pilot course - Binary Search - Step 5/C. K-th Sum.cpp create mode 100644 ITMO Academy: pilot course - Disjoint Sets Union - Step 1/C. Experience.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/A. Segment Tree for the Sum.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/B. Segment Tree for the Minimum.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/C. Number of Minimums on a Segment.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/B. K-th one.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/C. First element at least X.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/D. First element at least X - 2.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/A. Inversions.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/B. Inversions 2.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/C. Nested Segments.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/D. Intersecting Segments.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/E. Addition to Segment.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/A. Sign alternation.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/B. Cryptography.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/C. Number of Inversions on Segment.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/D. Number of Different on Segment.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/E. Earthquakes.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/A. Addition to Segment.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/B. Applying MAX to Segment.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/C. Assignment to Segment.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/A. Addition and Minimum.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/B. Multiplication and Sum.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/C. Bitwise OR and AND.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/D. Addition and Sum.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/E. Assignment and Minimum.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/F. Assignment and Sum.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/A. Assignment and Maximal Segment.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/B. Inverse and K-th one.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/C. Addition and First element at least X.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/A. Assignment, Addition, and Sum.cpp create mode 100644 ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/B. Add Arithmetic Progression On Segment.cpp create mode 100644 J. Journey To Stringland/J. Journey To Stringland.cpp create mode 100644 Manthan, Codefest 16/C. Spy Syndrome 2.cpp create mode 100644 OBI IV - 2024/J. A Viagem de Wevton.cpp create mode 100644 OBI V - 2024/F. Sanduíche.cpp create mode 100644 OBI VI - 2024/H. Retângulo.cpp create mode 100644 Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)/D. Frequency of String.cpp create mode 100644 Trie - Hash/C - Watto and Mechanism.cpp create mode 100644 VI UnBalloon Contest Mirror/B. Boundless Deck.cpp create mode 100644 VI UnBalloon Contest Mirror/D. Delirium at Unballoon.cpp create mode 100644 VI UnBalloon Contest Mirror/E. Elementary Magical School of Words.cpp create mode 100644 VI UnBalloon Contest Mirror/F. Fibonacci.cpp create mode 100644 VI UnBalloon Contest Mirror/G. Graph of Love.cpp create mode 100644 VIII Maratona Mineira de Programação/J. Wasserman, Rei das Águas.cpp create mode 100644 XII Maratona Mineira de Programação/B. Bares.cpp create mode 100644 XII Maratona Mineira de Programação/F. Fome de Queijo.cpp create mode 100644 XII Maratona Mineira de Programação/I. Interativo.cpp diff --git a/2015-2016 Petrozavodsk Winter Training Camp, Saratov SU Contest/G. Maximum Product.cpp b/2015-2016 Petrozavodsk Winter Training Camp, Saratov SU Contest/G. Maximum Product.cpp new file mode 100644 index 0000000..431a225 --- /dev/null +++ b/2015-2016 Petrozavodsk Winter Training Camp, Saratov SU Contest/G. Maximum Product.cpp @@ -0,0 +1,144 @@ +/* Problem URL: https://codeforces.com/gym/100886/problem/G */ + +#include + +using namespace std; +#define V vector +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string tmp, b; + cin >> tmp >> b; + + size_t diff = b.size() - tmp.size(); + + string a; + rep(i, diff) { + a.push_back('0'); + } + + a += tmp; + + rep(i, a.size()) { + if (a[i] != b[i]) { + break; + } + + if (a[i] == '0') { + cout << b << '\n'; + return 0; + } + } + + vvvvl memo(a.size(), vvvl(2, vvl(2, vl(2, -1)))); + + function dp = [&](int i, int over, int under, int started) -> ll { + if (i >= a.size()) { + return 1; + } + + ll &ans = memo[i][over][under][started]; + if (ans != -1) { + return ans; + } + + ans = 0; + + char limit = over ? '0' : a[i]; + for (int j = under ? '9' : b[i]; j >= limit; j--) { + rmax(ans, dp(i + 1, over || j > a[i], under || j < b[i], started || j != '0') * (!started && j == '0' ? 1 : j - '0')); + } + + return ans; + }; + + ll res = dp(0, 0, 0, 0); + + string ans; + function recover = [&](int i, int over, int under, int started, ll prod) { + if (i >= a.size() - 1) { + ans.push_back(prod + '0'); + return; + } + + char limit = over ? '0' : a[i]; + for (int j = under ? '9' : b[i]; j >= limit; j--) { + ll div = !started && j == '0' ? 1 : j - '0'; + + if (prod % div == 0 && memo[i + 1][over || j > a[i]][under || j < b[i]][started || j != '0'] == prod / div) { + ans.push_back(j); + recover(i + 1, over || j > a[i], under || j < b[i], started || j != '0', prod / div); + return; + } + } + }; + + recover(0, 0, 0, 0, res); + + cout << stoll(ans) << '\n'; +} diff --git a/2018 German Collegiate Programming Contest (GCPC 18)/A. Attack on Alpha-Zet.cpp b/2018 German Collegiate Programming Contest (GCPC 18)/A. Attack on Alpha-Zet.cpp new file mode 100644 index 0000000..8878348 --- /dev/null +++ b/2018 German Collegiate Programming Contest (GCPC 18)/A. Attack on Alpha-Zet.cpp @@ -0,0 +1,78 @@ +/* Problem URL: https://codeforces.com/gym/102021/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + +} diff --git a/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/B. Marbles.cpp b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/B. Marbles.cpp new file mode 100644 index 0000000..8c2e489 --- /dev/null +++ b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/B. Marbles.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/gym/101908/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int mr; + cin >> mr; + + const int n = 101; + const int inf = INT32_MAX >> 1; + + vvi grundy(n, vi(n)); + + rep(i, n) { + grundy[i][i] = 1000; + grundy[0][i] = 1000; + grundy[i][0] = 1000; + } + + auto search = [&](int i, int j) { + V exists(1001, false); + nrep(k, 1, n) { + if (i - k >= 0) { + exists[grundy[i - k][j]] = true; + } + + if (j - k >= 0) { + exists[grundy[i][j - k]] = true; + } + + if (i - k >= 0 && j - k >= 0) { + exists[grundy[i - k][j - k]] = true; + } + } + + int mex = 0; + while (exists[mex]) { + mex++; + } + + return mex; + }; + + nrep(i, 1, n) { + nrep(j, 1, n) { + if (i == j) { + continue; + } + grundy[i][j] = search(i, j); + } + } + + int state = 0; + + while (mr--) { + int i, j; + cin >> i >> j; + + state ^= grundy[i][j]; + } + + cout << (state == 0 ? "N\n" : "Y\n"); +} diff --git a/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/C. Pizza Cutter.cpp b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/C. Pizza Cutter.cpp new file mode 100644 index 0000000..01f0e86 --- /dev/null +++ b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/C. Pizza Cutter.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/gym/101908/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll x, y; + cin >> x >> y; + + ll h, v; + cin >> h >> v; + + ll ans = (h + 1) * (v + 1); + + V> ys(h); + repv(i, ys) { + cin >> i.first >> i.second; + } + + V> xs(v); + + repv(i, xs) { + cin >> i.first >> i.second; + } + + sortv(ys); + sortv(xs); + + function>&)> count = [&](V> &vec) -> ll { + if (vec.size() <= 1) { + return 0; + } + + int mid = vec.size() / 2; + + V> first(mid); + rep(i, mid) { + first[i] = vec[i]; + } + + V> second(vec.size() - mid); + rep(i, vec.size() - mid) { + second[i] = vec[vec.size() - i - 1]; + } + reverse(all(second)); + + ll ans = count(first); + ans += count(second); + + int now = 0; + int l = 0; + int r = 0; + while (l < first.size() && r < second.size()) { + if (first[l].second <= second[r].second) { + vec[now] = first[l]; + now++; + l++; + continue; + } + + ans += first.size() - l; + vec[now] = second[r]; + now++; + r++; + } + + while (l < first.size()) { + vec[now] = first[l]; + l++; + now++; + } + + while (r < second.size()) { + vec[now] = second[r]; + r++; + now++; + } + + return ans; + }; + + ans += count(xs); + ans += count(ys); + + cout << ans << '\n'; +} diff --git a/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/D. Unraveling Monty Hall.cpp b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/D. Unraveling Monty Hall.cpp new file mode 100644 index 0000000..999820a --- /dev/null +++ b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/D. Unraveling Monty Hall.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://codeforces.com/gym/101908/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + int count = 0; + while (t--) { + int n; + cin >> n; + + if (n == 2 || n == 3) { + count++; + } + } + cout << count << '\n'; +} diff --git a/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/E. Enigma.cpp b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/E. Enigma.cpp new file mode 100644 index 0000000..c9298f9 --- /dev/null +++ b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/E. Enigma.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/gym/101908/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a, b; + cin >> a >> b; + + int count = 0; + rep(i, a.size() - b.size() + 1) { + bool pos = true; + + rep(j, b.size()) { + if (a[i + j] == b[j]) { + pos = false; + break; + } + } + + if (pos) { + count++; + } + } + + cout << count << '\n'; +} diff --git a/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/F. Music Festival.cpp b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/F. Music Festival.cpp new file mode 100644 index 0000000..7c6b78a --- /dev/null +++ b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/F. Music Festival.cpp @@ -0,0 +1,81 @@ +/* Problem URL: https://codeforces.com/gym/101908/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> stuff; +} diff --git a/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/I. Switches.cpp b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/I. Switches.cpp new file mode 100644 index 0000000..b2192eb --- /dev/null +++ b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/I. Switches.cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/gym/101908/problem/I */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + int l; + cin >> l; + + int on = l; + + vi status(m); + while (l--) { + int a; + cin >> a; + status[a - 1] = 1; + } + + vvi graph(n); + rep(i, n) { + int k; + cin >> k; + + while (k--) { + int b; + cin >> b; + graph[i].push_back(b - 1); + } + } + + int i = 0; + while (i < 2 * n && on != 0) { + repv(j, graph[i % n]) { + on += 1 - 2 * status[j]; + status[j] ^= 1; + } + + i++; + } + + if (on != 0) { + cout << "-1\n"; + return 0; + } + + cout << i << '\n'; +} diff --git a/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/L. Subway Lines.cpp b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/L. Subway Lines.cpp new file mode 100644 index 0000000..2a65e55 --- /dev/null +++ b/2018-2019 ACM-ICPC Brazil Subregional Programming Contest/L. Subway Lines.cpp @@ -0,0 +1,159 @@ +/* Problem URL: https://codeforces.com/gym/101908/problem/L */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vvi graph(n); + + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vvi parent(n, vi(20, 0)); + vi depth(n, 0); + + function dfs = [&](int i, int p) { + depth[i] = depth[p] + 1; + parent[i][0] = p; + + nrep(j, 1, 20) { + parent[i][j] = parent[parent[i][j - 1]][j - 1]; + } + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i); + } + }; + + dfs(0, 0); + + auto lca = [&](int a, int b) { + if (depth[a] > depth[b]) { + swap(a, b); + } + + int diff = depth[b] - depth[a]; + + rep(i, 20) { + if ((diff >> i) & 1) { + b = parent[b][i]; + } + } + + if (a == b) { + return a; + } + + for (int i = 19; i >= 0; i--) { + if (parent[a][i] != parent[b][i]) { + a = parent[a][i]; + b = parent[b][i]; + } + } + + return parent[a][0]; + }; + + auto dis = [&](int a, int b) { + return depth[a] + depth[b] - 2 * depth[lca(a, b)]; + }; + + while (q--) { + int a, b, c, d; + cin >> a >> b >> c >> d; + a--, b--, c--, d--; + + int x = dis(a, b) + dis(c, d); + int y = min(dis(a, c) + dis(b, d), dis(a, d) + dis(b, c)); + + if (y > x) { + cout << "0\n"; + continue; + } + + cout << (x - y) / 2 + 1 << '\n'; + } +} diff --git a/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/B. Buffoon.cpp b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/B. Buffoon.cpp new file mode 100644 index 0000000..a231cf9 --- /dev/null +++ b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/B. Buffoon.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://codeforces.com/gym/102346/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + n--; + int carlos; + cin >> carlos; + + while (n--) { + int nm; + cin >> nm; + + if (nm > carlos) { + cout << "N\n"; + return 0; + } + } + + cout << "S\n"; +} diff --git a/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/D. Denouncing Mafia.cpp b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/D. Denouncing Mafia.cpp new file mode 100644 index 0000000..d520ee6 --- /dev/null +++ b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/D. Denouncing Mafia.cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/gym/102346/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vvi graph(n); + + nrep(i, 1, n) { + int p; + cin >> p; + + graph[p - 1].push_back(i); + } + + V>> sets(n); + + function dfs = [&](int i) { + for (auto j : graph[i]) { + dfs(j); + + if (sets[j].size() > sets[i].size()) { + swap(sets[j], sets[i]); + } + + for (auto k : sets[j]) { + sets[i].insert(k); + } + } + + if (sets[i].empty()) { + sets[i].insert(1); + return; + } + + auto itr = sets[i].begin(); + int v = *itr + 1; + sets[i].erase(itr); + sets[i].insert(v); + }; + + dfs(0); + + int ans = 0; + auto itr = sets[0].begin(); + rep(i, k) { + if (itr == sets[0].end()) { + break; + } + + ans += *itr; + itr++; + } + + cout << ans << '\n'; +} diff --git a/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/H. Hour for a Run.cpp b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/H. Hour for a Run.cpp new file mode 100644 index 0000000..23e2947 --- /dev/null +++ b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/H. Hour for a Run.cpp @@ -0,0 +1,91 @@ +/* Problem URL: https://codeforces.com/gym/102346/problem/H */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, v; + cin >> n >> v; + + v *= n; + + cout << (ll)ceil(v * 0.1) << ' '; + cout << (ll)ceil(v * 0.2) << ' '; + cout << (ll)ceil(v * 0.3) << ' '; + cout << (ll)ceil(v * 0.4) << ' '; + cout << (ll)ceil(v * 0.5) << ' '; + cout << (ll)ceil(v * 0.6) << ' '; + cout << (ll)ceil(v * 0.7) << ' '; + cout << (ll)ceil(v * 0.8) << ' '; + cout << (ll)ceil(v * 0.9) << '\n'; +} diff --git a/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/J. Jar of Water Game.cpp b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/J. Jar of Water Game.cpp new file mode 100644 index 0000000..5c89c06 --- /dev/null +++ b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/J. Jar of Water Game.cpp @@ -0,0 +1,204 @@ +/* Problem URL: https://codeforces.com/gym/102346/problem/J */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vvi cards(n, vi(14, 0)); + + map act; + + act['A'] = 1; + act['2'] = 2; + act['3'] = 3; + act['4'] = 4; + act['5'] = 5; + act['6'] = 6; + act['7'] = 7; + act['8'] = 8; + act['9'] = 9; + act['D'] = 10; + act['Q'] = 11; + act['J'] = 12; + act['K'] = 13; + + rep(i, n) { + string a; + cin >> a; + + repv(j, a) { + cards[i][act[j]]++; + } + } + + k--; + + rep(i, n) { + if (i == k) { + continue; + } + + nrep(j, 1, 14) { + if (cards[i][j] == 4) { + cout << i + 1 << '\n'; + return 0; + } + } + } + + rep(i, n) { + int minimal = 2; + int choice = 13; + + int now = (i + k) % n; + + for (int j = 13; j > 0; j--) { + if (cards[now][j] <= minimal && cards[now][j] > 0) { + minimal = cards[now][j]; + choice = j; + } + } + + cards[now][choice]--; + cards[(now + 1) % n][choice]++; + + if (now != k) { + nrep(j, 1, 14) { + if (cards[now][j] == 4) { + rep(l, n) { + nrep(c, 1, 14) { + if (cards[l][c] == 4 && l != k) { + cout << l + 1 << '\n'; + return 0; + } + } + } + } + } + } + } + + nrep(i, 1, 14) { + if (cards[k][i] == 4) { + cout << k + 1 << '\n'; + return 0; + } + } + + k = (k + 1) % n; + + while (true) { + rep(i, n - 1) { + int minimal = 2; + int choice = 13; + + int now = (k + i) % n; + + for (int j = 13; j > 0; j--) { + if (cards[now][j] <= minimal && cards[now][j] > 0) { + minimal = cards[now][j]; + choice = j; + } + } + + cards[now][choice]--; + cards[(now + 1) % n][choice]++; + if (now != ((k - 1) % n + n) % n) { + nrep(j, 1, 14) { + if (cards[now][j] == 4) { + rep(l, n) { + nrep(c, 1, 14) { + if (cards[l][c] == 4 && l != ((k - 1) % n + n) % n) { + cout << l + 1 << '\n'; + return 0; + } + } + } + } + } + } + } + + nrep(i, 1, 14) { + if (cards[k][i] == 4) { + cout << k + 1 << '\n'; + return 0; + } + } + + k = (k + 1) % n; + } +} diff --git a/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/L. Less Coin Tosses.cpp b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/L. Less Coin Tosses.cpp new file mode 100644 index 0000000..86fe3ae --- /dev/null +++ b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/L. Less Coin Tosses.cpp @@ -0,0 +1,80 @@ +/* Problem URL: https://codeforces.com/gym/102346/problem/L */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n; + cin >> n; + cout << (1LL << (__builtin_popcountll(n))) << '\n'; +} diff --git a/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/M. Maratona Brasileira de Popcorn.cpp b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/M. Maratona Brasileira de Popcorn.cpp new file mode 100644 index 0000000..2eaa21a --- /dev/null +++ b/2019-2020 ACM-ICPC Brazil Subregional Programming Contest/M. Maratona Brasileira de Popcorn.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://codeforces.com/gym/102346/problem/M */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, c, t; + cin >> n >> c >> t; + + vi pop(n); + + repv(i, pop) { + cin >> i; + // i = (i + (t - 1)) / t; + } + + ll ans = 1; + ll low = 1; + ll high = 1e9; + + while (low <= high) { + ll mid = (low + high) / 2; + + int count = 1; + ll now = 0; + + rep(i, n) { + if ((pop[i] + (t - 1)) / t > mid) { + now = 1e9 + 1; + break; + } + + now += pop[i]; + + if ((now + (t - 1)) / t > mid) { + if (count == c) { + break; + } + + count++; + now = pop[i]; + } + } + + if ((now + (t - 1)) / t <= mid) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} diff --git a/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/B. Battleship.cpp b/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/B. Battleship.cpp new file mode 100644 index 0000000..4a6b2ab --- /dev/null +++ b/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/B. Battleship.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/gym/102861/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> pos(10, V(10)); + + while (n--) { + int d, l, r, c; + cin >> d >> l >> r >> c; + r--, c--; + + if (d == 0) { + if (c + l - 1 > 9) { + cout << "N\n"; + return 0; + } + nrep(i, c, c + l) { + if (pos[r][i]) { + cout << "N\n"; + return 0; + } + + pos[r][i] = true; + } + } else { + if (r + l - 1 > 9) { + cout << "N\n"; + return 0; + } + nrep(i, r, r + l) { + if (pos[i][c]) { + cout << "N\n"; + return 0; + } + + pos[i][c] = true; + } + } + } + + cout << "Y\n"; +} diff --git a/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/E. Party Company.cpp b/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/E. Party Company.cpp new file mode 100644 index 0000000..0e63fbe --- /dev/null +++ b/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/E. Party Company.cpp @@ -0,0 +1,189 @@ +/* Problem URL: https://codeforces.com/gym/102861/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + vi age(n); + + rep(i, n) { + cin >> age[i]; + int j; + cin >> j; + + if (i == 0) { + continue; + } + + graph[j - 1].push_back(i); + } + + const int log = 17; + + vvi parent(n, vi(log)); + vvi maximal(n, vi(log)); + + function dfs = [&](int i, int p) { + parent[i][0] = p; + maximal[i][0] = age[p]; + + nrep(j, 1, log) { + parent[i][j] = parent[parent[i][j - 1]][j - 1]; + maximal[i][j] = max(age[parent[i][j]], maximal[i][j - 1]); + } + + for (auto j : graph[i]) { + dfs(j, i); + } + }; + + dfs(0, 0); + + vvi count(n); + + while (m--) { + int i, l, r; + cin >> i >> l >> r; + i--; + + auto add = [&]() { + for (int j = log - 1; j >= 0; j--) { + if (maximal[i][j] <= r) { + i = parent[i][j]; + } + } + + count[i].push_back(l); + }; + + add(); + } + + int size = 1 << (32 - __builtin_clz(1e5)); + vi seg(size * 2); + + auto update = [&](int i, int v) { + i += size; + seg[i] += v; + + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + vi ans(n); + + function getans = [&](int i) { + repv(j, count[i]) { + update(j, 1); + } + + ans[i] = query(1, 0, size - 1, 0, age[i]); + + for (auto j : graph[i]) { + getans(j); + } + + repv(j, count[i]) { + update(j, -1); + } + }; + + getans(0); + + cout << ans << '\n'; +} diff --git a/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/F. Fastminton.cpp b/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/F. Fastminton.cpp new file mode 100644 index 0000000..c764fad --- /dev/null +++ b/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/F. Fastminton.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/gym/102861/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a; + cin >> a; + + vi points(2); + vi games(2); + + int now = 0; + + const char *msg[] = { + "", + "", + "(winner)" + }; + + const char *left[] = { + "*", + "" + }; + + const char *right[] = { + "", + "*" + }; + + repv(i, a) { + if (i == 'S') { + points[now]++; + } else if (i == 'R') { + now ^= 1; + points[now]++; + } else if (i == 'Q') { + if (games[0] == 2 || games[1] == 2) { + printf("%d %s - %d %s\n", games[0], msg[games[0]], games[1], msg[games[1]]); + continue; + } + + printf("%d (%d%s) - %d (%d%s)\n", games[0], points[0], left[now], games[1], points[1], right[now]); + } + + if (points[now] == 10 || (points[now] >= 5 && points[now] - points[now^1] >= 2)) { + games[now]++; + points[0] = 0; + points[1] = 0; + } + } +} diff --git a/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/G. Game Show!.cpp b/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/G. Game Show!.cpp new file mode 100644 index 0000000..4b889cf --- /dev/null +++ b/2020-2021 ACM-ICPC Brazil Subregional Programming Contest/G. Game Show!.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://codeforces.com/gym/102861/problem/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + vi pref(n + 1); + pref[0] = 100; + + rep(i, n) { + pref[i + 1] = pref[i] + fds[i]; + } + + int maximal = 0; + repv(i, pref) { + rmax(maximal, i); + } + + cout << maximal << '\n'; +} diff --git a/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/C. Creating Multiples.cpp b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/C. Creating Multiples.cpp new file mode 100644 index 0000000..f81b13c --- /dev/null +++ b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/C. Creating Multiples.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://codeforces.com/gym/103388/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll b; + int l; + cin >> b >> l; + + ll mod = b + 1; + + auto fastpow = [&](ll a, ll p) { + ll ans = 1; + ll now = a; + + rep(i, 20) { + if ((p >> i) & 1) { + ans *= now; + ans %= mod; + } + + now *= now; + now %= mod; + } + + return ans; + }; + + vl fds(l); + cin >> fds; + + ll now = 0; + rep(i, l) { + now += fds[i] * fastpow(b, l - i - 1); + now %= mod; + } + + if (now == 0) { + cout << "0 0\n"; + return 0; + } + + rep(i, l) { + ll p = fastpow(b, l - i - 1); + ll tmp = (p * now) % mod; + + if (fds[i] >= tmp) { + cout << i + 1 << ' ' << fds[i] - tmp << '\n'; + return 0; + } + } + + cout << "-1 -1\n"; +} diff --git a/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/E. Escalator.cpp b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/E. Escalator.cpp new file mode 100644 index 0000000..6e4e1b9 --- /dev/null +++ b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/E. Escalator.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/gym/103388/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + int now = 0; + int dir = 0; + + now = 0; + dir = 0; + + V> fds(2); + + while (n--) { + int t, d; + cin >> t >> d; + + fds[d].push(t); + } + + while (!fds[0].empty() || !fds[1].empty()) { + if (fds[0].empty()) { + dir = 1; + } else if (fds[1].empty()) { + dir = 0; + } else { + dir = fds[0].front() > fds[1].front(); + } + + if (fds[dir].front() <= now) { + while (!fds[dir].empty() && fds[dir].front() <= now) { + fds[dir].pop(); + } + now += 10; + + while (!fds[dir].empty() && fds[dir].front() <= now) { + now = fds[dir].front() + 10; + fds[dir].pop(); + } + + continue; + } + + now = fds[dir].front() + 10; + fds[dir].pop(); + + while (!fds[dir].empty() && fds[dir].front() <= now) { + now = fds[dir].front() + 10; + fds[dir].pop(); + } + } + + cout << now << '\n'; +} diff --git a/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/G. Getting in Shape.cpp b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/G. Getting in Shape.cpp new file mode 100644 index 0000000..fc3eff9 --- /dev/null +++ b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/G. Getting in Shape.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/gym/103388/problem/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n; + cin >> n; + + vl dp(77); + dp[0] = 1; + dp[1] = 1; + + rep(i, 75) { + dp[i + 1] += dp[i]; + dp[i + 2] += dp[i]; + } + + unordered_map vis; + + vi ans; + + function dfs = [&](ll now) { + if (now == 1) { + return true; + } + + if (vis[now]) { + return false; + } + + vis[now] = true; + + for (int i = 74; i > 0; i--) { + if (now % dp[i] == 0) { + bool res = dfs(now / dp[i]); + + if (res) { + ans.push_back(i); + return true; + } + } + } + + return false; + }; + + if (!dfs(n)) { + cout << "IMPOSSIBLE\n"; + return 0; + } + + reverse(all(ans)); + + string tmp; + + repv(i, ans) { + tmp += string(i, 'A'); + tmp += 'B'; + } + + cout << tmp << '\n'; +} diff --git a/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/H. Handling the Blocks.cpp b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/H. Handling the Blocks.cpp new file mode 100644 index 0000000..40aeda9 --- /dev/null +++ b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/H. Handling the Blocks.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/gym/103388/problem/H */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vvi blocks(k); + + vi act; + + while (n--) { + int num, c; + cin >> num >> c; + + blocks[c - 1].push_back(num); + act.push_back(c - 1); + } + + rep(i, k) { + sort(all(blocks[i]), greater<>()); + } + + vi ans; + + rep(i, act.size()) { + ans.push_back(blocks[act[i]].back()); + blocks[act[i]].pop_back(); + } + + nrep(i, 1, ans.size()) { + if (ans[i - 1] > ans[i]) { + cout << "N\n"; + return 0; + } + } + + cout << "Y\n"; +} diff --git a/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/K. Kathmandu.cpp b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/K. Kathmandu.cpp new file mode 100644 index 0000000..2d8df55 --- /dev/null +++ b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/K. Kathmandu.cpp @@ -0,0 +1,100 @@ +/* Problem URL: https://codeforces.com/gym/103388/problem/K */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t, d, m; + cin >> t >> d >> m; + + int prev = 0; + + while (m--) { + int n; + cin >> n; + + if (n - prev >= t) { + cout << "Y\n"; + return 0; + } + + prev = n; + } + + if (d - prev >= t) { + cout << "Y\n"; + return 0; + } + + cout << "N\n"; +} diff --git a/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/M. Monarchy in Vertigo.cpp b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/M. Monarchy in Vertigo.cpp new file mode 100644 index 0000000..b4b476a --- /dev/null +++ b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/M. Monarchy in Vertigo.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/gym/103388/problem/M */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int q; + cin >> q; + + vvi graph(1); + int cur = 1; + + vi status(1); + vi chosen(1); + vi parent(1); + + function dfs = [&](int i) { + if (status[i] != -1) { + return i; + } + + while (chosen[i] < graph[i].size()) { + int res = dfs(graph[i][chosen[i]]); + + if (res == -1) { + chosen[i]++; + continue; + } + + return res; + } + + return -1; + }; + + while (q--) { + int p, c; + cin >> p >> c; + + if (p == 1) { + graph[c - 1].push_back(graph.size()); + graph.emplace_back(); + parent.push_back(c - 1); + status.push_back(0); + chosen.push_back(0); + continue; + } + + status[c - 1] = -1; + + int ans = cur - 1; + int tmp; + while ((tmp = dfs(ans)) == -1) { + ans = parent[ans]; + } + + cur = tmp + 1; + cout << cur << '\n'; + } +} diff --git a/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/N. No Luck.cpp b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/N. No Luck.cpp new file mode 100644 index 0000000..6316174 --- /dev/null +++ b/2021-2022 ACM-ICPC Brazil Subregional Programming Contest/N. No Luck.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/gym/103388/problem/N */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int y, n; + cin >> y >> n; + + vi fds(y); + cin >> fds; + + while (__builtin_popcount(y) != 1) { + y++; + fds.push_back(0); + } + + vvi seg(y * 2); + + function build = [&](int i, int l, int r) { + if (l == r) { + seg[i].push_back(fds[i - y]); + return; + } + + int mid = (l + r) / 2; + + build(i * 2, l, mid); + build(i * 2 + 1, mid + 1, r); + + merge(all(seg[i * 2]), all(seg[i * 2 + 1]), back_inserter(seg[i])); + }; + + build(1, 0, y - 1); + + function query = [&](int i, int l, int r, int tl, int tr, int v) -> int { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + auto lower = lower_bound(all(seg[i]), v); + return seg[i].end() - lower; + } + + int mid = (l + r) / 2; + + return query(i * 2, l, mid, tl, tr, v) + query(i * 2 + 1, mid + 1, r, tl, tr, v); + }; + + while (n--) { + int a, p ,f; + cin >> a >> p >> f; + + if (fds[a - 1] >= p) { + cout << "0\n"; + continue; + } + + cout << query(1, 0, y - 1, a - 1, a + f - 1, p) << '\n'; + } +} diff --git a/2022 Benelux Algorithm Programming Contest (BAPC 22)/F. Failing Flagship.cpp b/2022 Benelux Algorithm Programming Contest (BAPC 22)/F. Failing Flagship.cpp new file mode 100644 index 0000000..12babef --- /dev/null +++ b/2022 Benelux Algorithm Programming Contest (BAPC 22)/F. Failing Flagship.cpp @@ -0,0 +1,170 @@ +/* Problem URL: https://codeforces.com/gym/104020/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +#define double long double + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a, b; + cin >> a >> b; + + double ang1; + double ang2; + + map var; + var["N"] = 0; + var["E"] = 90; + var["S"] = 180; + var["W"] = 270; + var["NE"] = 45; + var["SE"] = 135; + var["SW"] = 225; + var["NW"] = 315; + + if (a.size() == 1) { + ang1 = var[a]; + } else { + ang1 = var[a.substr(a.size() - 2)]; + + map mul; + mul['W'] = 1; + mul['E'] = 1; + mul['S'] = 1; + mul['N'] = 1; + + string tmp = a.substr(a.size() - 2); + + if (tmp == "NE") { + mul['N'] = -1; + } else if (tmp == "SE") { + mul['E'] = -1; + } else if (tmp == "SW") { + mul['S'] = -1; + } else { + mul['W'] = -1; + } + + a.pop_back(); + a.pop_back(); + + double now = 45 / (double)2; + + while (!a.empty()) { + ang1 += now * mul[a.back()]; + a.pop_back(); + now /= 2; + } + } + + if (b.size() == 1) { + ang2 = var[b]; + } else { + ang2 = var[b.substr(b.size() - 2)]; + + map mul; + mul['W'] = 1; + mul['E'] = 1; + mul['S'] = 1; + mul['N'] = 1; + + string tmp = b.substr(b.size() - 2); + + if (tmp == "NE") { + mul['N'] = -1; + } else if (tmp == "SE") { + mul['E'] = -1; + } else if (tmp == "SW") { + mul['S'] = -1; + } else { + mul['W'] = -1; + } + + b.pop_back(); + b.pop_back(); + + double now = 45 / (double)2; + + while (!b.empty()) { + ang2 += now * mul[b.back()]; + b.pop_back(); + now /= 2; + } + } + + if (ang1 < ang2) { + swap(ang1, ang2); + } + + cout << fixed << setprecision(20) << min(ang1 - ang2, (ang2 - ang1) + 360) << '\n'; +} diff --git a/2022 Benelux Algorithm Programming Contest (BAPC 22)/J. Jagged Skyline.cpp b/2022 Benelux Algorithm Programming Contest (BAPC 22)/J. Jagged Skyline.cpp new file mode 100644 index 0000000..2574fd6 --- /dev/null +++ b/2022 Benelux Algorithm Programming Contest (BAPC 22)/J. Jagged Skyline.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/gym/104020/problem/J */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll w, h; + cin >> w >> h; + + vi columns(w); + rep(i, w) { + columns[i] = i; + } + + mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + shuffle(all(columns), rng); + + ll ansx = 1; + ll ansy = 0; + repv(i, columns) { + if (ansy == h) { + break; + } + cout << "? " << i + 1 << ' ' << ansy + 1 << endl; + + string a; + cin >> a; + + if (a == "sky") { + continue; + } + + ll low = ansy + 1; + ll high = h; + while (low <= high) { + ll mid = (high - low) / 2 + low; + + cout << "? " << i + 1 << ' ' << mid << endl; + + cin >> a; + + if (a == "building") { + ansy = mid; + ansx = i + 1; + low = mid + 1; + continue; + } + + high = mid - 1; + } + } + + cout << "! " << ansx << ' ' << ansy << endl; +} diff --git a/2022 Benelux Algorithm Programming Contest (BAPC 22)/K. Kiosk Construction.cpp b/2022 Benelux Algorithm Programming Contest (BAPC 22)/K. Kiosk Construction.cpp new file mode 100644 index 0000000..ed123a7 --- /dev/null +++ b/2022 Benelux Algorithm Programming Contest (BAPC 22)/K. Kiosk Construction.cpp @@ -0,0 +1,182 @@ +/* Problem URL: https://codeforces.com/gym/104020/problem/K */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + set attempts; + for (int i = 1; i <= n * m; i++) { + attempts.insert(i); + } + + vvi fds(n, vi(m)); + cin >> fds; + + auto choice = [&](int i, int j, int t) { + int add[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + + int ans = INT32_MAX >> 1; + + for (auto &k : add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < m) { + int now = fds[id][jd]; + if (abs(t - now) < abs(t - ans) || (abs(t - now) == abs(t - ans) && abs(fds[i][j] - now) < abs(fds[i][j] - ans))) { + ans = now; + } + } + } + + return ans; + }; + + vvvi graph(n * m, vvi(n * m)); + vvvi look(n * m, vvi(n * m)); + + for (int i = 1; i <= n * m; i++) { + rep(j, n) { + rep(k, m) { + if (fds[j][k] == i) { + continue; + } + + int tmp = choice(j, k, i); + graph[i - 1][tmp - 1].push_back(fds[j][k] - 1); + look[i - 1][fds[j][k] - 1].push_back(tmp - 1); + } + } + } + + vvi depth(n * m, vi(n * m, 0)); + + V vis(n * m); + function dfs = [&](int i, int t, int d) { + vis[i] = true; + depth[i][t] = d; + + for (auto j : graph[t][i]) { + if (vis[j]) { + continue; + } + + dfs(j, t, d + 1); + } + }; + + rep(i, n * m) { + fill(all(vis), false); + + dfs(i, i, 0); + + auto itr = attempts.begin(); + while (itr != attempts.end()) { + if (!vis[*itr]) { + itr = attempts.erase(itr); + } else { + itr++; + } + } + + if (attempts.empty()) { + break; + } + } + + if (attempts.empty()) { + cout << "impossible\n"; + return 0; + } + + int ans = 0; + int minimal = INT32_MAX >> 1; + + for (auto i : attempts) { + int get = 0; + + for (auto j : depth[i]) { + rmax(get, j); + } + + if (get < minimal) { + ans = i; + minimal = get; + } + } + + cout << ans + 1 << ' ' << minimal << '\n'; +} diff --git a/2022 USP Try-outs/B. Tuk-Tuk Express.cpp b/2022 USP Try-outs/B. Tuk-Tuk Express.cpp new file mode 100644 index 0000000..f4ff813 --- /dev/null +++ b/2022 USP Try-outs/B. Tuk-Tuk Express.cpp @@ -0,0 +1,196 @@ +/* Problem URL: https://codeforces.com/gym/103934/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll c, x, t, n; + cin >> c >> x >> t >> n; + + ll t1, t2, t3; + cin >> t1 >> t2 >> t3; + + if (c == 1) { + cout << max({t - t1, t - t2, t - t3}) << '\n'; + return 0; + } + + vl count(3); + vl prev(3); + vl prevadd(3); + + vl ti = {t1, t2, t3}; + + ll ans = 0; + + V> fds(n); + repv(i, fds) { + cin >> i.first >> i.second; + } + + sortv(fds); + + auto binsearch = [&](ll low, ll high, ll lim) { + ll ans = 0; + + while (low <= high) { + ll mid = (high + low) >> 1; + + if (x * mid <= lim) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + return ans; + }; + + auto updateprev = [&](int i, ll time) { + if (time - prev[i] <= x) { + return; + } + + if (prev[i] + x + ti[i] > t) { + count[i] = 0; + ll diff = time - prev[i]; + ll binans = binsearch(0, diff / x, t - ti[i] - prev[i]); + if (prev[i] + x * binans + ti[i] < t) { + rmax(ans, prev[i] + x * binans); + } + prev[i] += diff / x * x; + return; + } + + ll diff = time - prev[i]; + ll binans = binsearch(0, diff / x, t - ti[i] - prev[i]); + if (prev[i] + x * binans + ti[i] < t) { + rmax(ans, prev[i] + x * binans); + } + prev[i] += diff / x * x; + count[i] = 0; + + return; + }; + + auto updatetime = [&](int i, ll time) { + if (count[i] < c - 1) { + return; + } + + if (count[i] == c) { + if (prevadd[i] + ti[i] <= min(t, time)) { + rmax(ans, min(t, time) - ti[i]); + } + + if (time + ti[i] <= t) { + rmax(ans, time); + } + + count[i] = 0; + prev[i] = time; + prevadd[i] = time; + return; + } + + if (time + ti[i] <= t) { + rmax(ans, time); + } + + prevadd[i] = time; + }; + + repv(i, fds) { + ll time = i.first; + + updateprev(0, time); + updateprev(1, time); + updateprev(2, time); + + count[i.second - 1]++; + + updatetime(0, time); + updatetime(1, time); + updatetime(2, time); + } + + cout << ans << '\n'; +} diff --git a/2022-2023 ICPC Brazil Subregional Programming Contest/A. Finding Maximal Non-Trivial Monotones.cpp b/2022-2023 ICPC Brazil Subregional Programming Contest/A. Finding Maximal Non-Trivial Monotones.cpp new file mode 100644 index 0000000..043ed29 --- /dev/null +++ b/2022-2023 ICPC Brazil Subregional Programming Contest/A. Finding Maximal Non-Trivial Monotones.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://codeforces.com/gym/103960/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + string a; + cin >> n >> a; + + int ans = 0; + int count = 0; + repv(i, a) { + if (i == 'b') { + ans += count >= 2 ? count : 0; + count = 0; + continue; + } + + count++; + } + + ans += count >= 2 ? count : 0; + + cout << ans << '\n'; +} diff --git a/2022-2023 ICPC Brazil Subregional Programming Contest/D. Displacing Particles.cpp b/2022-2023 ICPC Brazil Subregional Programming Contest/D. Displacing Particles.cpp new file mode 100644 index 0000000..04f44f9 --- /dev/null +++ b/2022-2023 ICPC Brazil Subregional Programming Contest/D. Displacing Particles.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/gym/103960/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, is, js; + cin >> n >> is >> js; + + int inf = INT32_MAX >> 1; + vi dp(1 << n, inf); + + auto bfs = [&]() { + queue q; + + dp[1 << (n - 1)] = 0; + q.push(1 << (n - 1)); + + while (!q.empty()) { + int i = q.front(); + q.pop(); + + if (dp[i >> 1] > dp[i] + 1) { + dp[i >> 1] = dp[i] + 1; + q.push(i >> 1); + } + + if (dp[(i + (1 << n)) >> 1] > dp[i] + 1) { + dp[(i + (1 << n)) >> 1] = dp[i] + 1; + q.push((i + (1 << n)) >> 1); + } + } + }; + + bfs(); + + cout << dp[is] << '\n'; +} diff --git a/2022-2023 ICPC Brazil Subregional Programming Contest/E. Eliminating Ballons.cpp b/2022-2023 ICPC Brazil Subregional Programming Contest/E. Eliminating Ballons.cpp new file mode 100644 index 0000000..4827afa --- /dev/null +++ b/2022-2023 ICPC Brazil Subregional Programming Contest/E. Eliminating Ballons.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/gym/103960/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi c(1e6 + 1, 0); + + int ans = 0; + + while (n--) { + int now; + cin >> now; + + if (c[now] == 0) { + ans++; + c[now - 1]++; + continue; + } + + c[now]--; + c[now - 1]++; + } + + cout << ans << '\n'; +} diff --git a/2022-2023 ICPC Brazil Subregional Programming Contest/F. Multidimensional Hangman.cpp b/2022-2023 ICPC Brazil Subregional Programming Contest/F. Multidimensional Hangman.cpp new file mode 100644 index 0000000..a85bb07 --- /dev/null +++ b/2022-2023 ICPC Brazil Subregional Programming Contest/F. Multidimensional Hangman.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/gym/103960/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, c; + cin >> n >> c; + + vvi trie(1, vi(26)); + vvi count(1, vi(26)); + + function add = [&](string &a, int i, int p) { + if (a[i] == '*') { + int maximal = 0; + rep(j, 26) { + if (trie[p][j] == 0) { + trie[p][j] = trie.size(); + trie.emplace_back(26); + count.emplace_back(26); + } + + if (i == c - 1) { + count[p][j]++; + rmax(maximal, count[p][j]); + continue; + } + + rmax(count[p][j], add(a, i + 1, trie[p][j])); + rmax(maximal, count[p][j]); + } + + return maximal; + } + + if (trie[p][a[i] - 'a'] == 0) { + trie[p][a[i] - 'a'] = trie.size(); + trie.emplace_back(26); + count.emplace_back(26); + } + + if (i == c - 1) { + count[p][a[i] - 'a']++; + return count[p][a[i] - 'a']; + } + + rmax(count[p][a[i] - 'a'], add(a, i + 1, trie[p][a[i] - 'a'])); + + return count[p][a[i] - 'a']; + }; + + while (n--) { + string f; + cin >> f; + + add(f, 0, 0); + } + + string ans; + int now = 0; + int ansc = 0; + + rep(i, c) { + int tmp = 0; + int maximal = 0; + + rep(j, 26) { + if (count[now][j] > maximal) { + maximal = count[now][j]; + tmp = j; + } + } + + ans.push_back(tmp + 'a'); + ansc = maximal; + now = trie[now][tmp]; + } + + cout << ans << ' ' << ansc << '\n'; +} diff --git a/2022-2023 ICPC Brazil Subregional Programming Contest/H. Helping the Transit.cpp b/2022-2023 ICPC Brazil Subregional Programming Contest/H. Helping the Transit.cpp new file mode 100644 index 0000000..93be69d --- /dev/null +++ b/2022-2023 ICPC Brazil Subregional Programming Contest/H. Helping the Transit.cpp @@ -0,0 +1,185 @@ +/* Problem URL: https://codeforces.com/gym/103960/problem/H */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + vvi inv(n); + vvi comp(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + inv[b].push_back(a); + comp[a].push_back(b); + comp[b].push_back(a); + } + + stack preorder; + + V vis(n); + + function getpre = [&](int i) { + vis[i] = true; + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + + getpre(j); + } + + preorder.push(i); + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + getpre(i); + } + + int g = 0; + fill(all(vis), false); + V> condin; + V> condout; + vi group(n, -1); + + function dfs = [&](int i) { + vis[i] = true; + group[i] = g; + + for (auto j : inv[i]) { + if (group[j] != -1 && group[j] != g) { + condin[g].insert(group[j]); + condout[group[j]].insert(g); + continue; + } + + if (vis[j]) { + continue; + } + + dfs(j); + } + }; + + while (!preorder.empty()) { + int i = preorder.top(); + preorder.pop(); + + if (vis[i]) { + continue; + } + + condin.emplace_back(); + condout.emplace_back(); + dfs(i); + g++; + } + + if (g == 1) { + cout << "0\n"; + return 0; + } + + int nonein = 0; + int noneout = 0; + + rep(i, g) { + if (condin[i].empty()) { + nonein++; + } + + if (condout[i].empty()) { + noneout++; + } + } + + cout << max(nonein, noneout) << '\n'; +} diff --git a/2022-2023 ICPC Brazil Subregional Programming Contest/I. Intercepting Information.cpp b/2022-2023 ICPC Brazil Subregional Programming Contest/I. Intercepting Information.cpp new file mode 100644 index 0000000..1df6502 --- /dev/null +++ b/2022-2023 ICPC Brazil Subregional Programming Contest/I. Intercepting Information.cpp @@ -0,0 +1,88 @@ +/* Problem URL: https://codeforces.com/gym/103960/problem/I */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + rep(i, 9) { + int n; + cin >> n; + + if (n == 9) { + cout << "F\n"; + return 0; + } + } + + cout << "S\n"; +} diff --git a/2022-2023 ICPC Brazil Subregional Programming Contest/J. Playing 23.cpp b/2022-2023 ICPC Brazil Subregional Programming Contest/J. Playing 23.cpp new file mode 100644 index 0000000..12ab3f9 --- /dev/null +++ b/2022-2023 ICPC Brazil Subregional Programming Contest/J. Playing 23.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/gym/103960/problem/J */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + map card; + + vi cards(11, 4); + cards[10] = 16; + + int john = 0; + int mary = 0; + + vi value(14, 0); + nrep(i, 1, 11) { + value[i] = i; + } + + value[11] = 10; + value[12] = 10; + value[13] = 10; + + int c1, c2; + cin >> c1 >> c2; + cards[value[c1]]--; + cards[value[c2]]--; + + john = value[c1] + value[c2]; + + cin >> c1 >> c2; + cards[value[c1]]--; + cards[value[c2]]--; + + mary = value[c1] + value[c2]; + + while (n--) { + int c; + cin >> c; + + cards[value[c]]--; + john += value[c]; + mary += value[c]; + } + + if (mary >= john) { + if (23 - mary > 10 || cards[23 - mary] == 0) { + cout << "-1\n"; + return 0; + } + + cout << 23 - mary << '\n'; + return 0; + } + + nrep(i, 1, 11) { + if (mary + i > 23) { + cout << "-1\n"; + return 0; + } + + if (john + i > 23 && cards[i] > 0) { + cout << i << '\n'; + return 0; + } + } + + cout << "-1\n"; +} diff --git a/2023 ICPC HIAST Collegiate Programming Contest/K. Sam-Oh, the funny coach.cpp b/2023 ICPC HIAST Collegiate Programming Contest/K. Sam-Oh, the funny coach.cpp new file mode 100644 index 0000000..73d3492 --- /dev/null +++ b/2023 ICPC HIAST Collegiate Programming Contest/K. Sam-Oh, the funny coach.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/gym/104493/problem/K */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi count(n, vi(26)); + + rep(i, n) { + string a; + cin >> a; + + repv(j, a) { + count[i][j - 'a']++; + } + } + + int q; + cin >> q; + while (q--) { + int a, b; + cin >> a >> b; + a--, b--; + + int ans = 0; + vi pos(2); + + rep(i, 26) { + int act0 = count[a][i]; + int act1 = count[b][i]; + if (pos[0] <= pos[1]) { + int diff = pos[1] - pos[0]; + if (act0 < diff) { + pos[0] += count[a][i]; + pos[1] += count[b][i]; + continue; + } + + act0 -= diff; + pos[0] += diff; + } else { + int diff = pos[0] - pos[1]; + if (act1 < diff) { + pos[0] += count[a][i]; + pos[1] += count[b][i]; + continue; + } + + act1 -= diff; + pos[1] += diff; + } + + ans += min(act0, act1); + pos[0] += act0; + pos[1] += act1; + } + + cout << ans << '\n'; + } +} diff --git a/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/A. Assessment Disruption.cpp b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/A. Assessment Disruption.cpp new file mode 100644 index 0000000..523c752 --- /dev/null +++ b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/A. Assessment Disruption.cpp @@ -0,0 +1,83 @@ +/* Problem URL: https://codeforces.com/gym/104785/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, w; + cin >> n >> w; + + rep(i, n) { + cout << w - i << ' ' << 500 << '\n'; + } +} diff --git a/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/F. Fast Forward.cpp b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/F. Fast Forward.cpp new file mode 100644 index 0000000..f224492 --- /dev/null +++ b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/F. Fast Forward.cpp @@ -0,0 +1,157 @@ +/* Problem URL: https://codeforces.com/gym/104785/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int parent[(ll)2e6 + 1][21]; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, c; + cin >> n >> c; + + vl fds(n); + cin >> fds; + rep(i, n) { + fds.push_back(fds[i]); + } + + vl pref(n * 2 + 1); + rep(i, n * 2) { + pref[i + 1] = pref[i] + fds[i]; + } + + vvi graph(n * 2 + 1); + rep(i, n * 2) { + int ans = -1; + int low = i; + int high = min((int)i + n - 1, n * 2 - 1); + + while (low <= high) { + int mid = (low + high) / 2; + + if (pref[mid + 1] - pref[i] >= c) { + ans = mid + 1; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + if (ans != -1) { + graph[ans].push_back(i); + } + } + + int inf = INT32_MAX >> 1; + rep(i, n * 2 + 1) { + rep(j, 21) { + parent[i][j] = inf; + } + } + // vvi parent(n * 2 + 1, vi(21, inf)); + V vis(n * 2 + 1, false); + + function dfs = [&](int i, int p){ + parent[i][0] = p; + vis[i] = true; + + for (int j = 1; j <= 20 && parent[i][j - 1] != inf; j++) { + parent[i][j] = parent[parent[i][j - 1]][j - 1]; + } + + for (auto j : graph[i]) { + dfs(j, i); + } + }; + + for (int i = n * 2; i >= 0; i--) { + if (!vis[i]) { + dfs(i, inf); + } + } + + rep(i, n) { + int now = i; + int ans = 0; + for (int j = 20; j >= 0; j--) { + if (parent[now][j] < i + n) { + ans += (1 << j); + now = parent[now][j]; + } + } + + cout << ans << ' '; + } + cout << '\n'; +} diff --git a/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/K. Kernel Scheduler.cpp b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/K. Kernel Scheduler.cpp new file mode 100644 index 0000000..d716e44 --- /dev/null +++ b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/K. Kernel Scheduler.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/gym/104785/problem/K */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dec; + vi cres; + rep(i, m) { + int a, b; + cin >> a >> b; + + if (a > b) { + dec.push_back(i + 1); + } else { + cres.push_back(i + 1); + } + } + + cout << "YES\n"; + if (dec.size() > cres.size()) { + cout << dec.size() << '\n'; + for (auto i : dec) { + cout << i << ' '; + } + cout << '\n'; + return 0; + } + + cout << cres.size() << '\n'; + for (auto i : cres) { + cout << i << ' '; + } + cout << '\n'; +} diff --git a/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/L. Last One Standing.cpp b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/L. Last One Standing.cpp new file mode 100644 index 0000000..5422ede --- /dev/null +++ b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/L. Last One Standing.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/gym/104785/problem/L */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll h1, d1, t1; + cin >> h1 >> d1 >> t1; + ll h2, d2, t2; + cin >> h2 >> d2 >> t2; + + h1 -= d2; + h2 -= d1; + + if (h1 < 0 && h2 < 0) { + cout << "draw\n"; + return 0; + } + + if (h1 < 0) { + cout << "player two\n"; + return 0; + } + + if (h2 < 0) { + cout << "player one\n"; + return 0; + } + + ll killt1 = (h1 / d2 + min(1LL, h1 % d2)) * t2; + ll killt2 = (h2 / d1 + min(1LL, h2 % d1)) * t1; + + if (killt1 == killt2) { + cout << "draw\n"; + return 0; + } + + if (killt1 > killt2) { + cout << "player one\n"; + return 0; + } + + if (killt2 > killt1) { + cout << "player two\n"; + } +} diff --git a/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/M. Mini-Tetris 3023.cpp b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/M. Mini-Tetris 3023.cpp new file mode 100644 index 0000000..fcdfb21 --- /dev/null +++ b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/M. Mini-Tetris 3023.cpp @@ -0,0 +1,91 @@ +/* Problem URL: https://codeforces.com/gym/104785/problem/M */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int a, b, c; + cin >> a >> b >> c; + + int squares = 2 * a; + + int ret = 0; + if (c > 1 && b >= 1) { + c -= 2; + ret += 2 + b * 2; + ret++; + } + + ret += 3 * (c / 2); + cout << squares + ret << '\n'; +} diff --git a/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/N. Naming Wine Bottles.cpp b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/N. Naming Wine Bottles.cpp new file mode 100644 index 0000000..07597c2 --- /dev/null +++ b/2023 United Kingdom and Ireland Programming Contest (UKIEPC 2023)/N. Naming Wine Bottles.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/gym/104785/problem/N */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + map var; + var["0.75"] = "standard"; + var["15"] = "nebuchadnezzar"; + var["12"] = "balthazar"; + + string now = "a"; + + while (n--) { + string a; + cin >> a; + + size_t i = 0; + string b; + while (a[i] != 'L' && a[i] == '0') { + i++; + } + + if (a[i] == 'L') { + b = "0"; + } else { + while (a[i] != '.' && a[i] != 'L') { + b.push_back(a[i]); + i++; + } + + if (a[i] != 'L') { + i++; + string tmp; + while (a[i] != 'L') { + while (a[i] == '0') { + tmp.push_back(a[i]); + i++; + } + + if (a[i] != 'L') { + b += tmp; + b += a[i]; + i++; + } + } + } + } + + auto itr = var.find(b); + if (itr == var.end()) { + var[b] = now; + now += 'a'; + } + + cout << var[b] << '\n'; + } +} diff --git a/2023-2024 ACM-ICPC Latin American Regional Programming Contest/J. Journey of the Robber.cpp b/2023-2024 ACM-ICPC Latin American Regional Programming Contest/J. Journey of the Robber.cpp new file mode 100644 index 0000000..8233e45 --- /dev/null +++ b/2023-2024 ACM-ICPC Latin American Regional Programming Contest/J. Journey of the Robber.cpp @@ -0,0 +1,153 @@ +/* Problem URL: https://codeforces.com/gym/104736/problem/J */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvi graph(n); + + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + vi ans(n); + int prev = n - 1; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + int inf = INT32_MAX >> 1; + vi seg(n * 2, inf); + + auto update = [&](int i, int v) { + seg[i + n] = v; + for (int j = (i + n) / 2; j > 0; j /= 2) { + seg[j] = min(seg[j * 2], seg[j * 2 + 1]); + } + }; + + function search = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl || seg[i] == inf) { + return -1; + } + + if (l == r) { + return i - n; + } + + if (l >= tl && r <= tr) { + int mid = (l + r) / 2; + if (seg[i * 2] <= seg[i * 2 + 1]) { + return search(i * 2, l, mid, tl, tr); + } + return search(i * 2 + 1, mid + 1, r, tl, tr); + } + + int mid = (l + r) / 2; + int ans1 = search(i * 2, l, mid, tl, tr); + int ans2 = search(i * 2 + 1, mid + 1, r, tl, tr); + + if (ans2 == -1 || seg[ans1 + n] < seg[ans2 + n] || (seg[ans1 + n] == seg[ans2 + n] && ans1 < ans2)) { + return ans1; + } + return ans2; + }; + + function dfs = [&](int i, int p, int d) { + update(i, d); + + ans[i] = search(1, 0, n - 1, i, n - 1) + 1; + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i, d - 1); + } + + update(i, inf); + }; + + dfs(prev, -1, inf - 1); + + cout << ans; +} diff --git a/2023-2024 ICPC Brazil Subregional Programming Contest/A. Amusement Park Adventure.cpp b/2023-2024 ICPC Brazil Subregional Programming Contest/A. Amusement Park Adventure.cpp new file mode 100644 index 0000000..7ea4dc9 --- /dev/null +++ b/2023-2024 ICPC Brazil Subregional Programming Contest/A. Amusement Park Adventure.cpp @@ -0,0 +1,91 @@ +/* Problem URL: https://codeforces.com/gym/104555/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, h; + cin >> n >> h; + + int ans = 0; + while (n--) { + int num; + cin >> num; + + if (h >= num) { + ans++; + } + } + + cout << ans << '\n'; +} diff --git a/2023-2024 ICPC Brazil Subregional Programming Contest/E. Extracting Pollen.cpp b/2023-2024 ICPC Brazil Subregional Programming Contest/E. Extracting Pollen.cpp new file mode 100644 index 0000000..19966a3 --- /dev/null +++ b/2023-2024 ICPC Brazil Subregional Programming Contest/E. Extracting Pollen.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/gym/104555/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, k; + cin >> n >> k; + + vi count(1e6 + 1); + count[0] = 1e9; + + while (n--) { + int num; + cin >> num; + + count[num]++; + } + + auto calc = [&](int i) { + int tmp = i; + int sum = 0; + while (tmp > 0) { + sum += tmp % 10; + tmp /= 10; + } + + return sum; + }; + + int ans = 1e6; + while (count[ans] < k) { + k -= count[ans]; + count[ans - calc(ans)] += count[ans]; + ans--; + } + + cout << calc(ans) << '\n'; +} diff --git a/2023-2024 ICPC Brazil Subregional Programming Contest/F. Fatigue-Fighting Vacation.cpp b/2023-2024 ICPC Brazil Subregional Programming Contest/F. Fatigue-Fighting Vacation.cpp new file mode 100644 index 0000000..614080d --- /dev/null +++ b/2023-2024 ICPC Brazil Subregional Programming Contest/F. Fatigue-Fighting Vacation.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/gym/104555/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int d, c, r; + cin >> d >> c >> r; + + int ans = r; + + vi fds(c); + cin >> fds; + + while (r--) { + int num; + cin >> num; + d += num; + } + + rep(i, fds.size()) { + if (d >= fds[i]) { + d -= fds[i]; + ans++; + } else { + break; + } + } + + cout << ans << '\n'; +} diff --git a/2023-2024 ICPC Brazil Subregional Programming Contest/G. Great Treaty of Byteland.cpp b/2023-2024 ICPC Brazil Subregional Programming Contest/G. Great Treaty of Byteland.cpp new file mode 100644 index 0000000..8930853 --- /dev/null +++ b/2023-2024 ICPC Brazil Subregional Programming Contest/G. Great Treaty of Byteland.cpp @@ -0,0 +1,314 @@ +/* Problem URL: https://codeforces.com/gym/104555/problem/G */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +#define sq(x) ((x)*(ll)(x)) + +struct pt { // ponto + int x, y; + pt(int x_ = 0, int y_ = 0) : x(x_), y(y_) {} + bool operator < (const pt p) const { + if (x != p.x) return x < p.x; + return y < p.y; + } + bool operator == (const pt p) const { + return x == p.x and y == p.y; + } + pt operator + (const pt p) const { return pt(x+p.x, y+p.y); } + pt operator - (const pt p) const { return pt(x-p.x, y-p.y); } + pt operator * (const int c) const { return pt(x*c, y*c); } + ll operator * (const pt p) const { return x*(ll)p.x + y*(ll)p.y; } + ll operator ^ (const pt p) const { return x*(ll)p.y - y*(ll)p.x; } + friend istream& operator >> (istream& in, pt& p) { + return in >> p.x >> p.y; + } +}; + +struct line { // reta + pt p, q; + line() {} + line(pt p_, pt q_) : p(p_), q(q_) {} + friend istream& operator >> (istream& in, line& r) { + return in >> r.p >> r.q; + } +}; + +// PONTO & VETOR + +ll dist2(pt p, pt q) { // quadrado da distancia + return sq(p.x - q.x) + sq(p.y - q.y); +} + +ll sarea2(pt p, pt q, pt r) { // 2 * area com sinal + return (q-p)^(r-q); +} + +bool col(pt p, pt q, pt r) { // se p, q e r sao colin. + return sarea2(p, q, r) == 0; +} + +bool ccw(pt p, pt q, pt r) { // se p, q, r sao ccw + return sarea2(p, q, r) > 0; +} + +int quad(pt p) { // quadrante de um ponto + return (p.x<0)^3*(p.y<0); +} + +bool compare_angle(pt p, pt q) { // retorna se ang(p) < ang(q) + if (quad(p) != quad(q)) return quad(p) < quad(q); + return ccw(q, pt(0, 0), p); +} + +pt rotate90(pt p) { // rotaciona 90 graus + return pt(-p.y, p.x); +} + +// RETA + +bool isinseg(pt p, line r) { // se p pertence ao seg de r + pt a = r.p - p, b = r.q - p; + return (a ^ b) == 0 and (a * b) <= 0; +} + +bool interseg(line r, line s) { // se o seg de r intersecta o seg de s + if (isinseg(r.p, s) or isinseg(r.q, s) + or isinseg(s.p, r) or isinseg(s.q, r)) return 1; + + return ccw(r.p, r.q, s.p) != ccw(r.p, r.q, s.q) and + ccw(s.p, s.q, r.p) != ccw(s.p, s.q, r.q); +} + +int segpoints(line r) { // numero de pontos inteiros no segmento + return 1 + __gcd(abs(r.p.x - r.q.x), abs(r.p.y - r.q.y)); +} + +double get_t(pt v, line r) { // retorna t tal que t*v pertence a reta r + return (r.p^r.q) / (double) ((r.p-r.q)^v); +} + +// POLIGONO + +// quadrado da distancia entre os retangulos a e b (lados paralelos aos eixos) +// assume que ta representado (inferior esquerdo, superior direito) +ll dist2_rect(pair a, pair b) { + int hor = 0, vert = 0; + if (a.second.x < b.first.x) hor = b.first.x - a.second.x; + else if (b.second.x < a.first.x) hor = a.first.x - b.second.x; + if (a.second.y < b.first.y) vert = b.first.y - a.second.y; + else if (b.second.y < a.first.y) vert = a.first.y - b.second.y; + return sq(hor) + sq(vert); +} + +ll polarea2(vector v) { // 2 * area do poligono + ll ret = 0; + for (int i = 0; i < v.size(); i++) + ret += sarea2(pt(0, 0), v[i], v[(i + 1) % v.size()]); + return abs(ret); +} + +// se o ponto ta dentro do poligono: retorna 0 se ta fora, +// 1 se ta no interior e 2 se ta na borda +int inpol(vector& v, pt p) { // O(n) + int qt = 0; + for (int i = 0; i < v.size(); i++) { + if (p == v[i]) return 2; + int j = (i+1)%v.size(); + if (p.y == v[i].y and p.y == v[j].y) { + if ((v[i]-p)*(v[j]-p) <= 0) return 2; + continue; + } + bool baixo = v[i].y < p.y; + if (baixo == (v[j].y < p.y)) continue; + auto t = (p-v[i])^(v[j]-v[i]); + if (!t) return 2; + if (baixo == (t > 0)) qt += baixo ? 1 : -1; + } + return qt != 0; +} + +vector convex_hull(vector v) { // convex hull - O(n log(n)) + sort(v.begin(), v.end()); + v.erase(unique(v.begin(), v.end()), v.end()); + if (v.size() <= 1) return v; + vector l, u; + for (int i = 0; i < v.size(); i++) { + while (l.size() > 1 and !ccw(l.end()[-2], l.end()[-1], v[i])) + l.pop_back(); + l.push_back(v[i]); + } + for (int i = v.size() - 1; i >= 0; i--) { + while (u.size() > 1 and !ccw(u.end()[-2], u.end()[-1], v[i])) + u.pop_back(); + u.push_back(v[i]); + } + l.pop_back(); u.pop_back(); + for (pt i : u) l.push_back(i); + return l; +} + +ll interior_points(vector v) { // pontos inteiros dentro de um poligono simples + ll b = 0; + for (int i = 0; i < v.size(); i++) + b += segpoints(line(v[i], v[(i+1)%v.size()])) - 1; + return (polarea2(v) - b) / 2 + 1; +} + +struct convex_pol { + vector pol; + + // nao pode ter ponto colinear no convex hull + convex_pol() {} + convex_pol(vector v) : pol(convex_hull(v)) {} + + // se o ponto ta dentro do hull - O(log(n)) + bool is_inside(pt p) { + if (pol.size() == 0) return false; + if (pol.size() == 1) return p == pol[0]; + int l = 1, r = pol.size(); + while (l < r) { + int m = (l+r)/2; + if (ccw(p, pol[0], pol[m])) l = m+1; + else r = m; + } + if (l == 1) return isinseg(p, line(pol[0], pol[1])); + if (l == pol.size()) return false; + return !ccw(p, pol[l], pol[l-1]); + } + // ponto extremo em relacao a cmp(p, q) = p mais extremo q + // (copiado de https://github.com/gustavoM32/caderno-zika) + int extreme(const function& cmp) { + int n = pol.size(); + auto extr = [&](int i, bool& cur_dir) { + cur_dir = cmp(pol[(i+1)%n], pol[i]); + return !cur_dir and !cmp(pol[(i+n-1)%n], pol[i]); + }; + bool last_dir, cur_dir; + if (extr(0, last_dir)) return 0; + int l = 0, r = n; + while (l+1 < r) { + int m = (l+r)/2; + if (extr(m, cur_dir)) return m; + bool rel_dir = cmp(pol[m], pol[l]); + if ((!last_dir and cur_dir) or + (last_dir == cur_dir and rel_dir == cur_dir)) { + l = m; + last_dir = cur_dir; + } else r = m; + } + return l; + } + int max_dot(pt v) { + return extreme([&](pt p, pt q) { return p*v > q*v; }); + } + pair tangents(pt p) { + auto L = [&](pt q, pt r) { return ccw(p, r, q); }; + auto R = [&](pt q, pt r) { return ccw(p, q, r); }; + return {extreme(L), extreme(R)}; + } +}; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + map var; + + V points(n); + rep(i, n) { + cin >> points[i].x >> points[i].y; + var[{points[i].x, points[i].y}] = i + 1; + } + + convex_pol conv(points); + + set ans; + + repv(i, points) { + if (inpol(conv.pol, i) == 2) { + ans.insert(var[i]); + } + } + + repv(i, ans) { + cout << i << ' '; + } + cout << '\n'; +} diff --git a/2023-2024 ICPC Brazil Subregional Programming Contest/H. Honest Worker.cpp b/2023-2024 ICPC Brazil Subregional Programming Contest/H. Honest Worker.cpp new file mode 100644 index 0000000..cedd049 --- /dev/null +++ b/2023-2024 ICPC Brazil Subregional Programming Contest/H. Honest Worker.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://codeforces.com/gym/104555/problem/H */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll s; + cin >> n >> s; + + V> fds(n); + for (auto &[l, r, c] : fds) { + cin >> l >> r >> c; + } + + sortv(fds); + + ll ans = 0; + + ll cost = 0; + ll ln = 0; + ll rn = 0; + for (auto [l, r, c] : fds) { + ll tmp = (r - l + 1) * s - c; + + if (rn < l) { + if (tmp > 0) { + cost = c; + ln = l; + rn = r; + ans += tmp; + } + continue; + } + + + } +} diff --git a/2023-2024 ICPC Brazil Subregional Programming Contest/I. Investigating Zeroes and Ones.cpp b/2023-2024 ICPC Brazil Subregional Programming Contest/I. Investigating Zeroes and Ones.cpp new file mode 100644 index 0000000..59c4b99 --- /dev/null +++ b/2023-2024 ICPC Brazil Subregional Programming Contest/I. Investigating Zeroes and Ones.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/gym/104555/problem/I */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + vvl dp(n, vl(2, 0)); + dp[0][1] = fds[0]; + dp[0][0] = fds[0] ^ 1; + + nrep(i, 1, n) { + // if (fds[i] == 0) { + // dp[i][0] = dp[i - 1][0] + 1; + // dp[i][1] = dp[i - 1][1]; + // continue; + // } + // + // dp[i][0] = dp[i - 1][1]; + // dp[i][1] = dp[i - 1][0] + 1; + + dp[i][0] = dp[i - 1][fds[i]] + (fds[i] ^ 1); + dp[i][1] = dp[i - 1][fds[i] ^ 1] + fds[i]; + } + + ll ans = 0; + rep(i, n) { + ans += dp[i][1]; + } + + cout << ans << '\n'; +} diff --git a/2023-2024 ICPC Brazil Subregional Programming Contest/L. Lexicographical Challenge.cpp b/2023-2024 ICPC Brazil Subregional Programming Contest/L. Lexicographical Challenge.cpp new file mode 100644 index 0000000..773abdc --- /dev/null +++ b/2023-2024 ICPC Brazil Subregional Programming Contest/L. Lexicographical Challenge.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/gym/104555/problem/L */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a; + int k; + cin >> a >> k; + + V fds(k); + + rep(i, a.size()) { + fds[i % k].push_back(a[i]); + } + + rep(i, k) { + sort(all(fds[i]), greater<>()); + } + + rep(i, a.size()) { + cout << fds[i % k].back(); + fds[i % k].pop_back(); + } + + cout << '\n'; +} diff --git a/2023-2024 ICPC Brazil Subregional Programming Contest/M. Maximizing Flight Efficiency.cpp b/2023-2024 ICPC Brazil Subregional Programming Contest/M. Maximizing Flight Efficiency.cpp new file mode 100644 index 0000000..e736b52 --- /dev/null +++ b/2023-2024 ICPC Brazil Subregional Programming Contest/M. Maximizing Flight Efficiency.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/gym/104555/problem/M */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvl dis(n, vl(n)); + cin >> dis; + + vvl tmp = dis; + + ll ans = 0; + + rep(k, n) { + rep(i, n) { + rep(j, n) { + rmin(tmp[i][j], tmp[i][k] + tmp[k][j]); + } + } + } + + if (tmp != dis) { + cout << "-1\n"; + return 0; + } + + ll oo = INT64_MAX >> 1; + + rep(k, n) { + rep(i, n) { + rep(j, n) { + if (i != j && j != k && k != i && dis[i][j] == dis[i][k] + dis[k][j]) { + dis[i][j] = oo; + dis[j][i] = oo; + ans++; + } + } + } + } + + cout << ans << '\n'; +} diff --git a/2024 (ICPC) Jiangxi Provincial Contest -- Official Contest/I. Neuvillette Circling.cpp b/2024 (ICPC) Jiangxi Provincial Contest -- Official Contest/I. Neuvillette Circling.cpp new file mode 100644 index 0000000..e4a1c34 --- /dev/null +++ b/2024 (ICPC) Jiangxi Provincial Contest -- Official Contest/I. Neuvillette Circling.cpp @@ -0,0 +1,193 @@ +/* Problem URL: https://codeforces.com/gym/105231/problem/I */ + +#include + +#pragma GCC optimize("Ofast") +#pragma GCC optimize("unroll-loops") +#pragma GCC target("avx2") +#pragma GCC target("bmi,bmi2,popcnt,lzcnt") + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} +using pd = pair; + +pd operator+(pd a, pd b) { + pd fds = {a.first + b.first, a.second + b.second}; + return fds; +} + +pd operator-(pd a, pd b) { + pd fds = {a.first - b.first, a.second - b.second}; + return fds; +} + +pd operator/(pd a, double f) { + return make_pair(a.first / f, a.second / f); +} + +pd operator*(pd a, double f) { + return make_pair(a.first * f, a.second * f); +} + +pd rotate90(pd a) { + return pd(-a.second, a.first); +} + +double operator^(pd a, pd b) { + return a.first * b.second - a.second * b.first; +} + +struct line { + pd p, q; +}; + +double get_t(pd v, line r) { + return (r.p^r.q) / ((r.p - r.q)^v); +} + +double EPS = 1e-6; +double inf = 1e18; +pd inter(line r, line s) +{ + if (abs((r.p - r.q) ^ (s.p - s.q)) < EPS) { + return pd(inf, inf); + } + + r.q = r.q - r.p, s.p = s.p - r.p, s.q = s.q - r.p; + return (r.q * get_t(r.q, s)) + r.p; +} + +pd getcenter(pd a, pd b, pd c) { + b = (a + b) / 2; + c = (a + c) / 2; + return inter(line(b, b + rotate90(a - b)), line(c, c + rotate90(a - c))); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> points(n); + + repv(i, points) { + cin >> i.first >> i.second; + } + + V disses(n, 1e16); + + auto dis = [&](pair &a, pair &b) { + double x = a.first - b.first; + double y = a.second - b.second; + return sqrt(x * x + y * y); + }; + + V lol(n); + + auto calc = [&](pair center) { + rep(i, n) { + lol[i] = dis(center, points[i]); + } + }; + + auto search = [&](pair point) { + sortv(lol); + + nrep(i, 1, n) { + rmin(disses[i], lol[i]); + } + }; + + rep(i, n - 1) { + nrep(j, i + 1, n) { + pair center = {(points[i].first + points[j].first) / 2, (points[i].second + points[j].second) / 2}; + calc(center); + search(center); + } + } + + rep(i, n - 2) { + nrep(j, i + 1, n - 1) { + nrep(k, j + 1, n) { + auto center = getcenter(points[i], points[j], points[k]); + calc(center); + search(center); + } + } + } + + int act = 1; + nrep(i, 1, n) { + rep(j, act) { + cout << fixed << setprecision(8) << disses[i] << '\n'; + } + act++; + } +} diff --git a/2024 (ICPC) Jiangxi Provincial Contest -- Official Contest/L. Campus.cpp b/2024 (ICPC) Jiangxi Provincial Contest -- Official Contest/L. Campus.cpp new file mode 100644 index 0000000..f18a706 --- /dev/null +++ b/2024 (ICPC) Jiangxi Provincial Contest -- Official Contest/L. Campus.cpp @@ -0,0 +1,231 @@ +/* Problem URL: https://codeforces.com/gym/105231/problem/L */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k, t; + cin >> n >> m >> k >> t; + + vl fds(n); + cin >> fds; + + V> gates; + + rep(i, k) { + int n, l, r; + cin >> n >> l >> r; + n--; + + gates.emplace_back(n, l, r); + } + + ll inf = INT64_MAX >> 1; + vvl dis(n, vl(k, inf)); + + V>> graph(n); + + auto dijkstra = [&](int i, int k) { + priority_queue, V>, greater<>> pq; + + dis[i][k] = 0; + pq.emplace(0, i); + + while (!pq.empty()) { + auto [c, now] = pq.top(); + pq.pop(); + + if (c > dis[now][k]) { + continue; + } + + for (auto [j, cost] : graph[now]) { + if (dis[j][k] > cost + c) { + dis[j][k] = cost + c; + pq.emplace(cost + c, j); + } + } + } + }; + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + a--, b--; + + graph[a].emplace_back(b, c); + graph[b].emplace_back(a, c); + } + + rep(i, k) { + dijkstra(get<0>(gates[i]), i); + } + + vi pos(t + 2, 0); + for (auto [i, l, r] : gates) { + pos[l]++; + pos[r + 1]--; + } + + nrep(i, 1, t) { + pos[i] += pos[i - 1]; + } + + vl ans(t + 2, 0); + + rep(i, n) { + priority_queue> pq; + rep(j, k) { + pq.emplace(dis[i][j], j); + } + + set> inter; + while (!pq.empty()) { + auto [c, j] = pq.top(); + pq.pop(); + + auto [n, l, r] = gates[j]; + + auto itr = inter.begin(); + while (itr != inter.end()) { + auto [cost, tl, tr] = *itr; + if (l <= tl && r >= tr) { + inter.erase(make_tuple(cost, tl, tr)); + ans[tl] -= cost * fds[i]; + ans[tr + 1] += cost * fds[i]; + itr = inter.begin(); + continue; + } + + if (l >= tl && r <= tr) { + inter.erase(make_tuple(cost, tl, tr)); + ans[tl] -= cost * fds[i]; + ans[tr + 1] += cost * fds[i]; + if (l - 1 >= tl) { + inter.emplace(cost, tl, l - 1); + ans[tl] += cost * fds[i]; + ans[l] -= cost * fds[i]; + } + if (r + 1 <= tr) { + inter.emplace(cost, r + 1, tr); + ans[r + 1] += cost * fds[i]; + ans[tr + 1] -= cost * fds[i]; + } + itr = inter.begin(); + continue; + } + + if (l >= tl && l <= tr) { + inter.erase(make_tuple(cost, tl, tr)); + ans[tl] -= cost * fds[i]; + ans[tr + 1] += cost * fds[i]; + if (l - 1 >= tl) { + inter.emplace(cost, tl, l - 1); + ans[tl] += cost * fds[i]; + ans[l] -= cost * fds[i]; + } + itr = inter.begin(); + continue; + } + + if (r >= tl && r <= tr) { + inter.erase(make_tuple(cost, tl, tr)); + ans[tl] -= cost * fds[i]; + ans[tr + 1] += cost * fds[i]; + if (r + 1 <= tr) { + inter.emplace(cost, r + 1, tr); + ans[r + 1] += cost * fds[i]; + ans[tr + 1] -= cost * fds[i]; + } + itr = inter.begin(); + continue; + } + + itr++; + } + + inter.emplace(c, l, r); + ans[l] += c * fds[i]; + ans[r + 1] -= c * fds[i]; + } + } + + for (int i = 1; i <= t; i++) { + ans[i] += ans[i - 1]; + if (!pos[i]) { + cout << "-1\n"; + continue; + } + cout << ans[i] << '\n'; + } +} diff --git a/2024 Aleppo Collegiate Programming Contest/I. Nested Circles.cpp b/2024 Aleppo Collegiate Programming Contest/I. Nested Circles.cpp new file mode 100644 index 0000000..5bfa626 --- /dev/null +++ b/2024 Aleppo Collegiate Programming Contest/I. Nested Circles.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/gym/105316/problem/I */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +double dis(ll x1, ll y1, ll x2, ll y2) +{ + ll x = x1 - x2; + ll y = y1 - y2; + return sqrt(x * x + y * y); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, q; + cin >> n >> q; + + V> points; + + while (n--) { + int x, y, z; + cin >> x >> y >> z; + + for (size_t i = 0; i <= z; i++) { + int yn = y + i; + int xn = x; + while (dis(xn, yn, x, y) <= z) { + points.emplace_back(xn, yn); + int dx = xn - x; + int dy = yn - y; + if (dy != 0) { + points.emplace_back(xn, y - dy); + } + if (dx != 0) { + points.emplace_back(x - dx, yn); + } + if (dx != 0 && dy != 0) { + points.emplace_back(x - dx, y - dy); + } + xn++; + } + } + } + + sortv(points); + pair prev = {-1e6, -1e6}; + int now = 1; + + V> act; + vi count; + + for (auto i : points) { + if (i == prev) { + now++; + continue; + } + + act.emplace_back(prev); + count.push_back(now); + now = 1; + prev = i; + } + + act.emplace_back(prev); + count.push_back(now); + + while (q--) { + int x, y; + cin >> x >> y; + + auto itr = lower_bound(act.begin(), act.end(), make_pair(x, y)); + if (itr == act.end() || *itr != make_pair(x, y)) { + cout << "0\n"; + continue; + } + + cout << count[itr - act.begin()] << '\n'; + } + } +} diff --git a/2024 ICPC Belarus Regional Contest/E. Enter the Museum.cpp b/2024 ICPC Belarus Regional Contest/E. Enter the Museum.cpp new file mode 100644 index 0000000..7ea7c12 --- /dev/null +++ b/2024 ICPC Belarus Regional Contest/E. Enter the Museum.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/gym/105535/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + vvi graph(n); + nrep(i, 1, n) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi ans; + int total = 0; + + function dfs = [&](int i, int p) { + fds[i]--; + total++; + + ans.push_back(i + 1); + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + if (!dfs(j, i) || fds[i] == 0) { + return false; + } + + total++; + fds[i]--; + ans.push_back(i + 1); + + while (fds[j] > 0) { + if (fds[i] == 0) { + return false; + } + + ans.push_back(j + 1); + ans.push_back(i + 1); + + fds[i]--; + fds[j]--; + total += 2; + } + } + + return true; + }; + + fds[0]++; + int check = accumulate(all(fds), 0); + if (!dfs(0, 0) || total != check) { + cout << "0\n"; + return 0; + } + + cout << ans; +} diff --git a/2024 ICPC Gran Premio de Mexico 1ra Fecha/B. Bacterial Sampling.cpp b/2024 ICPC Gran Premio de Mexico 1ra Fecha/B. Bacterial Sampling.cpp new file mode 100644 index 0000000..988b63c --- /dev/null +++ b/2024 ICPC Gran Premio de Mexico 1ra Fecha/B. Bacterial Sampling.cpp @@ -0,0 +1,182 @@ +/* Problem URL: https://codeforces.com/gym/105164/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll mod = 1e9 + 7; + +vvl operator*(vvl &a, vvl &b) +{ + vvl ans(a.size(), vl(b[0].size())); + + rep(i, a.size()) { + rep(j, b[0].size()) { + rep(k, a[0].size()) { + ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % mod; + } + } + } + + return ans; +} + +ll exp(int n) +{ + vvl ans = { + {1}, + {0}, + {0}, + {0}, + {0}, + {3}, + {0}, + {0}, + {0}, + {3}, + {0}, + {9}, + {0}, + {3}, + {0}, + {18}, + {0}, + {30}, + {0}, + {27} + }; + + if (n < 20) { + ll total = 0; + for (int i = 0; i <= n; i++) { + total += ans[i][0]; + total %= mod; + } + + return total; + } + + n -= 20; + + vvl tmp(20, vl(20)); + + rep(i, 19) { + tmp[i][i + 1] = 1; + } + + for (int j = 20 - 6; j >= 0; j -= 4) { + tmp[19][j] = 3; + } + + rep(i, 31) { + if ((n >> i) & 1) { + ans = tmp * ans; + } + tmp = tmp * tmp; + } + + ll total = 0; + rep(i, 20) { + total += ans[i][0]; + total %= mod; + } + + return total; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + // vl dp(n + 1); + // dp[0] = 1; + // + // for (int i = 1; i <= n; i++) { + // for (int j = i - 6; j >= 0 && abs(i - j) <= 20; j -= 4) { + // dp[i] += dp[j] * 3; + // dp[i] %= mod; + // } + // } + // + // ll total = 0; + // for (int i = max(n - 19, 0); i <= n; i++) { + // total += dp[i]; + // total %= mod; + // } + + cout << exp(n) << '\n'; + // cout << total << '\n'; + } +} diff --git a/2024 ICPC Gran Premio de Mexico 2da Fecha/D. Dueling Digits.cpp b/2024 ICPC Gran Premio de Mexico 2da Fecha/D. Dueling Digits.cpp new file mode 100644 index 0000000..aad3885 --- /dev/null +++ b/2024 ICPC Gran Premio de Mexico 2da Fecha/D. Dueling Digits.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/gym/105216/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll mod = 1e9 + 7; +int dest = 7200; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vvl dp(801, vl(7201 * 2)); + dp[0][dest] = 1; + + for (int i = 0; i <= 14400; i++) { + for (int j = 1; j <= 8; j++) { + if (i - j >= 0) { + dp[1][i] += dp[0][i - j] * (9 - j); + } + if (i + j <= 14400) { + dp[1][i] += dp[0][i + j] * (9 - j); + } + dp[1][i] %= mod; + } + } + + for (int i = 2; i <= 800; i++) { + for (int j = 0; j <= 14400; j++) { + for (int k = 1; k <= 9; k++) { + if (j - k >= 0) { + dp[i][j] += dp[i - 1][j - k] * (10 - k); + } + if (j + k <= 14400) { + dp[i][j] += dp[i - 1][j + k] * (10 - k); + } + dp[i][j] %= mod; + } + } + } + + int q; + cin >> q; + while (q--) { + int n; + cin >> n; + + cout << dp[n][dest] << '\n'; + } +} diff --git a/2024 ICPC Gran Premio de Mexico 2da Fecha/E. Egotistical Command Chain.cpp b/2024 ICPC Gran Premio de Mexico 2da Fecha/E. Egotistical Command Chain.cpp new file mode 100644 index 0000000..4b2c4ba --- /dev/null +++ b/2024 ICPC Gran Premio de Mexico 2da Fecha/E. Egotistical Command Chain.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/gym/105216/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> fds(n); + rep(i, n) { + fds[i].second = i; + cin >> fds[i].first; + } + + V> ans; + vvi graph(n); + vi roots; + + int total = 0; + + sort(all(fds)); + rep(i, n) { + int now = fds[i].first - 1; + + if (total < now) { + cout << "-1\n"; + return 0; + } + + total++; + rep(j, now) { + ans.emplace_back(fds[i].second + 1, fds[j].second + 1); + } + } + + cout << ans.size() << '\n'; + for (auto [i, j] : ans) { + cout << i << ' ' << j << '\n'; + } +} diff --git a/2024 ICPC Gran Premio de Mexico 2da Fecha/J. Japanese Samurai Fight.cpp b/2024 ICPC Gran Premio de Mexico 2da Fecha/J. Japanese Samurai Fight.cpp new file mode 100644 index 0000000..8be2682 --- /dev/null +++ b/2024 ICPC Gran Premio de Mexico 2da Fecha/J. Japanese Samurai Fight.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/gym/105216/problem/J */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n == 1) { + cout << "NO\n"; + return 0; + } + + V> ans; + V> connect(n, V(n)); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + connect[a][b] = true; + connect[b][a] = true; + } + + cout << "YES\n"; + + rep(i, n / 2) { + nrep(j, i + 1, n / 2) { + if (!connect[i][j]) { + connect[i][j] = true; + connect[j][i] = true; + + ans.emplace_back(i + 1, j + 1); + } + } + } + + nrep(i, n / 2, n) { + nrep(j, i + 1, n) { + if (!connect[i][j]) { + connect[i][j] = true; + connect[j][i] = true; + + ans.emplace_back(i + 1, j + 1); + } + } + } + + cout << ans.size() << '\n'; + for (auto [i, j] : ans) { + cout << i << ' ' << j << '\n'; + } +} diff --git a/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/A. Amalgram.cpp b/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/A. Amalgram.cpp new file mode 100644 index 0000000..813b1ff --- /dev/null +++ b/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/A. Amalgram.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/gym/105446/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a, b; + cin >> a >> b; + + vi counta(26); + vi countb(26); + + repv(i, a) { + counta[i - 'a']++; + } + + repv(i, b) { + countb[i - 'a']++; + } + + rep(i, 26) { + int now = max(counta[i], countb[i]); + + while (now--) { + cout << (char)(i + 'a'); + } + } + cout << '\n'; +} diff --git a/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/D. Drone Control.cpp b/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/D. Drone Control.cpp new file mode 100644 index 0000000..63e0ee7 --- /dev/null +++ b/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/D. Drone Control.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/gym/105446/problem/D */ + +#include + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int q; + cin >> q; + while (q--) { + double p, r, y; + cin >> p >> r >> y; + + double low = -1.5; + double high = 1.5; + + while (high - low >= 1e-10) { + double third = (high - low) / 3; + double mid1 = low + third; + double mid2 = high - third; + + auto check = [&](double mid) { + double w = mid; + double s = (y - p - r) / 2 + w; + double n = r + s; + double e = p + w; + + return max({abs(w), abs(s), abs(n), abs(e)}); + }; + + if (check(mid1) < check(mid2)) { + high = mid1; + continue; + } + + if (check(mid1) > check(mid2)) { + low = mid2; + continue; + } + + low = mid1; + high = mid2; + } + + double w = low; + double s = (y - p - r) / 2 + w; + double n = r + s; + double e = p + w; + + cout << fixed << setprecision(6) << n << ' ' << e << ' ' << s << ' ' << w << '\n'; + } +} diff --git a/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/F. Finding Suspicious Proteins.cpp b/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/F. Finding Suspicious Proteins.cpp new file mode 100644 index 0000000..46d082a --- /dev/null +++ b/2024 United Kingdom and Ireland Programming Contest (UKIEPC 2024)/F. Finding Suspicious Proteins.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/gym/105446/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, l, k; + cin >> n >> l >> k; + + V vis(n); + + V rev(n); + + vvi s(n, vi(l)); + + rep(i, n) { + string fds; + cin >> fds; + rev[i] = fds; + + rep(j, l) { + cin >> s[i][j]; + } + } + + auto diff = [&](int i, int j) { + int ans = 0; + rep(f, l) { + ans += abs(s[i][f] - s[j][f]); + } + + return ans; + }; + + vi dis(n, INT32_MAX); + + vi choices; + int now = 0; + int maximal = 0; + + nrep(i, 1, n) { + int d = diff(0, i); + + if (d > maximal) { + maximal = d; + now = i; + } + } + + choices.push_back(now); + + nrep(i, 1, k) { + now = 0; + maximal = 0; + rep(j, n) { + rmin(dis[j], diff(choices.back(), j)); + + if (dis[j] > maximal) { + maximal = dis[j]; + now = j; + } + } + + choices.push_back(now); + } + + repv(i, choices) { + cout << rev[i] << '\n'; + } +} diff --git a/2024-2025 ICPC Brazil Subregional Programming Contest/A. Attention to the Meeting.cpp b/2024-2025 ICPC Brazil Subregional Programming Contest/A. Attention to the Meeting.cpp new file mode 100644 index 0000000..139a5ac --- /dev/null +++ b/2024-2025 ICPC Brazil Subregional Programming Contest/A. Attention to the Meeting.cpp @@ -0,0 +1,81 @@ +/* Problem URL: https://codeforces.com/gym/105327/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + cout << (k - (k / n) * n >= (n - 1) ? k / n : k / n - 1) << '\n'; +} diff --git a/2024-2025 ICPC Brazil Subregional Programming Contest/B. Bacon Number.cpp b/2024-2025 ICPC Brazil Subregional Programming Contest/B. Bacon Number.cpp new file mode 100644 index 0000000..7310193 --- /dev/null +++ b/2024-2025 ICPC Brazil Subregional Programming Contest/B. Bacon Number.cpp @@ -0,0 +1,239 @@ +/* Problem URL: https://codeforces.com/gym/105327/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi tree(n + m); + + vi dsu(n + m); + rep(i, dsu.size()) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a == b) { + return false; + } + + dsu[a] = b; + return true; + }; + + rep(i, n) { + int k; + cin >> k; + + while (k--) { + int j; + cin >> j; + j--; + + if (!join(i, j + n)) { + continue; + } + + tree[i].push_back(j + n); + tree[j + n].push_back(i); + } + } + + vvi parent(n + m, vi(25)); + vi depth(n + m, -1); + vi group(n + m, -1); + int g = 0; + V vis(n + m); + + function buildlca = [&](int i, int p) { + parent[i][0] = p; + depth[i] = depth[p] + 1; + group[i] = g; + + vis[i] = true; + + nrep(j, 1, 25) { + parent[i][j] = parent[parent[i][j - 1]][j - 1]; + } + + for (auto j : tree[i]) { + if (j == p) { + continue; + } + + buildlca(j, i); + } + }; + + rep(i, n + m) { + if (vis[i]) { + continue; + } + + buildlca(i, i); + g++; + } + + auto lca = [&](int a, int b) { + if (depth[a] > depth[b]) { + swap(a, b); + } + + int diff = depth[b] - depth[a]; + + rep(i, 25) { + if ((diff >> i) & 1) { + b = parent[b][i]; + } + } + + if (a == b) { + return a; + } + + for (int i = 24; i >= 0; i--) { + if (parent[a][i] == parent[b][i]) { + continue; + } + + a = parent[a][i]; + b = parent[b][i]; + } + + return parent[a][0]; + }; + + int q; + cin >> q; + while (q--) { + int a, b; + cin >> a >> b; + a--, b--; + + a += n; + b += n; + + if (group[a] != group[b]) { + cout << "-1\n"; + continue; + } + + int p = lca(a, b); + + vi ans1; + vi ans2; + + while (a != p) { + ans1.push_back(a + 1); + a = parent[a][0]; + } + + ans1.push_back(a + 1); + + while (b != p) { + ans2.push_back(b + 1); + b = parent[b][0]; + } + + reverse(all(ans2)); + + cout << (ans1.size() + ans2.size() + 1) / 2 << '\n'; + + repv(i, ans1) { + cout << i - (i > n ? n : 0) << ' '; + } + + repv(i, ans2) { + cout << i - (i > n ? n : 0) << ' '; + } + cout << '\n'; + } +} diff --git a/2024-2025 ICPC Brazil Subregional Programming Contest/E. Enigma of the Jewelry Case.cpp b/2024-2025 ICPC Brazil Subregional Programming Contest/E. Enigma of the Jewelry Case.cpp new file mode 100644 index 0000000..b312558 --- /dev/null +++ b/2024-2025 ICPC Brazil Subregional Programming Contest/E. Enigma of the Jewelry Case.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/gym/105327/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vvi fds(n, vi(n)); + cin >> fds; + + int ans = 0; + + auto valid = [&]() { + rep(i, n) { + nrep(j, 1, n) { + if (fds[i][j] <= fds[i][j - 1]) { + return false; + } + + if (fds[j][i] <= fds[j - 1][i]) { + return false; + } + } + } + + return true; + }; + + auto rotate = [&]() { + vvi tmp = fds; + + rep(i, n) { + rep(j, n) { + fds[i][j] = tmp[j][n - i - 1]; + } + } + }; + + while (!valid()) { + ans++; + rotate(); + } + + cout << ans << '\n'; +} diff --git a/2024-2025 ICPC Brazil Subregional Programming Contest/F. Fractions are better when continued.cpp b/2024-2025 ICPC Brazil Subregional Programming Contest/F. Fractions are better when continued.cpp new file mode 100644 index 0000000..7b156d9 --- /dev/null +++ b/2024-2025 ICPC Brazil Subregional Programming Contest/F. Fractions are better when continued.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://codeforces.com/gym/105327/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + ll a = 0; + ll b = 1; + + while (n--) { + swap(a, b); + b += a; + } + + cout << b << '\n'; +} diff --git a/2025 Argentinian Programming Tournament (TAP)/B. Block sum array.cpp b/2025 Argentinian Programming Tournament (TAP)/B. Block sum array.cpp new file mode 100644 index 0000000..4aba55e --- /dev/null +++ b/2025 Argentinian Programming Tournament (TAP)/B. Block sum array.cpp @@ -0,0 +1,141 @@ +/* Problem URL: https://codeforces.com/gym/106054/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + const ll mod = 998244353; + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + + rep(i, 31) { + if ((p >> i) & 1) { + ans *= a; + ans %= mod; + } + + a *= a; + a %= mod; + } + + return ans; + }; + + auto comb = [&](ll n, ll k) { + ll ans = 1; + ll div = 1; + + rep(i, k) { + ans *= (n - i); + ans %= mod; + + div *= (i + 1); + div %= mod; + } + + return (ans * fpow(div, mod - 2)) % mod; + }; + + vl b(n - k + 1); + cin >> b; + + ll ans = b[0]; + + rep(i, k) { + ll m = 0; + ll tmp = 0; + for (int j = i; j + 1 < b.size(); j += k) { + m += b[j] - b[j + 1]; + rmax(tmp, m); + } + + ans -= tmp; + } + + if (ans < 0) { + cout << "0\n"; + return 0; + } + + cout << comb(ans + k - 1, k - 1) << '\n'; +} diff --git a/8VC Venture Cup 2016 - Elimination Round/E. Simple Skewness.cpp b/8VC Venture Cup 2016 - Elimination Round/E. Simple Skewness.cpp new file mode 100644 index 0000000..959b52c --- /dev/null +++ b/8VC Venture Cup 2016 - Elimination Round/E. Simple Skewness.cpp @@ -0,0 +1,201 @@ +/* Problem URL: https://codeforces.com/contest/626/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +#define double long double + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + sortv(fds); + + pair median = {0, 0}; + double ans = 0; + int dev = 0; + + vl pref(n + 1); + rep(i, n) { + pref[i + 1] = pref[i] + fds[i]; + } + + auto search = [&](int med, int devi, int i) { + int low = 1; + int high = devi; + + ll ans = 0; + while (low <= high) { + int third = (high - low) / 3; + int mid1 = low + third; + int mid2 = high - third; + + double ans1 = (double)(pref[i + 1] - pref[i - mid1] + pref[n] - pref[n - mid1]) / (mid1 * 2 + med) - fds[i]; + double ans2 = (double)(pref[i + 1] - pref[i - mid2] + pref[n] - pref[n - mid2]) / (mid2 * 2 + med) - fds[i]; + + if (ans1 > ans2) { + ans = mid1; + high = mid2 - 1; + continue; + } + + if (ans2 > ans1) { + ans = mid2; + low = mid1 + 1; + continue; + } + + ans = mid1; + low = mid1 + 1; + high = mid2 - 1; + } + + return ans; + }; + auto search2 = [&](int med, int devi, int i) { + int low = 1; + int high = devi; + + ll ans = 0; + while (low <= high) { + int third = (high - low) / 3; + int mid1 = low + third; + int mid2 = high - third; + + double ans1 = (double)(pref[i + 2] - pref[i - mid1] + pref[n] - pref[n - mid1]) / (mid1 * 2 + med) - (fds[i] + fds[i + 1]) / 2.0L; + double ans2 = (double)(pref[i + 2] - pref[i - mid2] + pref[n] - pref[n - mid2]) / (mid2 * 2 + med) - (fds[i] + fds[i + 1]) / 2.0L; + + if (ans1 > ans2) { + ans = mid1; + high = mid2 - 1; + continue; + } + + if (ans2 > ans1) { + ans = mid2; + low = mid1 + 1; + continue; + } + + ans = mid1; + low = mid1 + 1; + high = mid2 - 1; + } + + return ans; + }; + + rep(i, n) { + int devi = min(i, n - i - 1); + + int best = search(1, devi, i); + double tmp = (double)(pref[i + 1] - pref[i - best] + pref[n] - pref[n - best]) / (best * 2 + 1) - fds[i]; + + if (tmp >= ans) { + ans = tmp; + median = {i, i}; + dev = best; + } + } + + rep(i, n - 1) { + int devi = min(i, n - i - 2); + + int best = search2(2, devi, i); + double tmp = (double)(pref[i + 2] - pref[i - best] + pref[n] - pref[n - best]) / (best * 2 + 2) - (fds[i] + fds[i + 1]) / 2.0L; + + if (tmp >= ans) { + ans = tmp; + median = {i, i + 1}; + dev = best; + } + } + + int size = dev * 2 + (median.first != median.second) + 1; + cout << size << '\n'; + + nrep(i, median.first - dev, median.first) { + cout << fds[i] << ' '; + } + cout << fds[median.first] << ' '; + if (median.first != median.second) { + cout << fds[median.second] << ' '; + } + + nrep(i, n - dev, n) { + cout << fds[i] << ' '; + } + cout << '\n'; +} diff --git a/Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)/B. Hamiiid, Haaamid... Hamid?.cpp b/Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)/B. Hamiiid, Haaamid... Hamid?.cpp new file mode 100644 index 0000000..c9cc3dd --- /dev/null +++ b/Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)/B. Hamiiid, Haaamid... Hamid?.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2127/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, x; + cin >> n >> x; + string a; + cin >> a; + x--; + + int l = -1; + rep(i, x) { + if (a[i] == '#') { + l = i; + } + } + + int r = n; + for (int i = n - 1; i > x; i--) { + if (a[i] == '#') { + r = i; + } + } + + cout << max(min(x + 1, n - r + 1), min(l + 2, n - x)) << '\n'; + } +} diff --git a/Codeforces Beta Round 19/E. Fairy.cpp b/Codeforces Beta Round 19/E. Fairy.cpp new file mode 100644 index 0000000..a24b42a --- /dev/null +++ b/Codeforces Beta Round 19/E. Fairy.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://codeforces.com/contest/19/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V>> graph(n); + + rep(i, m) { + int a, b; + cin >> a >> b; + a--, b--; + + + } +} diff --git a/Codeforces Beta Round 89 (Div. 2)/E. Bertown roads.cpp b/Codeforces Beta Round 89 (Div. 2)/E. Bertown roads.cpp new file mode 100644 index 0000000..c96a8af --- /dev/null +++ b/Codeforces Beta Round 89 (Div. 2)/E. Bertown roads.cpp @@ -0,0 +1,141 @@ +/* Problem URL: https://codeforces.com/contest/118/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + rep(i, m) { + int a, b; + cin >> a >> b; + a--, b--; + graph[a].push_back(b); + graph[b].push_back(a); + } + + vvi dfstree(n); + V> ans; + vi up(n); + vi down(n); + vi lvl(n); + + function make_tree = [&](int i, int d) { + lvl[i] = d; + + for (auto j : graph[i]) { + if (lvl[j] != 0) { + if (lvl[j] >= lvl[i]) { + ans.emplace_back(j, i); + down[i]++; + up[j]++; + } + continue; + } + + ans.emplace_back(i, j); + dfstree[i].push_back(j); + make_tree(j, d + 1); + } + }; + + make_tree(0, 1); + vi dp(n); + dp[0] = 1; + + function check = [&](int i) { + dp[i] += up[i]; + dp[i] -= down[i]; + + for (auto j : dfstree[i]) { + if (!check(j)) { + return false; + } + + dp[i] += dp[j]; + } + + return dp[i] != 0; + }; + + if (!check(0)) { + cout << "0\n"; + return 0; + } + + for (auto [u, v] : ans) { + cout << u + 1 << ' ' << v + 1 << '\n'; + } +} diff --git a/Codeforces Round 1027 (Div. 3)/D. Come a Little Closer.cpp b/Codeforces Round 1027 (Div. 3)/D. Come a Little Closer.cpp new file mode 100644 index 0000000..e2b9784 --- /dev/null +++ b/Codeforces Round 1027 (Div. 3)/D. Come a Little Closer.cpp @@ -0,0 +1,201 @@ +/* Problem URL: https://codeforces.com/contest/2114/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + V> fds(n); + repv(i, fds) { + cin >> i.first >> i.second; + } + + if (n == 1) { + cout << 1 << '\n'; + continue; + } + + ll inf = INT64_MAX >> 1; + priority_queue> minx; + minx.emplace(inf, inf); + minx.emplace(inf, inf); + priority_queue, V>, greater<>> maxx; + maxx.emplace(0, 0); + maxx.emplace(0, 0); + priority_queue> miny; + miny.emplace(inf, inf); + miny.emplace(inf, inf); + priority_queue, V>, greater<>> maxy; + maxy.emplace(0, 0); + maxy.emplace(0, 0); + + repv(i, fds) { + minx.emplace(i.first, i.second); + minx.pop(); + maxx.emplace(i.first, i.second); + maxx.pop(); + miny.emplace(i.second, i.first); + miny.pop(); + maxy.emplace(i.second, i.first); + maxy.pop(); + } + + pair minx2 = minx.top(); + minx.pop(); + pair minx1 = minx.top(); + + + pair maxx2 = maxx.top(); + maxx.pop(); + pair maxx1 = maxx.top(); + + + pair miny2 = miny.top(); + miny.pop(); + pair miny1 = miny.top(); + + pair maxy2 = maxy.top(); + maxy.pop(); + pair maxy1 = maxy.top(); + + ll ans = inf; + + rmin(ans, (maxx1.first - minx1.first + 1) * (maxy1.first - miny1.first + 1)); + + if ((maxx2.first - minx1.first + 1) * (maxy1.first - miny1.first + 1) <= n - 1) { + rmin(ans, (maxx2.first - minx1.first + 1) * (maxy1.first - miny1.first + 2)); + rmin(ans, (maxx2.first - minx1.first + 2) * (maxy1.first - miny1.first + 1)); + } else { + rmin(ans, (maxx2.first - minx1.first + 1) * (maxy1.first - miny1.first + 1)); + } + if ((maxx1.first - minx2.first + 1) * (maxy1.first - miny1.first + 1) <= n - 1) { + rmin(ans, (maxx1.first - minx2.first + 1) * (maxy1.first - miny1.first + 2)); + rmin(ans, (maxx1.first - minx2.first + 2) * (maxy1.first - miny1.first + 1)); + } else { + rmin(ans, (maxx1.first - minx2.first + 1) * (maxy1.first - miny1.first + 1)); + } + if ((maxx1.first - minx1.first + 1) * (maxy2.first - miny1.first + 1) <= n - 1) { + rmin(ans, (maxx1.first - minx1.first + 2) * (maxy2.first - miny1.first + 1)); + rmin(ans, (maxx1.first - minx1.first + 1) * (maxy2.first - miny1.first + 2)); + } else { + rmin(ans, (maxx1.first - minx1.first + 1) * (maxy2.first - miny1.first + 1)); + } + if ((maxx1.first - minx1.first + 1) * (maxy1.first - miny2.first + 1) <= n - 1) { + rmin(ans, (maxx1.first - minx1.first + 2) * (maxy1.first - miny2.first + 1)); + rmin(ans, (maxx1.first - minx1.first + 1) * (maxy1.first - miny2.first + 2)); + } else { + rmin(ans, (maxx1.first - minx1.first + 1) * (maxy1.first - miny2.first + 1)); + } + + if (maxx1.first == maxy1.second && maxx1.second == maxy1.first) { + if ((maxx2.first - minx1.first + 1) * (maxy2.first - miny1.first + 1) > n - 1) { + rmin(ans, (maxx2.first - minx1.first + 1) * (maxy2.first - miny1.first + 1)); + } else { + rmin(ans, (maxx2.first - minx1.first + 1) * (maxy2.first - miny1.first + 2)); + rmin(ans, (maxx2.first - minx1.first + 2) * (maxy2.first - miny1.first + 1)); + } + } + if (minx1.first == miny1.second && minx1.second == miny1.first) { + if ((maxx1.first - minx2.first + 1) * (maxy1.first - miny2.first + 1) > n - 1) { + rmin(ans, (maxx1.first - minx2.first + 1) * (maxy1.first - miny2.first + 1)); + } else { + rmin(ans, (maxx1.first - minx2.first + 1) * (maxy1.first - miny2.first + 2)); + rmin(ans, (maxx1.first - minx2.first + 2) * (maxy1.first - miny2.first + 1)); + } + } + + if (minx1.first == maxy1.second && minx1.second == maxy1.first) { + if ((maxx1.first - minx2.first + 1) * (maxy2.first - miny1.first + 1) > n - 1) { + rmin(ans, (maxx1.first - minx2.first + 1) * (maxy2.first - miny1.first + 1)); + } else { + rmin(ans, (maxx1.first - minx2.first + 2) * (maxy2.first - miny1.first + 1)); + rmin(ans, (maxx1.first - minx2.first + 1) * (maxy2.first - miny1.first + 2)); + } + } + if (maxx1.first == miny1.second && maxx1.second == miny1.first) { + if ((maxx2.first - minx1.first + 1) * (maxy1.first - miny2.first + 1) > n - 1) { + rmin(ans, (maxx2.first - minx1.first + 1) * (maxy1.first - miny2.first + 1)); + } else { + rmin(ans, (maxx2.first - minx1.first + 2) * (maxy1.first - miny2.first + 1)); + rmin(ans, (maxx2.first - minx1.first + 1) * (maxy1.first - miny2.first + 2)); + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1027 (Div. 3)/E. Kirei Attacks the Estate.cpp b/Codeforces Round 1027 (Div. 3)/E. Kirei Attacks the Estate.cpp new file mode 100644 index 0000000..a35e505 --- /dev/null +++ b/Codeforces Round 1027 (Div. 3)/E. Kirei Attacks the Estate.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/contest/2114/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vvl dp(n, vl(2, 0)); + + vi parent(n); + parent[0] = -1; + + function make_p = [&](int i, int p) { + parent[i] = p; + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + make_p(j, i); + } + }; + + make_p(0, -1); + vl ans(n); + + V vis(n, false); + + function calc = [&](int i) { + if (i == 0) { + ans[i] = fds[i]; + dp[i][0] = fds[i]; + dp[i][1] = -fds[i]; + return; + } + + if (vis[i]) { + return; + } + + vis[i] = true; + dp[i][0] = fds[i]; + dp[i][1] = -fds[i]; + + calc(parent[i]); + + rmax(dp[i][0], dp[i][0] + dp[parent[i]][1]); + rmax(dp[i][1], dp[i][1] + dp[parent[i]][0]); + + ans[i] = dp[i][0]; + }; + + rep(i, n) { + calc(i); + } + + cout << ans; + } +} diff --git a/Codeforces Round 834 (Div. 3)/A. Yes-Yes?.cpp b/Codeforces Round 834 (Div. 3)/A. Yes-Yes?.cpp new file mode 100644 index 0000000..f31d616 --- /dev/null +++ b/Codeforces Round 834 (Div. 3)/A. Yes-Yes?.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/contest/1759/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a; + cin >> a; + + bool yes = true; + for (size_t i = 0; i < a.size() && yes; i++) { + if (a[i] == 'Y') { + if (i + 1 < a.size() && a[i + 1] != 'e') { + yes = false; + continue; + } + if (i + 2 < a.size() && a[i + 2] != 's') { + yes = false; + continue; + } + } else if (a[i] == 'e') { + if (i + 1 < a.size() && a[i + 1] != 's') { + yes = false; + continue; + } + if (i + 2 < a.size() && a[i + 2] != 'Y') { + yes = false; + continue; + } + } else if (a[i] == 's') { + if (i + 1 < a.size() && a[i + 1] != 'Y') { + yes = false; + continue; + } + if (i + 2 < a.size() && a[i + 2] != 'e') { + yes = false; + continue; + } + } else { + yes = false; + } + } + + cout << (yes ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 834 (Div. 3)/B. Lost Permutation.cpp b/Codeforces Round 834 (Div. 3)/B. Lost Permutation.cpp new file mode 100644 index 0000000..584d8ad --- /dev/null +++ b/Codeforces Round 834 (Div. 3)/B. Lost Permutation.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/contest/1759/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int m, s; + cin >> m >> s; + + ll total = 0; + int maximus = 0; + while (m--) { + int c; + cin >> c; + total += c; + rmax(maximus, c); + } + + ll perm = maximus * (maximus + 1) / 2; + s -= perm - total; + + for (size_t i = maximus + 1; i <= s; i++) { + s -= i; + } + + cout << (s == 0 ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 834 (Div. 3)/C. Thermostat.cpp b/Codeforces Round 834 (Div. 3)/C. Thermostat.cpp new file mode 100644 index 0000000..c90de92 --- /dev/null +++ b/Codeforces Round 834 (Div. 3)/C. Thermostat.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/contest/1759/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll l, r, x; + cin >> l >> r >> x; + ll a, b; + cin >> a >> b; + + if (a == b) { + cout << "0\n"; + continue; + } + + if (abs(a - b) >= x) { + cout << "1\n"; + continue; + } + + if (a > b) { + swap(a, b); + } + + if (b - l >= x && a - l >= x) { + cout << "2\n"; + continue; + } + + if (r - b >= x && r - a >= x) { + cout << "2\n"; + continue; + } + + if (r - l >= x && ((b - l >= x && r - a >= x) || (r - b >= x && a - l >= x))) { + cout << "3\n"; + continue; + } + + cout << "-1\n"; + } +} diff --git a/Codeforces Round 834 (Div. 3)/D. Make It Round.cpp b/Codeforces Round 834 (Div. 3)/D. Make It Round.cpp new file mode 100644 index 0000000..c26fb42 --- /dev/null +++ b/Codeforces Round 834 (Div. 3)/D. Make It Round.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/1759/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, m; + cin >> n >> m; + + ll fives = 0; + ll twos = 0; + + ll tmp = n; + while (tmp % 5 == 0) { + tmp /= 5; + fives++; + } + + while (tmp % 2 == 0) { + tmp /= 2; + twos++; + } + + tmp = n; + while (tmp % 10 == 0) { + tmp /= 10; + twos--; + fives--; + } + + ll now = 1; + while (twos > 0 && now * 5 <= m) { + now *= 5; + twos--; + } + + while (fives > 0 && now * 2 <= m) { + now *= 2; + fives--; + } + + while (now * 10 <= m) { + now *= 10; + } + + + cout << n * now * (m / now) << '\n'; + } +} diff --git a/Codeforces Round 1003 (Div. 4)/A. Skibidus and Amog'u.cpp b/Codeforces Round 1003 (Div. 4)/A. Skibidus and Amog'u.cpp new file mode 100644 index 0000000..4692496 --- /dev/null +++ b/Codeforces Round 1003 (Div. 4)/A. Skibidus and Amog'u.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://codeforces.com/contest/2065/problem/0 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + scanf("%d", &t); + while (t--) { + char fds[11]; + scanf("%s", fds); + + char *ptr = fds; + while (*ptr != 0) { + ptr++; + } + + ptr[-1] = 0; + ptr[-2] = 'i'; + + puts(fds); + } +} diff --git a/Codeforces Round 1003 (Div. 4)/B. Skibidus and Ohio.cpp b/Codeforces Round 1003 (Div. 4)/B. Skibidus and Ohio.cpp new file mode 100644 index 0000000..a97a287 --- /dev/null +++ b/Codeforces Round 1003 (Div. 4)/B. Skibidus and Ohio.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://codeforces.com/contest/2065/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a; + cin >> a; + + size_t size = a.size(); + nrep(i, 1, a.size()) { + if (a[i] == a[i - 1]) { + size = 1; + break; + } + } + + cout << size << '\n'; + } +} diff --git a/Codeforces Round 1003 (Div. 4)/C1. Skibidus and Fanum Tax (easy version).cpp b/Codeforces Round 1003 (Div. 4)/C1. Skibidus and Fanum Tax (easy version).cpp new file mode 100644 index 0000000..0282119 --- /dev/null +++ b/Codeforces Round 1003 (Div. 4)/C1. Skibidus and Fanum Tax (easy version).cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2065/problem/C1 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + vi a(n); + cin >> a; + int num; + cin >> num; + + if (num - a[0] < a[0]) { + a[0] = num - a[0]; + } + + bool pos = true; + nrep(i, 1, n) { + if (num - a[i] < a[i] && num - a[i] >= a[i - 1]) { + a[i] = num - a[i]; + continue; + } + + if (a[i] < a[i - 1] && num - a[i] >= a[i - 1]) { + a[i] = num - a[i]; + continue; + } + + if (a[i] >= a[i - 1]) { + continue; + } + + pos = false; + break; + } + + if (pos) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } + } +} diff --git a/Codeforces Round 1003 (Div. 4)/C2. Skibidus and Fanum Tax (hard version).cpp b/Codeforces Round 1003 (Div. 4)/C2. Skibidus and Fanum Tax (hard version).cpp new file mode 100644 index 0000000..9189e95 --- /dev/null +++ b/Codeforces Round 1003 (Div. 4)/C2. Skibidus and Fanum Tax (hard version).cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/contest/2065/problem/C2 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + // cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + vl a(n); + cin >> a; + vl b(m); + cin >> b; + + sortv(b); + + if (b[0] - a[0] < a[0]) { + a[0] = b[0] - a[0]; + } + + bool pos = true; + nrep(i, 1, a.size()) { + int low = 0; + int high = b.size() - 1; + int ans = b.size(); + while (low <= high) { + int mid = (low + high) / 2; + + if (b[mid] - a[i] >= a[i - 1]) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + if (ans != b.size() && b[ans] - a[i] > a[i] && a[i] >= a[i - 1]) { + continue; + } + + if (ans != b.size() && b[ans] - a[i] >= a[i - 1]) { + a[i] = b[ans] - a[i]; + continue; + } + + if (a[i] >= a[i - 1]) { + continue; + } + + pos = false; + break; + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1003 (Div. 4)/D. Skibidus and Sigma.cpp b/Codeforces Round 1003 (Div. 4)/D. Skibidus and Sigma.cpp new file mode 100644 index 0000000..cd66d39 --- /dev/null +++ b/Codeforces Round 1003 (Div. 4)/D. Skibidus and Sigma.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://codeforces.com/contest/2065/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + V>> arrays(n, pair>(0, V(m))); + + for (auto &[num, arr] : arrays) { + for (auto &i : arr) { + cin >> i; + num += i; + } + } + + sort(arrays.begin(), arrays.end(), greater<>()); + + ll total = 0; + ll pref = 0; + for (auto &[num, arr] : arrays) { + for (auto i : arr) { + pref += i; + total += pref; + } + } + + cout << total << '\n'; + } +} diff --git a/Codeforces Round 1003 (Div. 4)/E. Skibidus and Rizz.cpp b/Codeforces Round 1003 (Div. 4)/E. Skibidus and Rizz.cpp new file mode 100644 index 0000000..9387da0 --- /dev/null +++ b/Codeforces Round 1003 (Div. 4)/E. Skibidus and Rizz.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2065/problem/E */ + +#include + +using namespace std; +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int x, y, z; + cin >> x >> y >> z; + + if (z < abs(x - y) || z > max(x, y)) { + cout << "-1\n"; + continue; + } + + string ans; + int zeroes = x > y ? 1 : 0; + + if (zeroes) { + rep(i, z) { + ans.push_back('0'); + x--; + } + } else { + rep(i, z) { + ans.push_back('1'); + y--; + } + } + + int lim = x + y - z; + zeroes ^= 1; + + while (x > 0 && y > 0) { + if (zeroes) { + ans.push_back('0'); + x--; + } else { + ans.push_back('1'); + y--; + } + zeroes ^= 1; + } + + while (x > 0) { + ans.push_back('0'); + x--; + } + while (y > 0) { + ans.push_back('1'); + y--; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1003 (Div. 4)/F. Skibidus and Slay.cpp b/Codeforces Round 1003 (Div. 4)/F. Skibidus and Slay.cpp new file mode 100644 index 0000000..a0185cb --- /dev/null +++ b/Codeforces Round 1003 (Div. 4)/F. Skibidus and Slay.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/contest/2065/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi color(n + 1); + rep(i, n) { + cin >> color[i + 1]; + } + + vvi graph(n + 1); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi parent(n + 1); + + string ans(n, '0'); + function dfs = [&](int i, int p){ + if (color[i] == color[p]) { + ans[color[i] - 1] = '1'; + } + + if (color[i] == color[parent[p]]) { + ans[color[i] - 1] = '1'; + } + + parent[i] = p; + + map count; + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + count[color[j]]++; + dfs(j, i); + } + + for (auto &i : count) { + if (i.second >= 2) { + ans[i.first - 1] = '1'; + } + } + }; + + dfs(1, 0); + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1003 (Div. 4)/G. Skibidus and Capping.cpp b/Codeforces Round 1003 (Div. 4)/G. Skibidus and Capping.cpp new file mode 100644 index 0000000..6605474 --- /dev/null +++ b/Codeforces Round 1003 (Div. 4)/G. Skibidus and Capping.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/contest/2065/problem/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + V isprime(2e5 + 1, true); + isprime[1] = false; + vi divs(2e5 + 1); + divs[1] = 1; + + for (int i = 2; i <= (int)2e5; i++) { + if (!isprime[i]) { + continue; + } + + divs[i] = i; + + for (int j = i * 2; j <= (int)2e5; j += i) { + isprime[j] = false; + divs[j] = i; + } + } + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi nums; + ll primes = 0; + ll ans = 0; + vl count(n + 1); + rep(i, n) { + int num; + cin >> num; + if (isprime[num]) { + ans += primes; + primes++; + ans -= count[num]; + count[num]++; + continue; + } + + int a = divs[num]; + int b = num / a; + + if (!isprime[b]) { + continue; + } + + nums.push_back(num); + } + + for (auto i : nums) { + int a = divs[i]; + int b = i / a; + + count[i]++; + ans += count[a] + count[b] + count[i]; + if (a == b) { + ans -= count[a]; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1006 (Div. 3)/A. New World, New Me, New Array.cpp b/Codeforces Round 1006 (Div. 3)/A. New World, New Me, New Array.cpp new file mode 100644 index 0000000..ff40fdd --- /dev/null +++ b/Codeforces Round 1006 (Div. 3)/A. New World, New Me, New Array.cpp @@ -0,0 +1,90 @@ +/* Problem URL: https://codeforces.com/contest/2072/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k, p; + cin >> n >> k >> p; + + int ans = (n * p); + if ((ans >= k && k >= 0) || (-ans <= k && k < 0)) { + cout << (k == 0 ? 0 : abs(k / p) + min(1, abs(k % p))) << '\n'; + } else { + cout << -1 << '\n'; + } + } +} diff --git a/Codeforces Round 1006 (Div. 3)/B. Having Been a Treasurer in the Past, I Help Goblins Deceive.cpp b/Codeforces Round 1006 (Div. 3)/B. Having Been a Treasurer in the Past, I Help Goblins Deceive.cpp new file mode 100644 index 0000000..f4dca82 --- /dev/null +++ b/Codeforces Round 1006 (Div. 3)/B. Having Been a Treasurer in the Past, I Help Goblins Deceive.cpp @@ -0,0 +1,92 @@ +/* Problem URL: https://codeforces.com/contest/2072/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + string s; + cin >> s; + + map count; + repv(i, s) { + count[i]++; + } + + cout << count['-'] / 2 * (count['-'] / 2 + (count['-'] & 1)) * count['_'] << '\n'; + } +} diff --git a/Codeforces Round 1006 (Div. 3)/C. Creating Keys for StORages Has Become My Main Skill.cpp b/Codeforces Round 1006 (Div. 3)/C. Creating Keys for StORages Has Become My Main Skill.cpp new file mode 100644 index 0000000..c3d2946 --- /dev/null +++ b/Codeforces Round 1006 (Div. 3)/C. Creating Keys for StORages Has Become My Main Skill.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/contest/2072/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, x; + cin >> n >> x; + + if (n == 1) { + cout << x << '\n'; + continue; + } + + int pop = x == 0 ? 0 : __builtin_popcount(x); + + int i = 0; + ll num = 0; + for (i = 0; i < n - 1 && (x | i) == x; i++) { + cout << i << ' '; + num |= i; + } + + if (i == n - 1 && (x | i) == x && (num | i) == x) { + cout << i << ' '; + } else { + cout << x - num << ' '; + } + i++; + for (; i < n; i++) { + cout << 0 << ' '; + } + + cout << '\n'; + } +} diff --git a/Codeforces Round 1006 (Div. 3)/D. For Wizards, the Exam Is Easy, but I Couldn't Handle It.cpp b/Codeforces Round 1006 (Div. 3)/D. For Wizards, the Exam Is Easy, but I Couldn't Handle It.cpp new file mode 100644 index 0000000..5fc77c7 --- /dev/null +++ b/Codeforces Round 1006 (Div. 3)/D. For Wizards, the Exam Is Easy, but I Couldn't Handle It.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2072/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vi nums(n); + cin >> nums; + + int total = 0; + rep(i, n - 1) { + nrep(j, i + 1, n) { + if (nums[i] > nums[j]) { + total++; + } + } + } + + int best = total; + int l = 0; + int r = 0; + + rep(i, n - 1) { + int right = 0; + int inv = 0; + nrep(j, i + 1, n) { + if (nums[j] > nums[i]) { + right++; + } else if (nums[j] < nums[i]) { + inv++; + } + + if (total - inv + right < best) { + best = total - inv + right; + l = i; + r = j; + } + } + } + + if (best == total) { + cout << "1 1\n"; + continue; + } + + cout << l + 1 << ' ' << r + 1 << '\n'; + } +} diff --git a/Codeforces Round 1006 (Div. 3)/E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?.cpp b/Codeforces Round 1006 (Div. 3)/E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?.cpp new file mode 100644 index 0000000..47ef3fb --- /dev/null +++ b/Codeforces Round 1006 (Div. 3)/E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/contest/2072/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll k; + cin >> k; + + int x = 1; + int y = 0; + + V> staffs = {{0, 0}}; + + int eq = 1; + int total = 0; + + while (total + eq <= k) { + total += eq; + eq++; + staffs.emplace_back(x, y); + x++; + } + + x = -1; + y = 1; + while (total < k) { + x++; + eq = 1; + + while (total + eq <= k) { + total += eq; + eq++; + staffs.emplace_back(x, y); + y++; + } + } + + cout << staffs.size() << '\n'; + for (auto [x, y] : staffs) { + cout << x << ' ' << y << '\n'; + } + } +} diff --git a/Codeforces Round 1006 (Div. 3)/F. Goodbye, Banker Life.cpp b/Codeforces Round 1006 (Div. 3)/F. Goodbye, Banker Life.cpp new file mode 100644 index 0000000..17e5216 --- /dev/null +++ b/Codeforces Round 1006 (Div. 3)/F. Goodbye, Banker Life.cpp @@ -0,0 +1,92 @@ +/* Problem URL: https://codeforces.com/contest/2072/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + for (int i = 0; i < n; i++) { + if ((i & ((n - 1) - i)) > 0) { + cout << "0 "; + } else { + cout << k << ' '; + } + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1009 (Div. 3)/C. XOR and Triangle.cpp b/Codeforces Round 1009 (Div. 3)/C. XOR and Triangle.cpp new file mode 100644 index 0000000..9b6b921 --- /dev/null +++ b/Codeforces Round 1009 (Div. 3)/C. XOR and Triangle.cpp @@ -0,0 +1,91 @@ +/* Problem URL: https://codeforces.com/contest/2074/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + if (__builtin_popcount(n) == 1 || 32 - __builtin_clz(n) - __builtin_ctz(~n) == 0) { + cout << "-1\n"; + continue; + } + + int ans = (1 << (31 - __builtin_clz(n))) - 1; + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1013 (Div. 3)/A. Olympiad Date.cpp b/Codeforces Round 1013 (Div. 3)/A. Olympiad Date.cpp new file mode 100644 index 0000000..647ca28 --- /dev/null +++ b/Codeforces Round 1013 (Div. 3)/A. Olympiad Date.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/contest/2091/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + vi count(10, 0); + count[0] = 3; + count[1] = 1; + count[2] = 2; + count[3] = 1; + count[5] = 1; + + size_t ans = 0; + ll counting = 8; + int n; + cin >> n; + vi nums(n); + cin >> nums; + + rep(i, n) { + if (count[nums[i]] > 0) { + counting--; + count[nums[i]]--; + } + + if (counting == 0) { + ans = i + 1; + break; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1013 (Div. 3)/B. Team Training.cpp b/Codeforces Round 1013 (Div. 3)/B. Team Training.cpp new file mode 100644 index 0000000..4437eba --- /dev/null +++ b/Codeforces Round 1013 (Div. 3)/B. Team Training.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/contest/2091/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, x; + cin >> n >> x; + + int count = 0; + priority_queue pq; + while (n--) { + ll num; + cin >> num; + if (num >= x) { + count++; + continue; + } + pq.push(num); + } + + ll now = 0; + ll memb = 0; + while (!pq.empty()) { + ll cur = pq.top(); + pq.pop(); + + now = cur; + memb++; + + if (now * memb >= x) { + count++; + now = 0; + memb = 0; + } + } + + cout << count << '\n'; + } +} diff --git a/Codeforces Round 1013 (Div. 3)/C. Combination Lock.cpp b/Codeforces Round 1013 (Div. 3)/C. Combination Lock.cpp new file mode 100644 index 0000000..b904210 --- /dev/null +++ b/Codeforces Round 1013 (Div. 3)/C. Combination Lock.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/contest/2091/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + while (t--) { + int n; + cin >> n; + + if ((n & 1) == 0) { + cout << "-1\n"; + continue; + } + + for (size_t i = 2; i <= n; i += 2) { + cout << i << ' '; + } + for (size_t i = 1; i <= n; i += 2) { + cout << i << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1013 (Div. 3)/D. Place of the Olympiad.cpp b/Codeforces Round 1013 (Div. 3)/D. Place of the Olympiad.cpp new file mode 100644 index 0000000..b25b8c6 --- /dev/null +++ b/Codeforces Round 1013 (Div. 3)/D. Place of the Olympiad.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://codeforces.com/contest/2091/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, m, k; + cin >> n >> m >> k; + + if (n >= k) { + cout << "1\n"; + continue; + } + + ll bigger = k / n; + if (k % n > 0) { + bigger++; + } + + ll empty = m - bigger + 1; + cout << bigger / empty + min(1LL, bigger % empty) << '\n'; + } +} diff --git a/Codeforces Round 1013 (Div. 3)/E. Interesting Ratio.cpp b/Codeforces Round 1013 (Div. 3)/E. Interesting Ratio.cpp new file mode 100644 index 0000000..8e06865 --- /dev/null +++ b/Codeforces Round 1013 (Div. 3)/E. Interesting Ratio.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://codeforces.com/contest/2091/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vi primes(1e7 + 1, 0); + + for (size_t i = 2; i <= (size_t)1e7; i++) { + if (primes[i] != 0) { + continue; + } + + primes[i]++; + for (size_t j = i * 2; j <= (size_t)1e7; j += i) { + primes[j]++; + } + } + + vl count(1e7 + 1, 0); + for (size_t i = 2; i <= (size_t)1e7; i++) { + count[i] = count[i - 1]; + count[i] += primes[i]; + } + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + cout << count[n] << '\n'; + } +} diff --git a/Codeforces Round 1013 (Div. 3)/F. Igor and Mountain.cpp b/Codeforces Round 1013 (Div. 3)/F. Igor and Mountain.cpp new file mode 100644 index 0000000..06be89b --- /dev/null +++ b/Codeforces Round 1013 (Div. 3)/F. Igor and Mountain.cpp @@ -0,0 +1,82 @@ +/* Problem URL: https://codeforces.com/contest/2091/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, d; + cin >> n >> m >> d; + + V a; + cin >> a; +} diff --git a/Codeforces Round 1013 (Div. 3)/G. Gleb and Boating.cpp b/Codeforces Round 1013 (Div. 3)/G. Gleb and Boating.cpp new file mode 100644 index 0000000..05b0041 --- /dev/null +++ b/Codeforces Round 1013 (Div. 3)/G. Gleb and Boating.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://codeforces.com/contest/2091/problem/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + auto bfs = [&](ll k, ll s){ + queue> q; + q.emplace(k, k, 1); + + while (!q.empty()) { + auto [now, p, dir] = q.front(); + q.pop(); + } + }; + + while (t--) { + ll s, k; + cin >> s >> k; + + cout << dfs(0, k, 1, s) << endl; + } +} diff --git a/Codeforces Round 1016 (Div. 3)/A. Ideal Generator.cpp b/Codeforces Round 1016 (Div. 3)/A. Ideal Generator.cpp new file mode 100644 index 0000000..816e2a2 --- /dev/null +++ b/Codeforces Round 1016 (Div. 3)/A. Ideal Generator.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://codeforces.com/contest/2093/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int k; + cin >> k; + + if (k & 1) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } + } +} diff --git a/Codeforces Round 1016 (Div. 3)/B. Expensive Number.cpp b/Codeforces Round 1016 (Div. 3)/B. Expensive Number.cpp new file mode 100644 index 0000000..dce26b6 --- /dev/null +++ b/Codeforces Round 1016 (Div. 3)/B. Expensive Number.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/contest/2093/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a; + cin >> a; + + vi pref(a.size(), 0); + vi suf(a.size(), 0); + + nrep(i, 1, a.size()) { + if (a[i - 1] != '0') { + pref[i]++; + } + pref[i] += pref[i - 1]; + } + + for (int i = a.size() - 2; i >= 0; i--) { + suf[i]++; + suf[i] += suf[i + 1]; + } + + int ans = INT32_MAX; + rep(i, a.size()) { + if (a[i] == '0') { + continue; + } + rmin(ans, pref[i] + suf[i]); + } + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1016 (Div. 3)/C. Simple Repetition.cpp b/Codeforces Round 1016 (Div. 3)/C. Simple Repetition.cpp new file mode 100644 index 0000000..b8aba80 --- /dev/null +++ b/Codeforces Round 1016 (Div. 3)/C. Simple Repetition.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2093/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +bool prime(ll n) +{ + if (n == 1) { + return false; + } + + ll lim = sqrt(n); + for (ll i = 2; i <= lim; i++) { + if (n % i == 0) { + return false; + } + } + return true; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + set search; + search.insert(2); + search.insert(19); + search.insert(23); + while (t--) { + string n; + int k; + cin >> n >> k; + + size_t count = 0; + rep(i, n.size()) { + if (n[i] != '1') { + break; + } + count++; + } + + if (count == n.size() && search.count(count * k)) { + cout << "YES\n"; + continue; + } + + if (k == 1 && prime(stoll(n))) { + cout << "YES\n"; + continue; + } + + cout << "NO\n"; + } +} diff --git a/Codeforces Round 1016 (Div. 3)/D. Skibidi Table.cpp b/Codeforces Round 1016 (Div. 3)/D. Skibidi Table.cpp new file mode 100644 index 0000000..356e820 --- /dev/null +++ b/Codeforces Round 1016 (Div. 3)/D. Skibidi Table.cpp @@ -0,0 +1,217 @@ +/* Problem URL: https://codeforces.com/contest/2093/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +using pl = pair; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, q; + cin >> n >> q; + + while (q--) { + string op; + cin >> op; + + function find_by_cord = [&](int n, pl p1, pl p2, pl p3, pl p4, ll x, ll y, ll tx, ll ty) + { + if (n == 1) { + if (x == tx && y == ty) { + return p1.first; + } + if (x == tx && y + 1 == ty) { + return p2.first; + } + if (x + 1 == tx && y == ty) { + return p3.first; + } + return p4.first; + } + + ll add = 1LL << (n - 1); + ll mini; + ll add1 = (1LL << (2 * (n - 1) - 2)) - 1; + ll xn = 0; + ll yn = 0; + if (tx < x + add && ty < y + add) { + mini = p1.first; + xn = x; + yn = y; + } + + if (tx < x + add && ty >= y + add) { + mini = p2.first; + xn = x; + yn = y + add; + } + + if (ty < y + add && tx >= x + add) { + mini = p3.first; + xn = x + add; + yn = y; + } + + if (tx >= x + add && ty >= y + add) { + mini = p4.first; + xn = x + add; + yn = y + add; + } + + return find_by_cord(n - 1, make_pair(mini, mini + add1), + make_pair(mini + add1 * 3 + 3, mini + add1 * 4 + 3), + make_pair(mini + add1 * 2 + 2, mini + add1 * 3 + 2), + make_pair(mini + add1 + 1, mini + add1 * 2 + 1), + xn, yn, tx, ty); + }; + + function find_by_value = [&](int n, pl p1, pl p2, pl p3, pl p4, ll x, ll y, ll v){ + if (n == 1) { + if (v == p1.first) { + return make_pair(x, y); + } + if (v == p2.first) { + return make_pair(x, y + 1); + } + if (v == p3.first) { + return make_pair(x + 1, y); + } + return make_pair(x + 1, y + 1); + } + + ll add = 1LL << (n - 1); + ll mini; + ll add1 = (1LL << (2 * (n - 1) - 2)) - 1; + ll xn = 0; + ll yn = 0; + if (v >= p1.first && v <= p1.second) { + mini = p1.first; + xn = x; + yn = y; + } + + if (v >= p2.first && v <= p2.second) { + mini = p2.first; + xn = x; + yn = y + add; + } + + if (v >= p3.first && v <= p3.second) { + mini = p3.first; + xn = x + add; + yn = y; + } + + if (v >= p4.first && v <= p4.second) { + mini = p4.first; + xn = x + add; + yn = y + add; + } + + return find_by_value(n - 1, make_pair(mini, mini + add1), + make_pair(mini + add1 * 3 + 3, mini + add1 * 4 + 3), + make_pair(mini + add1 * 2 + 2, mini + add1 * 3 + 2), + make_pair(mini + add1 + 1, mini + add1 * 2 + 1), + xn, yn, v); + + }; + + if (op == "->") { + ll x, y; + cin >> x >> y; + + ll add = (1LL << (2 * n - 2)); + cout << find_by_cord(n, make_pair(1, add), + make_pair(add * 3 + 1, add * 4), + make_pair(add * 2 + 1, add * 3), + make_pair(add + 1, add * 2), + 1, 1, x, y); + cout << '\n'; + } else { + ll v; + cin >> v; + + ll add = (1LL << (2 * n - 2)); + auto [x, y] = find_by_value(n, make_pair(1, add), + make_pair(add * 3 + 1, add * 4), + make_pair(add * 2 + 1, add * 3), + make_pair(add + 1, add * 2), + 1, 1, v); + + cout << x << ' ' << y << '\n'; + } + } + } +} diff --git a/Codeforces Round 1016 (Div. 3)/E. Min Max MEX.cpp b/Codeforces Round 1016 (Div. 3)/E. Min Max MEX.cpp new file mode 100644 index 0000000..8dc3a30 --- /dev/null +++ b/Codeforces Round 1016 (Div. 3)/E. Min Max MEX.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/contest/2093/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vvi act(n); + rep(i, n) { + ll num; + cin >> num; + + if (num >= n) { + continue; + } + + act[num].push_back(i); + } + + int ans = 0; + int l = 0; + int h = n - 1; + while (l <= h) { + int mid = (l + h) / 2; + + set ms; + int count = 0; + + int now = -1; + rep(i, k) { + int tmp = now; + bool stop = false; + for (int j = 0; j <= mid; j++) { + auto itr = upper_bound(act[j].begin(), act[j].end(), now); + if (itr == act[j].end()) { + stop = true; + break; + } + rmax(tmp, *itr); + } + + if (stop) { + break; + } + + count++; + now = tmp; + } + + // cout << mid << ' ' << count << '\n'; + if (count >= k) { + ans = mid + 1; + l = mid + 1; + continue; + } + + h = mid - 1; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1016 (Div. 3)/F. Hackers and Neural Networks.cpp b/Codeforces Round 1016 (Div. 3)/F. Hackers and Neural Networks.cpp new file mode 100644 index 0000000..b191ad2 --- /dev/null +++ b/Codeforces Round 1016 (Div. 3)/F. Hackers and Neural Networks.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2093/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + V a(n); + cin >> a; + + V> b(m, V(n)); + cin >> b; + + int matched = 0; + V matches(n, false); + bool pos = true; + + int ans = 0; + + priority_queue> pq; + + rep(i, m) { + int count = 0; + rep(j, n) { + if (b[i][j] == a[j]) { + count++; + if (!matches[j]) { + matches[j] = true; + matched++; + } + } + } + pq.emplace(count, i); + } + + if (matched < n) { + cout << "-1\n"; + continue; + } + + cout << n + (n - pq.top().first) * 2 << '\n'; + } +} diff --git a/Codeforces Round 1016 (Div. 3)/G. Shorten the Array.cpp b/Codeforces Round 1016 (Div. 3)/G. Shorten the Array.cpp new file mode 100644 index 0000000..b030ea9 --- /dev/null +++ b/Codeforces Round 1016 (Div. 3)/G. Shorten the Array.cpp @@ -0,0 +1,171 @@ +/* Problem URL: https://codeforces.com/contest/2093/problem/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +struct node { + int nodes[2], count[2]; + + node(): count(0) { + fill(nodes, nodes + 2, 0); + } + + int& operator[](int i) { + return nodes[i]; + } +}; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi array(n); + cin >> array; + + V bits(1); + + auto add = [&](int i){ + int now = 0; + for (int j = 30; j >= 0; j--) { + int bit = (i >> j) & 1; + if (bits[now][bit] == 0) { + bits[now][bit] = bits.size(); + bits.emplace_back(); + } + bits[now].count[bit]++; + now = bits[now][bit]; + } + }; + + auto rem = [&](int i){ + int now = 0; + for (int j = 30; j >= 0; j--) { + int bit = (i >> j) & 1; + bits[now].count[bit]--; + if (bits[now].count[bit] == 0) { + bits[now][bit] = 0; + return; + } + now = bits[now][bit]; + } + }; + + auto search = [&](int i){ + int now = 0; + for (int j = 30; j >= 0; j--) { + int bit = (i >> j) & 1; + if (bits[now][!bit] == 0) { + if (((k >> j) & 1) == 1) { + return false; + } + now = bits[now][bit]; + continue; + } + + if (((k >> j) & 1) == 0) { + return true; + } + + now = bits[now][!bit]; + } + + return true; + }; + + int ans = INT32_MAX; + + int l = 0, r = 0; + add(array[0]); + while (r < array.size() && l <= r) { + if (search(array[r])) { + rmin(ans, r - l + 1); + rem(array[l]); + l++; + continue; + } + + r++; + if (r < array.size()) { + add(array[r]); + } + } + + if (r == array.size() && search(array[l])) { + rmin(ans, r - l + 1); + } + + cout << (ans == INT32_MAX ? -1 : ans) << '\n'; + } +} diff --git a/Codeforces Round 1017 (Div. 4)/A. Trippi Troppi.cpp b/Codeforces Round 1017 (Div. 4)/A. Trippi Troppi.cpp new file mode 100644 index 0000000..55d8fc3 --- /dev/null +++ b/Codeforces Round 1017 (Div. 4)/A. Trippi Troppi.cpp @@ -0,0 +1,85 @@ +/* Problem URL: https://codeforces.com/contest/2094/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a, b, c; + cin >> a >> b >> c; + + cout << a[0] << b[0] << c[0] << '\n'; + } +} diff --git a/Codeforces Round 1017 (Div. 4)/B. Bobritto Bandito.cpp b/Codeforces Round 1017 (Div. 4)/B. Bobritto Bandito.cpp new file mode 100644 index 0000000..491349a --- /dev/null +++ b/Codeforces Round 1017 (Div. 4)/B. Bobritto Bandito.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://codeforces.com/contest/2094/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m, l, r; + cin >> n >> m >> l >> r; + + while (l < 0 && n > m) { + l++; + n--; + } + + while (r > 0 && n > m) { + r--; + n--; + } + + cout << l << ' ' << r << '\n'; + } +} diff --git a/Codeforces Round 1017 (Div. 4)/C. Brr Brrr Patapim.cpp b/Codeforces Round 1017 (Div. 4)/C. Brr Brrr Patapim.cpp new file mode 100644 index 0000000..9f746bb --- /dev/null +++ b/Codeforces Round 1017 (Div. 4)/C. Brr Brrr Patapim.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/contest/2094/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + V exists(2 * n, false); + vvi perm(n, vi(n)); + cin >> perm; + + vi ans(2 * n); + + rep(i, n) { + ans[i + 1] = perm[0][i]; + exists[perm[0][i] - 1] = true; + } + + nrep(i, 1, n) { + rep(j, n) { + ans[i + j + 1] = perm[i][j]; + exists[perm[i][j] - 1] = true; + } + } + + rep(i, 2 * n) { + if (!exists[i]) { + ans[0] = i + 1; + } + } + + cout << ans; + } +} diff --git a/Codeforces Round 1017 (Div. 4)/D. Tung Tung Sahur.cpp b/Codeforces Round 1017 (Div. 4)/D. Tung Tung Sahur.cpp new file mode 100644 index 0000000..f7861b6 --- /dev/null +++ b/Codeforces Round 1017 (Div. 4)/D. Tung Tung Sahur.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/contest/2094/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string s, t; + cin >> s >> t; + + V> acts; + V> actt; + + char prev = 0; + int count = 0; + + rep(i, s.size()) { + if (prev == 0) { + prev = s[i]; + count = 1; + continue; + } + + if (prev != s[i]) { + acts.emplace_back(count, prev); + prev = s[i]; + count = 1; + continue; + } + + count++; + } + + acts.emplace_back(count, prev); + + prev = 0; + count = 0; + + rep(i, t.size()) { + if (prev == 0) { + prev = t[i]; + count = 1; + continue; + } + + if (prev != t[i]) { + actt.emplace_back(count, prev); + prev = t[i]; + count = 1; + continue; + } + + count++; + } + + actt.emplace_back(count, prev); + + if (acts.size() != actt.size()) { + cout << "NO\n"; + continue; + } + + bool pos = true; + rep(i, acts.size()) { + if (acts[i].second != actt[i].second) { + pos = false; + break; + } + + if (actt[i].first < acts[i].first || actt[i].first > acts[i].first * 2) { + pos = false; + break; + } + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1017 (Div. 4)/E. Boneca Ambalabu.cpp b/Codeforces Round 1017 (Div. 4)/E. Boneca Ambalabu.cpp new file mode 100644 index 0000000..9c0f25d --- /dev/null +++ b/Codeforces Round 1017 (Div. 4)/E. Boneca Ambalabu.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/contest/2094/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi nums(n); + cin >> nums; + + vvl bits(31, vl(2, 0)); + + repv(i, nums) { + rep(j, 31) { + int bit = (i >> j) & 1; + bits[j][bit]++; + } + } + + ll ans = 0; + repv(i, nums) { + ll attempt = 0; + rep(j, 31) { + int bit = (i >> j) & 1; + attempt += (bits[j][!bit] << j); + } + + rmax(ans, attempt); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1017 (Div. 4)/F. Trulimero Trulicina.cpp b/Codeforces Round 1017 (Div. 4)/F. Trulimero Trulicina.cpp new file mode 100644 index 0000000..f83b6b8 --- /dev/null +++ b/Codeforces Round 1017 (Div. 4)/F. Trulimero Trulicina.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/contest/2094/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m, k; + cin >> n >> m >> k; + + vvi ans(n, vi(m)); + + if (k == n) { + rep(i, m) { + int act = i % k; + rep(j, act) { + ans[j][i] = j + n - act + 1; + } + nrep(j, act, n) { + ans[j][i] = j - act + 1; + } + } + + cout << ans; + continue; + } + + if (k == m) { + rep(i, n) { + int act = i % k; + + rep(j, act) { + ans[i][j] = j + m - act + 1; + } + nrep(j, act, m) { + ans[i][j] = j - act + 1; + } + } + + cout << ans; + continue; + } + + if (m % k == 0) { + rep(i, n) { + rep(j, m) { + ans[i][j] = 1 + (m * i + j + (i & 1)) % k; + } + } + + cout << ans; + continue; + } + + rep(i, n) { + rep(j, m) { + ans[i][j] = 1 + (m * i + j) % k; + } + } + + cout << ans; + } +} diff --git a/Codeforces Round 1017 (Div. 4)/G. Chimpanzini Bananini.cpp b/Codeforces Round 1017 (Div. 4)/G. Chimpanzini Bananini.cpp new file mode 100644 index 0000000..29d5df4 --- /dev/null +++ b/Codeforces Round 1017 (Div. 4)/G. Chimpanzini Bananini.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/contest/2094/problem/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int q; + cin >> q; + list fds; + ll n = 0; + ll nums = 0; + ll total = 0; + ll act = 0; + bool rev = false; + while (q--) { + int op; + cin >> op; + if (op == 1) { + ll num = fds.back(); + if (rev) { + num = fds.front(); + } + total -= num * n; + total += nums - num; + total += num; + if (rev) { + fds.push_back(num); + fds.pop_front(); + } else { + fds.push_front(num); + fds.pop_back(); + } + cout << total << '\n'; + } else if (op == 2) { + ll tmp = total - nums; + total = act - tmp; + + rev = !rev; + + cout << total << '\n'; + } else { + ll num; + cin >> num; + + n++; + if (rev) { + fds.push_front(num); + } else { + fds.push_back(num); + } + act += num * n + nums; + nums += num; + total += num * n; + cout << total << '\n'; + } + } + } +} diff --git a/Codeforces Round 1017 (Div. 4)/H. La Vaca Saturno Saturnita.cpp b/Codeforces Round 1017 (Div. 4)/H. La Vaca Saturno Saturnita.cpp new file mode 100644 index 0000000..6eb4ab4 --- /dev/null +++ b/Codeforces Round 1017 (Div. 4)/H. La Vaca Saturno Saturnita.cpp @@ -0,0 +1,157 @@ +/* Problem URL: https://codeforces.com/contest/2094/problem/H */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int inf = INT32_MAX >> 1; + +static vvi vec(1e5 + 1); + +struct persistent_vector { + set used; + + void insert(int num, int i) { + used.insert(num); + vec[num].push_back(i); + } + + int get(int num, int i) { + auto itr = lower_bound(all(vec[num]), i); + if (itr == vec[num].end()) { + return inf; + } + + return *itr; + } + + ~persistent_vector() { + for (auto i : used) { + vec[i].clear(); + } + } +}; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vvi muls(1e5 + 1); + nrep(i, 2, 1e5 + 1) { + for (int j = i; j <= 1e5; j += i) { + muls[j].push_back(i); + } + } + + int t; + cin >> t; + while (t--) { + int n, q; + cin >> n >> q; + + vi nums(n); + cin >> nums; + + persistent_vector divs; + rep(i, n) { + divs.insert(nums[i], i); + } + + while (q--) { + ll k, l, r; + cin >> k >> l >> r; + + l--, r--; + + int now = l; + ll ans = 0; + while (now <= r) { + ll tmp = inf; + for (auto i : muls[k]) { + rmin(tmp, (ll)divs.get(i, now)); + } + rmin(tmp, r); + ans += k * (tmp - now); + while (k % nums[tmp] == 0) { + k /= nums[tmp]; + } + + now = tmp + 1; + ans += k; + + if (k == 1) { + ans += r - now + 1; + break; + } + } + + cout << ans << '\n'; + } + } +} diff --git a/Codeforces Round 1020 (Div. 3)/A. Dr. TC.cpp b/Codeforces Round 1020 (Div. 3)/A. Dr. TC.cpp new file mode 100644 index 0000000..f374e13 --- /dev/null +++ b/Codeforces Round 1020 (Div. 3)/A. Dr. TC.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://codeforces.com/contest/2106/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int fds; + string a; + cin >> fds >> a; + + int n = 0; + + int count = 0; + repv(i, a) { + if (i == '1') { + count++; + } + } + + rep(i, a.size()) { + if (a[i] == '1') { + n += count - 1; + } else { + n += count + 1; + } + } + + cout << n << '\n'; + } +} diff --git a/Codeforces Round 1020 (Div. 3)/B. St. Chroma.cpp b/Codeforces Round 1020 (Div. 3)/B. St. Chroma.cpp new file mode 100644 index 0000000..9c2745f --- /dev/null +++ b/Codeforces Round 1020 (Div. 3)/B. St. Chroma.cpp @@ -0,0 +1,92 @@ +/* Problem URL: https://codeforces.com/contest/2106/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, x; + cin >> n >> x; + + for (int i = 0; i < x; i++) { + cout << i << ' '; + } + + for (int i = n - 1; i >= x; i--) { + cout << i << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1020 (Div. 3)/C. Cherry Bomb.cpp b/Codeforces Round 1020 (Div. 3)/C. Cherry Bomb.cpp new file mode 100644 index 0000000..0229330 --- /dev/null +++ b/Codeforces Round 1020 (Div. 3)/C. Cherry Bomb.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/contest/2106/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, k; + cin >> n >> k; + + vl a(n); + cin >> a; + vl b(n); + cin >> b; + + ll tmp = -1; + bool pos = true; + rep(i, n) { + if (b[i] != -1) { + if (tmp != -1 && a[i] + b[i] != tmp) { + pos = false; + break; + } + tmp = a[i] + b[i]; + continue; + } + } + + for (int i = 0; i < n && tmp != -1 && pos; i++) { + if (a[i] > tmp) { + pos = false; + break; + } + } + + if (!pos) { + cout << "0\n"; + continue; + } + + if (tmp != -1) { + repv(i, a) { + if (tmp - i > k) { + pos = false; + break; + } + } + cout << (pos ? "1\n" : "0\n"); + continue; + } + + ll minimal = INT32_MAX; + ll maximal = 0; + + repv(i, a) { + rmin(minimal, i); + rmax(maximal, i); + } + + cout << k - (maximal - minimal) + 1 << '\n'; + } +} diff --git a/Codeforces Round 1020 (Div. 3)/D. Flower Boy.cpp b/Codeforces Round 1020 (Div. 3)/D. Flower Boy.cpp new file mode 100644 index 0000000..dfad2c9 --- /dev/null +++ b/Codeforces Round 1020 (Div. 3)/D. Flower Boy.cpp @@ -0,0 +1,136 @@ +/* Problem URL: https://codeforces.com/contest/2106/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + vl a(n); + cin >> a; + vl b(m); + cin >> b; + b.push_back(0); + + vi pref(m, -1); + vi suf(m, -1); + + int now = 0; + for (int i = 0; i < n && now < m; i++) { + if (a[i] >= b[now]) { + pref[now] = i; + now++; + } + } + + now = m - 1; + for (int i = n - 1; i >= 0 && now >= 0; i--) { + if (a[i] >= b[now]) { + suf[now] = i; + now--; + } + } + + ll ans = INT64_MAX >> 1; + + if (pref.back() != -1) { + cout << "0\n"; + continue; + } + + if (m == 1) { + cout << b[0] << '\n'; + continue; + } + + if (pref[m - 1] == -1 && pref[m - 2] != -1) { + rmin(ans, b[m - 1]); + } + + if (suf[1] != -1 && suf[0] == -1) { + rmin(ans, b[0]); + } + + for (int i = 1; i < m - 1; i++) { + if (pref[i - 1] != -1 && suf[i + 1] != -1 && pref[i - 1] < suf[i + 1]) { + rmin(ans, b[i]); + } + } + + cout << (ans == INT64_MAX >> 1 ? -1 : ans) << '\n'; + } +} diff --git a/Codeforces Round 1020 (Div. 3)/E. Wolf.cpp b/Codeforces Round 1020 (Div. 3)/E. Wolf.cpp new file mode 100644 index 0000000..af470ec --- /dev/null +++ b/Codeforces Round 1020 (Div. 3)/E. Wolf.cpp @@ -0,0 +1,181 @@ +/* Problem URL: https://codeforces.com/contest/2106/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, q; + cin >> n >> q; + + vi elem(n); + cin >> elem; + + vi pos(n + 1); + rep(i, n) { + pos[elem[i]] = i; + } + + while (q--) { + int l, r, x; + cin >> l >> r >> x; + l--, r--; + + if (pos[x] > r || pos[x] < l) { + cout << "-1 "; + continue; + } + + int greater = n - x; + int lesser = x - 1; + int cgreater = 0; + int clesser = 0; + int ans = 0; + + int low = l; + int high = r; + + bool valid = true; + + while (elem[(low + high) / 2] != x) { + int mid = (low + high) / 2; + + if (elem[mid] > x) { + if (pos[x] > mid) { + if (clesser) { + clesser--; + low = mid + 1; + continue; + } + + if (greater == 0 || lesser == 0) { + valid = false; + break; + } + + lesser--; + greater--; + cgreater++; + ans += 2; + + low = mid + 1; + continue; + } + + if (greater == 0) { + valid = false; + break; + } + + greater--; + high = mid - 1; + continue; + } + + if (pos[x] < mid) { + if (cgreater) { + cgreater--; + high = mid - 1; + continue; + } + + if (greater == 0 || lesser == 0) { + valid = false; + break; + } + + greater--; + lesser--; + clesser++; + ans += 2; + + high = mid - 1; + continue; + } + + if (lesser == 0) { + valid = false; + break; + } + + lesser--; + low = mid + 1; + } + + cout << (valid ? ans : -1) << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1020 (Div. 3)/F. Goblin.cpp b/Codeforces Round 1020 (Div. 3)/F. Goblin.cpp new file mode 100644 index 0000000..dd6c81c --- /dev/null +++ b/Codeforces Round 1020 (Div. 3)/F. Goblin.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://codeforces.com/contest/2106/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + string a; + cin >> n >> a; + + ll part1 = 0; + ll part2 = 0; + ll ans = 0; + + rep(i, a.size()) { + if (a[i] == '0') { + part1 += i; + part2 += n - i - 1; + } else { + swap(part1, part2); + part2 = 0; + part1++; + } + + rmax(ans, part1); + rmax(ans, part2); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1020 (Div. 3)/G1. Baudelaire (easy version).cpp b/Codeforces Round 1020 (Div. 3)/G1. Baudelaire (easy version).cpp new file mode 100644 index 0000000..b775718 --- /dev/null +++ b/Codeforces Round 1020 (Div. 3)/G1. Baudelaire (easy version).cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/contest/2106/problem/G1 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + cout << "? 1 1 1" << endl; + int ans; + cin >> ans; + + if (ans > 1 || ans < -1) { + cout << "? 2 1" << endl; + ans = 0; + } + + vi tmp(n); + nrep(i, 1, n) { + cout << "? 1 1 " << i + 1 << endl; + int now; + cin >> now; + + tmp[i] = now - ans; + } + + int one = 0; + if (ans == 0) { + cout << "? 2 1" << endl; + int fds; + cout << "? 1 1 1" << endl; + cin >> fds; + + if (fds == -2) { + one = -1; + } else { + one = 1; + } + } else { + one = ans; + } + + tmp[0] = one; + + cout << "! "; + repv(i, tmp) { + cout << ' ' << i; + } + cout << endl; + } +} diff --git a/Codeforces Round 1022 (Div. 2)/A. Permutation Warm-Up.cpp b/Codeforces Round 1022 (Div. 2)/A. Permutation Warm-Up.cpp new file mode 100644 index 0000000..5d9c305 --- /dev/null +++ b/Codeforces Round 1022 (Div. 2)/A. Permutation Warm-Up.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/contest/2108/problem/0 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vl dp(501, 0); + dp[1] = 1; + int count = 0; + int add = 1; + + nrep(i, 2, 501) { + dp[i] = dp[i - 1] + add; + count++; + if (count == 2) { + count = 0; + add++; + } + } + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + cout << dp[n] << '\n'; + } +} diff --git a/Codeforces Round 1022 (Div. 2)/B. SUMdamental Decomposition.cpp b/Codeforces Round 1022 (Div. 2)/B. SUMdamental Decomposition.cpp new file mode 100644 index 0000000..45c7340 --- /dev/null +++ b/Codeforces Round 1022 (Div. 2)/B. SUMdamental Decomposition.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2108/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, x; + cin >> n >> x; + + if (x == 0) { + if (n == 1) { + cout << "-1\n"; + continue; + } + + if (n & 1) { + cout << n + 3 << '\n'; + continue; + } + + cout << n << '\n'; + continue; + } + + int count = __builtin_popcountll(x); + + if (n <= count) { + cout << x << '\n'; + continue; + } + + n -= count; + + if (n & 1) { + if (count >= 2) { + cout << n + 1 + x << '\n'; + continue; + } + ll ans = 1; + while (x & ans) { + ans <<= 1; + } + + cout << n + ans * 2 + x - 1 << '\n'; + continue; + } + + cout << n + x << '\n'; + } +} diff --git a/Codeforces Round 1022 (Div. 2)/C. Neo's Escape.cpp b/Codeforces Round 1022 (Div. 2)/C. Neo's Escape.cpp new file mode 100644 index 0000000..ae79695 --- /dev/null +++ b/Codeforces Round 1022 (Div. 2)/C. Neo's Escape.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/contest/2108/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +struct func { + bool operator()(pair &a, pair &b) { + if (a.first == b.first) { + return a.second > b.second; + } + return a.first < b.first; + }; +}; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds; + ll prev = 0; + rep(i, n) { + ll now; + cin >> now; + + if (now == prev) { + continue; + } + + fds.push_back(now); + prev = now; + } + + int ans = 0; + rep(i, fds.size()) { + int count = 0; + if (i + 1 >= fds.size() || (i + 1 < fds.size() && fds[i + 1] < fds[i])) { + count++; + } + + if (i - 1 < 0 || (i - 1 >= 0 && fds[i - 1] < fds[i])) { + count++; + } + + if (count == 2) { + ans++; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1023 (Div. 2)/A. LRC and VIP.cpp b/Codeforces Round 1023 (Div. 2)/A. LRC and VIP.cpp new file mode 100644 index 0000000..8b62fc0 --- /dev/null +++ b/Codeforces Round 1023 (Div. 2)/A. LRC and VIP.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/contest/2107/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + vi pref(n); + vi suf(n); + + pref[0] = fds[0]; + suf.back() = fds.back(); + + nrep(i, 1, n) { + pref[i] = __gcd(pref[i - 1], fds[i]); + suf[n - i - 1] = __gcd(suf[n - i], fds[n - i - 1]); + } + + if (fds[0] != suf[1]) { + cout << "Yes\n"; + cout << "1"; + nrep(i, 1, n) { + cout << " 2"; + } + cout << '\n'; + continue; + } + + if (fds[n - 1] != pref[n - 2]) { + cout << "Yes\n"; + rep(i, n - 1) { + cout << "1 "; + } + cout << "2\n"; + continue; + } + + int choice = -1; + nrep(i, 1, n - 1) { + int act = __gcd(pref[i - 1], suf[i + 1]); + if (act != fds[i]) { + choice = i; + break; + } + + } + + if (choice != -1) { + cout << "Yes\n"; + rep(i, choice) { + cout << "1 "; + } + cout << "2"; + nrep(i, choice + 1, n) { + cout << " 1"; + } + cout << "\n"; + } else { + cout << "No\n"; + } + } +} diff --git a/Codeforces Round 1023 (Div. 2)/B. Apples in Boxes.cpp b/Codeforces Round 1023 (Div. 2)/B. Apples in Boxes.cpp new file mode 100644 index 0000000..7597aa6 --- /dev/null +++ b/Codeforces Round 1023 (Div. 2)/B. Apples in Boxes.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/contest/2107/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi fds(n); + int minimal = INT32_MAX; + int maximal = 0; + int prev = 0; + ll total = 0; + + repv(i, fds) { + cin >> i; + + rmin(minimal, i); + if (i >= maximal) { + prev = maximal; + maximal = i; + } + + total += i; + } + + if (max(maximal - 1, prev) - minimal > k || total % 2 == 0) { + cout << "Jerry\n"; + continue; + } + + cout << "Tom\n"; + } +} diff --git a/Codeforces Round 1023 (Div. 2)/C. Maximum Subarray Sum.cpp b/Codeforces Round 1023 (Div. 2)/C. Maximum Subarray Sum.cpp new file mode 100644 index 0000000..b97f7d2 --- /dev/null +++ b/Codeforces Round 1023 (Div. 2)/C. Maximum Subarray Sum.cpp @@ -0,0 +1,173 @@ +/* Problem URL: https://codeforces.com/contest/2107/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll inf = 1e17; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + ll k; + cin >> n >> k; + + string a; + cin >> a; + + vl fds(n); + rep(i, n) { + cin >> fds[i]; + if (a[i] == '0') { + fds[i] = -inf; + } + } + + if (n == 1) { + if (a[0] == '0') { + fds[0] = k; + } + + if (fds[0] == k) { + cout << "Yes\n" << k << "\n"; + continue; + } + + cout << "No\n"; + continue; + } + + ll total = 0; + ll maximus = 0; + rep(i, n) { + total += fds[i]; + rmax(total, 0LL); + rmax(maximus, total); + } + + if (maximus > k) { + cout << "No\n"; + continue; + } + + if (maximus == k) { + cout << "Yes\n"; + cout << fds; + continue; + } + + vl pref(n); + vl suf(n); + + pref[0] = fds[0]; + suf.back() = fds.back(); + nrep(i, 1, n) { + pref[i] = max(fds[i], fds[i] + pref[i - 1]); + suf[n - i - 1] = max(fds[n - i - 1], fds[n - i - 1] + suf[n - i]); + } + + + if (suf[1] <= k && a[0] == '0') { + cout << "Yes\n"; + fds[0] = k - max(suf[1], 0LL); + cout << fds; + continue; + } + + if (pref[n - 2] <= k && a[n - 1] == '0') { + cout << "Yes\n"; + fds[n - 1] = k - max(pref[n - 2], 0LL); + cout << fds; + continue; + } + + bool pos = false; + nrep(i, 1, n - 1) { + if (a[i] == '0') { + pos = true; + fds[i] = k - (max(pref[i - 1], 0LL) + max(suf[i + 1], 0LL)); + break; + } + } + + if (pos) { + cout << "Yes\n"; + cout << fds; + + continue; + } + + cout << "No\n"; + } +} diff --git a/Codeforces Round 1023 (Div. 2)/D. Apple Tree Traversing.cpp b/Codeforces Round 1023 (Div. 2)/D. Apple Tree Traversing.cpp new file mode 100644 index 0000000..1881e92 --- /dev/null +++ b/Codeforces Round 1023 (Div. 2)/D. Apple Tree Traversing.cpp @@ -0,0 +1,344 @@ +/* Problem URL: https://codeforces.com/contest/2107/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +#define MAX_N 150000 + +vvi graph; +int inf = INT32_MAX >> 1; +int n; +int parent[MAX_N]; +int vis[MAX_N]; +int dis[MAX_N]; +queue used; + +V> ans; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + fill(dis, dis + MAX_N, inf); + + int t; + cin >> t; + while (t--) { + cin >> n; + graph.assign(n, vi()); + fill(vis, vis + n, inf); + + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + ans.clear(); + + function solve = [&solve](int i, int t){ + int diam; + + auto getcenter = [&](int i) { + int inf = INT32_MAX; + + auto bfs = [&](int i) { + dis[i] = 0; + used.push(i); + + int ans = i; + + queue q; + q.push(i); + + while (!q.empty()) { + int i = q.front(); + q.pop(); + + for (auto j : graph[i]) { + if (vis[j] >= t && dis[j] > dis[i] + 1) { + used.push(j); + dis[j] = dis[i] + 1; + ans = j; + parent[j] = i; + q.push(j); + } + } + } + + return ans; + }; + + int now = bfs(i); + while (!used.empty()) { + dis[used.front()] = inf; + used.pop(); + } + now = bfs(now); + diam = dis[now] + 1; + + while (!used.empty()) { + dis[used.front()] = inf; + used.pop(); + } + + rep(i, diam / 2) { + now = parent[now]; + } + + return now; + }; + + int center = getcenter(i); + + if (diam == 1) { + ans.emplace_back(diam, i, i); + vis[i] = t; + return; + } + + if (diam == 2) { + int other = parent[center]; + + if (other < center) { + swap(other, center); + } + + vis[other] = t; + vis[center] = t; + + ans.emplace_back(diam, other, center); + return; + } + + function setvis = [&](int x, int y) { + int now = x; + while (now != -1) { + vis[now] = t; + now = parent[now]; + } + + now = y; + while (now != -1) { + vis[now] = t; + now = parent[now]; + } + }; + + function iamtiredmaster = [&](int i, int p) { + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + if (vis[j] < t) { + continue; + } + + if (vis[j] == t) { + iamtiredmaster(j, i); + continue; + } + + solve(j, t + 1); + } + }; + + if (diam % 2 == 0) { + function(int, int, int)> dfs = [&](int i, int p, int d) { + bool leaf = true; + parent[i] = p; + + pair ans = {-1, -1}; + + for (auto j : graph[i]) { + if (vis[j] < t || j == p) { + continue; + } + + leaf = false; + pair tmp = dfs(j, i, d + 1); + + rmax(ans.first, tmp.first); + rmax(ans.second, tmp.second); + } + + if (leaf) { + if (d == diam / 2) { + ans.first = i; + } else if (d == diam / 2 - 1) { + ans.second = i; + } + } + + return ans; + }; + + pair prev = {-1, -1}; + pair final = {0, 0}; + + for (auto j : graph[center]) { + if (vis[j] < t) { + continue; + } + + pair tmp = dfs(j, center, 1); + pair cont1 = {tmp.first, prev.second}; + pair cont2 = {tmp.second, prev.first}; + + if (cont1.first < cont1.second) { + swap(cont1.first, cont1.second); + } + + if (cont1.second == -1) { + cont1 = {-1, -1}; + } + + if (cont2.first < cont2.second) { + swap(cont2.first, cont2.second); + } + + if (cont2.second == -1) { + cont2 = {-1, -1}; + } + + rmax(prev.first, tmp.first); + rmax(prev.second, tmp.second); + + rmax(final, cont1); + rmax(final, cont2); + } + + ans.emplace_back(diam, final.first, final.second); + parent[center] = -1; + + vis[center] = t; + setvis(final.first, final.second); + iamtiredmaster(center, center); + return; + } + + function dfs = [&](int i, int p, int d) { + parent[i] = p; + if (d == diam / 2) { + return i; + } + + int ans = -1; + + for (auto j : graph[i]) { + if (vis[j] < t || j == p) { + continue; + } + + rmax(ans, dfs(j, i, d + 1)); + } + + return ans; + }; + + priority_queue> fds; + fds.push(0); + fds.push(0); + + for (auto j : graph[center]) { + if (vis[j] < t) { + continue; + } + + fds.push(dfs(j, center, 1)); + fds.pop(); + } + + pair tmp; + tmp.second = fds.top(); + fds.pop(); + tmp.first = fds.top(); + + ans.emplace_back(diam, tmp.first, tmp.second); + + vis[center] = t; + parent[center] = -1; + setvis(tmp.first, tmp.second); + iamtiredmaster(center, center); + }; + + solve(0, 0); + + sort(all(ans), greater<>()); + for (auto [diam, x, y] : ans) { + cout << diam << ' ' << x + 1 << ' ' << y + 1 << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1027 (Div. 3)/A. Square Year.cpp b/Codeforces Round 1027 (Div. 3)/A. Square Year.cpp new file mode 100644 index 0000000..5cb2ada --- /dev/null +++ b/Codeforces Round 1027 (Div. 3)/A. Square Year.cpp @@ -0,0 +1,92 @@ +/* Problem URL: https://codeforces.com/contest/2114/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int a; + cin >> a; + + int sq = sqrt(a); + + if (sq * sq != a) { + cout << "-1\n"; + continue; + } + + cout << 0 << ' ' << sq << '\n'; + } +} diff --git a/Codeforces Round 1027 (Div. 3)/B. Not Quite a Palindromic String.cpp b/Codeforces Round 1027 (Div. 3)/B. Not Quite a Palindromic String.cpp new file mode 100644 index 0000000..af6c31d --- /dev/null +++ b/Codeforces Round 1027 (Div. 3)/B. Not Quite a Palindromic String.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://codeforces.com/contest/2114/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + string fds; + cin >> fds; + + vi count(2); + repv(i, fds) { + count[i - '0']++; + } + + rep(i, k) { + if (count[0] > count[1]) { + count[0] -= 2; + } else { + count[1] -= 2; + } + } + + if (count[0] != count[1] || (n % 2 == 1 && abs(count[0] - count[1]) > 1)) { + cout << "NO\n"; + continue; + } + cout << "YES\n"; + } +} diff --git a/Codeforces Round 1027 (Div. 3)/C. Need More Arrays.cpp b/Codeforces Round 1027 (Div. 3)/C. Need More Arrays.cpp new file mode 100644 index 0000000..12d6c5a --- /dev/null +++ b/Codeforces Round 1027 (Div. 3)/C. Need More Arrays.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/contest/2114/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + int count = 1; + nrep(i, 1, n) { + if (fds[i] <= fds[i - 1] + 1) { + fds[i] = fds[i - 1]; + continue; + } + + count++; + } + + cout << count << '\n'; + } +} diff --git a/Codeforces Round 1027 (Div. 3)/F. Small Operations.cpp b/Codeforces Round 1027 (Div. 3)/F. Small Operations.cpp new file mode 100644 index 0000000..9a8f090 --- /dev/null +++ b/Codeforces Round 1027 (Div. 3)/F. Small Operations.cpp @@ -0,0 +1,165 @@ +/* Problem URL: https://codeforces.com/contest/2114/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + // vi divs(1e6 + 1, 1); + // + // nrep(i, 2, 1e6 + 1) { + // if (divs[i] != 1) { + // continue; + // } + // + // divs[i] = i; + // + // for (int j = i * 2; j <= 1e6; j += i) { + // divs[j] = i; + // } + // } + // + vvi divisors(1e6 + 1); + + nrep(i, 2, 1e6 + 1) { + divisors[i].push_back(i); + + for (int j = i * 2; j <= 1e6; j += i) { + divisors[j].push_back(i); + } + } + + int t; + cin >> t; + while (t--) { + ll x, y, z; + cin >> x >> y >> z; + + // auto fact = [&](ll num, vl &primes) { + // while (num > 1) { + // primes.push_back(divs[num]); + // num /= divs[num]; + // } + // + // if (num != 1) { + // primes.push_back(num); + // } + // + // reverse(all(primes)); + // }; + + ll gcd = __gcd(x, y); + x /= gcd; + y /= gcd; + + // vl primesx; + // vl primesy; + + // fact(x, primesx); + // fact(y, primesy); + + // if ((!primesx.empty() && primesx.back() > z) || (!primesy.empty() && primesy.back() > z)) { + // cout << "-1\n"; + // continue; + // } + + bool valid = true; + + auto getans = [&](ll n) { + int ans = 0; + + while (n > 1) { + auto itr = upper_bound(all(divisors[n]), z); + if (itr == divisors[n].begin()) { + valid = false; + return 0; + } + + itr--; + n /= *itr; + ans++; + } + + return ans; + }; + + int ans1 = getans(x); + int ans2 = getans(y); + + if (!valid) { + cout << "-1\n"; + continue; + } + + cout << ans1 + ans2 << '\n'; + } +} diff --git a/Codeforces Round 1029 (Div. 3)/A. False Alarm.cpp b/Codeforces Round 1029 (Div. 3)/A. False Alarm.cpp new file mode 100644 index 0000000..f0a832d --- /dev/null +++ b/Codeforces Round 1029 (Div. 3)/A. False Alarm.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/contest/2117/problem/0 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, x; + cin >> n >> x; + + vi fds(n); + cin >> fds; + + int count = 0; + bool but = false; + bool pos = true; + rep(i, n) { + if (but) { + count++; + } + + if (fds[i] == 1 && count >= x) { + pos = false; + break; + } + + if (fds[i] == 1) { + but = true; + } + } + + cout << (pos ? "Yes\n" : "No\n"); + } +} diff --git a/Codeforces Round 1029 (Div. 3)/B. Shrink.cpp b/Codeforces Round 1029 (Div. 3)/B. Shrink.cpp new file mode 100644 index 0000000..3d2dc38 --- /dev/null +++ b/Codeforces Round 1029 (Div. 3)/B. Shrink.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://codeforces.com/contest/2117/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + cout << "1 "; + for (int i = n; i > 1; i--) { + cout << i << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1029 (Div. 3)/C. Cool Partition.cpp b/Codeforces Round 1029 (Div. 3)/C. Cool Partition.cpp new file mode 100644 index 0000000..10d5489 --- /dev/null +++ b/Codeforces Round 1029 (Div. 3)/C. Cool Partition.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2117/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + vi next(n + 1, -1); + vi prev(n + 1, -1); + + rep(i, fds.size()) { + if (prev[fds[i]] != -1) { + next[prev[fds[i]]] = i; + } + prev[fds[i]] = i; + } + + vi pos(n + 1, 0); + + int ans = 1; + int maximus = 0; + int limit = -1; + set s; + rep(i, fds.size()) { + if (next[i] == -1) { + break; + } + + if (i < limit) { + pos[fds[i]] = next[i]; + rmax(maximus, pos[fds[i]]); + continue; + } + + ans++; + + pos[fds[i]] = next[i]; + rmax(maximus, pos[fds[i]]); + + limit = maximus; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1029 (Div. 3)/D. Retaliation.cpp b/Codeforces Round 1029 (Div. 3)/D. Retaliation.cpp new file mode 100644 index 0000000..b21dd16 --- /dev/null +++ b/Codeforces Round 1029 (Div. 3)/D. Retaliation.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/contest/2117/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + vl fds(n); + cin >> fds; + + bool dec = true; + bool cres = true; + + nrep(i, 1, n) { + if (fds[i] > fds[i - 1]) { + dec = false; + } + + if (fds[i] < fds[i - 1]) { + cres = false; + } + } + + if (!dec && !cres) { + cout << "NO\n"; + continue; + } + + if (dec) { + reverse(all(fds)); + } + + ll ans = fds.back() - n * fds[0]; + bool pos = true; + if (ans % (-n * n + 1) || ans > 0) { + cout << "NO\n"; + continue; + } + + ans /= -n * n + 1; + + rep(i, n) { + fds[i] -= ans * (n - i); + } + + ll tmp = fds[0]; + nrep(i, 1, n) { + if (fds[i] - tmp * (i + 1) != 0) { + pos = false; + break; + } + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1029 (Div. 3)/E. Lost Soul.cpp b/Codeforces Round 1029 (Div. 3)/E. Lost Soul.cpp new file mode 100644 index 0000000..3258738 --- /dev/null +++ b/Codeforces Round 1029 (Div. 3)/E. Lost Soul.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2117/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi a(n); + vi b(n); + + cin >> a >> b; + + reverse(all(a)); + reverse(all(b)); + + V ahas(n); + V bhas(n); + V ayes(n); + V byes(n); + + int ans = 0; + + rep(i, n) { + a[i]--; + b[i]--; + if (a[i] == b[i] || bhas[b[i]] || ahas[a[i]] || ayes[b[i]] || byes[a[i]]) { + ans = n - i; + break; + } + + ahas[a[i]] = true; + bhas[b[i]] = true; + + if (i > 0) { + ayes[a[i - 1]] = true; + byes[b[i - 1]] = true; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1032 (Div. 3)/A. Letter Home.cpp b/Codeforces Round 1032 (Div. 3)/A. Letter Home.cpp new file mode 100644 index 0000000..c401ab9 --- /dev/null +++ b/Codeforces Round 1032 (Div. 3)/A. Letter Home.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/contest/2121/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, s; + cin >> n >> s; + + vi fds(n); + cin >> fds; + + if (fds.back() <= s) { + cout << s - fds[0] << '\n'; + continue; + } + + if (fds[0] >= s) { + cout << fds.back() - s << '\n'; + continue; + } + + cout << min(abs(fds[0] - s) + abs(fds.back() - s) * 2, abs(fds.back() - s) + abs(fds[0] - s) * 2) << '\n'; + } +} diff --git a/Codeforces Round 1032 (Div. 3)/B. Above the Clouds.cpp b/Codeforces Round 1032 (Div. 3)/B. Above the Clouds.cpp new file mode 100644 index 0000000..2afd799 --- /dev/null +++ b/Codeforces Round 1032 (Div. 3)/B. Above the Clouds.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/contest/2121/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + while (t--) { + int n; + cin >> n; + + string s; + cin >> s; + + vi count(26); + rep(i, n) { + count[s[i] - 'a']++; + } + + bool pos = false; + nrep(i, 1, n - 1) { + if (count[s[i] - 'a'] > 1) { + pos = true; + break; + } + } + + cout << (pos ? "Yes\n" : "No\n"); + } +} diff --git a/Codeforces Round 1032 (Div. 3)/C. Those Who Are With Us.cpp b/Codeforces Round 1032 (Div. 3)/C. Those Who Are With Us.cpp new file mode 100644 index 0000000..cc9c67c --- /dev/null +++ b/Codeforces Round 1032 (Div. 3)/C. Those Who Are With Us.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/contest/2121/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + vvi fds(n, vi(m)); + cin >> fds; + + V>> count(101); + + rep(i, n) { + rep(j, m) { + count[fds[i][j]].emplace_back(i, j); + } + } + + int i = 100; + while (count[i].empty()) { + i--; + } + + bool pos = true; + map countsi; + int maximal = 0; + int choice = 0; + + bool byj = false; + + rep(j, count[i].size()) { + countsi[count[i][j].first]++; + if (countsi[count[i][j].first] > maximal) { + maximal = countsi[count[i][j].first]; + choice = count[i][j].first; + } + } + + map countsj; + + rep(j, count[i].size()) { + countsj[count[i][j].second]++; + if (countsj[count[i][j].second] > maximal) { + maximal = countsj[count[i][j].second]; + choice = count[i][j].second; + byj = true; + } + } + + + set c; + for (int j = 0; j < count[i].size(); j++) { + if ((!byj && count[i][j].first == choice) || (byj && count[i][j].second == choice)) { + continue; + } + c.insert(byj ? count[i][j].first : count[i][j].second); + } + + if (c.size() < 2) { + cout << i - 1 << '\n'; + } else { + cout << i << '\n'; + } + } +} diff --git a/Codeforces Round 1032 (Div. 3)/D. 1709.cpp b/Codeforces Round 1032 (Div. 3)/D. 1709.cpp new file mode 100644 index 0000000..4ba52fe --- /dev/null +++ b/Codeforces Round 1032 (Div. 3)/D. 1709.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2121/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vvi fds(2, vi(n)); + + rep(i, 2) { + rep(j, n) { + cin >> fds[i][j]; + } + } + + V> ops; + + for (int j = n - 1; j > 0; j--) { + rep(i, j) { + if (fds[0][i] > fds[0][i + 1]) { + ops.emplace_back(1, i + 1); + swap(fds[0][i], fds[0][i + 1]); + } + + if (fds[1][i] > fds[1][i + 1]) { + ops.emplace_back(2, i + 1); + swap(fds[1][i], fds[1][i + 1]); + } + } + } + + rep(i, n) { + if (fds[0][i] > fds[1][i]) { + ops.emplace_back(3, i + 1); + } + } + + cout << ops.size() << '\n'; + for (auto [op, p] : ops) { + cout << op << ' ' << p << '\n'; + } + } +} diff --git a/Codeforces Round 1032 (Div. 3)/E. Sponsor of Your Problems.cpp b/Codeforces Round 1032 (Div. 3)/E. Sponsor of Your Problems.cpp new file mode 100644 index 0000000..82c5c5a --- /dev/null +++ b/Codeforces Round 1032 (Div. 3)/E. Sponsor of Your Problems.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/contest/2121/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + while (t--) { + string l, r; + cin >> l >> r; + + vvvi memo(l.size(), vvi(2, vi(2, -1))); + + function dp = [&](int i, int under, int over) { + if (i == l.size()) { + return 0; + } + if (memo[i][under][over] != -1) { + return memo[i][under][over]; + } + + auto &ans = memo[i][under][over]; + ans = INT32_MAX >> 1; + + char limit = over ? '0' : l[i]; + for (char itr = under ? '9' : r[i]; itr >= limit; itr--) { + rmin(ans, dp(i + 1, under || itr < r[i], over || itr > l[i]) + (itr == l[i]) + (itr == r[i])); + } + + return ans; + }; + + cout << dp(0, 0, 0) << '\n'; + } +} diff --git a/Codeforces Round 1032 (Div. 3)/F. Yamakasi.cpp b/Codeforces Round 1032 (Div. 3)/F. Yamakasi.cpp new file mode 100644 index 0000000..1f5e056 --- /dev/null +++ b/Codeforces Round 1032 (Div. 3)/F. Yamakasi.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2121/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, s, x; + cin >> n >> s >> x; + + vl fds(n); + cin >> fds; + + map count; + map rem; + vl pref(n + 1); + + rep(i, n) { + pref[i + 1] = pref[i] + fds[i]; + } + + ll ans = 0; + + rep(i, n) { + if (fds[i] == x) { + count[pref[i]]++; + ans += count[pref[i + 1] - s]; + rem.clear(); + continue; + } + + + if (fds[i] > x) { + count.clear(); + rem.clear(); + continue; + } + + ans += count[pref[i + 1] - s] - rem[pref[i + 1] - s]; + count[pref[i]]++; + rem[pref[i]]++; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1034 (Div. 3)/A. Blackboard Game.cpp b/Codeforces Round 1034 (Div. 3)/A. Blackboard Game.cpp new file mode 100644 index 0000000..c44ec7c --- /dev/null +++ b/Codeforces Round 1034 (Div. 3)/A. Blackboard Game.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://codeforces.com/contest/2123/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + if (n % 4 == 0) { + cout << "Bob\n"; + continue; + } + cout << "Alice\n"; + } +} diff --git a/Codeforces Round 1034 (Div. 3)/B. Tournament.cpp b/Codeforces Round 1034 (Div. 3)/B. Tournament.cpp new file mode 100644 index 0000000..e88245c --- /dev/null +++ b/Codeforces Round 1034 (Div. 3)/B. Tournament.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/contest/2123/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, j, k; + cin >> n >> j >> k; + j--; + + vi fds(n); + cin >> fds; + + int lesser = 0; + int greater = 0; + + rep(i, n) { + if (i == j) { + continue; + } + + if (fds[i] > fds[j]) { + greater++; + continue; + } + + if (fds[i] <= fds[j]) { + lesser++; + } + } + + if (n - lesser - greater + (greater == 0 ? 0 : 1) <= k) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } + } +} diff --git a/Codeforces Round 1034 (Div. 3)/C. Prefix Min and Suffix Max.cpp b/Codeforces Round 1034 (Div. 3)/C. Prefix Min and Suffix Max.cpp new file mode 100644 index 0000000..dfdc697 --- /dev/null +++ b/Codeforces Round 1034 (Div. 3)/C. Prefix Min and Suffix Max.cpp @@ -0,0 +1,153 @@ +/* Problem URL: https://codeforces.com/contest/2123/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + while (__builtin_popcount(fds.size()) != 1) { + fds.push_back(0); + } + + auto comp = [&](pair a, pair b) { + return make_pair(max(a.first, b.first), min(a.second, b.second)); + }; + + V> seg(fds.size() * 2); + + for (int i = fds.size(); i < fds.size() * 2; i++) { + seg[i] = {fds[i - fds.size()], fds[i - fds.size()]}; + } + + for (int i = fds.size() - 1; i > 0; i--) { + seg[i] = comp(seg[i * 2], seg[i * 2 + 1]); + } + + function(int, int, int, int, int)> search = [&](int i, int l, int r, int tl, int tr) -> pair { + if (l > tr || r < tl) { + return {-1, 1e9}; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) / 2; + + return comp(search(i * 2, l, mid, tl, tr), search(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + rep(i, n) { + auto [lmax, lmin] = search(1, 0, fds.size() - 1, 0, i - 1); + auto [rmax, rmin] = search(1, 0, fds.size() - 1, i + 1, n - 1); + + int now = fds[i]; + + if (now < lmin && now < rmin) { + cout << "1"; + continue; + } + + if (now > lmax && now > rmax) { + cout << "1"; + continue; + } + + if (now < rmax && now < lmin) { + cout << "1"; + continue; + } + + if (now > rmax && now > lmin) { + cout << "1"; + continue; + } + + if (now > rmax && now < lmin) { + cout << "1"; + continue; + } + + cout << "0"; + } + cout << "\n"; + } +} diff --git a/Codeforces Round 1034 (Div. 3)/D. Binary String Battle.cpp b/Codeforces Round 1034 (Div. 3)/D. Binary String Battle.cpp new file mode 100644 index 0000000..e39a54a --- /dev/null +++ b/Codeforces Round 1034 (Div. 3)/D. Binary String Battle.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/contest/2123/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + string a; + cin >> n >> k >> a; + + int ones = 0; + repv(i, a) { + if (i == '1') { + ones++; + } + } + + if (ones <= k || n / 2 + 1 <= k) { + cout << "Alice\n"; + continue; + } + + cout << "Bob\n"; + } +} diff --git a/Codeforces Round 1034 (Div. 3)/E. MEX Count.cpp b/Codeforces Round 1034 (Div. 3)/E. MEX Count.cpp new file mode 100644 index 0000000..c4fdc75 --- /dev/null +++ b/Codeforces Round 1034 (Div. 3)/E. MEX Count.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/contest/2123/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi a(n); + cin >> a; + + vi count(n + 2); + rep(i, n) { + count[a[i]]++; + } + + vi pref(n + 3); + pref[0] = count[0] - 1; + + vi suf(n + 3); + suf[n] = count[n]; + + nrep(i, 1, n + 1) { + pref[i] = pref[i - 1] + count[i] - 1; + suf[n - i] = suf[n - i + 1] + count[n - i]; + } + + int first = 0; + while (count[first] > 0) { + first++; + } + + vi ans(n + 1); + + for (int i = first; i >= 0; i--) { + int low = count[i]; + int high = pref[i] + 2 + suf[i + 1]; + + ans[low]++; + if (high > n) { + continue; + } + + ans[high]--; + } + + cout << "1"; + nrep(i, 1, n + 1) { + ans[i] += ans[i - 1]; + cout << ' ' << ans[i]; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1034 (Div. 3)/F. Minimize Fixed Points.cpp b/Codeforces Round 1034 (Div. 3)/F. Minimize Fixed Points.cpp new file mode 100644 index 0000000..7047415 --- /dev/null +++ b/Codeforces Round 1034 (Div. 3)/F. Minimize Fixed Points.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/contest/2123/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vi div(1e5 + 1); + rep(i, 1e5 + 1) { + div[i] = i; + } + + vi primes(1e5 + 1); + + for (int i = 2; i <= 1e5; i++) { + if (div[i] != i) { + continue; + } + + primes.push_back(i); + + for (int j = i * 2; j <= 1e5; j += i) { + div[j] = i; + } + } + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + vi pos(n + 1); + rep(i, n) { + fds[i] = i + 1; + pos[i + 1] = i; + } + + nrep(i, 1, n) { + if (div[fds[i]] == fds[i]) { + continue; + } + + int now = i; + int change = pos[div[fds[i]]]; + + int num1 = fds[i]; + int num2 = fds[pos[div[fds[i]]]]; + + swap(fds[now], fds[change]); + swap(pos[num1], pos[num2]); + } + + cout << fds; + } +} diff --git a/Codeforces Round 1037 (Div. 3)/A. Only One Digit.cpp b/Codeforces Round 1037 (Div. 3)/A. Only One Digit.cpp new file mode 100644 index 0000000..8fa801b --- /dev/null +++ b/Codeforces Round 1037 (Div. 3)/A. Only One Digit.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/contest/2126/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + while (t--) { + string a; + cin >> a; + + char choice = '9'; + repv(i, a) { + rmin(choice, i); + } + + cout << choice << '\n'; + } +} diff --git a/Codeforces Round 1037 (Div. 3)/B. No Casino in the Mountains.cpp b/Codeforces Round 1037 (Div. 3)/B. No Casino in the Mountains.cpp new file mode 100644 index 0000000..96bbc2b --- /dev/null +++ b/Codeforces Round 1037 (Div. 3)/B. No Casino in the Mountains.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2126/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi fds(n); + cin >> fds; + + int count = 0; + int ans = 0; + + bool rest = false; + + repv(i, fds) { + if (rest) { + rest = false; + continue; + } + + if (i == 1) { + count = 0; + continue; + } + + count++; + if (count == k) { + rest = true; + ans++; + count = 0; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1037 (Div. 3)/C. I Will Definitely Make It.cpp b/Codeforces Round 1037 (Div. 3)/C. I Will Definitely Make It.cpp new file mode 100644 index 0000000..875868e --- /dev/null +++ b/Codeforces Round 1037 (Div. 3)/C. I Will Definitely Make It.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/contest/2126/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi fds(n); + cin >> fds; + + int now = fds[k - 1]; + + sortv(fds); + + int water = 0; + + rep(i, n) { + if (fds[i] < now) { + continue; + } + + if (fds[i] - now > now - water) { + break; + } + + water += fds[i] - now; + now = fds[i]; + } + + cout << (now == fds.back() ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1037 (Div. 3)/D. This Is the Last Time.cpp b/Codeforces Round 1037 (Div. 3)/D. This Is the Last Time.cpp new file mode 100644 index 0000000..ac3d9a5 --- /dev/null +++ b/Codeforces Round 1037 (Div. 3)/D. This Is the Last Time.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/contest/2126/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + ll k; + cin >> n >> k; + + V> fds(n); + + for (auto &[c, l, r] : fds) { + cin >> l >> r >> c; + } + + sortv(fds); + + for (auto [c, l, r] : fds) { + if (k >= l && k <= r) { + rmax(k, c); + } + } + + cout << k << '\n'; + } +} diff --git a/Codeforces Round 1037 (Div. 3)/E. G-C-D, Unlucky!.cpp b/Codeforces Round 1037 (Div. 3)/E. G-C-D, Unlucky!.cpp new file mode 100644 index 0000000..baa11bb --- /dev/null +++ b/Codeforces Round 1037 (Div. 3)/E. G-C-D, Unlucky!.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/2126/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl a(n); + vl b(n); + cin >> a >> b; + + if (n == 1) { + cout << (a[0] == b[0] ? "YES\n" : "NO\n"); + continue; + } + + vl tmp(n); + + rep(i, n) { + tmp[i] = lcm(a[i], b[i]); + } + + vl f(n); + f[0] = tmp[0]; + nrep(i, 1, n) { + f[i] = gcd(f[i - 1], tmp[i]); + } + + vl s(n); + s[n - 1] = tmp[n - 1]; + for (int i = n - 2; i >= 0; i--) { + s[i] = gcd(s[i + 1], tmp[i]); + } + + if (f != a || s != b) { + cout << "NO\n"; + } else { + cout << "YES\n"; + } + } +} diff --git a/Codeforces Round 1037 (Div. 3)/F. 1-1-1, Free Tree!.cpp b/Codeforces Round 1037 (Div. 3)/F. 1-1-1, Free Tree!.cpp new file mode 100644 index 0000000..7d02a59 --- /dev/null +++ b/Codeforces Round 1037 (Div. 3)/F. 1-1-1, Free Tree!.cpp @@ -0,0 +1,153 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2126/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, q; + cin >> n >> q; + + vi fds(n); + cin >> fds; + + V> cl(n); + V>> graph(n); + vi par(n); + vl cost(n); + + rep(i, n - 1) { + int a, b; + ll c; + cin >> a >> b >> c; + a--, b--; + + graph[a].emplace_back(b, c); + graph[b].emplace_back(a, c); + } + + ll ans = 0; + + function dfs = [&](int i, int p) { + for (auto [j, c] : graph[i]) { + if (j == p) { + continue; + } + + if (fds[j] != fds[i]) { + ans += c; + } + + cl[i][fds[j]] += c; + par[j] = i; + cost[j] = c; + + dfs(j, i); + } + }; + + dfs(0, 0); + + while (q--) { + int a, c; + cin >> a >> c; + a--; + + ans += cl[a][fds[a]]; + ans -= cl[a][c]; + + if (a != 0) { + cl[par[a]][fds[a]] -= cost[a]; + cl[par[a]][c] += cost[a]; + + if (c != fds[par[a]] && fds[a] == fds[par[a]]) { + ans += cost[a]; + } else if (c == fds[par[a]] && fds[a] != fds[par[a]]) { + ans -= cost[a]; + } + } + + fds[a] = c; + + cout << ans << '\n'; + } + } +} diff --git a/Codeforces Round 1039 (Div. 2)/A. Recycling Center.cpp b/Codeforces Round 1039 (Div. 2)/A. Recycling Center.cpp new file mode 100644 index 0000000..6aa2b7b --- /dev/null +++ b/Codeforces Round 1039 (Div. 2)/A. Recycling Center.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/contest/2128/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + ll c; + cin >> n >> c; + + vl fds(n); + cin >> fds; + + sort(all(fds), greater<>()); + + int count = 0; + int pow = 1; + + repv(i, fds) { + if (i * pow <= c) { + count++; + pow *= 2; + } + } + + cout << n - count << '\n'; + } +} diff --git a/Codeforces Round 1039 (Div. 2)/B. Deque Process.cpp b/Codeforces Round 1039 (Div. 2)/B. Deque Process.cpp new file mode 100644 index 0000000..4999ebc --- /dev/null +++ b/Codeforces Round 1039 (Div. 2)/B. Deque Process.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2128/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + int l = 0; + int r = n - 1; + + string ans; + int now = 0; + + while (l != r) { + if (now) { + if (fds[l] > fds[r]) { + l++; + ans.push_back('L'); + } else { + r--; + ans.push_back('R'); + } + } else { + if (fds[l] < fds[r]) { + l++; + ans.push_back('L'); + } else { + r--; + ans.push_back('R'); + } + } + + now ^= 1; + } + + ans.push_back('R'); + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1039 (Div. 2)/C. Leftmost Below.cpp b/Codeforces Round 1039 (Div. 2)/C. Leftmost Below.cpp new file mode 100644 index 0000000..8821b45 --- /dev/null +++ b/Codeforces Round 1039 (Div. 2)/C. Leftmost Below.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/contest/2128/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + ll minimal = fds[0]; + + bool pos = true; + + nrep(i, 1, n) { + if (fds[i] > minimal * 2 - 1) { + pos = false; + break; + } + + rmin(minimal, fds[i]); + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1039 (Div. 2)/D. Sum of LDS.cpp b/Codeforces Round 1039 (Div. 2)/D. Sum of LDS.cpp new file mode 100644 index 0000000..df8d423 --- /dev/null +++ b/Codeforces Round 1039 (Div. 2)/D. Sum of LDS.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/contest/2128/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + vi fds(n); + cin >> fds; + + ll ans = n * (n + 1) * (n + 2) / 6; + + rep(i, n - 1) { + if (fds[i] < fds[i + 1]) { + ans -= (i + 1) * (n - i - 1); + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1040 (Div. 1)/B. Stay or Mirror.cpp b/Codeforces Round 1040 (Div. 1)/B. Stay or Mirror.cpp new file mode 100644 index 0000000..00e8d3d --- /dev/null +++ b/Codeforces Round 1040 (Div. 1)/B. Stay or Mirror.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2129/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vi fds(n); + cin >> fds; + + int ans = 0; + + rep(i, n) { + int l = 0; + int r = 0; + rep(j, i) { + if (fds[j] > fds[i]) { + l++; + } + } + + nrep(j, i + 1, n) { + if (fds[j] > fds[i]) { + r++; + } + } + + ans += min(l, r); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1040 (Div. 2)/A. Submission is All You Need.cpp b/Codeforces Round 1040 (Div. 2)/A. Submission is All You Need.cpp new file mode 100644 index 0000000..42d7cc1 --- /dev/null +++ b/Codeforces Round 1040 (Div. 2)/A. Submission is All You Need.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2130/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + vi count(51); + int n; + cin >> n; + + while (n--) { + int a; + cin >> a; + + count[a]++; + } + + int ans = 0; + while (count[0] && count[1]) { + count[0]--; + count[1]--; + ans += 2; + } + + ans += count[0]; + + nrep(i, 1, 51) { + ans += count[i] * i; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1042 (Div. 3)/A. Lever.cpp b/Codeforces Round 1042 (Div. 3)/A. Lever.cpp new file mode 100644 index 0000000..eccdb4a --- /dev/null +++ b/Codeforces Round 1042 (Div. 3)/A. Lever.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/contest/2131/problem/0 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi a(n); + cin >> a; + + vi b(n); + cin >> b; + + int count = 0; + while (true) { + count++; + int ans = 0; + for (; ans < a.size() && a[ans] <= b[ans]; ans++) {} + + if (ans == a.size()) { + break; + } + + a[ans]--; + + rep(i, a.size()) { + if (a[i] < b[i]) { + a[i]++; + break; + } + } + } + + cout << count << '\n'; + } +} diff --git a/Codeforces Round 1042 (Div. 3)/B. Alternating Series.cpp b/Codeforces Round 1042 (Div. 3)/B. Alternating Series.cpp new file mode 100644 index 0000000..ca6108e --- /dev/null +++ b/Codeforces Round 1042 (Div. 3)/B. Alternating Series.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://codeforces.com/contest/2131/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + + rep(i, n) { + if (i & 1) { + if (i == n - 1 && (n & 1) == 0) { + fds[i] = 2; + continue; + } + fds[i] = 3; + } else { + fds[i] = -1; + } + } + + cout << fds; + } +} diff --git a/Codeforces Round 1042 (Div. 3)/C. Make it Equal.cpp b/Codeforces Round 1042 (Div. 3)/C. Make it Equal.cpp new file mode 100644 index 0000000..7336896 --- /dev/null +++ b/Codeforces Round 1042 (Div. 3)/C. Make it Equal.cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/contest/2131/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + ll k; + cin >> n >> k; + + vl s(n); + vl t(n); + cin >> s >> t; + + map count; + + repv(i, t) { + count[i % k]++; + } + + bool pos = true; + repv(i, s) { + if (count[i % k]) { + count[i % k]--; + continue; + } + + int tmp = (-i % k + k) % k; + + if (count[tmp]) { + count[tmp]--; + continue; + } + + pos = false; + break; + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1042 (Div. 3)/D. Arboris Contractio.cpp b/Codeforces Round 1042 (Div. 3)/D. Arboris Contractio.cpp new file mode 100644 index 0000000..80bf47e --- /dev/null +++ b/Codeforces Round 1042 (Div. 3)/D. Arboris Contractio.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/contest/2131/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + int dim = 0; + + auto bfs = [&](int i) { + int inf = INT32_MAX >> 1; + vi dis(n, inf); + int ans = 0; + + queue q; + q.push(i); + dis[i] = 0; + + while (!q.empty()) { + i = q.front(); + q.pop(); + + for (auto j : graph[i]) { + if (dis[j] > dis[i] + 1) { + dis[j] = dis[i] + 1; + dim = dis[j]; + q.push(j); + ans = j; + } + } + } + + return ans; + }; + + int tmp = bfs(0); + int fds = bfs(tmp); + + if (dim <= 2) { + cout << "0\n"; + continue; + } + + vi count(n); + + int total = 0; + + rep(i, n) { + if (graph[i].size() == 1) { + count[graph[i][0]]++; + total++; + } + } + + int ans = total; + + rep(i, n) { + rmin(ans, total - count[i]); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1042 (Div. 3)/E. Adjacent XOR.cpp b/Codeforces Round 1042 (Div. 3)/E. Adjacent XOR.cpp new file mode 100644 index 0000000..b54f3df --- /dev/null +++ b/Codeforces Round 1042 (Div. 3)/E. Adjacent XOR.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2131/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi a(n); + vi b(n); + cin >> a >> b; + + if (a.back() != b.back()) { + cout << "NO\n"; + continue; + } + + rep(i, n - 1) { + if ((a[i] ^ a[i + 1]) == b[i]) { + a[i] ^= a[i + 1]; + } + } + + bool pos = true; + for (int i = n - 2; i >= 0; i--) { + if (a[i] != b[i]) { + a[i] ^= a[i + 1]; + + if (a[i] != b[i]) { + pos = false; + break; + } + } + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1042 (Div. 3)/F. Unjust Binary Life.cpp b/Codeforces Round 1042 (Div. 3)/F. Unjust Binary Life.cpp new file mode 100644 index 0000000..b9425c8 --- /dev/null +++ b/Codeforces Round 1042 (Div. 3)/F. Unjust Binary Life.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/contest/2131/problem/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + string at, bt; + cin >> at >> bt; + + vi a(n); + vi b(n); + + rep(i, n) { + a[i] = at[i] - '0'; + b[i] = bt[i] - '0'; + } + + vl minimal(n * 2 + 2); + vl count(n * 2 + 2); + + int middle = n; + vi bc(2); + + rep(i, n) { + bc[b[i]]++; + + int diff = bc[0] - bc[1]; + + minimal[middle + diff] += min(bc[0], bc[1]); + count[middle + diff]++; + } + + nrep(i, 2, n + 1) { + minimal[middle + i] += minimal[middle + i - 1]; + minimal[middle - i] += minimal[middle - i + 1]; + count[middle + i] += count[middle + i - 1]; + count[middle - i] += count[middle - i + 1]; + } + + vl ac(2); + ll ans = 0; + + rep(i, n) { + ac[a[i]]++; + + int diff = ac[1] - ac[0]; + + ans += minimal[middle + diff] + count[middle + diff] * min(ac[1], ac[0]); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1042 (Div. 3)/G. Wafu!.cpp b/Codeforces Round 1042 (Div. 3)/G. Wafu!.cpp new file mode 100644 index 0000000..ff4bab4 --- /dev/null +++ b/Codeforces Round 1042 (Div. 3)/G. Wafu!.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/contest/2131/problem/G */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + ll k; + cin >> n >> k; + + vl fds(n); + cin >> fds; + + sortv(fds); + + ll mod = 1e9 + 7; + + auto fastpow = [&](ll a, ll p) { + ll ans = 1; + ll now = a; + + rep(i, 30) { + if ((p >> i) & 1) { + ans *= now; + ans %= mod; + } + + now *= now; + now %= mod; + } + + return ans; + }; + + ll ans = 1; + int j = 0; + + while (k > 0) { + auto func = [&](ll num) { + ll ans = num; + k--; + + ll act = min(k, (1LL << (min(31LL, num - 1))) - 1); + + ll count = 0; + for (ll i = min(31LL, num - 1); i >= 1; i--) { + ll now = act / (1 << (i - 1)) - count; + + ans *= fastpow(i, now); + ans %= mod; + + count += now; + } + + k -= count; + + return ans; + }; + + ans *= func(fds[j]); + ans %= mod; + j++; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1043 (Div. 3)/A. Homework.cpp b/Codeforces Round 1043 (Div. 3)/A. Homework.cpp new file mode 100644 index 0000000..815dcb5 --- /dev/null +++ b/Codeforces Round 1043 (Div. 3)/A. Homework.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/contest/2132/problem/0 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + string a; + cin >> a; + + int m; + cin >> m; + + string b; + cin >> b; + + string c; + cin >> c; + + string ans1; + string ans2; + + rep(i, m) { + if (c[i] == 'V') { + ans1.push_back(b[i]); + } else { + ans2.push_back(b[i]); + } + } + + reverse(all(ans1)); + + cout << ans1 << a << ans2 << '\n'; + } +} diff --git a/Codeforces Round 1043 (Div. 3)/B. The Secret Number.cpp b/Codeforces Round 1043 (Div. 3)/B. The Secret Number.cpp new file mode 100644 index 0000000..9b46420 --- /dev/null +++ b/Codeforces Round 1043 (Div. 3)/B. The Secret Number.cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://codeforces.com/contest/2132/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll x; + cin >> x; + + ll div = 10; + set ans; + + while (div <= x) { + if (x % (div + 1) == 0) { + ans.insert(x / (div + 1)); + } + div *= 10; + } + + cout << ans.size() << '\n'; + + if (ans.empty()) { + continue; + } + + for (auto &i : ans) { + cout << i << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1043 (Div. 3)/C1. The Cunning Seller (easy version).cpp b/Codeforces Round 1043 (Div. 3)/C1. The Cunning Seller (easy version).cpp new file mode 100644 index 0000000..7689564 --- /dev/null +++ b/Codeforces Round 1043 (Div. 3)/C1. The Cunning Seller (easy version).cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://codeforces.com/contest/2132/problem/C1 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vl pows(21); + pows[0] = 1; + + nrep(i, 1, 21) { + pows[i] = pows[i - 1] * 3; + } + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + ll ans = 0; + + while (n > 0) { + ll now = 0; + while (pows[now + 1] <= n) { + now++; + } + + n -= pows[now]; + ans += pows[now + 1] + pows[max(now - 1, 0LL)] * now; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1043 (Div. 3)/C2. The Cunning Seller (hard version).cpp b/Codeforces Round 1043 (Div. 3)/C2. The Cunning Seller (hard version).cpp new file mode 100644 index 0000000..165a3e9 --- /dev/null +++ b/Codeforces Round 1043 (Div. 3)/C2. The Cunning Seller (hard version).cpp @@ -0,0 +1,145 @@ +/* Problem URL: https://codeforces.com/contest/2132/problem/C2 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vl pows(21); + pows[0] = 1; + + nrep(i, 1, 21) { + pows[i] = pows[i - 1] * 3; + } + + int t; + cin >> t; + while (t--) { + ll n, k; + cin >> n >> k; + + vi count(21); + + ll now = 20; + ll total = 0; + + while (now >= 0) { + count[now] = n / pows[now]; + total += count[now]; + n %= pows[now]; + + now--; + } + + if (total > k) { + cout << "-1\n"; + continue; + } + + for (int j = 20; j > 0; j--) { + for (int i = j - 1; i >= 0; i--) { + int low = 0; + int high = count[j]; + ll ans = 0; + while (low <= high) { + int mid = (low + high) >> 1; + + if (total - mid + (pows[j] / pows[i]) * mid <= k) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + total -= ans; + total += ans * (pows[j] / pows[i]); + count[j] -= ans; + count[i] += ans * (pows[j] / pows[i]); + } + } + + ll ans = 0; + rep(i, 20) { + ans += count[i] * (pows[i + 1] + pows[max(0LL, (ll)i - 1)] * i); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1043 (Div. 3)/D. From 1 to Infinity.cpp b/Codeforces Round 1043 (Div. 3)/D. From 1 to Infinity.cpp new file mode 100644 index 0000000..0d40572 --- /dev/null +++ b/Codeforces Round 1043 (Div. 3)/D. From 1 to Infinity.cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/contest/2132/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int q; + cin >> q; + while (q--) { + ll k; + cin >> k; + k--; + + ll now = 1; + int digs = 1; + while (k >= digs * now * 9) { + k -= digs * now * 9; + now *= 10; + digs++; + } + + ll act = now; + act += (k / digs); + k %= digs; + + ll ans = 0; + ll prev = 0; + + rep(i, digs) { + ll dig = act / now % 10; + ans += dig * (2 * prev + dig - 1) / 2 * now; + ans += dig * (digs - i - 1) * 45 * (now / 10); + prev += dig; + now /= 10; + } + + string fds = to_string(act); + rep(i, k + 1) { + ans += fds[i] - '0'; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1043 (Div. 3)/E. Arithmetics Competition.cpp b/Codeforces Round 1043 (Div. 3)/E. Arithmetics Competition.cpp new file mode 100644 index 0000000..93804dc --- /dev/null +++ b/Codeforces Round 1043 (Div. 3)/E. Arithmetics Competition.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/contest/2132/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m, q; + cin >> n >> m >> q; + + vl af(n); + vl bf(m); + + cin >> af >> bf; + + vl a(n + 1); + vl b(m + 1); + + sort(all(af), greater<>()); + sort(all(bf), greater<>()); + + rep(i, n) { + a[i + 1] = a[i] + af[i]; + } + + rep(i, m) { + b[i + 1] = b[i] + bf[i]; + } + + while (q--) { + int x, y, z; + cin >> x >> y >> z; + + ll ans = 0; + ll low = 0; + ll high = min(x, z); + + while ((high - low) >= 3) { + ll third = (high - low) / 3; + ll mid1 = low + third; + ll mid2 = high - third; + + ll ans1 = a[mid1] + b[max(min(z - mid1, (ll)y), 0LL)]; + ll ans2 = a[mid2] + b[max(min(z - mid2, (ll)y), 0LL)]; + + ans = max({ans, ans1, ans2}); + + if (ans1 > ans2) { + high = mid2 - 1; + continue; + } + + if (ans2 > ans1) { + low = mid1 + 1; + continue; + } + + low = mid1 + 1; + high = mid2 - 1; + } + + nrep(i, low, high + 1) { + rmax(ans, a[i] + b[max(min(z - (ll)i, (ll)y), 0LL)]); + } + + cout << ans << '\n'; + } + } +} diff --git a/Codeforces Round 1043 (Div. 3)/F. Rada and the Chamomile Valley.cpp b/Codeforces Round 1043 (Div. 3)/F. Rada and the Chamomile Valley.cpp new file mode 100644 index 0000000..396c6c1 --- /dev/null +++ b/Codeforces Round 1043 (Div. 3)/F. Rada and the Chamomile Valley.cpp @@ -0,0 +1,223 @@ +/* Problem URL: https://codeforces.com/contest/2132/problem/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + V>> graph(n); + + rep(i, m) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].emplace_back(b, i); + graph[b].emplace_back(a, i); + } + + V>> tree(n); + + int inf = INT32_MAX >> 1; + + int t = 0; + vi low(n, inf); + vi time(n, inf); + V vis(n); + V bridge(m); + vi ans(n, inf); + + function dfs = [&](int i, int p) { + low[i] = t; + time[i] = t; + t++; + + vis[i] = true; + + for (auto [j, id] : graph[i]) { + if (j == p) { + continue; + } + + if (vis[j]) { + rmin(low[i], time[j]); + continue; + } + + dfs(j, i); + tree[i].emplace_back(j, id); + rmin(low[i], low[j]); + + if (low[j] > time[i]) { + bridge[id] = true; + } + } + }; + + dfs(0, -1); + + vi tmp; + + function search = [&](int i) { + if (i == n - 1) { + return true; + } + + for (auto [j, id] : tree[i]) { + if (search(j)) { + if (bridge[id]) { + tmp.push_back(i); + tmp.push_back(j); + rmin(ans[i], id); + rmin(ans[j], id); + } + + return true; + } + } + + return false; + }; + + search(0); + + set> fds; + repv(i, tmp) { + fds.emplace(ans[i], i); + } + + int q; + cin >> q; + + if (tmp.empty()) { + while (q--) { + int t; + cin >> t; + cout << "-1 "; + } + cout << '\n'; + + continue; + } + + + auto getans = [&]() { + queue q; + vi level(n, inf); + + for (auto [id, i] : fds) { + q.push(i); + level[i] = 0; + } + + while (!q.empty()) { + int i = q.front(); + q.pop(); + + for (auto [j, id] : graph[i]) { + if (level[j] > level[i] + 1) { + level[j] = level[i] + 1; + ans[j] = ans[i]; + q.push(j); + continue; + } + + if (level[j] == level[i] + 1) { + rmin(ans[j], ans[i]); + } + } + } + }; + + getans(); + + while (q--) { + int x; + cin >> x; + + cout << ans[x - 1] + 1 << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1044 (Div. 2)/A. Redstone?.cpp b/Codeforces Round 1044 (Div. 2)/A. Redstone?.cpp new file mode 100644 index 0000000..8941bd9 --- /dev/null +++ b/Codeforces Round 1044 (Div. 2)/A. Redstone?.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://codeforces.com/contest/2133/problem/0 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + vi count(101); + + bool pos = false; + rep(i, n) { + count[fds[i]]++; + if (count[fds[i]] > 1) { + pos = true; + break; + } + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1044 (Div. 2)/B. Villagers.cpp b/Codeforces Round 1044 (Div. 2)/B. Villagers.cpp new file mode 100644 index 0000000..dc485e1 --- /dev/null +++ b/Codeforces Round 1044 (Div. 2)/B. Villagers.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://codeforces.com/contest/2133/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl fds(n); + cin >> fds; + + sort(all(fds), greater<>()); + + ll ans = 0; + for (int i = 0; i + 1 < n; i += 2) { + ans += max(fds[i], fds[i + 1]); + } + + if (n & 1) { + ans += fds.back(); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1044 (Div. 2)/C. The Nether.cpp b/Codeforces Round 1044 (Div. 2)/C. The Nether.cpp new file mode 100644 index 0000000..0733107 --- /dev/null +++ b/Codeforces Round 1044 (Div. 2)/C. The Nether.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/contest/2133/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi act(n); + string tmp = to_string(n) + " "; + rep(i, n) { + act[i] = i + 1; + tmp += to_string(i + 1); + tmp += " "; + } + + vi dp(n); + + rep(i, n) { + cout << "? " << i + 1 << ' ' << tmp << endl; + int ans; + cin >> ans; + + dp[i] = ans; + } + + int maximal = 0; + int choice = 0; + rep(i, n) { + if (dp[i] > maximal) { + maximal = dp[i]; + choice = i; + } + } + + vi ans = {choice + 1}; + + while (dp[choice] > 1) { + int now = dp[choice]; + + vi attempt; + rep(i, n) { + if (dp[i] == now - 1) { + attempt.push_back(i); + } + } + + int tmp = 0; + repv(i, attempt) { + cout << "? " << choice + 1 << " 2 " << choice + 1 << ' ' << i + 1 << endl; + int ans; + cin >> ans; + if (ans > 1) { + tmp = i; + break; + } + } + + ans.push_back(tmp + 1); + choice = tmp; + } + + cout << "! " << ans.size() << ' '; + repv(i, ans) { + cout << i << ' '; + } + cout << endl; + } +} diff --git a/Codeforces Round 1044 (Div. 2)/D. Chicken Jockey.cpp b/Codeforces Round 1044 (Div. 2)/D. Chicken Jockey.cpp new file mode 100644 index 0000000..6672110 --- /dev/null +++ b/Codeforces Round 1044 (Div. 2)/D. Chicken Jockey.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/contest/2133/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + vvl memo(n, vl(3, -1)); + function dp = [&](int i, int s) { + if (i >= n) { + return 0LL; + } + + ll &ans = memo[i][s]; + if (ans != -1) { + return ans; + } + + if (s == 2) { + return ans = fds[i] - 1 + min(dp(i + 1, 0), dp(i + 1, 2)); + } + + if (s == 1) { + return ans = max(fds[i] - i, 0LL) + min(dp(i + 1, 0), dp(i + 1, 2)); + } + + return ans = fds[i] + min(dp(i + 1, 0), dp(i + 1, 1)); + }; + + cout << dp(0, 0) << '\n'; + } +} diff --git a/Codeforces Round 1044 (Div. 2)/E. I Yearned For The Mines.cpp b/Codeforces Round 1044 (Div. 2)/E. I Yearned For The Mines.cpp new file mode 100644 index 0000000..5369434 --- /dev/null +++ b/Codeforces Round 1044 (Div. 2)/E. I Yearned For The Mines.cpp @@ -0,0 +1,188 @@ +/* Problem URL: https://codeforces.com/contest/2133/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vvi graph(n); + vi count(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + count[a]++; + count[b]++; + } + + vi color(n); + vi rem; + vi checks; + + function dfs = [&](int i, int p) { + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i); + } + + int g = 0; + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + if (color[j] == 1) { + color[i] = 2; + break; + } + + if (color[j] == 0) { + g++; + if (g >= 3) { + color[i] = 2; + break; + } + } + } + + if (color[i] == 2) { + rem.push_back(i + 1); + for (auto j : graph[i]) { + if (color[j] == 2) { + continue; + } + count[j]--; + count[i]--; + } + } else if (g == 2) { + color[i] = 1; + } + }; + + dfs(0, -1); + + V vis(n); + function get = [&](int i) { + vis[i] = true; + checks.push_back(i + 1); + + if (color[i] == 2) { + return; + } + + for (auto j : graph[i]) { + if (vis[j] || color[j] == 2) { + continue; + } + + get(j); + } + }; + + rep(i, n) { + if (vis[i] || count[i] > 1) { + continue; + } + + get(i); + } + + cout << rem.size() + checks.size() << '\n'; + + for (auto j : rem) { + cout << "2 " << j << '\n'; + } + + for (auto j : checks) { + cout << "1 " << j << '\n'; + } + } +} diff --git a/Codeforces Round 1045 (Div. 2)/A. Painting With Two Colors.cpp b/Codeforces Round 1045 (Div. 2)/A. Painting With Two Colors.cpp new file mode 100644 index 0000000..2a20c23 --- /dev/null +++ b/Codeforces Round 1045 (Div. 2)/A. Painting With Two Colors.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://codeforces.com/contest/2134/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, a, b; + cin >> n >> a >> b; + + if (((n & 1) != (b & 1)) || (a > b && (n & 1) != (a & 1))) { + cout << "NO\n"; + continue; + } + + cout << "YES\n"; + } +} diff --git a/Codeforces Round 1045 (Div. 2)/B. Add 0 or K.cpp b/Codeforces Round 1045 (Div. 2)/B. Add 0 or K.cpp new file mode 100644 index 0000000..cedcef9 --- /dev/null +++ b/Codeforces Round 1045 (Div. 2)/B. Add 0 or K.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/contest/2134/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + ll k; + cin >> n >> k; + + vl a(n); + cin >> a; + + if (k & 1) { + repv(i, a) { + if (i & 1) { + i += k; + } + } + + cout << a; + continue; + } + + ll at = k + 1; + + repv(i, a) { + i += (i % at) * k; + } + + cout << a; + } +} diff --git a/Codeforces Round 1045 (Div. 2)/C. Even Larger.cpp b/Codeforces Round 1045 (Div. 2)/C. Even Larger.cpp new file mode 100644 index 0000000..e035f06 --- /dev/null +++ b/Codeforces Round 1045 (Div. 2)/C. Even Larger.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/contest/2134/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + while (t--) { + int n; + cin >> n; + + vl a(n); + cin >> a; + + ll ans = 0; + for (int i = 1; i < n; i += 2) { + ll sum = a[i - 1]; + if (i < n - 1) { + sum += a[i + 1]; + } + + if (sum > a[i]) { + ll diff = sum - a[i]; + if (i < n - 1) { + if (a[i + 1] <= diff) { + a[i + 1] = 0; + } else { + a[i + 1] -= diff; + } + } + + ans += diff; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1045 (Div. 2)/D. Sliding Tree.cpp b/Codeforces Round 1045 (Div. 2)/D. Sliding Tree.cpp new file mode 100644 index 0000000..fcca567 --- /dev/null +++ b/Codeforces Round 1045 (Div. 2)/D. Sliding Tree.cpp @@ -0,0 +1,168 @@ +/* Problem URL: https://codeforces.com/contest/2134/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + if (n == 1) { + cout << "-1\n"; + continue; + } + + vvi graph(n); + + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi parent(n); + + auto bfs = [&](int i) { + const int inf = INT32_MAX >> 1; + vi dis(n, inf); + queue q; + + dis[i] = 0; + q.push(i); + + int ans = 0; + + while (!q.empty()) { + i = q.front(); + q.pop(); + + repv(j, graph[i]) { + if (dis[j] > dis[i] + 1) { + dis[j] = dis[i] + 1; + q.push(j); + ans = j; + parent[j] = i; + } + } + } + + return ans; + }; + + int end1 = bfs(0); + fill(all(parent), -1); + int end2 = bfs(end1); + + int a, b, c; + int p = end2; + int now = parent[end2]; + + bool pos = false; + + while (parent[now] != -1 && !pos) { + repv(j, graph[now]) { + if (j == p || j == parent[now]) { + continue; + } + + a = p; + b = now; + c = j; + pos = true; + break; + } + + p = now; + now = parent[now]; + } + + if (pos) { + cout << a + 1 << ' ' << b + 1 << ' ' << c + 1 << '\n'; + continue; + } + + cout << "-1\n"; + } +} diff --git a/Codeforces Round 1046 (Div. 2)/A. In the Dream.cpp b/Codeforces Round 1046 (Div. 2)/A. In the Dream.cpp new file mode 100644 index 0000000..f03ea70 --- /dev/null +++ b/Codeforces Round 1046 (Div. 2)/A. In the Dream.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/contest/2136/problem/0 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int a, b, c, d; + cin >> a >> b >> c >> d; + + auto solve = [&]() { + c -= a; + d -= b; + + return (min(a, b) + 1) * 2 >= max(a, b) && (min(c, d) + 1) * 2 >= max(c, d); + }; + + cout << (solve() ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1046 (Div. 2)/B. Like the Bitset.cpp b/Codeforces Round 1046 (Div. 2)/B. Like the Bitset.cpp new file mode 100644 index 0000000..f006079 --- /dev/null +++ b/Codeforces Round 1046 (Div. 2)/B. Like the Bitset.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/contest/2136/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + string a; + cin >> a; + + int maximal = 0; + int count = 0; + repv(i, a) { + if (i == '0') { + rmax(maximal, count); + count = 0; + continue; + } + + count++; + } + + rmax(maximal, count); + + if (maximal >= k) { + cout << "NO\n"; + continue; + } + + cout << "YES\n"; + + int now = 1; + vi ans(n); + + rep(i, n) { + if (a[i] == '1') { + ans[i] = now; + now++; + } + } + + rep(i, n) { + if (a[i] == '0') { + ans[i] = now; + now++; + } + } + + cout << ans; + } +} diff --git a/Codeforces Round 1046 (Div. 2)/C. Against the Difference.cpp b/Codeforces Round 1046 (Div. 2)/C. Against the Difference.cpp new file mode 100644 index 0000000..7efa529 --- /dev/null +++ b/Codeforces Round 1046 (Div. 2)/C. Against the Difference.cpp @@ -0,0 +1,155 @@ +/* Problem URL: https://codeforces.com/contest/2136/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + int now = 0; + vi prev(n + 1, -1); + vvi next(n, vi(25, -1)); + + rep(i, n) { + if (prev[fds[i]] == -1) { + prev[fds[i]] = i; + continue; + } + + next[prev[fds[i]]][0] = i; + prev[fds[i]] = i; + } + + for (int i = n - 1; i >= 0; i--) { + nrep(j, 1, 25) { + if (next[i][j - 1] == -1) { + break; + } + next[i][j] = next[next[i][j - 1]][j - 1]; + } + } + + cout.flush(); + + vvi memo(n, vi(2, -1)); + + function dp = [&](int i, int p) { + if (i >= n) { + return 0; + } + + int &ans = memo[i][p]; + if (ans != -1) { + return ans; + } + + if (p == 1) { + int now = i; + // int c = 1; + rep(j, 25) { + if (((fds[i] - 1) >> j) & 1) { + if (next[now][j] == -1) { + now = -1; + break; + } + now = next[now][j]; + // c += 1 << j; + } + } + + if (now == -1) { + return 0; + } + + return ans = max(dp(now + 1, 0), dp(now + 1, 1)) + fds[i]; + } + + return ans = max(dp(i + 1, 0), dp(i + 1, 1)); + }; + + cout << max(dp(0, 1), dp(0, 0)) << '\n'; + } +} diff --git a/Codeforces Round 1046 (Div. 2)/D. For the Champion.cpp b/Codeforces Round 1046 (Div. 2)/D. For the Champion.cpp new file mode 100644 index 0000000..f94b0b4 --- /dev/null +++ b/Codeforces Round 1046 (Div. 2)/D. For the Champion.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/contest/2136/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + ll lim = 1e9; + + V> fds(n); + int choice1 = 0; + ll bestdis1 = INT64_MAX >> 1; + int choice2 = 0; + ll bestdis2 = INT64_MAX >> 1; + rep(i, n) { + cin >> fds[i].first >> fds[i].second; + + if (abs(2 * lim - fds[i].first) + abs(2 * lim - fds[i].second) <= bestdis1) { + bestdis1 = abs(2 * lim - fds[i].first) + abs(2 * lim - fds[i].second); + choice1 = i; + } + + if (abs(2 * lim - fds[i].first) + abs(2 * (-lim) - fds[i].second) <= bestdis2) { + bestdis2 = abs(2 * lim - fds[i].first) + abs(2 * (-lim) - fds[i].second); + choice2 = i; + } + } + + auto query = [&](char op) { + cout << "? " << op << ' ' << lim << endl; + ll ans; + cin >> ans; + return ans; + }; + + query('R'); + query('R'); + query('U'); + ll one = query('U'); + one = one - 4 * lim + fds[choice1].first + fds[choice1].second; + + query('D'); + query('D'); + query('D'); + ll two = query('D'); + two = two - 4 * lim + fds[choice2].first - fds[choice2].second; + + ll x = (one + two) / 2; + ll y = one - x; + + cout << "! " << x << ' ' << y << endl; + } +} diff --git a/Codeforces Round 1046 (Div. 2)/E. By the Assignment.cpp b/Codeforces Round 1046 (Div. 2)/E. By the Assignment.cpp new file mode 100644 index 0000000..9e0c99c --- /dev/null +++ b/Codeforces Round 1046 (Div. 2)/E. By the Assignment.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/2136/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + ll v; + cin >> n >> m >> v; + + vi fds(n); + cin >> fds; + + vvi graph(n); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + int act = min(a, b); + int to = max(a, b); + graph[act].push_back(to); + } + + V vis(n); + vl check(n, -1); + + function dfs = [&](int i) { + vis[i] = true; + + ll &ans = check[i]; + if (ans != -1) { + return ans; + } + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + } + }; + } +} diff --git a/Codeforces Round 1047 (Div. 3)/A. Collatz Conjecture.cpp b/Codeforces Round 1047 (Div. 3)/A. Collatz Conjecture.cpp new file mode 100644 index 0000000..8307c14 --- /dev/null +++ b/Codeforces Round 1047 (Div. 3)/A. Collatz Conjecture.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://codeforces.com/contest/2137/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int k, x; + cin >> k >> x; + + rep(i, k) { + x *= 2; + } + + cout << x << '\n'; + } +} diff --git a/Codeforces Round 1047 (Div. 3)/B. Fun Permutation.cpp b/Codeforces Round 1047 (Div. 3)/B. Fun Permutation.cpp new file mode 100644 index 0000000..5bccc15 --- /dev/null +++ b/Codeforces Round 1047 (Div. 3)/B. Fun Permutation.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/contest/2137/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl fds(n); + cin >> fds; + + if (n == 2) { + if (fds[0] == 1) { + cout << "2 1\n"; + } else { + cout << "1 2\n"; + } + continue; + } + + rep(i, n) { + if (fds[i] == n) { + cout << n << ' '; + continue; + } + cout << n - fds[i] << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 1047 (Div. 3)/C. Maximum Even Sum.cpp b/Codeforces Round 1047 (Div. 3)/C. Maximum Even Sum.cpp new file mode 100644 index 0000000..a0ba78b --- /dev/null +++ b/Codeforces Round 1047 (Div. 3)/C. Maximum Even Sum.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/contest/2137/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll a, b; + cin >> a >> b; + + if ((a & 1) && (b & 1)) { + cout << a * b + 1 << '\n'; + continue; + } + + if ((!(a & 1) && (b & 1)) || ((a & 1) && (b & 3))) { + cout << "-1\n"; + continue; + } + + cout << ((a * b) >> 1) + 2 << '\n'; + } +} diff --git a/Codeforces Round 1047 (Div. 3)/D. Replace with Occurrences.cpp b/Codeforces Round 1047 (Div. 3)/D. Replace with Occurrences.cpp new file mode 100644 index 0000000..e9d48e5 --- /dev/null +++ b/Codeforces Round 1047 (Div. 3)/D. Replace with Occurrences.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/contest/2137/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + V> b(n); + rep(i, n) { + cin >> b[i].first; + b[i].second = i; + } + + sort(b.begin(), b.end()); + + int i = 0; + int now = 0; + int count = 0; + int prev = 0; + + vi a(n); + + bool pos = true; + + while (i < n) { + if (count == prev) { + count = 0; + prev = b[i].first; + now++; + } + + if (b[i].first != prev) { + pos = false; + break; + } + + a[b[i].second] = now; + i++; + count++; + } + + if (count != prev) { + pos = false; + } + + if (!pos) { + cout << "-1\n"; + continue; + } + + cout << a; + } +} diff --git a/Codeforces Round 1047 (Div. 3)/E. Mexification.cpp b/Codeforces Round 1047 (Div. 3)/E. Mexification.cpp new file mode 100644 index 0000000..b31be78 --- /dev/null +++ b/Codeforces Round 1047 (Div. 3)/E. Mexification.cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/contest/2137/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, k; + cin >> n >> k; + + vl a(n); + cin >> a; + + vi count(n + 3); + + auto simulate = [&]() { + repv(i, a) { + count[i]++; + } + + int now = -1; + while (count[now + 1] == 1) { + now++; + } + + int tmp = now + 1; + while (count[tmp] > 0) { + tmp++; + } + + repv(i, a) { + if (i > tmp || count[i] > 1) { + i = tmp; + } + } + }; + + simulate(); + + if (k == 1) { + cout << accumulate(all(a), 0LL) << '\n'; + continue; + } + k--; + + fill(all(count), 0); + + simulate(); + + if (k == 1) { + cout << accumulate(all(a), 0LL) << '\n'; + continue; + } + k--; + + fill(all(count), 0); + repv(i, a) { + count[i]++; + } + + int now = -1; + while (count[now + 1] == 1) { + now++; + } + + int tmp = now + 1; + while (count[tmp] > 1) { + tmp++; + } + + ll ans = 0; + repv(i, a) { + if (i > now) { + if (tmp == now + 1) { + ans += (k & 1) ? now + 1 : now + 2; + } else { + ans += (k & 1) ? now + 2 : now + 1; + } + continue; + } + ans += i; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1047 (Div. 3)/F. Prefix Maximum Invariance.cpp b/Codeforces Round 1047 (Div. 3)/F. Prefix Maximum Invariance.cpp new file mode 100644 index 0000000..d6ef484 --- /dev/null +++ b/Codeforces Round 1047 (Div. 3)/F. Prefix Maximum Invariance.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/contest/2137/problem/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + vi a(n); + vi b(n); + cin >> a >> b; + + vvi sparse(n, vi(30)); + + rep(i, n) { + sparse[i][0] = a[i]; + } + + nrep(j, 1, 30) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + sparse[i][j] = max(sparse[i][j - 1], sparse[i + (1 << (j - 1))][j - 1]); + } + } + + auto query = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + + return max(sparse[l][log], sparse[r - (1 << log) + 1][log]); + }; + + ll ans = 0; + + nrep(i, 0, n) { + if (a[i] == b[i]) { + ans += (i + 1) * (n - i); + continue; + } + + int low = 0; + int high = i - 1; + ll act = -1; + while (low <= high) { + int mid = (low + high) >> 1; + + if (query(mid, i - 1) >= max(b[i], a[i])) { + act = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + if (act == -1) { + continue; + } + + ans += (act + 1) * (n - i); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1048 (Div. 2)/A. Maple and Multiplication.cpp b/Codeforces Round 1048 (Div. 2)/A. Maple and Multiplication.cpp new file mode 100644 index 0000000..903e82b --- /dev/null +++ b/Codeforces Round 1048 (Div. 2)/A. Maple and Multiplication.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/contest/2139/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int a, b; + cin >> a >> b; + + if (a == b) { + cout << "0\n"; + continue; + } + + int gcd = __gcd(a, b); + if (a == gcd || b == gcd) { + cout << "1\n"; + continue; + } + + cout << "2\n"; + } +} diff --git a/Codeforces Round 1049 (Div. 2)/A. Shift Sort.cpp b/Codeforces Round 1049 (Div. 2)/A. Shift Sort.cpp new file mode 100644 index 0000000..7308d99 --- /dev/null +++ b/Codeforces Round 1049 (Div. 2)/A. Shift Sort.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/contest/2140/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + string a; + cin >> a; + + int ans = 0; + + for (int i = n - 1; i >= 0; i--) { + if (a[i] == '0') { + rep(j, i) { + if (a[j] == '1') { + a[j] = '0'; + a[i] = '1'; + ans++; + break; + } + } + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1049 (Div. 2)/B. Another Divisibility Problem.cpp b/Codeforces Round 1049 (Div. 2)/B. Another Divisibility Problem.cpp new file mode 100644 index 0000000..48604f7 --- /dev/null +++ b/Codeforces Round 1049 (Div. 2)/B. Another Divisibility Problem.cpp @@ -0,0 +1,91 @@ +/* Problem URL: https://codeforces.com/contest/2140/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll x; + cin >> x; + + cout << x * 2 << '\n'; + } +} diff --git a/Codeforces Round 1049 (Div. 2)/C. Ultimate Value.cpp b/Codeforces Round 1049 (Div. 2)/C. Ultimate Value.cpp new file mode 100644 index 0000000..3de1318 --- /dev/null +++ b/Codeforces Round 1049 (Div. 2)/C. Ultimate Value.cpp @@ -0,0 +1,208 @@ +/* Problem URL: https://codeforces.com/contest/2140/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + if (n == 1) { + cout << fds[0] << '\n'; + continue; + } + + ll total = 0; + rep(i, n) { + if (i & 1) { + total -= fds[i]; + } else { + total += fds[i]; + } + } + + vl vet; + rep(i, n) { + if (i & 1) { + vet.push_back(fds[i] * 2); + } + } + + int now = 1; + repv(i, vet) { + i += now; + now += 2; + } + + int size = vet.size(); + + while (__builtin_popcount(vet.size()) != 1) { + vet.push_back(0); + } + + vl seg(vet.size() * 2); + vl lazy(vet.size() * 2); + + rep(i, vet.size()) { + seg[i + vet.size()] = vet[i]; + } + + for (int i = vet.size() - 1; i > 0; i--) { + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + } + + auto propagate = [&](int i) { + if (lazy[i] == 0) { + return; + } + + if (i >= vet.size()) { + seg[i] += lazy[i]; + lazy[i] = 0; + return; + } + + seg[i] += lazy[i]; + lazy[i * 2] += lazy[i]; + lazy[i * 2 + 1] += lazy[i]; + lazy[i] = 0; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] += v; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + propagate(i); + if (l > tr || r < tl) { + return 0LL; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return max(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + ll ans = 0; + + int ind = 0; + for (int i = 0; i < n; i += 2) { + ll tmp = query(1, 0, vet.size() - 1, 0, size - 1); + + rmax(ans, tmp - fds[i] * 2); + + update(1, 0, vet.size() - 1, 0, ind - 1, 2); + update(1, 0, vet.size() - 1, ind + 1, size - 1, -2); + ind++; + } + + int tmp = n; + if (tmp % 2 == 0) { + tmp--; + } + + cout << max(ans + total, total + tmp - 1) << '\n'; + } +} diff --git a/Codeforces Round 147 (Div. 2)/E. Build String.cpp b/Codeforces Round 147 (Div. 2)/E. Build String.cpp new file mode 100644 index 0000000..2304c71 --- /dev/null +++ b/Codeforces Round 147 (Div. 2)/E. Build String.cpp @@ -0,0 +1,207 @@ +/* Problem URL: https://codeforces.com/problemset/problem/237/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a; + cin >> a; + + vi acount(26, 0); + int tmp = 0; + repv(i, a) { + if (acount[i - 'a'] == 0) { + tmp++; + } + acount[i - 'a']++; + } + + int n; + cin >> n; + size_t size = n + 2 + tmp; + + vector> graph(size); + vector> cost(size, vector(size, 0)); + vector> capacity(size, vector(size, 0)); + + auto addedge = [&](int u, int v, int c, int p){ + graph[u].push_back(v); + graph[v].push_back(u); + cost[u][v] = c; + cost[v][u] = -c; + capacity[u][v] = p; + }; + + vi act(26, -1); + int v = 1; + rep(i, 26) { + if (acount[i] > 0) { + act[i] = v; + addedge(v, size - 1, 0, acount[i]); + v++; + } + } + + int start = v; + + for (size_t i = 1; i <= n; i++) { + string b; + int lol; + cin >> b >> lol; + + vi bcount(26, 0); + repv(j, b) { + bcount[j - 'a']++; + } + + rep(j, 26) { + if (bcount[j] > 0 && acount[j] > 0) { + addedge(v, act[j], i, bcount[j]); + } + } + + addedge(0, v, 0, lol); + + v++; + } + + int inf = INT32_MAX >> 1; + vector p(size, inf); + vector d(size, -1); + + int s = 0; + int t = size - 1; + + auto bfs = [&](){ + d[s] = 0; + vector inq(size, false); + queue q; + q.push(s); + + while (!q.empty()) { + int u = q.front(); + q.pop(); + inq[u] = false; + + for (auto v : graph[u]) { + if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) { + d[v] = d[u] + cost[u][v]; + p[v] = u; + if (!inq[v]) { + inq[v] = true; + q.push(v); + } + } + } + } + + return d[t] != inf; + }; + + vector vis(n * 2 + 2, false); + + + int flow = 0; + int c = 0; + while (flow < a.size()) { + fill(d.begin(), d.end(), inf); + fill(p.begin(), p.end(), -1); + if (!bfs()) { + break; + } + + int f = inf; + int cur = t; + while (cur != s) { + f = min(f, capacity[p[cur]][cur]); + cur = p[cur]; + } + + flow += f; + c += f * d[t]; + cur = t; + while (cur != s) { + capacity[p[cur]][cur] -= f; + capacity[cur][p[cur]] += f; + cur = p[cur]; + } + } + + if (flow < a.size()) { + cout << "-1\n"; + return 0; + } + + cout << c << '\n'; + +} diff --git a/Codeforces Round 199 (Div. 2)/E. Xenia and Tree.cpp b/Codeforces Round 199 (Div. 2)/E. Xenia and Tree.cpp new file mode 100644 index 0000000..4f6b3e8 --- /dev/null +++ b/Codeforces Round 199 (Div. 2)/E. Xenia and Tree.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/problemset/problem/342/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + V used(n, false); + + vvi cd(n); + + vi sizes(n, 0); + function dfs = [&](int i, int p) { + sizes[i] = 1; + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + sizes[i] += dfs(j, i); + } + + return sizes[i]; + }; + + dfs(0, 0); + + function find_centroid = [&](int i, int n) { + for (auto j : graph[i]) { + if (sizes[j] > n / 2) { + return find_centroid(j, n); + } + } + + return i; + }; +} diff --git a/Codeforces Round 216 (Div. 2)/E. Valera and Queries.cpp b/Codeforces Round 216 (Div. 2)/E. Valera and Queries.cpp new file mode 100644 index 0000000..599aa0f --- /dev/null +++ b/Codeforces Round 216 (Div. 2)/E. Valera and Queries.cpp @@ -0,0 +1,195 @@ +/* Problem URL: https://codeforces.com/contest/369/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + V> segs(n); + for (auto &[l, r] : segs) { + cin >> l >> r; + } + + int inf = INT32_MAX >> 1; + + segs.emplace_back(0, 0); + segs.emplace_back(inf - 1, inf - 1); + n += 2; + + sortv(segs); + + while (__builtin_popcount(n) != 1) { + n++; + segs.emplace_back(inf - 1, inf - 1); + } + + vvi seg(n * 2); + + function build = [&](int i, int l, int r) { + if (l == r) { + seg[i].push_back(segs[i - n].second); + return; + } + + int mid = (l + r) / 2; + build(i * 2, l, mid); + build(i * 2 + 1, mid + 1, r); + merge(all(seg[i * 2]), all(seg[i * 2 + 1]), back_inserter(seg[i])); + }; + + build(1, 0, n - 1); + + while (q--) { + int cnt; + cin >> cnt; + vi points(cnt); + cin >> points; + + points.push_back(0); + points.push_back(inf); + + sortv(points); + points.erase(unique(all(points)), points.end()); + + int count = 0; + + nrep(i, 1, points.size()) { + auto find_l = [&](){ + int low = 0; + int high = n - 1; + int ans = 0; + while (low <= high) { + int mid = (low + high) / 2; + + if (segs[mid].first > points[i - 1]) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + return ans; + }; + + auto find_r = [&](){ + int low = 0; + int high = n - 1; + int ans = 0; + while (low <= high) { + int mid = (low + high) / 2; + + if (segs[mid].first < points[i]) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + return ans; + }; + + int tl = find_l(); + int tr = find_r(); + + // cout << points[i - 1] << ' ' << points[i] << ' ' << tl << ' ' << tr << '\n'; + + if (tl > tr) { + continue; + } + + function search = [&](int nod, int l, int r) -> int { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + auto itr = upper_bound(all(seg[nod]), points[i] - 1); + return itr - upper_bound(all(seg[nod]), points[i - 1]); + } + + int mid = (l + r) / 2; + return search(nod * 2, l, mid) + search(nod * 2 + 1, mid + 1, r); + }; + + count += search(1, 0, n - 1); + } + + cout << n - count - 1 << '\n'; + } +} diff --git a/Codeforces Round 291 (Div. 2)/C. Watto and Mechanism.cpp b/Codeforces Round 291 (Div. 2)/C. Watto and Mechanism.cpp new file mode 100644 index 0000000..38075e2 --- /dev/null +++ b/Codeforces Round 291 (Div. 2)/C. Watto and Mechanism.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/problemset/problem/514/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + ll mod = (1LL << 61) - 1; + mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); + ll b = uniform_int_distribution(1, mod - 1)(rng); + + unordered_set act; + + auto gethash = [=](const string &a){ + ll hash = 0; + + repv(i, a) { + hash = ((__int128)hash * b + i) % mod; + } + + return hash; + }; + + vl sufhash; + vl pow = {1}; + + function add = [&](string &a, ll hash, int i, bool diff) { + if (i >= a.size()) { + if (diff) { + act.insert(hash); + } + return; + } + + if (diff) { + hash = ((__int128)hash * pow[a.size() - i] + sufhash[i]) % mod; + act.insert(hash); + return; + } + + for (char j = 'a'; j <= 'c'; j++) { + add(a, ((__int128)hash * b + j) % mod, i + 1, j != a[i]); + } + }; + + while (n--) { + string a; + cin >> a; + + while (pow.size() <= a.size()) { + pow.push_back(((__int128)pow.back() * b) % mod); + } + + sufhash.resize(a.size()); + ll hash = 0; + for (int i = a.size() - 1; i >= 0; i--) { + hash = ((__int128)a[i] * pow[a.size() - i - 1] + hash) % mod; + sufhash[i] = hash; + } + + add(a, 0, 0, false); + } + + while (m--) { + string a; + cin >> a; + + ll hash = gethash(a); + if (act.find(hash) == act.end()) { + cout << "NO\n"; + } else { + cout << "YES\n"; + } + } +} diff --git a/Codeforces Round 304 (Div. 2)/E. Soldier and Traveling.cpp b/Codeforces Round 304 (Div. 2)/E. Soldier and Traveling.cpp new file mode 100644 index 0000000..91b6082 --- /dev/null +++ b/Codeforces Round 304 (Div. 2)/E. Soldier and Traveling.cpp @@ -0,0 +1,213 @@ +/* Problem URL: https://codeforces.com/problemset/problem/546/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int tmp, m; + cin >> tmp >> m; + + int n = tmp * 2 + 2; + + vvi graph(n); + V> edges; + + auto makeedge = [&](int a, int b, int c){ + graph[a].push_back(edges.size()); + edges.emplace_back(b, c, 0); + graph[b].push_back(edges.size()); + edges.emplace_back(a, 0, 0); + }; + + int inf = INT32_MAX >> 1; + + vi a(tmp); + vi b(tmp); + cin >> a >> b; + + int total = accumulate(all(b), 0); + if (total != accumulate(all(a), 0)) { + cout << "NO\n"; + return 0; + } + + int s = 0; + int t = n - 1; + + rep(i, tmp) { + makeedge(s, i + 1, a[i]); + makeedge(i + 1, i + tmp + 1, inf); + makeedge(i + tmp + 1, t, b[i]); + } + + while (m--) { + int a, b; + cin >> a >> b; + + makeedge(a, b + tmp, inf); + makeedge(b, a + tmp, inf); + } + + vi level(n); + + auto bfs = [&](){ + queue q; + q.push(s); + level[s] = 0; + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + for (auto id : graph[u]) { + auto [v, c, f] = edges[id]; + if (level[v] > level[u] + 1 && c != f) { + level[v] = level[u] + 1; + q.push(v); + } + } + } + + return level[t] != inf; + }; + + V vis(n); + + function dfs = [&](int i, int f){ + if (f == 0) { + return 0; + } + + if (i == t) { + return f; + } + + for (auto id : graph[i]) { + auto &[j, c, fj] = edges[id]; + if (vis[j] || level[j] <= level[i]) { + continue; + } + + int ans = dfs(j, min(f, c - fj)); + if (ans == 0) { + continue; + } + + auto &[_, __, fc] = edges[id ^ 1]; + + fj += ans; + fc -= ans; + + return ans; + } + + vis[i] = true; + + return 0; + }; + + int ans = 0; + while (true) { + fill(all(level), inf); + if (!bfs()) { + break; + } + + fill(all(vis), false); + + int count = 0; + while ((count = dfs(s, inf))) { + ans += count; + } + } + + if (ans != total) { + cout << "NO\n"; + return 0; + } + + vvi act(tmp, vi(tmp, 0)); + + rep(i, tmp) { + for (auto _ : graph[i + 1]) { + if ((_ & 1) == 1) { + continue; + } + + auto [j, __, f] = edges[_]; + act[i][j - tmp - 1] = f; + } + } + + cout << "YES\n" << act; +} diff --git a/Codeforces Round 305 (Div. 1)/B. Mike and Feet.cpp b/Codeforces Round 305 (Div. 1)/B. Mike and Feet.cpp new file mode 100644 index 0000000..28e8a43 --- /dev/null +++ b/Codeforces Round 305 (Div. 1)/B. Mike and Feet.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/contest/547/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + vl fds(n); + cin >> fds; + + stack> s; + vl sizes(n, -1); + rep(i, n) { + rmax(sizes[0], fds[i]); + + int tmp = i; + while (!s.empty() && s.top().first >= fds[i]) { + rmax(sizes[i - s.top().second - 1], s.top().first); + rmax(sizes[i - s.top().second], fds[i]); + tmp = s.top().second; + s.pop(); + } + + s.emplace(fds[i], tmp); + } + + while (!s.empty()) { + rmax(sizes[n - s.top().second - 1], s.top().first); + s.pop(); + } + + int i = n - 1; + while (i--) { + rmax(sizes[i], sizes[i + 1]); + } + + cout << sizes; +} diff --git a/Codeforces Round 321 (Div. 2)/C. Kefa and Park.cpp b/Codeforces Round 321 (Div. 2)/C. Kefa and Park.cpp new file mode 100644 index 0000000..2a32f1e --- /dev/null +++ b/Codeforces Round 321 (Div. 2)/C. Kefa and Park.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/problemset/problem/580/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V cat(n); + + rep(i, n) { + bool c; + cin >> c; + + cat[i] = c; + } + + vvi graph(n); + + nrep(i, 1, n) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + function dfs = [&](int i, int p, int c){ + if (cat[i]) { + c++; + } else { + c = 0; + } + + if (c > m) { + return 0; + } + + if (graph[i].size() == 1 && graph[i][0] == p) { + return 1; + } + + int ans = 0; + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + ans += dfs(j, i, c); + } + + return ans; + }; + + cout << dfs(0, 0, 0) << '\n'; +} diff --git a/Codeforces Round 367 (Div. 2)/D. Vasiliy's Multiset.cpp b/Codeforces Round 367 (Div. 2)/D. Vasiliy's Multiset.cpp new file mode 100644 index 0000000..a601f12 --- /dev/null +++ b/Codeforces Round 367 (Div. 2)/D. Vasiliy's Multiset.cpp @@ -0,0 +1,158 @@ +/* Problem URL: https://codeforces.com/problemset/problem/706/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +struct node { + int nodes[2]; + int count[2]; + + node(): count(0) { + fill(nodes, nodes + 2, 0); + } + + int& operator[](int i) { + return nodes[i]; + } +}; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V bits(1); + + while (n--) { + char op; + ll num; + cin >> op >> num; + + auto add = [&](ll num){ + int now = 0; + for (int i = 31; i >= 0; i--) { + int bit = (num >> i) & 1; + if (bits[now][bit] == 0) { + bits[now][bit] = bits.size(); + bits.emplace_back(); + } + + bits[now].count[bit]++; + now = bits[now][bit]; + } + }; + + auto rem = [&](ll num) { + int now = 0; + for (int i = 31; i >= 0; i--) { + int bit = (num >> i) & 1; + bits[now].count[bit]--; + if (bits[now].count[bit] == 0) { + bits[now][bit] = 0; + break; + } + now = bits[now][bit]; + } + }; + + function search = [&](ll num, int i, int count){ + if (count < 0) { + return num; + } + + ll bit = (num >> count) & 1; + if (bits[i][!bit] == 0) { + if (bits[i][bit] == 0) { + return num; + } + return search(num ^ (bit << count), bits[i][bit], count - 1); + } + + ll ans = search(num ^ (!bit << count), bits[i][!bit], count - 1); + if (ans > num || bits[i][bit] == 0) { + return ans; + } + + ll tmp = search(num ^ (bit << count), bits[i][bit], count - 1); + + return max(ans, tmp); + }; + + if (op == '+') { + add(num); + } else if (op == '-') { + rem(num); + } else { + cout << max(search(num, 0, 31), num) << '\n'; + } + } +} diff --git a/Codeforces Round 396 (Div. 2)/B. Mahmoud and a Triangle.cpp b/Codeforces Round 396 (Div. 2)/B. Mahmoud and a Triangle.cpp new file mode 100644 index 0000000..5ae0416 --- /dev/null +++ b/Codeforces Round 396 (Div. 2)/B. Mahmoud and a Triangle.cpp @@ -0,0 +1,90 @@ +/* Problem URL: https://codeforces.com/contest/766/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl vec(n); + cin >> vec; + sortv(vec); + nrep(i, 2, n) { + if (vec[i] < vec[i - 1] + vec[i - 2]) { + cout << "YES\n"; + return 0; + } + } + cout << "NO\n"; +} diff --git a/Codeforces Round 419 (Div. 2)/B. Karen and Coffee.cpp b/Codeforces Round 419 (Div. 2)/B. Karen and Coffee.cpp new file mode 100644 index 0000000..c1ec1c3 --- /dev/null +++ b/Codeforces Round 419 (Div. 2)/B. Karen and Coffee.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/problemset/problem/816/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k, q; + cin >> n >> k >> q; + + vi pref(2 * 1e5 + 1); + + while (n--) { + int a, b; + cin >> a >> b; + + pref[a - 1]++; + pref[b]--; + } + + n = 2 * 1e5 + 1; + nrep(i, 1, n) { + pref[i] += pref[i - 1]; + } + + while (__builtin_popcount(n) != 1) { + n++; + pref.push_back(0); + } + + vvi seg(n * 2); + nrep(i, n, n * 2) { + seg[i] = {pref[i - n]}; + } + + function build = [&](int i, int l, int r) { + if (l == r) { + return; + } + + int mid = (l + r) / 2; + build(i * 2, l, mid); + build(i * 2 + 1, mid + 1, r); + + merge(all(seg[i * 2]), all(seg[i * 2 + 1]), back_inserter(seg[i])); + }; + + build(1, 0, n - 1); + + while (q--) { + int tl, tr; + cin >> tl >> tr; + tl--, tr--; + + function search = [&](int i, int l, int r) { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + return (int)(seg[i].size() - (lower_bound(all(seg[i]), k) - seg[i].begin())); + } + + int mid = (l + r) / 2; + return search(i * 2, l, mid) + search(i * 2 + 1, mid + 1, r); + }; + + cout << search(1, 0, n - 1) << '\n'; + } +} diff --git a/Codeforces Round 447 (Div. 2)/A. QAQ.cpp b/Codeforces Round 447 (Div. 2)/A. QAQ.cpp new file mode 100644 index 0000000..e136047 --- /dev/null +++ b/Codeforces Round 447 (Div. 2)/A. QAQ.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/problemset/problem/894/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a; + cin >> a; + + vi dp1(a.size()); + vi dp2(a.size()); + vi dp3(a.size()); + + dp1[0] = a[0] == 'Q'; + + nrep(i, 1, a.size()) { + if (a[i] == 'Q') { + dp1[i]++; + dp3[i] += dp2[i - 1]; + + } + + if (a[i] == 'A') { + dp2[i] += dp1[i - 1]; + } + + dp1[i] += dp1[i - 1]; + dp2[i] += dp2[i - 1]; + dp3[i] += dp3[i - 1]; + } + + cout << dp3.back() << '\n'; +} diff --git a/Codeforces Round 460 (Div. 2)/D. Substring.cpp b/Codeforces Round 460 (Div. 2)/D. Substring.cpp new file mode 100644 index 0000000..c6d97bc --- /dev/null +++ b/Codeforces Round 460 (Div. 2)/D. Substring.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/problemset/problem/919/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + string a; + cin >> a; + + vvi graph(n); + while (m--) { + int u, v; + cin >> u >> v; + u--, v--; + + graph[u].push_back(v); + } + + vvi dp(n, vi(26, 0)); + V vis(n, false); + V current(n, false); + + function dfs = [&](int i){ + vis[i] = true; + current[i] = true; + + for (auto j : graph[i]) { + if (current[j]) { + return true; + } + + if (!vis[j]) { + if (dfs(j)) { + return true; + } + } + + rep(k, 26) { + rmax(dp[i][k], dp[j][k]); + } + } + + dp[i][a[i] - 'a']++; + current[i] = false; + return false; + }; + + int ans = 0; + rep(i, n) { + if (!vis[i]) { + if (dfs(i)) { + cout << "-1\n"; + return 0; + } + rmax(ans, *max_element(all(dp[i]))); + } + } + + cout << ans << '\n'; +} diff --git a/Codeforces Round 479 (Div. 3)/E. Cyclic Components.cpp b/Codeforces Round 479 (Div. 3)/E. Cyclic Components.cpp new file mode 100644 index 0000000..0006889 --- /dev/null +++ b/Codeforces Round 479 (Div. 3)/E. Cyclic Components.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/problemset/problem/977/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + + vi deegree(n, 0); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + deegree[a]++; + deegree[b]++; + } + + V vis(n, false); + + function dfs = [&](int i){ + vis[i] = true; + + bool pos = true; + if (deegree[i] != 2) { + pos = false; + } + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + + if (!dfs(j)) { + pos = false; + } + } + + return pos; + }; + + int ans = 0; + rep(i, n) { + if (!vis[i]) { + if (dfs(i)) { + ans++; + } + } + } + + cout << ans << '\n'; +} diff --git a/Codeforces Round 498 (Div. 3)/F. Xor-Paths.cpp b/Codeforces Round 498 (Div. 3)/F. Xor-Paths.cpp new file mode 100644 index 0000000..13bb16d --- /dev/null +++ b/Codeforces Round 498 (Div. 3)/F. Xor-Paths.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/contest/1006/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + ll k; + cin >> n >> m >> k; + + + vvl fds(n, vl(m)); + cin >> fds; + int mid = n / 2; + + if (n == 1) { + ll ans = 0; + repv(i, fds[0]) { + ans ^= i; + } + + cout << (ans == k) << '\n'; + return 0; + } + + vvl ans1(m); + + function calc1 = [&](int i, int j, ll now) { + if (i >= n - mid - 1) { + ans1[j].push_back(now); + } + + if (j < m - 1) { + calc1(i, j + 1, now ^ fds[i][j + 1]); + } + if (i < n - mid - 1) { + calc1(i + 1, j, now ^ fds[i + 1][j]); + } + }; + + calc1(0, 0, fds[0][0]); + rep(j, m) { + sortv(ans1[j]); + } + + ll ans = 0; + + function calc2 = [&](int i, int j, ll now, int t) { + if (i == n - 1 && j == m - 1) { + auto lower = lower_bound(all(ans1[t]), k ^ now); + auto upper = upper_bound(all(ans1[t]), k ^ now); + ans += upper - lower; + return; + } + + if (i < n - 1) { + calc2(i + 1, j, now ^ fds[i + 1][j], t); + } + + if (j < m - 1) { + calc2(i, j + 1, now ^ fds[i][j + 1], t); + } + }; + + rep(j, m) { + calc2(n - mid, j, fds[n - mid][j], j); + } + + cout << ans << '\n'; +} diff --git a/Codeforces Round 560 (Div. 3)/D. Almost All Divisors.cpp b/Codeforces Round 560 (Div. 3)/D. Almost All Divisors.cpp new file mode 100644 index 0000000..348979f --- /dev/null +++ b/Codeforces Round 560 (Div. 3)/D. Almost All Divisors.cpp @@ -0,0 +1,158 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1165/D */ + +/* what? */ +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vi check(1e6 + 1); + for (size_t i = 1; i <= 1e6; i++) { + check[i] = i; + } + + check[1] = 0; + + vi primes; + + for (size_t i = 2; i <= 1e6; i++) { + if (check[i] != i) { + continue; + } + + primes.push_back(i); + + for (size_t j = i * 2; j <= 1e6; j += i) { + check[j] = i; + } + } + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + if (n == 1) { + ll num; + cin >> num; + if (check[num] != num) { + cout << "-1\n"; + continue; + } + cout << num * num << '\n'; + continue; + } + + vl nums(n); + cin >> nums; + sortv(nums); + + ll num = nums.back() * nums.front(); + ll ans = num; + + bool pos = true; + + for (auto i : nums) { + if (num % i != 0) { + pos = false; + break; + } + } + + if (!pos) { + cout << "-1\n"; + continue; + } + + int count = 1; + for (size_t i = 0; primes[i] * primes[i] * primes[i] <= num; i++) { + int c = 0; + while (num % primes[i] == 0) { + c++; + num /= primes[i]; + } + + count *= c + 1; + } + + if (check[num] == num) { + count *= 2; + } else if (floor(sqrt(num)) * floor(sqrt(num)) == num && check[sqrt(num)] == floor(sqrt(num))) { + count *= 3; + } else if (num != 1) { + count *= 4; + } + + cout << (count - 2 == nums.size() ? ans : -1) << '\n'; + } +} diff --git a/Codeforces Round 595 (Div. 3)/E. By Elevator or Stairs?.cpp b/Codeforces Round 595 (Div. 3)/E. By Elevator or Stairs?.cpp new file mode 100644 index 0000000..06ab7e7 --- /dev/null +++ b/Codeforces Round 595 (Div. 3)/E. By Elevator or Stairs?.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1249/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, c; + cin >> n >> c; + + vi stairs(n - 1); + vi elevator(n - 1); + cin >> stairs >> elevator; + + vvi dp(n, vi(2)); + dp[0][0] = 0; + dp[0][1] = 0; + + nrep(i, 1, n) { + dp[i][0] = min(dp[i - 1][0] + stairs[i - 1], dp[i - 1][1] + elevator[i - 1] + c); + dp[i][1] = min(dp[i - 1][0] + stairs[i - 1], dp[i - 1][1] + elevator[i - 1]); + } + + rep(i, n) { + cout << min(dp[i][0], dp[i][1] + c) << ' '; + } + cout << '\n'; +} diff --git a/Codeforces Round 709 (Div. 1, based on Technocup 2021 Final Round)/C. Skyline Photo.cpp b/Codeforces Round 709 (Div. 1, based on Technocup 2021 Final Round)/C. Skyline Photo.cpp new file mode 100644 index 0000000..690dac5 --- /dev/null +++ b/Codeforces Round 709 (Div. 1, based on Technocup 2021 Final Round)/C. Skyline Photo.cpp @@ -0,0 +1,237 @@ +/* Problem URL: https://codeforces.com/contest/1483/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi h(n); + vl v(n); + cin >> h >> v; + + vi left(n); + vi right(n); + + stack> s; + rep(i, n) { + while (!s.empty() && s.top().first > h[i]) { + s.pop(); + } + + if (s.empty()) { + left[i] = -1; + } else { + left[i] = s.top().second; + } + + s.emplace(h[i], i); + } + + while (!s.empty()) { + s.pop(); + } + + for (int i = n - 1; i >= 0; i--) { + while (!s.empty() && s.top().first > h[i]) { + s.pop(); + } + + if (s.empty()) { + right[i] = -1; + } else { + right[i] = s.top().second; + } + + s.emplace(h[i], i); + } + + while (__builtin_popcount(v.size()) != 1) { + v.push_back(0); + } + + V seg(v.size() * 2); + V segzero(v.size() * 2); + + nrep(i, v.size(), v.size() * 2) { + seg[i] = v[i - v.size()]; + } + + for (int i = v.size() - 1; i > 0; i--) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + + function update = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + segzero[i] = true; + seg[i] = 0; + return; + } + + int mid = (l + r) / 2; + + update(i * 2, l, mid, tl, tr); + update(i * 2 + 1, mid + 1, r, tl, tr); + + segzero[i] = segzero[i * 2] && segzero[i * 2 + 1]; + seg[i] = (segzero[i * 2] ? 0 : seg[i * 2]) + (segzero[i * 2 + 1] ? 0 : seg[i * 2 + 1]); + }; + + function get = [&](int i, int l, int r, int tl, int tr) -> ll { + if (l > tr || r < tl || segzero[i]) { + return 0; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) / 2; + + return get(i * 2, l, mid, tl, tr) + get(i * 2 + 1, mid + 1, r, tl, tr); + }; + + priority_queue, V>, greater<>> pq; + rep(i, n) { + ll r = INT64_MAX >> 1; + ll l = INT64_MAX >> 1; + + if (right[i] != -1) { + r = get(1, 0, v.size() - 1, i, right[i] - 1); + } + + if (left[i] != -1) { + l = get(1, 0, v.size() - 1, left[i] + 1, i); + } + + pq.emplace(min(r, l), i); + } + + // vi ans(n + 1); + while (!pq.empty()) { + auto [vi, i] = pq.top(); + pq.pop(); + + if (get(1, 0, v.size() - 1, i, i) >= 0) { + continue; + } + + ll r = 0; + if (right[i] != -1) { + // r = pref[actright] - pref[i]; + r = get(1, 0, v.size() - 1, i, right[i] - 1); + } + + ll l = 0; + if (left[i] != -1) { + // l = pref[i + 1] - pref[actleft + 1]; + l = get(1, 0, v.size() - 1, left[i] + 1, i); + } + + if (r >= 0 && l >= 0) { + continue; + } + + if (r < l) { + // ans[i]++; + // ans[actright]--; + update(1, 0, v.size() - 1, i, right[i] - 1); + continue; + } + + // ans[i + 1]--; + // ans[actleft + 1]++; + update(1, 0, v.size() - 1, left[i] + 1, i); + } + + // ll act = ans[0] == 0 ? v[0] : 0; + // ll act = !iszero(0) ? v[0] : 0; + // cout << get(0) << ' '; + // ll act = 0; + // rep(i, n) { + // ans[i] += ans[i - 1]; + // if (ans[i] == 0) { + // if (!iszero(i)) { + // act += v[i]; + // } + // cout << get(i) << ' '; + // } + // cout << '\n'; + + cout << seg[1] << '\n'; +} diff --git a/Codeforces Round 784 (Div. 4)/H. Maximal AND.cpp b/Codeforces Round 784 (Div. 4)/H. Maximal AND.cpp new file mode 100644 index 0000000..43daa2a --- /dev/null +++ b/Codeforces Round 784 (Div. 4)/H. Maximal AND.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1669/H */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, k; + cin >> n >> k; + + vl count(31); + rep(i, n) { + ll num; + cin >> num; + rep(j, 31) { + if (num & (1 << j)) { + count[j]++; + } + } + } + + ll ans = 0; + size_t j = 31; + while (j--) { + if (count[j] + k >= n) { + ans |= 1 << j; + k -= n - count[j]; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 786 (Div. 3)/A. Number Transformation.cpp b/Codeforces Round 786 (Div. 3)/A. Number Transformation.cpp new file mode 100644 index 0000000..d2b0bd4 --- /dev/null +++ b/Codeforces Round 786 (Div. 3)/A. Number Transformation.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://codeforces.com/contest/1674/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int x, y; + cin >> x >> y; + + if (y % x != 0) { + cout << "0 0\n"; + continue; + } + cout << "1 " << y / x << '\n'; + } +} diff --git a/Codeforces Round 786 (Div. 3)/B. Dictionary.cpp b/Codeforces Round 786 (Div. 3)/B. Dictionary.cpp new file mode 100644 index 0000000..a1121d1 --- /dev/null +++ b/Codeforces Round 786 (Div. 3)/B. Dictionary.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://codeforces.com/contest/1674/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a; + cin >> a; + + int ans = (a[0] - 'a') * 25; + + if (a[0] > a[1]) { + ans += a[1] - 'a' + 1; + } else { + ans += a[1] - 'a'; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 786 (Div. 3)/C. Infinite Replacement.cpp b/Codeforces Round 786 (Div. 3)/C. Infinite Replacement.cpp new file mode 100644 index 0000000..fa5af45 --- /dev/null +++ b/Codeforces Round 786 (Div. 3)/C. Infinite Replacement.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/contest/1674/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a, t; + cin >> a >> t; + if (t.size() == 1 && t[0] == 'a') { + cout << "1\n"; + continue; + } + + size_t k = 0; + for (; k < t.size() && t[k] != 'a'; k++) {} + + if (k < t.size()) { + cout << "-1\n"; + continue; + } + + cout << (1LL << a.size()) << '\n'; + } +} diff --git a/Codeforces Round 786 (Div. 3)/D. A-B-C Sort.cpp b/Codeforces Round 786 (Div. 3)/D. A-B-C Sort.cpp new file mode 100644 index 0000000..c00d0e1 --- /dev/null +++ b/Codeforces Round 786 (Div. 3)/D. A-B-C Sort.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/1674/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vi a(n); + cin >> a; + + if (n == 1) { + cout << "YES\n"; + continue; + } + + // int maximus = max(a[n - 1], a[n - 2]); + int minim = min(a[n - 1], a[n - 2]); + + bool pos = true; + for (int i = n - 3; i > 0 && pos; i -= 2) { + if (i == 0) { + if (a[0] > minim) { + pos = false; + } + continue; + } + + int man = max(a[i], a[i - 1]); + + if (man > minim) { + pos = false; + continue; + } + + minim = min(a[i], a[i - 1]); + } + + if (a.size() & 1 && a[0] > minim) { + pos = false; + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 786 (Div. 3)/E. Breaking the Wall.cpp b/Codeforces Round 786 (Div. 3)/E. Breaking the Wall.cpp new file mode 100644 index 0000000..035062f --- /dev/null +++ b/Codeforces Round 786 (Div. 3)/E. Breaking the Wall.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/contest/1674/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll inf = INT64_MAX >> 3; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi a(n); + cin >> a; + + ll ans = inf; + ll minimal = inf; + + rep(i, n) { + ll m = a[i] / 2 + a[i] % 2; + rmin(ans, minimal + m); + rmin(minimal, m); + } + + rep(i, n) { + ll ans1 = a[i] / 2 + a[i] % 2; + + ll ans2 = inf; + if (i > 0) { + ans2 = a[i - 1]; + rmin(ans, ans1 + max((ans2 - ans1) / 2 + (ans2 - ans1) % 2, 0LL)); + } + + ll ans3 = inf; + if (i < n - 1) { + ans3 = a[i + 1]; + rmin(ans, ans1 + max((ans3 - ans1) / 2 + (ans3 - ans1) % 2, 0LL)); + } + + rmin(ans, max((ans2 - ans1) / 2 + (ans2 - ans1) % 2, 0LL) + max((ans3 - ans1) / 2 + (ans3 - ans1) % 2, 0LL) + min(ans1, max(ans2, ans3))); + } + + cout << ans << '\n'; +} diff --git a/Codeforces Round 786 (Div. 3)/F. Desktop Rearrangement.cpp b/Codeforces Round 786 (Div. 3)/F. Desktop Rearrangement.cpp new file mode 100644 index 0000000..3bce3ea --- /dev/null +++ b/Codeforces Round 786 (Div. 3)/F. Desktop Rearrangement.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/contest/1674/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, q; + cin >> n >> m >> q; + + V fds(n); + cin >> fds; + + string act(n * m, ' '); + + rep(i, n) { + rep(j, m) { + act[j * n + i] = fds[i][j]; + } + } + + int count = 0; + rep(i, act.size()) { + if (act[i] == '*') { + count++; + } + } + + int ans = 0; + rep(i, count) { + if (act[i] == '*') { + ans++; + } + } + + while (q--) { + int x, y; + cin >> x >> y; + x--, y--; + + if (act[y * n + x] == '*') { + act[y * n + x] = '.'; + + if (y * n + x < count) { + ans--; + } + if (act[count - 1] == '*') { + ans--; + } + count--; + } else { + act[y * n + x] = '*'; + if (y * n + x < count) { + ans++; + } + count++; + if (act[count - 1] == '*') { + ans++; + } + } + + cout << count - ans << '\n'; + } +} diff --git a/Codeforces Round 791 (Div. 2)/B. Stone Age Problem.cpp b/Codeforces Round 791 (Div. 2)/B. Stone Age Problem.cpp new file mode 100644 index 0000000..5f50103 --- /dev/null +++ b/Codeforces Round 791 (Div. 2)/B. Stone Age Problem.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/1679/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vl fds(n); + cin >> fds; + + ll total = accumulate(all(fds), 0LL); + + vi versions(n, 0); + + int ver = 0; + ll num = 0; + + while (q--) { + int x; + cin >> x; + + if (x == 1) { + int i; + ll nu; + cin >> i >> nu; + i--; + + if (versions[i] < ver) { + total -= num; + total += nu; + versions[i] = ver; + fds[i] = nu; + } else { + total -= fds[i]; + total += nu; + fds[i] = nu; + } + } else { + ll nu; + cin >> nu; + + ver++; + num = nu; + total = num * n; + } + + cout << total << '\n'; + } +} diff --git a/Codeforces Round 861 (Div. 2)/B. Playing in a Casino.cpp b/Codeforces Round 861 (Div. 2)/B. Playing in a Casino.cpp new file mode 100644 index 0000000..959d8c2 --- /dev/null +++ b/Codeforces Round 861 (Div. 2)/B. Playing in a Casino.cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://codeforces.com/contest/1808/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + while (t--) { + int n, m; + cin >> n >> m; + + vvl fds(n, vl(m)); + cin >> fds; + + vvl act(m, vl(n)); + + rep(i, n) { + rep(j, m) { + act[j][i] = fds[i][j]; + } + } + + ll total = 0; + + rep(i, m) { + sort(all(act[i]), greater<>()); + + ll now = act[i][0]; + + nrep(j, 1, n) { + total += now - act[i][j] * j; + now += act[i][j]; + } + } + + cout << total << '\n'; + } +} diff --git a/Codeforces Round 870 (Div. 2)/D. Running Miles.cpp b/Codeforces Round 870 (Div. 2)/D. Running Miles.cpp new file mode 100644 index 0000000..d66f6d7 --- /dev/null +++ b/Codeforces Round 870 (Div. 2)/D. Running Miles.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1826/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl fds(n); + cin >> fds; + + V> suf(n); + V> pref(n); + + auto maxi = [&](pair a, pair b) { + return a.first - abs(a.second - b.second) > b.first ? a : b; + }; + + pref[0] = {fds[0], 0}; + suf.back() = {fds.back(), n - 1}; + nrep(i, 1, n) { + pref[i] = maxi(pref[i - 1], {fds[i], i}); + suf[n - i - 1] = maxi(suf[n - i], {fds[n - i - 1], n - i - 1}); + } + + ll ans = 0; + nrep(i, 1, n - 1) { + rmax(ans, fds[i] + pref[i - 1].first + suf[i + 1].first - suf[i + 1].second + pref[i - 1].second); + } + + cout << ans << '\n'; + + } +} diff --git a/Codeforces Round 871 (Div. 4)/A. Love Story.cpp b/Codeforces Round 871 (Div. 4)/A. Love Story.cpp new file mode 100644 index 0000000..ea34c84 --- /dev/null +++ b/Codeforces Round 871 (Div. 4)/A. Love Story.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://codeforces.com/contest/1829/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string s; + cin >> s; + + string fds = "codeforces"; + + int count = 0; + rep(i, fds.size()) { + if (fds[i] != s[i]) { + count++; + } + } + cout << count << '\n'; + } +} diff --git a/Codeforces Round 871 (Div. 4)/B. Blank Space.cpp b/Codeforces Round 871 (Div. 4)/B. Blank Space.cpp new file mode 100644 index 0000000..33a6ddf --- /dev/null +++ b/Codeforces Round 871 (Div. 4)/B. Blank Space.cpp @@ -0,0 +1,100 @@ +/* Problem URL: https://codeforces.com/contest/1829/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + int count = 0; + int maximus = 0; + + while (n--) { + int num; + cin >> num; + if (num == 0) { + count++; + } else { + count = 0; + } + + rmax(maximus, count); + } + + cout << maximus << '\n'; + } +} diff --git a/Codeforces Round 871 (Div. 4)/C. Mr. Perfectly Fine.cpp b/Codeforces Round 871 (Div. 4)/C. Mr. Perfectly Fine.cpp new file mode 100644 index 0000000..8baca4d --- /dev/null +++ b/Codeforces Round 871 (Div. 4)/C. Mr. Perfectly Fine.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/contest/1829/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + ll allm = INT32_MAX; + ll firstm = INT32_MAX; + ll secondm = INT32_MAX; + + while (n--) { + ll cost; + string a; + cin >> cost >> a; + + if (a == "11") { + rmin(allm, cost); + } else if (a == "01") { + rmin(secondm, cost); + } else if (a == "10") { + rmin(firstm, cost); + } + } + + if (allm == INT32_MAX && (firstm == INT32_MAX || secondm == INT32_MAX)) { + cout << "-1\n"; + continue; + } + + cout << min(allm, firstm + secondm) << '\n'; + } +} diff --git a/Codeforces Round 871 (Div. 4)/D. Gold Rush.cpp b/Codeforces Round 871 (Div. 4)/D. Gold Rush.cpp new file mode 100644 index 0000000..ab6c190 --- /dev/null +++ b/Codeforces Round 871 (Div. 4)/D. Gold Rush.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/contest/1829/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, m; + cin >> n >> m; + + stack s; + s.push(n); + + while (!s.empty() && s.top() != m) { + ll tmp = s.top(); + s.pop(); + if (tmp % 3 != 0) { + continue; + } + + s.push(tmp / 3); + s.push(tmp - tmp / 3); + } + + cout << (s.empty() ? "NO\n" : "YES\n"); + } +} diff --git a/Codeforces Round 871 (Div. 4)/E. The Lakes.cpp b/Codeforces Round 871 (Div. 4)/E. The Lakes.cpp new file mode 100644 index 0000000..85fcd2c --- /dev/null +++ b/Codeforces Round 871 (Div. 4)/E. The Lakes.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/1829/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int n, m; + +vvl grid; +V> vis; + +ll dfs(int i, int j) +{ + vis[i][j] = true; + + int add[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + ll ans = 0; + + for (auto &k : add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < m && !vis[id][jd] && grid[id][jd] != 0) { + ans += dfs(id, jd); + } + } + + return ans + grid[i][j]; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + cin >> n >> m; + + vis.assign(n, V(m, false)); + grid.assign(n, vl(m)); + + + cin >> grid; + ll ans = 0; + + rep(i, n) { + rep(j, m) { + if (grid[i][j] != 0) { + rmax(ans, dfs(i, j)); + } + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 871 (Div. 4)/F. Forever Winter.cpp b/Codeforces Round 871 (Div. 4)/F. Forever Winter.cpp new file mode 100644 index 0000000..365982c --- /dev/null +++ b/Codeforces Round 871 (Div. 4)/F. Forever Winter.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/contest/1829/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + vvi graph(n); + vi count(n); + + int x = 0; + int y = 0; + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + count[a]++; + count[b]++; + graph[a].push_back(b); + graph[b].push_back(a); + } + + size_t choice = 0; + for (; count[choice] > 1; choice++) {} + + y = count[graph[choice][0]]; + for (auto i : graph[graph[choice][0]]) { + if (count[i] != y && count[i] > 1) { + x = count[i]; + break; + } + } + + cout << (x == 0 ? y : x) << ' ' << y - 1 << '\n'; + } +} diff --git a/Codeforces Round 871 (Div. 4)/G. Hits Different.cpp b/Codeforces Round 871 (Div. 4)/G. Hits Different.cpp new file mode 100644 index 0000000..0b175d5 --- /dev/null +++ b/Codeforces Round 871 (Div. 4)/G. Hits Different.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/contest/1829/problem/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vl dp(1e6 + 1, 0); + dp[1] = 1; + + vl stage(1e6 + 2, 0); + ll now = 1; + size_t cur = 1; + for (size_t st = 1; cur < (size_t)1e6;) { + for (; cur <= now && cur <= (size_t)1e6; cur++) { + stage[cur] = st; + } + st++; + now += st; + } + + for (ll i = 2; i <= (int)1e6; i++) { + dp[i] = i * i; + if (stage[i - stage[i] + 1] != stage[i] - 1) { + dp[i] += dp[i - stage[i]]; + continue; + } + if (stage[i - stage[i]] != stage[i] - 1) { + dp[i] += dp[i - stage[i] + 1]; + continue; + } + + dp[i] += dp[i - stage[i]] + dp[i - stage[i] + 1] - dp[i - (stage[i] - 1) * 2]; + } + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + cout << dp[n] << '\n'; + } +} diff --git a/Codeforces Round 871 (Div. 4)/H. Don't Blame Me.cpp b/Codeforces Round 871 (Div. 4)/H. Don't Blame Me.cpp new file mode 100644 index 0000000..17b3b71 --- /dev/null +++ b/Codeforces Round 871 (Div. 4)/H. Don't Blame Me.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/contest/1829/problem/H */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll mod = 1e9 + 7; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi nums(n); + cin >> nums; + + vl dp((1 << 6) + 1, 0); + + repv(i, nums) { + for (int j = 0; j < (1 << 6); j++) { + dp[i & j] = (dp[i & j] + dp[j]) % mod; + } + dp[i] = (dp[i] + 1) % mod; + } + + ll ans = 0; + for (int j = 0; j < (1 << 6); j++) { + if (__builtin_popcount(j) == k) { + ans = (ans + dp[j]) % mod; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 900 (Div. 3)/A. How Much Does Daytona Cost?.cpp b/Codeforces Round 900 (Div. 3)/A. How Much Does Daytona Cost?.cpp new file mode 100644 index 0000000..684928d --- /dev/null +++ b/Codeforces Round 900 (Div. 3)/A. How Much Does Daytona Cost?.cpp @@ -0,0 +1,94 @@ +/* Problem URL: https://codeforces.com/contest/1878/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + bool pos = false; + while (n--) { + int num; + cin >> num; + if (num == k) { + pos = true; + } + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 900 (Div. 3)/B. Aleksa and Stack.cpp b/Codeforces Round 900 (Div. 3)/B. Aleksa and Stack.cpp new file mode 100644 index 0000000..c724f5e --- /dev/null +++ b/Codeforces Round 900 (Div. 3)/B. Aleksa and Stack.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://codeforces.com/contest/1878/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl nums(n); + + nums[0] = 1; + nums[1] = 3; + + nrep(i, 2, n) { + nums[i] = nums[i - 1] + 2; + } + + cout << nums; + } +} diff --git a/Codeforces Round 900 (Div. 3)/C. Vasilije in Cacak.cpp b/Codeforces Round 900 (Div. 3)/C. Vasilije in Cacak.cpp new file mode 100644 index 0000000..3b25ab1 --- /dev/null +++ b/Codeforces Round 900 (Div. 3)/C. Vasilije in Cacak.cpp @@ -0,0 +1,92 @@ +/* Problem URL: https://codeforces.com/contest/1878/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, k, x; + cin >> n >> k >> x; + + ll maximus = k * (n + n - k + 1) / 2; + ll minimum = k * (k + 1) / 2; + + if (maximus >= x && minimum <= x) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } + } +} diff --git a/Codeforces Round 900 (Div. 3)/E. Iva & Pav.cpp b/Codeforces Round 900 (Div. 3)/E. Iva & Pav.cpp new file mode 100644 index 0000000..93098c8 --- /dev/null +++ b/Codeforces Round 900 (Div. 3)/E. Iva & Pav.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/contest/1878/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl nums(n); + cin >> nums; + + int q; + cin >> q; + + int log = 32 - __builtin_clz(n); + vvl sparse(n, vl(log)); + rep(i, n) { + sparse[i][0] = nums[i]; + } + nrep(i, 1, log) { + for (size_t j = 0; j + (1 << (i)) - 1 < n; j++) { + sparse[j][i] = sparse[j][i - 1] & sparse[j + (1 << (i - 1))][i - 1]; + } + } + + while (q--) { + ll l, k; + cin >> l >> k; + l--; + + ll low = l; + ll high = n - 1; + ll ans = -1; + while (low <= high) { + ll mid = (low + high) / 2; + + ll log = 0; + log = 31 - __builtin_clz(abs(mid - l) + 1); + + ll tmp = sparse[l][log] & sparse[mid - (1 << log) + 1][log]; + if (tmp >= k) { + ans = mid + 1; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + cout << ans << ' '; + } + cout << '\n'; + } +} diff --git a/Codeforces Round 905 (Div. 3)/A. Morning.cpp b/Codeforces Round 905 (Div. 3)/A. Morning.cpp new file mode 100644 index 0000000..d8901d4 --- /dev/null +++ b/Codeforces Round 905 (Div. 3)/A. Morning.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/contest/1883/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a; + cin >> a; + + vi act(a.size()); + rep(i, a.size()) { + if (a[i] == '0') { + act[i] = 10; + continue; + } + act[i] = a[i] - '0'; + } + + int now = 1; + ll count = 0; + repv(i, act) { + count += abs(now - i) + 1; + now = i; + } + + cout << count << '\n'; + } +} diff --git a/Codeforces Round 905 (Div. 3)/B. Chemistry.cpp b/Codeforces Round 905 (Div. 3)/B. Chemistry.cpp new file mode 100644 index 0000000..fc5b304 --- /dev/null +++ b/Codeforces Round 905 (Div. 3)/B. Chemistry.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://codeforces.com/contest/1883/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + string a; + cin >> a; + + int count[26] = {0}; + + repv(i, a) { + count[i - 'a']++; + } + + bool pos = true; + bool inv = false; + + rep(i, 26) { + if (count[i] & 1) { + if (k > 0) { + k--; + continue; + } + + if (!inv) { + inv = true; + continue; + } + + pos = false; + break; + } + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 905 (Div. 3)/C. Raspberries.cpp b/Codeforces Round 905 (Div. 3)/C. Raspberries.cpp new file mode 100644 index 0000000..a508fe2 --- /dev/null +++ b/Codeforces Round 905 (Div. 3)/C. Raspberries.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/1883/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi exists(20); + int odds = 0; + int evens = 0; + while (n--) { + int a; + cin >> a; + if (a & 1) { + odds++; + } else { + evens++; + } + exists[a]++; + } + + int minimal = INT32_MAX; + int prev = 0; + + rep(i, 20) { + if (exists[i]) { + prev = i; + } + if (i % k == 0 && prev > 0) { + rmin(minimal, (int)i - prev); + } + } + + if (k == 4) { + if (evens >= 2) { + minimal = 0; + } else if (evens >= 1 && odds >= 1) { + rmin(minimal, 1); + } else if (odds >= 2) { + rmin(minimal, 2); + } + } + + cout << minimal << '\n'; + } +} diff --git a/Codeforces Round 905 (Div. 3)/D. In Love.cpp b/Codeforces Round 905 (Div. 3)/D. In Love.cpp new file mode 100644 index 0000000..7acf73c --- /dev/null +++ b/Codeforces Round 905 (Div. 3)/D. In Love.cpp @@ -0,0 +1,158 @@ +/* Problem URL: https://codeforces.com/contest/1883/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll search(int i, int l, int r, int la, int ra, vi &seg) +{ + if (l > ra || r < la) { + return 0; + } + + if (l >= la && r <= ra) { + return seg[i]; + } + + int mid = (l + r) / 2; + return search(i * 2, l, mid, la, ra, seg) + search(i * 2 + 1, mid + 1, r, la, ra, seg); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int q; + cin >> q; + V> queries(q); + set nums; + for (auto &[op, l, r] : queries) { + cin >> op >> l >> r; + nums.insert(l); + nums.insert(r); + } + + map var; + int act = 0; + repv(i, nums) { + var[i] = act; + act++; + } + + int n = q * 2; + if (n > 1) { + n = 1 << (32 - __builtin_clz(n)); + } + + vi segstart(n * 2); + vi segend(n * 2); + + int internot = 0; + for (auto &[op, l, r] : queries) { + int al = var[l]; + int ar = var[r]; + if (op == '+') { + segstart[n + al]++; + for (size_t i = (n + al) / 2; i > 0; i /= 2) { + segstart[i] = segstart[i * 2] + segstart[i * 2 + 1]; + } + segend[n + ar]++; + for (size_t i = (n + ar) / 2; i > 0; i /= 2) { + segend[i] = segend[i * 2] + segend[i * 2 + 1]; + } + if (al > 0) { + internot += search(1, 0, n - 1, 0, al - 1, segend); + } + if (ar < n - 1) { + internot += search(1, 0, n - 1, ar + 1, n - 1, segstart); + } + } else { + segstart[n + al]--; + for (size_t i = (n + al) / 2; i > 0; i /= 2) { + segstart[i] = segstart[i * 2] + segstart[i * 2 + 1]; + } + segend[n + ar]--; + for (size_t i = (n + ar) / 2; i > 0; i /= 2) { + segend[i] = segend[i * 2] + segend[i * 2 + 1]; + } + if (al > 0) { + internot -= search(1, 0, n - 1, 0, al - 1, segend); + } + if (ar < n - 1) { + internot -= search(1, 0, n - 1, ar + 1, n - 1, segstart); + } + } + + if (internot > 0) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } + } +} diff --git a/Codeforces Round 905 (Div. 3)/E. Look Back.cpp b/Codeforces Round 905 (Div. 3)/E. Look Back.cpp new file mode 100644 index 0000000..9170fe5 --- /dev/null +++ b/Codeforces Round 905 (Div. 3)/E. Look Back.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/contest/1883/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl a(n); + cin >> a; + + ll ops = 0; + nrep(i, 1, n) { + while (a[i - 1] > a[i]) { + if (a[i] >= INT64_MAX >> 1) { + a[i - 1] -= (a[i] - 1); + a[i] = 1; + } + a[i] *= 2; + ops++; + } + } + + cout << ops << '\n'; + } +} diff --git a/Codeforces Round 905 (Div. 3)/G1. Dances (Easy version).cpp b/Codeforces Round 905 (Div. 3)/G1. Dances (Easy version).cpp new file mode 100644 index 0000000..ee155e8 --- /dev/null +++ b/Codeforces Round 905 (Div. 3)/G1. Dances (Easy version).cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/1883/problem/G1 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + multiset a; + multiset b; + + a.insert(1); + nrep(i, 1, n) { + ll num; + cin >> num; + a.insert(num); + } + + + rep(i, n) { + ll num; + cin >> num; + b.insert(num); + } + + auto itr = prev(a.end()); + while (itr != a.begin()) { + auto now = b.upper_bound(*itr); + if (now != b.end()) { + auto aux = itr; + itr--; + b.erase(now); + a.erase(aux); + } else { + itr--; + } + } + + auto now = b.upper_bound(*itr); + if (now != b.end()) { + b.erase(now); + a.erase(itr); + } + + cout << b.size() << '\n'; + } +} diff --git a/Codeforces Round 905 (Div. 3)/G2. Dances (Hard Version).cpp b/Codeforces Round 905 (Div. 3)/G2. Dances (Hard Version).cpp new file mode 100644 index 0000000..d2dd83c --- /dev/null +++ b/Codeforces Round 905 (Div. 3)/G2. Dances (Hard Version).cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/contest/1883/problem/G2 */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + multiset a; + multiset b; + + nrep(i, 1, n) { + ll num; + cin >> num; + a.insert(num); + } + + + rep(i, n) { + ll num; + cin >> num; + b.insert(num); + } + + auto itr = prev(a.end()); + while (itr != a.begin()) { + auto now = b.upper_bound(*itr); + if (now != b.end()) { + auto aux = itr; + itr--; + b.erase(now); + a.erase(aux); + } else { + itr--; + } + } + + auto now = b.upper_bound(*itr); + if (now != b.end()) { + b.erase(now); + a.erase(itr); + } + + ll high = *prev(b.end()); + if (high > m) { + cout << (m * (b.size() - 1)) << '\n'; + continue; + } + + cout << ((high - 1) * (b.size() - 1)) + ((m - high + 1) * (b.size())) << '\n'; + } +} diff --git a/Codeforces Round 918 (Div. 4)/F. Greetings.cpp b/Codeforces Round 918 (Div. 4)/F. Greetings.cpp new file mode 100644 index 0000000..dc5046a --- /dev/null +++ b/Codeforces Round 918 (Div. 4)/F. Greetings.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/contest/1915/problem/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template +using ordered_set = tree , rb_tree_tag , tree_order_statistics_node_update>; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ordered_set ord; + + int n; + cin >> n; + + V> fds(n); + repv(i, fds) { + cin >> i.first >> i.second; + } + + sortv(fds); + + ll ans = 0; + rep(i, n) { + ans += ord.order_of_key(fds[i].second); + ord.insert(fds[i].second); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 918 (Div. 4)/G. Bicycles.cpp b/Codeforces Round 918 (Div. 4)/G. Bicycles.cpp new file mode 100644 index 0000000..a6327e4 --- /dev/null +++ b/Codeforces Round 918 (Div. 4)/G. Bicycles.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1915/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + V>> graph(n); + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + a--, b--; + + graph[a].emplace_back(b, c); + graph[b].emplace_back(a, c); + } + + vl fact(n); + cin >> fact; + + auto dijkstra = [&]() { + priority_queue, V>, greater<>> pq; + ll inf = INT64_MAX >> 1; + vvl dis(n, vl(n, inf)); + + dis[0][0] = 0; + pq.emplace(0, 0, 0); + + while (!pq.empty()) { + auto [c, i, f] = pq.top(); + pq.pop(); + + if (fact[i] < fact[f]) { + f = i; + } + + if (c > dis[i][f]) { + continue; + } + + for (auto [j, cc] : graph[i]) { + if (dis[j][f] > c + cc * fact[f]) { + dis[j][f] = c + cc * fact[f]; + pq.emplace(c + cc * fact[f], j, f); + } + } + } + + return *min_element(all(dis[n - 1])); + }; + + cout << dijkstra() << '\n'; + } +} diff --git a/Codeforces Round 925 (Div. 3)/D. Divisible Pairs.cpp b/Codeforces Round 925 (Div. 3)/D. Divisible Pairs.cpp new file mode 100644 index 0000000..ff1bd0d --- /dev/null +++ b/Codeforces Round 925 (Div. 3)/D. Divisible Pairs.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1931/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + + while (t--) { + int n; + cin >> n; + + ll x, y; + cin >> x >> y; + + vl fds(n); + + map> count; + + ll ans = 0; + cin >> fds; + + repv(i, fds) { + ans += count[(-i % x + x) % x][i % y]; + count[i % x][i % y]++; + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 944 (Div. 4)/D. Binary Cut.cpp b/Codeforces Round 944 (Div. 4)/D. Binary Cut.cpp new file mode 100644 index 0000000..9301efd --- /dev/null +++ b/Codeforces Round 944 (Div. 4)/D. Binary Cut.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1971/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a; + cin >> a; + + int count = 1; + bool dec = false; + nrep(i, 1, a.size()) { + if (a[i] != a[i - 1]) { + count++; + } + + if (a[i] == '1' && a[i - 1] == '0') { + dec = true; + } + } + + if (dec) { + count--; + } + + cout << count << '\n'; + } +} diff --git a/Codeforces Round 944 (Div. 4)/G. XOUR.cpp b/Codeforces Round 944 (Div. 4)/G. XOUR.cpp new file mode 100644 index 0000000..753c5c4 --- /dev/null +++ b/Codeforces Round 944 (Div. 4)/G. XOUR.cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1971/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + map var; + vl fds(n); + + repv(i, fds) { + cin >> i; + + var[i & ~((1LL << 2) - 1)] = 0; + } + + int v = 0; + repv(i, var) { + i.second = v; + v++; + } + + vvl vals(var.size()); + vvi pos(var.size()); + + rep(i, n) { + int act = var[fds[i] & ~((1LL << 2) - 1)]; + + vals[act].push_back(fds[i]); + pos[act].push_back(i); + } + + rep(i, var.size()) { + sortv(vals[i]); + } + + rep(i, var.size()) { + rep(j, vals[i].size()) { + fds[pos[i][j]] = vals[i][j]; + } + } + + cout << fds; + } +} diff --git a/Codeforces Round 975 (Div. 1)/C. Tree Pruning.cpp b/Codeforces Round 975 (Div. 1)/C. Tree Pruning.cpp new file mode 100644 index 0000000..d5f37eb --- /dev/null +++ b/Codeforces Round 975 (Div. 1)/C. Tree Pruning.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2018/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vvi graph(n); + + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n)); + } + + vi seg(n * 2); + + function update = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + seg[i]++; + return; + } + + int mid = (l + r) / 2; + update(i * 2, l, mid, tl, tr ); + update(i * 2 + 1, mid + 1, r, tl, tr); + }; + + auto get = [&](int i) { + int ans = 0; + for (i = (i + n); i > 0; i /= 2) { + ans += seg[i]; + } + + return ans; + }; + + function dfs = [&](int i, int p, int d) { + int ans = d; + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + rmax(ans, dfs(j, i, d + 1)); + } + + update(1, 0, n - 1, 0, d - 1); + update(1, 0, n - 1, ans + 1, n - 1); + return ans; + }; + + dfs(0, 0, 0); + + int ans = INT32_MAX >> 1; + + rep(i, n) { + rmin(ans, get(i)); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 991 (Div. 3)/A. Line Breaks.cpp b/Codeforces Round 991 (Div. 3)/A. Line Breaks.cpp new file mode 100644 index 0000000..e4ee0b4 --- /dev/null +++ b/Codeforces Round 991 (Div. 3)/A. Line Breaks.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/contest/2050/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +bool comp(const string &a, const string &b) +{ + return a.size() < b.size(); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, m; + cin >> n >> m; + + V fds(n); + cin >> fds; + + size_t i = 0; + ll total = 0; + + while (i < fds.size() && total + fds[i].size() <= m) { + total += fds[i].size(); + i++; + } + + cout << i << '\n'; + } +} diff --git a/Codeforces Round 991 (Div. 3)/B. Transfusion.cpp b/Codeforces Round 991 (Div. 3)/B. Transfusion.cpp new file mode 100644 index 0000000..24ec16a --- /dev/null +++ b/Codeforces Round 991 (Div. 3)/B. Transfusion.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/contest/2050/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl fds(n); + cin >> fds; + + ll odd = 0; + ll even = 0; + ll odds = 0; + ll evens = 0; + + rep(i, n) { + if (i & 1) { + odds++; + odd += fds[i]; + } else { + evens++; + even += fds[i]; + } + } + + if (odd % odds == 0 && even % evens == 0 && odd / odds == even / evens) { + cout << "YES\n"; + continue; + } + cout << "NO\n"; + } +} diff --git a/Codeforces Round 991 (Div. 3)/C. Uninteresting Number.cpp b/Codeforces Round 991 (Div. 3)/C. Uninteresting Number.cpp new file mode 100644 index 0000000..d0f2024 --- /dev/null +++ b/Codeforces Round 991 (Div. 3)/C. Uninteresting Number.cpp @@ -0,0 +1,115 @@ +/* Problem URL: https://codeforces.com/contest/2050/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +bool vis[9]; + +void pos(int mod, int two, int three) +{ + vis[mod] = true; + + + if (!vis[(mod + 2) % 9] && two > 0) { + pos((mod + 2) % 9, two - 1, three); + } + if (!vis[(mod + 6) % 9] && three > 0) { + pos((mod + 6) % 9, two, three - 1); + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a; + cin >> a; + + ll total = 0; + ll three = 0; + ll two = 0; + repv(i, a) { + total += i - '0'; + if (i == '3') { + three++; + } else if (i == '2') { + two++; + } + } + + memset(vis, 0, sizeof(vis)); + pos(total % 9, two, three); + + cout << (vis[0] ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 991 (Div. 3)/D. Digital string maximization.cpp b/Codeforces Round 991 (Div. 3)/D. Digital string maximization.cpp new file mode 100644 index 0000000..96e9b38 --- /dev/null +++ b/Codeforces Round 991 (Div. 3)/D. Digital string maximization.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/contest/2050/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a; + cin >> a; + + rep(i, a.size()) { + size_t choice = i; + char max = a[i]; + nrep(j, 1, 9) { + if (i + j < a.size() && a[i + j] - j > max) { + choice = i + j; + max = a[i + j] - j; + } + } + + for (size_t j = choice; j > i; j--) { + swap(a[j - 1], a[j]); + } + a[i] = max; + } + + cout << a << '\n'; + } +} diff --git a/Codeforces Round 991 (Div. 3)/E. Three Strings.cpp b/Codeforces Round 991 (Div. 3)/E. Three Strings.cpp new file mode 100644 index 0000000..fb4ca48 --- /dev/null +++ b/Codeforces Round 991 (Div. 3)/E. Three Strings.cpp @@ -0,0 +1,162 @@ +/* Problem URL: https://codeforces.com/contest/2050/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int dp(int i, int j, int k, string &a, string &b, string &c) +{ + if (k >= c.size()) { + return 0; + } + + if (i >= a.size()) { + ll tmp = 0; + while (j < b.size()) { + if (b[j] != c[k]) { + tmp++; + } + j++; + k++; + } + return tmp; + } + + if (j >= b.size()) { + ll tmp = 0; + while (i < a.size()) { + if (a[i] != c[k]) { + tmp++; + } + i++; + k++; + } + return tmp; + } + + if (a[i] != c[k] && b[j] != c[k]) { + return min(dp(i + 1, j, k + 1, a, b, c), dp(i, j + 1, k + 1, a, b, c)) + 1; + } + + if (a[i] == b[j]) { + return min(dp(i + 1, j, k + 1, a, b, c), dp(i, j + 1, k + 1, a, b, c)); + } + + if (a[i] == c[k]) { + return dp(i + 1, j, k + 1, a, b, c); + } + + return dp(i, j + 1, k + 1, a, b, c); +} + +int inf = INT32_MAX >> 1; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a, b, c; + cin >> a >> b >> c; + + // cout << dp(0, 0, 0, a, b, c) << '\n'; + + vvi dp(a.size() + 1, vi(b.size() + 1, inf)); + + dp[0][0] = 0; + + for (size_t i = 0; i <= a.size(); i++) { + for (size_t j = 0; j <= b.size(); j++) { + if (i == 0 && j == 0) { + continue; + } + + if (i > 0) { + if (a[i - 1] == c[i + j - 1]) { + rmin(dp[i][j], dp[i - 1][j]); + } else { + rmin(dp[i][j], dp[i - 1][j] + 1); + } + } + + if (j > 0) { + if (b[j - 1] == c[i + j - 1]) { + rmin(dp[i][j], dp[i][j - 1]); + } else { + rmin(dp[i][j], dp[i][j - 1] + 1); + } + } + } + } + + cout << dp[a.size()][b.size()] << '\n'; + } +} diff --git a/Codeforces Round 991 (Div. 3)/F. Maximum modulo equality.cpp b/Codeforces Round 991 (Div. 3)/F. Maximum modulo equality.cpp new file mode 100644 index 0000000..15cab1c --- /dev/null +++ b/Codeforces Round 991 (Div. 3)/F. Maximum modulo equality.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/contest/2050/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int dp(int i, int j, int k, string &a, string &b, string &c) +{ + if (i >= a.size() || j >= b.size() || k >= c.size()) { + return 0; + } + + if (a[i] != c[k] && b[j] != c[k]) { + return min(dp(i + 1, j, k + 1, a, b, c), dp(i, j + 1, k, a, b, c)) + 1; + } + + if (a[i] == b[j]) { + return min(dp(i + 1, j, k + 1, a, b, c), dp(i, j + 1, k, a, b, c)); + } + + if (a[i] == c[k]) { + return dp(i + 1, j, k + 1, a, b, c); + } + + return dp(i, j + 1, k + 1, a, b, c); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string a, b, c; + cin >> a >> b >> c; + + cout << dp(0, 0, 0, a, b, c) << '\n'; + } +} diff --git a/Codeforces Round 995 (Div. 3)/A. Preparing for the Olympiad.cpp b/Codeforces Round 995 (Div. 3)/A. Preparing for the Olympiad.cpp new file mode 100644 index 0000000..305bfd8 --- /dev/null +++ b/Codeforces Round 995 (Div. 3)/A. Preparing for the Olympiad.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/contest/2051/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vi a(n); + vi b(n); + cin >> a >> b; + + ll count = 0; + nrep(i, 1, n) { + if (b[i] < a[i - 1]) { + count += a[i - 1] - b[i]; + } + } + count += a.back(); + + cout << count << '\n'; + } +} diff --git a/Codeforces Round 995 (Div. 3)/B. Journey.cpp b/Codeforces Round 995 (Div. 3)/B. Journey.cpp new file mode 100644 index 0000000..fad6cb0 --- /dev/null +++ b/Codeforces Round 995 (Div. 3)/B. Journey.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/contest/2051/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, a, b, c; + cin >> n >> a >> b >> c; + + ll day = n / (a + b + c) * 3; + n %= a + b + c; + + if (n > 0) { + if (n <= a) { + day++; + } else if (n <= a + b) { + day += 2; + } else { + day += 3; + } + } + + cout << day << '\n'; + } +} diff --git a/Codeforces Round 995 (Div. 3)/C. Preparing for the Exam.cpp b/Codeforces Round 995 (Div. 3)/C. Preparing for the Exam.cpp new file mode 100644 index 0000000..aac3a33 --- /dev/null +++ b/Codeforces Round 995 (Div. 3)/C. Preparing for the Exam.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/contest/2051/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m, k; + cin >> n >> m >> k; + + vi q(m); + cin >> q; + + set ans; + while (k--) { + int num; + cin >> num; + ans.insert(num); + } + + if (ans.size() < n - 1) { + rep(i, q.size()) { + cout << '0'; + } + } else if (ans.size() == n) { + rep(i, q.size()) { + cout << '1'; + } + } else { + repv(i, q) { + if (ans.find(i) == ans.end()) { + cout << '1'; + } else { + cout << '0'; + } + } + } + cout << '\n'; + } +} diff --git a/Codeforces Round 995 (Div. 3)/D. Counting Pairs.cpp b/Codeforces Round 995 (Div. 3)/D. Counting Pairs.cpp new file mode 100644 index 0000000..06a78a3 --- /dev/null +++ b/Codeforces Round 995 (Div. 3)/D. Counting Pairs.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/contest/2051/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, x, y; + cin >> n >> x >> y; + + vl items(n); + ll total = 0; + repv(i, items) { + cin >> i; + total += i; + } + sortv(items); + + ll count = 0; + + ll ans = 0; + for (size_t i = n - 1; i > 0; i--) { + int low = 0; + int high = i - 1; + int ansx = -1; + ll tmp = total - items[i]; + while (low <= high) { + int mid = (low + high) / 2; + + if (tmp - items[mid] >= x) { + ansx = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + if (ansx == -1) { + continue; + } + + low = 0; + high = i - 1; + int ansy = -1; + while (low <= high) { + int mid = (low + high) / 2; + + if (tmp - items[mid] <= y) { + ansy = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + if (ansy == -1) { + continue; + } + + ans += (ansx - ansy) + 1; + } + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 995 (Div. 3)/E. Best Price.cpp b/Codeforces Round 995 (Div. 3)/E. Best Price.cpp new file mode 100644 index 0000000..416420c --- /dev/null +++ b/Codeforces Round 995 (Div. 3)/E. Best Price.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/contest/2051/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vl a(n), b(n); + cin >> a >> b; + + set set; + repv(i, a) { + set.insert(i); + } + repv(i, b) { + set.insert(i); + } + + sort(a.begin(), a.end(), greater<>()); + sort(b.begin(), b.end(), greater<>()); + + ll ans = 0; + repv(i, set) { + int count = 0; + int not_negs = 0; + + int low = 0; + int high = n - 1; + while (low <= high) { + int mid = (low + high) / 2; + + if (i <= b[mid]) { + count = mid + 1; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + low = 0; + high = n - 1; + while (low <= high) { + int mid = (low + high) / 2; + + if (i <= a[mid]) { + not_negs = mid + 1; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + if (count - not_negs > k) { + continue; + } + + rmax(ans, i * count); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 998 (Div. 3)/A. Fibonacciness.cpp b/Codeforces Round 998 (Div. 3)/A. Fibonacciness.cpp new file mode 100644 index 0000000..65c223f --- /dev/null +++ b/Codeforces Round 998 (Div. 3)/A. Fibonacciness.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/contest/2060/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + vi fds(5); + cin >> fds[0] >> fds[1] >> fds[3] >> fds[4]; + + ll ans = 0; + for (int i = -100; i <= 100; i++) { + ll now = 0; + fds[2] = i; + + nrep(j, 2, 5) { + if (fds[j] == fds[j - 1] + fds[j - 2]) { + now++; + } + } + + rmax(ans, now); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 998 (Div. 3)/B. Farmer John's Card Game.cpp b/Codeforces Round 998 (Div. 3)/B. Farmer John's Card Game.cpp new file mode 100644 index 0000000..cf66f03 --- /dev/null +++ b/Codeforces Round 998 (Div. 3)/B. Farmer John's Card Game.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/contest/2060/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + priority_queue, V>, greater<>> pq; + + rep(i, n) { + rep(j, m) { + int card; + cin >> card; + + pq.emplace(card, i); + } + } + + vi played(n, -1); + + bool pos = true; + for (size_t j = 0; j < m && pos; j++) { + for (size_t i = 0; i < n; i++) { + auto [card, cow] = pq.top(); + pq.pop(); + + if (played[cow] == -1) { + played[cow] = i; + continue; + } + + if (played[cow] != i) { + pos = false; + break; + } + } + } + + if (pos) { + priority_queue, V>, greater<>> ord; + + rep(i, n) { + ord.emplace(played[i], i + 1); + } + + while (!ord.empty()) { + cout << ord.top().second << ' '; + ord.pop(); + } + cout << '\n'; + } else { + cout << "-1\n"; + } + } +} diff --git a/Codeforces Round 998 (Div. 3)/C. Game of Mathletes.cpp b/Codeforces Round 998 (Div. 3)/C. Game of Mathletes.cpp new file mode 100644 index 0000000..788fcf0 --- /dev/null +++ b/Codeforces Round 998 (Div. 3)/C. Game of Mathletes.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/contest/2060/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + vi nums(n); + cin >> nums; + + multiset count; + + repv(i, nums) { + count.insert(i); + } + + ll score = n / 2; + ll no = 0; + repv(i, nums) { + auto itr = count.find(i); + if (itr == count.end()) { + continue; + } + + count.erase(itr); + + itr = count.find(k - i); + if (itr == count.end()) { + no++; + continue; + } + + count.erase(itr); + } + + cout << score - (no + 1) / 2 << '\n'; + } +} diff --git a/Codeforces Round 998 (Div. 3)/D. Subtract Min Sort.cpp b/Codeforces Round 998 (Div. 3)/D. Subtract Min Sort.cpp new file mode 100644 index 0000000..c4aa72d --- /dev/null +++ b/Codeforces Round 998 (Div. 3)/D. Subtract Min Sort.cpp @@ -0,0 +1,100 @@ +/* Problem URL: https://codeforces.com/contest/2060/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + for (size_t i = 0; i < n - 1 && fds[i] <= fds[i + 1]; i++) { + fds[i + 1] -= fds[i]; + fds[i] = 0; + } + + bool yes = true; + for (size_t i = 0; i < n - 1 && yes; i++) { + if (fds[i] > fds[i + 1]) { + yes = false; + } + } + + cout << (yes ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 998 (Div. 3)/E. Graph Composition.cpp b/Codeforces Round 998 (Div. 3)/E. Graph Composition.cpp new file mode 100644 index 0000000..1491f25 --- /dev/null +++ b/Codeforces Round 998 (Div. 3)/E. Graph Composition.cpp @@ -0,0 +1,158 @@ +/* Problem URL: https://codeforces.com/contest/2060/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +void dothething(int i, V &vis, vi &groups, int group, vvi &graph) +{ + vis[i] = true; + groups[i] = group; + + repv(c, graph[i]) { + if (vis[c]) { + continue; + } + + dothething(c, vis, groups, group, graph); + } +} + +int get(int i, V &vis, vi &groups, vvi &graph) +{ + vis[i] = true; + + int invalid = 0; + repv(c, graph[i]) { + if (vis[c]) { + continue; + } + + if (groups[i] != groups[c]) { + invalid++; + continue; + } + + invalid += get(c, vis, groups, graph); + } + + return invalid; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m1, m2; + cin >> n >> m1 >> m2; + + vvi f(n); + rep(i, m1) { + int a, b; + cin >> a >> b; + a--, b--; + f[a].push_back(b); + f[b].push_back(a); + } + + vvi g(n); + rep(i, m2) { + int a, b; + cin >> a >> b; + a--, b--; + g[a].push_back(b); + g[b].push_back(a); + } + + V vis(n, false); + int group = 0; + vi groups(n, -1); + rep(i, n) { + if (!vis[i]) { + dothething(i, vis, groups, group, g); + group++; + } + } + + vis.assign(n, false); + int count = 0; + int invalid = 0; + rep(i, n) { + if (!vis[i]) { + invalid += get(i, vis, groups, f); + count++; + } + } + + cout << invalid + (count - group) << '\n'; + } +} diff --git a/Educational Codeforces Round 12/F. Four Divisors.cpp b/Educational Codeforces Round 12/F. Four Divisors.cpp new file mode 100644 index 0000000..e5a42b5 --- /dev/null +++ b/Educational Codeforces Round 12/F. Four Divisors.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/problemset/problem/665/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n; + cin >> n; + + V isprime(1e6 + 1, true); + vi div(1e6 + 1); + vl primes; + isprime[1] = false; + + for (int i = 2; i <= 1e6; i++) { + if (!isprime[i]) { + continue; + } + + primes.push_back(i); + + for (int j = i * 2; j <= 1e6; j += i) { + isprime[j] = false; + div[j] = i; + } + } + + ll count = 0; + ll lim = cbrt(n); + + for (int i = 0; i < primes.size() && primes[i] <= lim; i++) { + count++; + } + + lim = sqrt(n); + int tmp = lower_bound(all(primes), lim) - primes.begin() + 1; + + count += tmp; + + cout << count << '\n'; +} diff --git a/Educational Codeforces Round 160 (Rated for Div. 2)/D. Array Collapse.cpp b/Educational Codeforces Round 160 (Rated for Div. 2)/D. Array Collapse.cpp new file mode 100644 index 0000000..8711a15 --- /dev/null +++ b/Educational Codeforces Round 160 (Rated for Div. 2)/D. Array Collapse.cpp @@ -0,0 +1,155 @@ +/* Problem URL: https://codeforces.com/contest/1913/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + stack> s; + + vi left(n, 0); + vi right(n, n - 1); + + rep(i, n) { + while (!s.empty() && s.top().first > fds[i]) { + s.pop(); + } + + if (s.empty()) { + s.emplace(fds[i], i + 1); + continue; + } + + left[i] = s.top().second; + s.emplace(fds[i], i); + } + + s = stack>(); + + for (int i = n - 1; i >= 0; i--) { + while (!s.empty() && s.top().first > fds[i]) { + s.pop(); + } + + if (s.empty()) { + s.emplace(fds[i], i - 1); + continue; + } + + right[i] = s.top().second; + s.emplace(fds[i], i); + } + + vl dp1(n + 1); + dp1[0] = 1; + + vl dp2(n + 1); + dp2[n] = 1; + + const ll mod = 998244353; + + rep(i, n) { + if (left[i] > 0) { + dp1[i + 1] += dp1[i]; + continue; + } + + dp1[i + 1] = (dp1[i] + i) % mod; + } + + for (int i = n - 1; i >= 0; i--) { + if (right[i] < n - 1) { + dp2[i] += dp2[i + 1]; + continue; + } + + dp2[i] = (dp2[i] + (n - i + 1)) % mod; + } + + cout << dp1[n] + dp2[0] << '\n'; + } +} diff --git a/Educational Codeforces Round 168 (Rated for Div. 2)/E. Level Up.cpp b/Educational Codeforces Round 168 (Rated for Div. 2)/E. Level Up.cpp new file mode 100644 index 0000000..9baabb4 --- /dev/null +++ b/Educational Codeforces Round 168 (Rated for Div. 2)/E. Level Up.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/contest/1997/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template +using ordered_set = tree , rb_tree_tag , tree_order_statistics_node_update>; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vi a(n); + cin >> a; + + ordered_set> s; + vi inv(n); + + rep(i, n) { + inv[i] = s.order_of_key({a[i], -i}); + s.insert({a[i], i}); + } + + while (q--) { + int i, x; + cin >> i >> x; + + if (inv[i - 1] / x >= a[i - 1]) { + cout << "NO\n"; + } else { + cout << "YES\n"; + } + } +} diff --git a/Educational Codeforces Round 175 (Rated for Div. 2)/A. FizzBuzz Remixed.cpp b/Educational Codeforces Round 175 (Rated for Div. 2)/A. FizzBuzz Remixed.cpp new file mode 100644 index 0000000..e32fd64 --- /dev/null +++ b/Educational Codeforces Round 175 (Rated for Div. 2)/A. FizzBuzz Remixed.cpp @@ -0,0 +1,90 @@ +/* Problem URL: https://codeforces.com/contest/2070/problem/A */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + if (n <= 2) { + cout << n + 1 << '\n'; + continue; + } + + cout << n / 15 * 3 + min(n % 15, 2LL) + 1 << '\n'; + } +} diff --git a/Educational Codeforces Round 175 (Rated for Div. 2)/B. Robot Program.cpp b/Educational Codeforces Round 175 (Rated for Div. 2)/B. Robot Program.cpp new file mode 100644 index 0000000..91ff33d --- /dev/null +++ b/Educational Codeforces Round 175 (Rated for Div. 2)/B. Robot Program.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/2070/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n, x, k; + cin >> n >> x >> k; + + string a; + cin >> a; + + ll count = 0; + ll zeroes = 0; + bool repeat = false; + for (size_t i = 0; i < a.size() && x != 0 && k > 0; i++) { + k--; + if (a[i] == 'L') { + x--; + } else { + x++; + } + } + + if (x != 0) { + cout << "0\n"; + continue; + } + + count = 1; + x += a[0] == 'L' ? -1 : 1; + size_t i; + for (i = 1; i < a.size() && x != 0 && k > 0; i++) { + count++; + if (a[i] == 'L') { + x--; + } else { + x++; + } + } + + if (x != 0) { + cout << "1\n"; + continue; + } + + cout << k / count + 1 << '\n'; + } +} diff --git a/Educational Codeforces Round 175 (Rated for Div. 2)/C. Limited Repainting.cpp b/Educational Codeforces Round 175 (Rated for Div. 2)/C. Limited Repainting.cpp new file mode 100644 index 0000000..36e60d4 --- /dev/null +++ b/Educational Codeforces Round 175 (Rated for Div. 2)/C. Limited Repainting.cpp @@ -0,0 +1,78 @@ +/* Problem URL: https://codeforces.com/contest/2070/problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + +} diff --git a/Educational Codeforces Round 179 (Rated for Div. 2)/E. Changing the String.cpp b/Educational Codeforces Round 179 (Rated for Div. 2)/E. Changing the String.cpp new file mode 100644 index 0000000..9f27683 --- /dev/null +++ b/Educational Codeforces Round 179 (Rated for Div. 2)/E. Changing the String.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2111/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + string a; + cin >> a; + + repv(i, a) { + i -= 'a'; + } + + vvi ops(3, vi(3)); + + while (m--) { + char b, c; + cin >> b >> c; + b -= 'a'; + c -= 'a'; + + ops[b][c]++; + } + + repv(i, a) { + if (i == 0) { + continue; + } + + if (i == 1 && ops[1][0]) { + ops[1][0]--; + i = 0; + continue; + } + + if (i == 1 && ops[1][2] && ops[2][0]) { + ops[1][2]--; + ops[2][0]--; + i = 0; + continue; + } + + if (i == 2 && ops[2][0]) { + ops[2][0]--; + i = 0; + continue; + } + + if (i == 2 && ops[2][1] && ops[1][0]) { + ops[2][1]--; + ops[1][0]--; + i = 0; + continue; + } + + if (i == 2 && ops[2][1]) { + ops[2][1]--; + i = 1; + } + } + + repv(i, a) { + cout << (char)(i + 'a'); + } + cout << '\n'; + } +} diff --git a/Educational Codeforces Round 181 (Rated for Div. 2)/D. Segments Covering.cpp b/Educational Codeforces Round 181 (Rated for Div. 2)/D. Segments Covering.cpp new file mode 100644 index 0000000..babc2c4 --- /dev/null +++ b/Educational Codeforces Round 181 (Rated for Div. 2)/D. Segments Covering.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2125/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + ll mod = 998244353; + + auto fastpow = [&](ll a, ll p) { + ll ans = 1; + ll now = a; + + rep(i, 30) { + if ((p >> i) & 1) { + ans *= now; + ans %= mod; + } + + now *= now; + now %= mod; + } + + return ans; + }; + + if (m > 1) { + m = 1 << (32 - __builtin_clz(m - 1)); + } + + vl lazy(m * 2); + V> seg(m * 2); + + auto propagate = [&](int i) { + if (lazy[i] == 0) { + return; + } + + ll npos = ((1 - lazy[i]) % mod + mod) % mod; + seg[i].first = ((seg[i].first + lazy[i] - seg[i].first * lazy[i]) % mod + mod) % mod; + seg[i].second = (seg[i].second * npos) % mod; + + if (i >= m) { + return; + } + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] = v; + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + }; + + while (n--) { + + } +} diff --git a/Educational Codeforces Round 32/E. Maximum Subsequence.cpp b/Educational Codeforces Round 32/E. Maximum Subsequence.cpp new file mode 100644 index 0000000..ac1c6c0 --- /dev/null +++ b/Educational Codeforces Round 32/E. Maximum Subsequence.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/contest/888/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll m; + cin >> n >> m; + + vl fds(n); + repv(i, fds) { + cin >> i; + + i %= m; + } + + int mid = n / 2; + + vl sumsmid; + + rep(i, (1 << mid)) { + ll sum = 0; + rep(j, mid) { + if ((i >> j) & 1) { + sum += fds[j]; + } + + } + + sumsmid.push_back(sum % m); + } + + vl sums2; + + rep(i, (1 << (n - mid))) { + ll sum = 0; + rep(j, n - mid) { + if ((i >> j) & 1) { + sum += fds[j + mid]; + } + } + + sums2.push_back(sum % m); + } + + sortv(sumsmid); + sortv(sums2); + + ll ans = max(sumsmid.back(), sums2.back()); + + repv(i, sumsmid) { + auto itr = upper_bound(all(sums2), m - i - 1); + if (itr == sums2.begin()) { + continue; + } + + rmax(ans, (*prev(itr) + i) % m); + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 33 (Rated for Div. 2)/A. Chess For Three.cpp b/Educational Codeforces Round 33 (Rated for Div. 2)/A. Chess For Three.cpp new file mode 100644 index 0000000..1416784 --- /dev/null +++ b/Educational Codeforces Round 33 (Rated for Div. 2)/A. Chess For Three.cpp @@ -0,0 +1,105 @@ +/* Problem URL: https://codeforces.com/contest/893/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + int spec = 3; + while (n--) { + int i; + cin >> i; + + if (i == spec) { + cout << "NO\n"; + return 0; + } + + nrep(j, 1, 4) { + if (j != spec && j != i) { + spec = j; + break; + } + } + } + + cout << "YES\n"; +} diff --git a/Educational Codeforces Round 33 (Rated for Div. 2)/B. Beautiful Divisors.cpp b/Educational Codeforces Round 33 (Rated for Div. 2)/B. Beautiful Divisors.cpp new file mode 100644 index 0000000..dd340f0 --- /dev/null +++ b/Educational Codeforces Round 33 (Rated for Div. 2)/B. Beautiful Divisors.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://codeforces.com/contest/893/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + int ans = 0; + for (int i = n; i > 0; i--) { + int tmp = i; + + bool valid = true; + int zeroes = 0; + int ones = 0; + while ((tmp & 1) == 0) { + zeroes++; + tmp >>= 1; + } + + while ((tmp & 1) == 1) { + ones++; + tmp >>= 1; + } + + if (tmp > 0 || ones != zeroes + 1) { + continue; + } + + if (n % i == 0) { + ans = i; + break; + } + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 33 (Rated for Div. 2)/C. Rumor.cpp b/Educational Codeforces Round 33 (Rated for Div. 2)/C. Rumor.cpp new file mode 100644 index 0000000..0812899 --- /dev/null +++ b/Educational Codeforces Round 33 (Rated for Div. 2)/C. Rumor.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/893/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vvi graph(n); + vl cost(n); + cin >> cost; + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + V vis(n); + + function dfs = [&](int i) { + ll ans = cost[i]; + + vis[i] = true; + + for (auto j : graph[i]) { + if (vis[j]) { + continue; + } + + rmin(ans, dfs(j)); + } + + return ans; + }; + + ll ans = 0; + rep(i, n) { + if (vis[i]) { + continue; + } + + ans += dfs(i); + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 33 (Rated for Div. 2)/D. Credit Card.cpp b/Educational Codeforces Round 33 (Rated for Div. 2)/D. Credit Card.cpp new file mode 100644 index 0000000..6c1052c --- /dev/null +++ b/Educational Codeforces Round 33 (Rated for Div. 2)/D. Credit Card.cpp @@ -0,0 +1,155 @@ +/* Problem URL: https://codeforces.com/contest/893/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll d; + cin >> n >> d; + + vl fds(n); + cin >> fds; + + ll test = 0; + vl trans(n); + rep(i, n) { + test += fds[i]; + + if (test > d) { + cout << "-1\n"; + return 0; + } + + trans[i] = test; + } + + vvl sparse(n, vl(20)); + // vvl minimal(n, vl(20)); + + rep(i, n) { + sparse[i][0] = trans[i]; + // minimal[i][0] = trans[i]; + } + + nrep(j, 1, 20) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + sparse[i][j] = max(sparse[i][j - 1], sparse[i + (1 << (j - 1))][j - 1]); + // minimal[i][j] = min(minimal[i][j - 1], minimal[i + (j << 1)][j - 1]); + } + } + + auto query = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + + return max(sparse[l][log], sparse[r - (1 << log) + 1][log]); + }; + + // auto minquery = [&](int l, int r) { + // int log = 31 - __builtin_clz(r - l + 1); + // + // return min(minimal[l][log], minimal[r - (1 << log) + 1][log]); + // }; + + int i = 0; + + ll added = 0; + int ans = 0; + + rep(i, n) { + if (fds[i] == 0) { + if (trans[i] + added < 0) { + ll maximal = query(i, n - 1); + ll act = trans[i] + added; + + ll now = d - (maximal + added); + + if (now < -act) { + cout << "-1\n"; + return 0; + } + + ans++; + added += now; + } + } + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 33 (Rated for Div. 2)/E. Counting Arrays.cpp b/Educational Codeforces Round 33 (Rated for Div. 2)/E. Counting Arrays.cpp new file mode 100644 index 0000000..7084b46 --- /dev/null +++ b/Educational Codeforces Round 33 (Rated for Div. 2)/E. Counting Arrays.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/contest/893/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vl fact(1e6 + 1, 1); + nrep(i, 2, 1e6 + 1) { + if (fact[i] != 1) { + continue; + } + + for (int j = i; j <= 1e6; j += i) { + fact[j] = i; + } + } + + int q; + cin >> q; + + const ll mod = 1e9 + 7; + + vl f(2e6 + 1); + f[0] = 1; + + nrep(i, 1, 2e6 + 1) { + f[i] = (f[i - 1] * i) % mod; + } + + while (q--) { + ll x, y; + cin >> x >> y; + + ll tmp = x; + map c; + + while (tmp > 1) { + c[fact[tmp]]++; + tmp /= fact[tmp]; + } + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + ll now = a; + + rep(i, 31) { + if ((p >> i) & 1) { + ans *= now; + ans %= mod; + } + + now *= now; + now %= mod; + } + + return ans; + }; + + auto comb = [&](ll n, ll k) { + ll ans = f[n]; + + return (ans * fpow((f[k] * f[n - k]) % mod, mod - 2)) % mod; + }; + + ll ans = 1; + for (auto i : c) { + ans = (ans * comb(y + i.second - 1, i.second)) % mod; + } + + ans = (ans * fpow(2, y - 1)) % mod; + cout << ans << '\n'; + } +} diff --git a/Educational Codeforces Round 34 (Rated for Div. 2)/A. Hungry Student Problem.cpp b/Educational Codeforces Round 34 (Rated for Div. 2)/A. Hungry Student Problem.cpp new file mode 100644 index 0000000..e9cb591 --- /dev/null +++ b/Educational Codeforces Round 34 (Rated for Div. 2)/A. Hungry Student Problem.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/contest/903/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + bool valid = false; + + rep(i, 101) { + rep(j, 101) { + if (3 * i + 7 * j == n) { + valid = true; + break; + } + } + + if (valid) { + break; + } + } + + cout << (valid ? "YES\n" : "NO\n"); + } +} diff --git a/Educational Codeforces Round 34 (Rated for Div. 2)/B. The Modcrab.cpp b/Educational Codeforces Round 34 (Rated for Div. 2)/B. The Modcrab.cpp new file mode 100644 index 0000000..53e0071 --- /dev/null +++ b/Educational Codeforces Round 34 (Rated for Div. 2)/B. The Modcrab.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://codeforces.com/contest/903/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int h, a, c; + cin >> h >> a >> c; + + int h2, a2; + cin >> h2 >> a2; + + vi ops; + + while (h2 > 0) { + if (h2 <= a) { + ops.push_back(0); + h2 = 0; + continue; + } + + if (a2 >= h) { + h += c; + h -= a2; + ops.push_back(1); + continue; + } + + ops.push_back(0); + h2 -= a; + h -= a2; + } + + cout << ops.size() << '\n'; + repv(i, ops) { + cout << (i ? "HEAL\n" : "STRIKE\n"); + } +} diff --git a/Educational Codeforces Round 34 (Rated for Div. 2)/C. Boxes Packing.cpp b/Educational Codeforces Round 34 (Rated for Div. 2)/C. Boxes Packing.cpp new file mode 100644 index 0000000..2927cc8 --- /dev/null +++ b/Educational Codeforces Round 34 (Rated for Div. 2)/C. Boxes Packing.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://codeforces.com/contest/903/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + map var; + int v = 0; + + vl a(n); + + repv(i, a) { + cin >> i; + var[i] = 0; + } + + repv(i, var) { + i.second = v; + v++; + } + + vi count(var.size()); + + repv(i, a) { + count[var[i]]++; + } + + nrep(i, 1, count.size()) { + int minimal = min(count[i], count[i - 1]); + count[i] += count[i - 1]; + count[i] -= minimal; + } + + cout << count.back() << '\n'; +} diff --git a/Educational Codeforces Round 34 (Rated for Div. 2)/D. Almost Difference.cpp b/Educational Codeforces Round 34 (Rated for Div. 2)/D. Almost Difference.cpp new file mode 100644 index 0000000..6e8eb43 --- /dev/null +++ b/Educational Codeforces Round 34 (Rated for Div. 2)/D. Almost Difference.cpp @@ -0,0 +1,170 @@ +/* Problem URL: https://codeforces.com/contest/903/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + +#define int long long + + int n; + cin >> n; + + map var; + int v = 0; + + vl a(n); + repv(i, a) { + cin >> i; + var[i] = 0; + } + + vl inv(var.size()); + repv(i, var) { + i.second = v; + inv[v] = i.first; + v++; + } + + int size = v; + if (size > 1) { + size = 1 << (32 - __builtin_clz(size - 1)); + } + + V> seg(size * 2); + + auto update = [&](int i) { + i += size; + seg[i].first++; + seg[i].second += inv[i - size]; + + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = {seg[i * 2].first + seg[i * 2 + 1].first, seg[i * 2].second + seg[i * 2 + 1].second}; + } + }; + + function(int, int, int, int, int)> query = [&](int i, int l, int r, int tl, int tr) -> pair { + if (l > tr || r < tl) { + return {0, 0}; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + auto ans1 = query(i * 2, l, mid, tl, tr); + auto ans2 = query(i * 2 + 1, mid + 1, r, tl, tr); + + return {ans1.first + ans2.first, ans1.second + ans2.second}; + }; + + __int128 ans = 0; + + rep(i, n) { + int act = var[a[i]]; + int upper = act + 1 + var.count(a[i] + 1); + int lower = act - 1 - var.count(a[i] - 1); + + auto tmp = query(1, 0, size - 1, upper, v - 1); + ans += (__int128)tmp.first * a[i] - tmp.second; + + tmp = query(1, 0, size - 1, 0, lower); + ans += (__int128)tmp.first * a[i] - tmp.second; + + update(act); + } + + bool isneg = ans < 0; + bool iszero = ans == 0; + + if (isneg) { + ans = -ans; + } + + string act; + while (ans > 0) { + act.push_back((ans % 10) + '0'); + ans /= 10; + } + + reverse(all(act)); + + cout << (isneg ? "-" : "") << (iszero ? "0" : "") << act << '\n'; +} diff --git a/Educational Codeforces Round 34 (Rated for Div. 2)/E. Swapping Characters.cpp b/Educational Codeforces Round 34 (Rated for Div. 2)/E. Swapping Characters.cpp new file mode 100644 index 0000000..1715aff --- /dev/null +++ b/Educational Codeforces Round 34 (Rated for Div. 2)/E. Swapping Characters.cpp @@ -0,0 +1,188 @@ +/* Problem URL: https://codeforces.com/contest/903/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> k >> n; + + V fds(k); + cin >> fds; + + vi diffs; + + int other = 0; + + vi c1(26); + rep(i, n) { + c1[fds[0][i] - 'a']++; + } + + nrep(i, 1, k) { + vi c2(26); + rep(j, n) { + if (fds[0][j] != fds[i][j]) { + diffs.push_back(j); + } + c2[fds[i][j] - 'a']++; + } + + if (c1 != c2) { + cout << "-1\n"; + return 0; + } + + if (!diffs.empty()) { + other = i; + break; + } + } + + if (diffs.empty()) { + swap(fds[0][0], fds[0][1]); + cout << fds[0] << '\n'; + return 0; + } + + if (diffs.size() > 4) { + cout << "-1\n"; + return 0; + } + + string now = fds[0]; + auto test = [&]() { + rep(i, diffs.size()) { + rep(j, n) { + if (diffs[i] == j) { + continue; + } + swap(now[diffs[i]], now[j]); + + auto diff = [&](string &a, string &b) { + vi count(26); + bool lmao = false; + + int ans = 0; + rep(i, n) { + count[a[i] - 'a']++; + lmao = lmao || count[a[i] - 'a'] >= 2; + if (a[i] != b[i]) { + ans++; + } + } + + return ans == 2 || (ans == 0 && lmao); + }; + + bool pos = true; + + rep(l, k) { + int d = diff(now, fds[l]); + if (!diff(now, fds[l])) { + pos = false; + break; + } + } + + if (pos) { + return true; + } + + swap(now[diffs[i]], now[j]); + } + } + + return false; + }; + + if (test()) { + cout << now << '\n'; + return 0; + } + + now = fds[other]; + + if (test()) { + cout << now << '\n'; + return 0; + } + + cout << "-1\n"; +} diff --git a/Educational Codeforces Round 35 (Rated for Div. 2)/A. Nearest Minimums.cpp b/Educational Codeforces Round 35 (Rated for Div. 2)/A. Nearest Minimums.cpp new file mode 100644 index 0000000..72597ef --- /dev/null +++ b/Educational Codeforces Round 35 (Rated for Div. 2)/A. Nearest Minimums.cpp @@ -0,0 +1,101 @@ +/* Problem URL: https://codeforces.com/contest/911/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + map var; + + int n; + cin >> n; + + rep(i, n) { + int num; + cin >> num; + var[num].push_back(i); + } + + int ans = INT32_MAX >> 1; + + auto &vet = var.begin()->second; + nrep(i, 1, vet.size()) { + rmin(ans, vet[i] - vet[i - 1]); + } + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 35 (Rated for Div. 2)/B. Two Cakes.cpp b/Educational Codeforces Round 35 (Rated for Div. 2)/B. Two Cakes.cpp new file mode 100644 index 0000000..a6d4b38 --- /dev/null +++ b/Educational Codeforces Round 35 (Rated for Div. 2)/B. Two Cakes.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://codeforces.com/contest/911/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, a, b; + cin >> n >> a >> b; + + int ans = 0; + nrep(i, 1, min(a, b) + 1) { + int now = a / i + b / i; + if (now >= n) { + ans = i; + } + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 35 (Rated for Div. 2)/C. Three Garlands.cpp b/Educational Codeforces Round 35 (Rated for Div. 2)/C. Three Garlands.cpp new file mode 100644 index 0000000..2115a7c --- /dev/null +++ b/Educational Codeforces Round 35 (Rated for Div. 2)/C. Three Garlands.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/contest/911/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vi fds(3); + cin >> fds; + sortv(fds); + + auto check = [&]() { + V time(1e5 + 1); + + int now = 0; + + rep(i, 1e5 + 1) { + if (time[i]) { + continue; + } + + if (now == 3) { + return false; + } + + for (int j = i; j <= 1e5; j += fds[now]) { + time[j] = true; + } + now++; + } + + return true; + }; + + cout << (check() ? "YES\n" : "NO\n"); +} diff --git a/Educational Codeforces Round 35 (Rated for Div. 2)/D. Inversion Counting.cpp b/Educational Codeforces Round 35 (Rated for Div. 2)/D. Inversion Counting.cpp new file mode 100644 index 0000000..7e3eda3 --- /dev/null +++ b/Educational Codeforces Round 35 (Rated for Div. 2)/D. Inversion Counting.cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/contest/911/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + vi bit(n + 1); + + auto update = [&](int i) { + for (; i <= n; i += (i&(-i))) { + bit[i]++; + } + }; + + auto get = [&](int i) { + ll ans = 0; + for (; i > 0; i -= (i&(-i))) { + ans += bit[i]; + } + return ans; + }; + + ll now = 0; + rep(i, n) { + now += get(n); + if (fds[i] > 1) { + now -= get(fds[i] - 1); + } + update(fds[i]); + } + + now &= 1; + + int q; + cin >> q; + while (q--) { + int l, r; + cin >> l >> r; + l--, r--; + + if (((r - l + 1) / 2) & 1) { + now ^= 1; + } + + cout << ((now & 1) ? "odd\n" : "even\n"); + } +} diff --git a/Educational Codeforces Round 35 (Rated for Div. 2)/E. Stack Sorting.cpp b/Educational Codeforces Round 35 (Rated for Div. 2)/E. Stack Sorting.cpp new file mode 100644 index 0000000..776c401 --- /dev/null +++ b/Educational Codeforces Round 35 (Rated for Div. 2)/E. Stack Sorting.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/contest/911/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vi fds(k); + cin >> fds; + + vi ans; + + stack s; + + rep(i, k) { + if (ans.size() == fds[i] - 1) { + ans.push_back(fds[i]); + + while (!s.empty() && s.top() == ans.size() + 1) { + ans.push_back(s.top()); + s.pop(); + } + + continue; + } + + if (!s.empty() && s.top() < fds[i]) { + cout << "-1\n"; + return 0; + } + + s.push(fds[i]); + } + + while (!s.empty() && s.top() == ans.size() - 1) { + ans.push_back(s.top()); + s.pop(); + } + + int minimal = ans.size() + 1; + int count = ans.size(); + + while (!s.empty()) { + if (s.top() != count + 1) { + for (int i = s.top() - 1; i >= minimal; i--) { + fds.push_back(i); + count++; + } + } + + count++; + minimal = s.top() + 1; + s.pop(); + } + + int now = 0; + + while (fds.size() < n) { + fds.push_back(n - now); + now++; + } + + cout << fds; +} diff --git a/Educational Codeforces Round 36 (Rated for Div. 2)/A. Garden.cpp b/Educational Codeforces Round 36 (Rated for Div. 2)/A. Garden.cpp new file mode 100644 index 0000000..a07faa9 --- /dev/null +++ b/Educational Codeforces Round 36 (Rated for Div. 2)/A. Garden.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/contest/915/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vi fds(n); + cin >> fds; + + int ans = INT32_MAX >> 1; + + repv(i, fds) { + if (k % i == 0) { + rmin(ans, k / i); + } + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 36 (Rated for Div. 2)/B. Browser.cpp b/Educational Codeforces Round 36 (Rated for Div. 2)/B. Browser.cpp new file mode 100644 index 0000000..19cf7cc --- /dev/null +++ b/Educational Codeforces Round 36 (Rated for Div. 2)/B. Browser.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/contest/915/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, pos, l, r; + cin >> n >> pos >> l >> r; + + int ans = 0; + + if (pos >= l && pos <= r) { + int first = (pos - l + (l != 1)) * (l != 1) + (r - l + (r != n)) * (r != n); + int second = (r - pos + (r != n)) * (r != n) + (r - l + (l != 1)) * (l != 1); + + cout << min(first, second) << '\n'; + return 0; + } + + if (pos < l) { + ans += l - pos; + + pos = l; + } + + if (pos > r) { + ans += pos - r; + + pos = r; + } + + if (r != n) { + ans += abs(r - pos) + 1; + } + + if (l != 1) { + ans += abs(pos - l) + 1; + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 36 (Rated for Div. 2)/C. Permute Digits.cpp b/Educational Codeforces Round 36 (Rated for Div. 2)/C. Permute Digits.cpp new file mode 100644 index 0000000..9cfa747 --- /dev/null +++ b/Educational Codeforces Round 36 (Rated for Div. 2)/C. Permute Digits.cpp @@ -0,0 +1,153 @@ +/* Problem URL: https://codeforces.com/contest/915/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a, b; + cin >> a >> b; + + sort(all(a), greater<>()); + + if (b.size() > a.size()) { + cout << a << '\n'; + return 0; + } + + V maximal(b.size()); + + maximal.back() = b.back(); + + for (int i = (int)b.size() - 2; i >= 0; i--) { + maximal[i] = max(maximal[i + 1], b[i]); + } + + vi count(10); + + repv(i, a) { + count[i - '0']++; + } + + function getans = [&](int i, int p) { + if (i >= a.size()) { + return a <= b; + } + + if (p) { + int now = 0; + while (count[now] == 0) { + now++; + } + + count[now]--; + a[i] = now + '0'; + bool res = getans(i + 1, 1); + count[now]++; + + return res; + } + + int now = 9; + + while (count[now] == 0) { + now--; + } + + a[i] = now + '0'; + count[now]--; + + while (!getans(i + 1, 1)) { + count[now]++; + now--; + + while (count[now] == 0) { + now--; + } + + a[i] = now + '0'; + count[now]--; + } + + return getans(i + 1, 0); + }; + + getans(0, 0); + + cout << a << '\n'; +} diff --git a/Educational Codeforces Round 36 (Rated for Div. 2)/D. Almost Acyclic Graph.cpp b/Educational Codeforces Round 36 (Rated for Div. 2)/D. Almost Acyclic Graph.cpp new file mode 100644 index 0000000..d03701a --- /dev/null +++ b/Educational Codeforces Round 36 (Rated for Div. 2)/D. Almost Acyclic Graph.cpp @@ -0,0 +1,191 @@ +/* Problem URL: https://codeforces.com/contest/915/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V>> graph(n); + + rep(i, m) { + int a, b; + cin >> a >> b; + + graph[a - 1].emplace_back(b - 1, i); + } + + V vis(n); + V frame(n); + + vi choices; + + function dfs = [&](int i) { + vis[i] = true; + frame[i] = true; + + for (auto [j, id] : graph[i]) { + if (frame[j]) { + choices.push_back(id); + return true; + } + + if (vis[j]) { + continue; + } + + if (dfs(j)) { + choices.push_back(id); + return true; + } + } + + frame[i] = false; + + return false; + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + if (dfs(i)) { + break; + } + } + + if (choices.empty()) { + cout << "YES\n"; + return 0; + } + + function cyclecheck = [&](int i, int ig) { + vis[i] = true; + frame[i] = true; + + for (auto [j, id] : graph[i]) { + if (id == ig) { + continue; + } + + if (frame[j]) { + return true; + } + + if (vis[j]) { + continue; + } + + if (cyclecheck(j, ig)) { + return true; + } + } + + frame[i] = false; + + return false; + }; + + repv(ig, choices) { + fill(all(vis), false); + fill(all(frame), false); + + bool valid = true; + + rep(i, n) { + if (vis[i]) { + continue; + } + + if (cyclecheck(i, ig)) { + valid = false; + break; + } + } + + if (valid) { + cout << "YES\n"; + return 0; + } + } + + cout << "NO\n"; +} diff --git a/Educational Codeforces Round 36 (Rated for Div. 2)/E. Physical Education Lessons.cpp b/Educational Codeforces Round 36 (Rated for Div. 2)/E. Physical Education Lessons.cpp new file mode 100644 index 0000000..009f79f --- /dev/null +++ b/Educational Codeforces Round 36 (Rated for Div. 2)/E. Physical Education Lessons.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/contest/915/problem/E */ + +/* + * TODO: Finish the problem + */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + set> var; + + ll ans = 0; + + while (q--) { + int l, r, k; + cin >> l >> r >> k; + + auto itr = var.lower_bound({l, 0}); + + while (itr != var.end() && itr->second <= r) { + ans -= itr->first - itr->second + 1; + int lone = itr->second; + int rone = min(l - 1, itr->first); + + if (lone <= rone) { + ans += rone - lone + 1; + var.emplace(rone, lone); + } + + int ltwo = max(itr->second, r + 1); + int rtwo = itr->first; + + if (ltwo <= rtwo) { + ans += rtwo - ltwo + 1; + var.emplace(rtwo, ltwo); + } + + itr = var.erase(itr); + } + + if (k == 1) { + ans += r - l + 1; + var.emplace(r, l); + } + + cout << n - ans << '\n'; + } +} diff --git a/Educational Codeforces Round 37 (Rated for Div. 2)/A. Water The Garden.cpp b/Educational Codeforces Round 37 (Rated for Div. 2)/A. Water The Garden.cpp new file mode 100644 index 0000000..5f1cac5 --- /dev/null +++ b/Educational Codeforces Round 37 (Rated for Div. 2)/A. Water The Garden.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/contest/920/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + int oo = INT32_MAX >> 1; + vi fds(n, oo); + + while (k--) { + int now; + cin >> now; + now--; + + fds[now] = 1; + + nrep(i, 1, n) { + if (now - i >= 0) { + rmin(fds[now - i], i + 1); + } + + if (now + i < n) { + rmin(fds[now + i], i + 1); + } + } + } + + cout << *max_element(all(fds)) << '\n'; + } +} diff --git a/Educational Codeforces Round 37 (Rated for Div. 2)/B. Tea Queue.cpp b/Educational Codeforces Round 37 (Rated for Div. 2)/B. Tea Queue.cpp new file mode 100644 index 0000000..818ca2e --- /dev/null +++ b/Educational Codeforces Round 37 (Rated for Div. 2)/B. Tea Queue.cpp @@ -0,0 +1,115 @@ +/* Problem URL: https://codeforces.com/contest/920/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + int now = 1; + + vi ans(n); + + rep(i, n) { + int l, r; + cin >> l >> r; + + if (now > r) { + continue; + } + + if (now <= l) { + ans[i] = l; + now = l + 1; + continue; + } + + if (now <= r) { + ans[i] = now; + now++; + } + } + + cout << ans; + } +} diff --git a/Educational Codeforces Round 37 (Rated for Div. 2)/C. Swap Adjacent Elements.cpp b/Educational Codeforces Round 37 (Rated for Div. 2)/C. Swap Adjacent Elements.cpp new file mode 100644 index 0000000..9eb5763 --- /dev/null +++ b/Educational Codeforces Round 37 (Rated for Div. 2)/C. Swap Adjacent Elements.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/contest/920/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + string a; + cin >> fds >> a; + + vvi count(1); + + count[0].push_back(fds[0]); + + nrep(i, 1, n) { + if (a[i - 1] == '0') { + count.emplace_back(); + } + + count.back().push_back(fds[i]); + } + + vi ans(n); + int now = 0; + + repv(j, count) { + sortv(j); + + repv(k, j) { + ans[now] = k; + now++; + } + } + + bool valid = true; + + nrep(i, 1, n) { + if (ans[i] < ans[i - 1]) { + valid = false; + break; + } + } + + cout << (valid ? "YES\n" : "NO\n"); +} diff --git a/Educational Codeforces Round 37 (Rated for Div. 2)/D. Tanks.cpp b/Educational Codeforces Round 37 (Rated for Div. 2)/D. Tanks.cpp new file mode 100644 index 0000000..2835880 --- /dev/null +++ b/Educational Codeforces Round 37 (Rated for Div. 2)/D. Tanks.cpp @@ -0,0 +1,244 @@ +/* Problem URL: https://codeforces.com/contest/920/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + ll v; + cin >> n >> k >> v; + + vl a(n); + cin >> a; + + if (v == 0) { + cout << "YES\n"; + cout << (ll)1e9 << ' ' << 1 << ' ' << 2 << '\n'; + return 0; + } + + if (accumulate(all(a), 0LL) < v) { + cout << "NO\n"; + return 0; + } + + auto printans = [&](V> &ans) { + for (auto [c, i, j] : ans) { + if (c == 0) { + continue; + } + + cout << c << ' ' << i << ' ' << j << '\n'; + } + }; + + ll mod = v % k; + vi ans; + + if (mod == 0) { + cout << "YES\n"; + V> lol; + + int choice = 0; + int other = 1; + + nrep(i, 1, n) { + lol.emplace_back(1e9, i + 1, choice + 1); + } + + lol.emplace_back(v / k, choice + 1, other + 1); + + printans(lol); + return 0; + } + + ll oo = INT64_MAX >> 1; + + V> dp(n + 1, V(k)); + + nrep(i, 1, n + 1) { + dp[i][a[i - 1] % k] = true; + rep(j, k) { + dp[i][j] = dp[i][j] || dp[i - 1][j] || dp[i - 1][((j - a[i - 1]) % k + k) % k]; + } + } + + auto getans1 = [&](ll mod) { + if (!dp[n][mod]) { + return false; + } + + int ind = n - 1; + int act = mod; + while (ind >= 0) { + while (dp[ind][act]) { + ind--; + } + + if (!dp[ind + 1][act]) { + break; + } + + ans.push_back(ind); + act = ((act - a[ind]) % k + k) % k; + ind--; + } + + return !ans.empty(); + }; + + if (!getans1(mod)) { + cout << "NO\n"; + return 0; + } + + if (ans.empty()) { + cout << "NO\n"; + return 0; + } + // + // if (ans.size() == 1) { + // ll total = a[ans[0]]; + // V> lol; + // + // ll other = ans[0] == 0 ? 1 : 0; + // + // rep(i, n) { + // if (i == ans[0]) { + // continue; + // } + // + // lol.emplace_back(a[i] / k, i + 1, ans[0] + 1); + // total += (a[i] / k) * k; + // } + // + // if (total < v) { + // cout << "NO\n"; + // return 0; + // } + // + // cout << "YES\n"; + // ll diff = total - v; + // lol.emplace_back(diff / k, ans[0] == 0 ? 2 : 1, ans[0] + 1); + // printans(lol); + // + // return 0; + // } + // + V> lol; + + set ignore; + ignore.insert(ans[0]); + + nrep(i, 1, ans.size()) { + a[ans[0]] += a[ans[i]]; + a[ans[i]] = 0; + lol.emplace_back(1e9, ans[i] + 1, ans[0] + 1); + ignore.insert(ans[i]); + } + + int other = ans[0] == 0 ? 1 : 0; + ignore.insert(other); + + rep(i, n) { + if (ignore.count(i)) { + continue; + } + + lol.emplace_back(1e9, i + 1, other + 1); + a[other] += a[i]; + a[i] = 0; + } + + lol.emplace_back(a[other] / k, other + 1, ans[0] + 1); + + a[ans[0]] += a[other] / k * k; + + if (a[ans[0]] < v) { + cout << "NO\n"; + return 0; + } + + ll diff = a[ans[0]] - v; + + cout << "YES\n"; + lol.emplace_back(diff / k, ans[0] + 1, ans[0] == 0 ? 2 : 1); + + printans(lol); +} diff --git a/Educational Codeforces Round 37 (Rated for Div. 2)/E. Connected Components?.cpp b/Educational Codeforces Round 37 (Rated for Div. 2)/E. Connected Components?.cpp new file mode 100644 index 0000000..fd1a1f5 --- /dev/null +++ b/Educational Codeforces Round 37 (Rated for Div. 2)/E. Connected Components?.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/contest/920/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, m; + cin >> n >> m; + + V> notgraph(n); + + while (m--) { + int a, b; + cin >> a >> b; + + a--, b--; + + notgraph[a].insert(b); + notgraph[b].insert(a); + } + + set v; + + rep(i, n) { + v.insert(i); + } + + vi ans; + + function dfs = [&](int i) { + int ans = 1; + + v.erase(i); + + auto itr = v.begin(); + + while (itr != v.end()) { + if (notgraph[i].count(*itr)) { + itr = v.upper_bound(*itr); + continue; + } + + int now = *itr; + ans += dfs(*itr); + itr = v.upper_bound(now); + } + + return ans; + }; + + while (!v.empty()) { + ans.push_back(dfs(*v.begin())); + } + + sortv(ans); + cout << ans.size() << '\n'; + cout << ans; +} diff --git a/Educational Codeforces Round 37 (Rated for Div. 2)/F. SUM and REPLACE.cpp b/Educational Codeforces Round 37 (Rated for Div. 2)/F. SUM and REPLACE.cpp new file mode 100644 index 0000000..e8b1623 --- /dev/null +++ b/Educational Codeforces Round 37 (Rated for Div. 2)/F. SUM and REPLACE.cpp @@ -0,0 +1,183 @@ +/* Problem URL: https://codeforces.com/contest/920/problem/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vi div(1e6 + 1, 1); + + nrep(i, 2, 1e6 + 1) { + if (div[i] > 1) { + continue; + } + + for (int j = i; j <= 1e6; j += i) { + div[j] = i; + } + } + + vi f(1e6 + 1); + f[1] = 1; + f[2] = 2; + + nrep(i, 3, 1e6 + 1) { + int now = 1; + int tmp = i; + + while (tmp > 1) { + int count = 1; + int prime = div[tmp]; + + while (tmp % prime == 0) { + tmp /= prime; + count++; + } + + now *= count; + } + + f[i] = now; + } + + int n, m; + cin >> n >> m; + + vi fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(1); + } + + V> seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = {fds[i - n], fds[i - n]}; + } + + for (int i = n - 1; i > 0; i--) { + seg[i] = {max(seg[i * 2].first, seg[i * 2 + 1].first), seg[i * 2].second + seg[i * 2 + 1].second}; + } + + function update = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl || seg[i].first <= 2) { + return; + } + + if (l == r) { + seg[i] = {f[seg[i].first], f[seg[i].first]}; + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr); + update(i * 2 + 1, mid + 1, r, tl, tr); + + seg[i] = {max(seg[i * 2].first, seg[i * 2 + 1].first), seg[i * 2].second + seg[i * 2 + 1].second}; + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0LL; + } + + if (l >= tl && r <= tr) { + return seg[i].second; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + while (m--) { + int t, l, r; + cin >> t >> l >> r; + l--, r--; + + if (t == 1) { + update(1, 0, n - 1, l, r); + continue; + } + + cout << query(1, 0, n - 1, l, r) << '\n'; + } +} diff --git a/Educational Codeforces Round 37 (Rated for Div. 2)/G. List Of Integers.cpp b/Educational Codeforces Round 37 (Rated for Div. 2)/G. List Of Integers.cpp new file mode 100644 index 0000000..26b287f --- /dev/null +++ b/Educational Codeforces Round 37 (Rated for Div. 2)/G. List Of Integers.cpp @@ -0,0 +1,162 @@ +/* Problem URL: https://codeforces.com/contest/920/problem/G */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + vi div(1e6 + 1, 1); + nrep(i, 2, 1e6 + 1) { + if (div[i] > 1) { + continue; + } + + for (int j = i; j <= 1e6; j += i) { + div[j] = i; + } + } + + int t; + cin >> t; + while (t--) { + int x, p, k; + cin >> x >> p >> k; + + vi primes; + + auto getprimes = [&]() { + int tmp = p; + while (tmp > 1) { + primes.push_back(div[tmp]); + int prime = div[tmp]; + + while (tmp % prime == 0) { + tmp /= prime; + } + } + }; + + getprimes(); + + auto getmuls = [&](ll now) { + ll ans = 0; + + nrep(i, 1, 1 << primes.size()) { + ll tmp = 1; + + rep(j, primes.size()) { + if ((i >> j) & 1) { + tmp *= primes[j]; + } + } + + if (__builtin_popcount(i) & 1) { + ans += now / tmp; + } else { + ans -= now / tmp; + } + } + + return ans; + }; + + ll inv = getmuls(x); + + ll low = x + 1; + ll high = 1e18; + ll ans = 0; + while (low <= high) { + ll mid = ((high - low) >> 1) + low; + + ll act = getmuls(mid) - inv; + if (mid - x - act >= k) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + while (__gcd(ans, (ll)p) != 1) { + ans++; + } + + cout << ans << '\n'; + } +} diff --git a/Educational Codeforces Round 38 (Rated for Div. 2)/A. Word Correction.cpp b/Educational Codeforces Round 38 (Rated for Div. 2)/A. Word Correction.cpp new file mode 100644 index 0000000..e8a3f67 --- /dev/null +++ b/Educational Codeforces Round 38 (Rated for Div. 2)/A. Word Correction.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/contest/938/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + string a; + cin >> a; + + map isvowel; + + isvowel['a'] = true; + isvowel['e'] = true; + isvowel['i'] = true; + isvowel['o'] = true; + isvowel['u'] = true; + isvowel['y'] = true; + + cout << a[0]; + nrep(i, 1, n) { + if (isvowel[a[i - 1]] && isvowel[a[i]]) { + continue; + } + + cout << a[i]; + } + cout << '\n'; +} diff --git a/Educational Codeforces Round 38 (Rated for Div. 2)/B. Run For Your Prize.cpp b/Educational Codeforces Round 38 (Rated for Div. 2)/B. Run For Your Prize.cpp new file mode 100644 index 0000000..86e1bf8 --- /dev/null +++ b/Educational Codeforces Round 38 (Rated for Div. 2)/B. Run For Your Prize.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://codeforces.com/contest/938/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi val(1e6); + + while (n--) { + int u; + cin >> u; + + val[u] = 1; + } + + int ans = 0; + rep(i, 1e6) { + if (val[i]) { + rmax(ans, min(i - 1, (int)1e6 - i)); + } + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 38 (Rated for Div. 2)/C. Constructing Tests.cpp b/Educational Codeforces Round 38 (Rated for Div. 2)/C. Constructing Tests.cpp new file mode 100644 index 0000000..b178fe2 --- /dev/null +++ b/Educational Codeforces Round 38 (Rated for Div. 2)/C. Constructing Tests.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/contest/938/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll x; + cin >> x; + + ll ansn = -1; + ll ansm = -1; + + nrep(i, 1, 1e5 + 1) { + if ((ll)i * i < x) { + continue; + } + + ll low = 1; + ll high = i; + ll tmp = 1; + + while (low <= high) { + ll mid = (low + high) >> 1; + + ll now = (i / mid) * (i / mid); + + if ((ll)i * i - now <= x) { + tmp = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + ll now = (i / tmp) * (i / tmp); + + if ((ll)i * i - now == x) { + ansn = i; + ansm = tmp; + break; + } + } + + if (ansn == -1) { + cout << "-1\n"; + continue; + } + + cout << ansn << ' ' << ansm << '\n'; + } +} diff --git a/Educational Codeforces Round 38 (Rated for Div. 2)/D. Buy a Ticket.cpp b/Educational Codeforces Round 38 (Rated for Div. 2)/D. Buy a Ticket.cpp new file mode 100644 index 0000000..1a6df0a --- /dev/null +++ b/Educational Codeforces Round 38 (Rated for Div. 2)/D. Buy a Ticket.cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/contest/938/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V>> graph(n); + + while (m--) { + int a, b; + ll c; + cin >> a >> b >> c; + + a--, b--; + + graph[a].emplace_back(b, c); + graph[b].emplace_back(a, c); + } + + vl dis(n); + cin >> dis; + + auto dijkstra = [&]() { + priority_queue, V>, greater<>> pq; + + rep(i, n) { + pq.emplace(dis[i], i); + } + + while (!pq.empty()) { + auto [c, i] = pq.top(); + pq.pop(); + + if (c != dis[i]) { + continue; + } + + for (auto [j, cc] : graph[i]) { + if (dis[j] > c + 2 * cc) { + dis[j] = c + 2 * cc; + pq.emplace(dis[j], j); + } + } + } + }; + + dijkstra(); + + cout << dis; +} diff --git a/Educational Codeforces Round 38 (Rated for Div. 2)/E. Max History.cpp b/Educational Codeforces Round 38 (Rated for Div. 2)/E. Max History.cpp new file mode 100644 index 0000000..b89cb5d --- /dev/null +++ b/Educational Codeforces Round 38 (Rated for Div. 2)/E. Max History.cpp @@ -0,0 +1,83 @@ +/* Problem URL: https://codeforces.com/contest/938/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + +} diff --git a/Educational Codeforces Round 39 (Rated for Div. 2)/A. Partition.cpp b/Educational Codeforces Round 39 (Rated for Div. 2)/A. Partition.cpp new file mode 100644 index 0000000..87978c5 --- /dev/null +++ b/Educational Codeforces Round 39 (Rated for Div. 2)/A. Partition.cpp @@ -0,0 +1,96 @@ +/* Problem URL: https://codeforces.com/contest/946/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + ll ans = 0; + + repv(i, fds) { + ans += abs(i); + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 39 (Rated for Div. 2)/B. Weird Subtraction Process.cpp b/Educational Codeforces Round 39 (Rated for Div. 2)/B. Weird Subtraction Process.cpp new file mode 100644 index 0000000..310fd74 --- /dev/null +++ b/Educational Codeforces Round 39 (Rated for Div. 2)/B. Weird Subtraction Process.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://codeforces.com/contest/946/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll a, b; + cin >> a >> b; + + while (a * 2 <= b || b * 2 <= a) { + ll tmp = a / (b << 1); + a -= tmp * (b << 1); + + if (a == 0) { + break; + } + + tmp = b / (a << 1); + b -= tmp * (a << 1); + + if (b == 0) { + break; + } + } + + cout << a << ' ' << b << '\n'; +} diff --git a/Educational Codeforces Round 39 (Rated for Div. 2)/C. String Transformation.cpp b/Educational Codeforces Round 39 (Rated for Div. 2)/C. String Transformation.cpp new file mode 100644 index 0000000..e3648bc --- /dev/null +++ b/Educational Codeforces Round 39 (Rated for Div. 2)/C. String Transformation.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/contest/946/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string s; + cin >> s; + + if (s.size() < 26) { + cout << "-1\n"; + return 0; + } + + int now = 0; + + rep(i, s.size()) { + if (s[i] - 'a' <= now) { + s[i] = now + 'a'; + now++; + } + + if (now == 26) { + break; + } + } + + if (now < 26) { + cout << "-1\n"; + return 0; + } + + cout << s << '\n'; +} diff --git a/Educational Codeforces Round 39 (Rated for Div. 2)/D. Timetable.cpp b/Educational Codeforces Round 39 (Rated for Div. 2)/D. Timetable.cpp new file mode 100644 index 0000000..d77d1db --- /dev/null +++ b/Educational Codeforces Round 39 (Rated for Div. 2)/D. Timetable.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/contest/946/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k; + cin >> n >> m >> k; + + vvi hours(n); + + repv(i, hours) { + string fds; + cin >> fds; + + rep(j, m) { + if (fds[j] == '1') { + i.push_back(j); + } + } + } + + vvi bestbyk(n, vi(k + 1)); + + rep(i, n) { + if (hours[i].empty()) { + continue; + } + + bestbyk[i][0] = (hours[i].back() - hours[i].front() + 1); + + for (int j = 1; j <= k && j < hours[i].size(); j++) { + bestbyk[i][j] = INT32_MAX >> 1; + rep(l, j + 1) { + rmin(bestbyk[i][j], hours[i][hours[i].size() - (j - l + 1)] - hours[i][l] + 1); + } + } + } + + int oo = INT32_MAX >> 1; + + vvi dp(n + 1, vi(k + 1, oo)); + fill(all(dp[0]), 0); + + nrep(i, 1, n + 1) { + rep(j, k + 1) { + rep(l, j + 1) { + rmin(dp[i][j], bestbyk[i - 1][l] + dp[i - 1][j - l]); + } + } + } + + cout << dp.back().back() << '\n'; +} diff --git a/Educational Codeforces Round 39 (Rated for Div. 2)/E. Largest Beautiful Number.cpp b/Educational Codeforces Round 39 (Rated for Div. 2)/E. Largest Beautiful Number.cpp new file mode 100644 index 0000000..f5257bf --- /dev/null +++ b/Educational Codeforces Round 39 (Rated for Div. 2)/E. Largest Beautiful Number.cpp @@ -0,0 +1,93 @@ +/* Problem URL: https://codeforces.com/contest/946/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + string s; + cin >> s; + + string ans; + + cout << ans << '\n'; + } +} diff --git a/Educational Codeforces Round 39 (Rated for Div. 2)/F. Fibonacci String Subsequences.cpp b/Educational Codeforces Round 39 (Rated for Div. 2)/F. Fibonacci String Subsequences.cpp new file mode 100644 index 0000000..3cc639f --- /dev/null +++ b/Educational Codeforces Round 39 (Rated for Div. 2)/F. Fibonacci String Subsequences.cpp @@ -0,0 +1,84 @@ +/* Problem URL: https://codeforces.com/contest/946/problem/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + +} diff --git a/I Maratona do Cerrado (mirror)/F. Fatorial.cpp b/I Maratona do Cerrado (mirror)/F. Fatorial.cpp new file mode 100644 index 0000000..60ca85d --- /dev/null +++ b/I Maratona do Cerrado (mirror)/F. Fatorial.cpp @@ -0,0 +1,224 @@ +/* Problem URL: https://codeforces.com/group/btcK4I5D5f/contest/601720/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll fact(ll n) +{ + if (n == 1) { + return n; + } + return n * fact(n - 1); +} + +ll fastpow(ll n, ll p) +{ + ll ans = 1; + ll power = n % 10; + + for (size_t i = 0; i <= 100; i++) { + if (p & ((__int128)1 << i)) { + ans *= power; + ans %= 10; + } + power *= power; + power %= 10; + } + + return ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll no; + cin >> no; + __int128 n = no; + + __int128 n2 = 0; + __int128 n3 = 0; + __int128 n4 = 0; + __int128 n5 = 0; + __int128 n6 = 0; + __int128 n7 = 0; + __int128 n8 = 0; + __int128 n9 = 0; + for (__int128 i = 10; i <= n * (__int128)10; i *= 10) { + if (n / (i / 10) % 10 >= 2) { + n2 += n / i + 1; + } else { + n2 += n / i; + } + if (n / (i / 10) % 10 >= 3) { + n3 += n / i + 1; + } else { + n3 += n / i; + } + if (n / (i / 10) % 10 >= 4) { + n4 += n / i + 1; + } else { + n4 += n / i; + } + if (n / (i / 10) % 10 >= 5) { + n5 += n / i + 1; + } else { + n5 += n / i; + } + if (n / (i / 10) % 10 >= 6) { + n6 += n / i + 1; + } else { + n6 += n / i; + } + if (n / (i / 10) % 10 >= 7) { + n7 += n / i + 1; + } else { + n7 += n / i; + } + if (n / (i / 10) % 10 >= 8) { + n8 += n / i + 1; + } else { + n8 += n / i; + } + if (n / (i / 10) % 10 >= 9) { + n9 += n / i + 1; + } else { + n9 += n / i; + } + } + + // cout << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << ' ' << n6 << ' ' << n7 << ' ' << n8 << ' ' << n9 << '\n'; + + n5 = 0; + for (__int128 i = 5; i <= n; i *= 5) { + for (__int128 j = 1; i * j <= n; j *= 10) { + __int128 tmp = n / (i * j); + + // n2 += tmp / 10; + // if (tmp % 10 >= 2) { + // n2++; + // } + n3 += tmp / 10; + if (tmp % 10 >= 3) { + n3++; + } + // n4 += tmp / 10; + // if (tmp % 10 >= 4) { + // n4++; + // } + n6 += tmp / 10; + if (tmp % 10 >= 6) { + n6++; + } + n7 += tmp / 10; + if (tmp % 10 >= 7) { + n7++; + } + // n8 += tmp / 10; + // if (tmp % 10 >= 8) { + // n8++; + // } + n9 += tmp / 10; + if (tmp % 10 >= 9) { + n9++; + } + } + n5 += n / i; + } + + for (__int128 i = 10; i <= n; i *= 10) { + n5 -= n / i; + } + + // cout << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << ' ' << n6 << ' ' << n7 << ' ' << n8 << ' ' << n9 << '\n'; + n2 -= n5; + if (n2 < 0) { + ll diff = -n2; + n2 = diff; + n4 -= diff; + } + // n2 += n10; + // cout << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << ' ' << n6 << ' ' << n7 << ' ' << n8 << ' ' << n9 << '\n'; + ll ans = fastpow(2, n2); + ans %= 10; + ans *= fastpow(3, n3); + ans %= 10; + ans *= fastpow(4, n4); + ans %= 10; + ans *= fastpow(6, n6); + ans %= 10; + ans *= fastpow(7, n7); + ans %= 10; + ans *= fastpow(8, n8); + ans %= 10; + ans *= fastpow(9, n9); + ans %= 10; + cout << ans << '\n'; +} diff --git a/I Maratona do Cerrado (mirror)/M. Mangabas.cpp b/I Maratona do Cerrado (mirror)/M. Mangabas.cpp new file mode 100644 index 0000000..6dac800 --- /dev/null +++ b/I Maratona do Cerrado (mirror)/M. Mangabas.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/group/btcK4I5D5f/contest/601720/problem/M */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k, m; + cin >> n >> k >> m; + + vvi dp(51, vi(m + 1, 0)); + rep(i, 51) { + nrep(j, 1, m + 1) { + if (i <= k) { + dp[i][j] = i; + continue; + } + + rep(k, j) { + rmax(dp[i][j], dp[i / 2][k] + dp[i / 2 + i % 2][j - k - 1]); + } + } + } + + vi mangas(n); + cin >> mangas; + + vvi dp2(n + 1, vi(m + 1, 0)); + + rep(i, n) { + nrep(j, 1, m + 1) { + rep(k, j + 1) { + rmax(dp2[i + 1][j], dp2[i][k] + dp[mangas[i]][j - k]); + } + rmax(dp2[i + 1][j], dp2[i][j]); + } + } + + V> ops; + + function getops = [&](int manga, int j){ + if (j == 0) { + return; + } + + if (manga <= k) { + ops.emplace_back(1, manga); + return; + } + + rep(i, j) { + if (dp[manga / 2][i] + dp[manga / 2 + manga % 2][j - i - 1] == dp[manga][j]) { + ops.emplace_back(2, manga); + getops(manga / 2, i); + getops(manga / 2 + manga % 2, j - i - 1); + return; + } + } + }; + + auto recover = [&](){ + int i = n; + int j = m; + + while (j > 0 && i > 0 && dp2[i][j] > 0) { + while (dp2[i - 1][j] == dp2[i][j]) { + i--; + } + + while (dp2[i][j - 1] == dp2[i][j]) { + j--; + } + + rep(k, j) { + if (dp2[i - 1][k] + dp[mangas[i - 1]][j - k] == dp2[i][j]) { + getops(mangas[i - 1], j - k); + j = k; + i--; + break; + } + } + } + }; + + recover(); + + cout << dp2.back().back() << ' ' << ops.size() << '\n'; + for (auto [i, j] : ops) { + cout << i << ' ' << j << '\n'; + } +} diff --git a/I Maratona do Cerrado (mirror)/N. Não é permitido erros!.cpp b/I Maratona do Cerrado (mirror)/N. Não é permitido erros!.cpp new file mode 100644 index 0000000..6c5c32b --- /dev/null +++ b/I Maratona do Cerrado (mirror)/N. Não é permitido erros!.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/group/btcK4I5D5f/contest/601720/problem/N */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int m; + cin >> m; + + if (m == 1) { + int n; + cin >> n; + string a; + cin >> a; + + a += a; + char sum = 0; + rep(i, n) { + sum = (sum + a[i] - 'a') % 26; + } + a.push_back(sum + 'a'); + sum = 0; + nrep(i, n, n * 2) { + sum = (sum + a[i] - 'a') % 26; + } + a.push_back(sum + 'a'); + cout << a << '\n'; + } else { + int n; + cin >> n; + string a; + cin >> a; + + n = (n - 2) / 2; + string a1 = a.substr(0, n); + string a2 = a.substr(n, n); + int p1 = a[n * 2] - 'a'; + + int test = 0; + repv(i, a1) { + test = (test + i - 'a') % 26; + } + + if (test == p1) { + cout << a1 << '\n'; + return 0; + } + + cout << a2 << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 1/A. Binary Search.cpp b/ITMO Academy: pilot course - Binary Search - Step 1/A. Binary Search.cpp new file mode 100644 index 0000000..9ee4a32 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 1/A. Binary Search.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/1/practice/contest/283911/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vl fds(n); + cin >> fds; + sortv(fds); + + while (q--) { + ll now; + cin >> now; + + auto itr = lower_bound(all(fds), now); + + if (itr != fds.end() && *itr == now) { + cout << "YES\n"; + continue; + } + + cout << "NO\n"; + } +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 1/B. Closest to the Left.cpp b/ITMO Academy: pilot course - Binary Search - Step 1/B. Closest to the Left.cpp new file mode 100644 index 0000000..0713dee --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 1/B. Closest to the Left.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/1/practice/contest/283911/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vl fds(n); + cin >> fds; + + while (q--) { + ll now; + cin >> now; + + auto itr = upper_bound(all(fds), now); + + cout << itr - fds.begin() << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 1/C. Closest to the Right.cpp b/ITMO Academy: pilot course - Binary Search - Step 1/C. Closest to the Right.cpp new file mode 100644 index 0000000..7961977 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 1/C. Closest to the Right.cpp @@ -0,0 +1,97 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/1/practice/contest/283911/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vl fds(n); + cin >> fds; + + while (q--) { + ll now; + cin >> now; + + auto itr = lower_bound(all(fds), now); + + cout << itr - fds.begin() + 1 << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 1/D. Fast search.cpp b/ITMO Academy: pilot course - Binary Search - Step 1/D. Fast search.cpp new file mode 100644 index 0000000..2013c79 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 1/D. Fast search.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/1/practice/contest/283911/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + sortv(fds); + + int q; + cin >> q; + while (q--) { + ll l, r; + cin >> l >> r; + + auto lower = lower_bound(all(fds), l); + auto upper = upper_bound(all(fds), r); + + if (lower == fds.end() || upper == fds.begin()) { + cout << "0" << " \n"[q == 0]; + continue; + } + + upper--; + + cout << upper - lower + 1 << " \n"[q == 0]; + } +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 2/A. Packing Rectangles.cpp b/ITMO Academy: pilot course - Binary Search - Step 2/A. Packing Rectangles.cpp new file mode 100644 index 0000000..13da31d --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 2/A. Packing Rectangles.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/2/practice/contest/283932/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll w, h, n; + cin >> w >> h >> n; + + ll low = 1; + ll high = 1e18; + ll ans = 1e18; + while (low <= high) { + ll mid = ((high - low) >> 1) + low; + + if (((__int128)mid / w) * ((__int128)mid / h) >= n) { + rmin(ans, mid); + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 2/B. Ropes.cpp b/ITMO Academy: pilot course - Binary Search - Step 2/B. Ropes.cpp new file mode 100644 index 0000000..94d8302 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 2/B. Ropes.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/2/practice/contest/283932/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vl fds(n); + cin >> fds; + + long double eps = 1e-9; + + long double low = 0; + long double high = 1e18; + long double ans = 0; + + while ((high - low) >= eps) { + long double mid = (low + high) / 2; + + ll total = 0; + + repv(i, fds) { + total += (ll)(i / mid); + if (total >= k) { + break; + } + } + + if (total >= k) { + rmax(ans, mid); + low = mid; + continue; + } + + high = mid; + } + + cout << setprecision(9) << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 2/E. Equation.cpp b/ITMO Academy: pilot course - Binary Search - Step 2/E. Equation.cpp new file mode 100644 index 0000000..b29b448 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 2/E. Equation.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/2/practice/contest/283932/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + long double c; + cin >> c; + + long double low = 0; + long double high = 1e9; + long double eps = 1e-10; + + while ((high - low) >= eps) { + long double mid = (high + low) / 2; + + if (mid * mid + sqrt(mid) <= c) { + low = mid; + continue; + } + + high = mid; + } + + cout << setprecision(9) << low << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 2/G. Student Councils.cpp b/ITMO Academy: pilot course - Binary Search - Step 2/G. Student Councils.cpp new file mode 100644 index 0000000..3ee19ee --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 2/G. Student Councils.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/2/practice/contest/283932/problem/G */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int k, n; + cin >> k >> n; + + vl fds(n); + cin >> fds; + + ll ans = 0; + ll low = 0; + ll high = INT64_MAX >> 1; + + while (low <= high) { + ll mid = (high + low) >> 1; + + vvl dp(n, vl(k + 1)); + dp[0][1] = min(fds[0], mid); + dp[0][0] = mid - dp[0][1]; + + nrep(i, 1, n) { + ll now = fds[i]; + rep(j, k) { + ll minimal = min(dp[i - 1][j], now); + + dp[i][j + 1] += minimal; + dp[i][j] += dp[i - 1][j] - minimal; + now -= minimal; + } + + dp[i][k] += dp[i - 1][k]; + } + + if (dp.back().back() >= mid) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 2/H. Hamburgers.cpp b/ITMO Academy: pilot course - Binary Search - Step 2/H. Hamburgers.cpp new file mode 100644 index 0000000..d5b7b11 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 2/H. Hamburgers.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/2/practice/contest/283932/problem/H */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string fds; + cin >> fds; + + map var; + var['B'] = 0; + var['S'] = 1; + var['C'] = 2; + + vl items(3); + + repv(i, fds) { + items[var[i]]++; + } + + ll a, b, c; + cin >> a >> b >> c; + + ll pa, pb, pc; + cin >> pa >> pb >> pc; + + ll m; + cin >> m; + + ll ans = 0; + ll low = 0; + ll high = 1e13; + + while (low <= high) { + ll mid = (high + low) >> 1; + + ll total = pa * max(items[0] * mid - a, 0LL) + max(pb * (items[1] * mid - b), 0LL) + max(pc * (items[2] * mid - c), 0LL); + + if (total <= m) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 3/A. Get together.cpp b/ITMO Academy: pilot course - Binary Search - Step 3/A. Get together.cpp new file mode 100644 index 0000000..bc3c5ee --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 3/A. Get together.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/3/practice/contest/285083/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> fds(n); + + repv(i, fds) { + cin >> i.first >> i.second; + } + + long double ans = 3e9; + long double low = 0; + long double high = 3e9; + long double eps = 1e-9; + + while ((high - low) >= eps) { + long double mid = (low + high) / 2; + + long double l = -1e18; + long double r = 1e18; + + repv(i, fds) { + rmax(l, i.first - (mid * i.second)); + rmin(r, i.first + (mid * i.second)); + } + + if (l <= r) { + ans = mid; + high = mid; + continue; + } + + low = mid; + } + + cout << setprecision(9) << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 3/B. Splitting an Array.cpp b/ITMO Academy: pilot course - Binary Search - Step 3/B. Splitting an Array.cpp new file mode 100644 index 0000000..6bfe703 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 3/B. Splitting an Array.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/3/practice/contest/285083/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vl fds(n); + cin >> fds; + + ll ans = 1e15; + ll low = 1; + ll high = 1e15; + + while (low <= high) { + ll mid = (low + high) >> 1; + + ll count = 0; + ll now = 0; + ll midans = 0; + int i = 0; + for (; i < n && count < k - 1; i++) { + if (now + fds[i] > mid) { + now = 0; + count++; + } + + now += fds[i]; + rmax(midans, now); + } + + for (; i < n; i++) { + now += fds[i]; + } + rmax(midans, now); + + if (midans <= mid) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 3/C. Cows in Stalls.cpp b/ITMO Academy: pilot course - Binary Search - Step 3/C. Cows in Stalls.cpp new file mode 100644 index 0000000..2d2d062 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 3/C. Cows in Stalls.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/3/practice/contest/285083/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vl fds(n); + cin >> fds; + + ll ans = 0; + ll low = 1; + ll high = 1e9; + + while (low <= high) { + ll mid = (low + high) >> 1; + + ll now = k - 1; + ll prev = fds[0]; + + for (int i = 1; i < n && now > 0; i++) { + if ((fds[i] - prev) >= mid) { + prev = fds[i]; + now--; + } + } + + if (now == 0) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 3/D. Minimum maximum on the Path.cpp b/ITMO Academy: pilot course - Binary Search - Step 3/D. Minimum maximum on the Path.cpp new file mode 100644 index 0000000..e4cb4d0 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 3/D. Minimum maximum on the Path.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/3/practice/contest/285083/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, d; + cin >> n >> m >> d; + + V>> graph(n); + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + + graph[a - 1].emplace_back(b - 1, c); + } + + vi parent(n); + + int ans = -1; + int low = 0; + int high = 1e9; + + while (low <= high) { + int mid = ((high - low) >> 1) + low; + + vi p(n); + + auto bfs = [&]() { + const int inf = INT32_MAX >> 1; + + vi dis(n, inf); + queue q; + + dis[0] = 0; + q.push(0); + + while (!q.empty()) { + int i = q.front(); + q.pop(); + + for (auto [j, c] : graph[i]) { + if (c > mid) { + continue; + } + + if (dis[j] > dis[i] + 1) { + dis[j] = dis[i] + 1; + p[j] = i; + q.push(j); + } + } + } + + return dis.back(); + }; + + int now = bfs(); + + if (now <= d) { + ans = mid; + parent = move(p); + high = mid - 1; + continue; + } + + low = mid + 1; + } + + if (ans == -1) { + cout << "-1\n"; + return 0; + } + + vi tmp = {n}; + int now = parent[n - 1]; + while (now != 0) { + tmp.push_back(now + 1); + now = parent[now]; + } + + tmp.push_back(1); + reverse(all(tmp)); + + cout << tmp.size() - 1 << '\n' << tmp; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 4/A. Maximum Average Segment.cpp b/ITMO Academy: pilot course - Binary Search - Step 4/A. Maximum Average Segment.cpp new file mode 100644 index 0000000..c94059b --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 4/A. Maximum Average Segment.cpp @@ -0,0 +1,141 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/4/practice/contest/285069/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, d; + cin >> n >> d; + + vl fds(n); + cin >> fds; + + V pref(n + 1); + + int lans = 1; + int rans = 1; + long double low = 0; + long double high = 1e7; + long double eps = 1e-9; + + while ((high - low) >= eps) { + long double mid = (low + high) / 2; + + rep(i, d - 1) { + pref[i + 1] = pref[i] + fds[i] - mid; + } + + int tmpl = 0; + int tmpr = 0; + bool pos = false; + + long double minimal = 0; + int ind = 0; + + nrep(i, d, n + 1) { + pref[i] = pref[i - 1] + fds[i - 1] - mid; + + if (pref[i - d] <= minimal) { + minimal = pref[i - d]; + ind = i - d; + } + + if (pref[i] - minimal < 0) { + continue; + } + + pos = true; + tmpl = ind + 1; + tmpr = i; + break; + } + + if (pos) { + lans = tmpl; + rans = tmpr; + + low = mid; + continue; + } + + high = mid; + } + + cout << lans << ' ' << rans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 4/B. Minimum Average Path.cpp b/ITMO Academy: pilot course - Binary Search - Step 4/B. Minimum Average Path.cpp new file mode 100644 index 0000000..1dd33d9 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 4/B. Minimum Average Path.cpp @@ -0,0 +1,159 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/4/practice/contest/285069/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V>> graph(n); + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + + graph[a - 1].emplace_back(b - 1, c); + } + + vi parent(n); + + long double low = 0; + long double high = 1e3; + long double act = 0; + long double eps = 1e-9; + + vi next(n, -1); + + long double inf = 1e18; + + while ((high - low) >= eps) { + long double mid = (high + low) / 2; + + V memo(n, -inf); + + function dfs = [&](int i) { + long double &ans = memo[i]; + if (ans != -inf) { + return ans; + } + + if (i == n - 1) { + ans = 0; + return ans; + } + + ans = inf; + + for (auto [j, c] : graph[i]) { + // rmin(ans, dfs(j) + c - mid); + long double tmp = dfs(j) + c - mid; + if (tmp <= ans) { + next[i] = j; + ans = tmp; + } + } + + return ans; + }; + + long double tmp = dfs(0); + + if (tmp <= 0) { + high = mid; + act = mid; + continue; + } + + low = mid; + } + + vi ans = {1}; + + int now = next[0]; + + while (now != n - 1) { + ans.push_back(now + 1); + now = next[now]; + } + + ans.push_back(n); + + cout << ans.size() - 1 << '\n' << ans; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 4/C. Pair Selection.cpp b/ITMO Academy: pilot course - Binary Search - Step 4/C. Pair Selection.cpp new file mode 100644 index 0000000..cf7ba1b --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 4/C. Pair Selection.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/4/practice/contest/285069/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + V> fds(n); + + repv(i, fds) { + cin >> i.first >> i.second; + } + + V vec(n); + + long double low = 0; + long double high = 1e10; + long double eps = 1e-12; + + while ((high - low) >= eps) { + long double mid = (low + high) / 2; + + rep(i, n) { + vec[i] = fds[i].first - fds[i].second * mid; + } + + sort(all(vec), greater<>()); + + long double ans = 0; + rep(i, k) { + ans += vec[i]; + } + + if (ans >= 0) { + low = mid; + continue; + } + + high = mid; + } + + cout << setprecision(10) << low << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 5/A. K-th Number in the Union of Segments.cpp b/ITMO Academy: pilot course - Binary Search - Step 5/A. K-th Number in the Union of Segments.cpp new file mode 100644 index 0000000..10eb5a1 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 5/A. K-th Number in the Union of Segments.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/5/practice/contest/285084/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + V> fds(n); + repv(i, fds) { + cin >> i.first >> i.second; + } + + ll low = -1e10; + ll high = 1e10; + ll ans = 0; + while (low <= high) { + ll mid = (high + low) >> 1; + + ll count = 0; + repv(i, fds) { + if (i.first > mid) { + continue; + } + + count += min(i.second, mid) - i.first + 1; + } + + if (count >= k + 1) { + high = mid - 1; + ans = mid; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 5/B. Multiplication Table.cpp b/ITMO Academy: pilot course - Binary Search - Step 5/B. Multiplication Table.cpp new file mode 100644 index 0000000..4037b37 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 5/B. Multiplication Table.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/5/practice/contest/285084/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, k; + cin >> n >> k; + + ll ans = 0; + ll low = 1; + ll high = n * n; + + while (low <= high) { + ll mid = (high + low) >> 1; + + ll count = 0; + nrep(i, 1, n + 1) { + count += min(n, mid / i); + } + + if (count >= k) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Binary Search - Step 5/C. K-th Sum.cpp b/ITMO Academy: pilot course - Binary Search - Step 5/C. K-th Sum.cpp new file mode 100644 index 0000000..8704bf6 --- /dev/null +++ b/ITMO Academy: pilot course - Binary Search - Step 5/C. K-th Sum.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/6/5/practice/contest/285084/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, k; + cin >> n >> k; + + vl a(n); + vl b(n); + cin >> a >> b; + + sortv(a); + sortv(b); + + ll ans = 0; + ll low = 1; + ll high = 2e9; + + while (low <= high) { + ll mid = (high + low) >> 1; + + ll count = 0; + rep(i, n) { + auto itr = upper_bound(all(b), mid - a[i]); + + count += itr - b.begin(); + } + + if (count >= k) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/C. Experience.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/C. Experience.cpp new file mode 100644 index 0000000..e1b825d --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 1/C. Experience.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/1/practice/contest/289390/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + vl add(n); + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + int res = find_p(dsu[i]); + if (res != dsu[i]) { + add[i] += add[dsu[i]]; + } + return dsu[i] = res; + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a == b) { + return; + } + + dsu.push_back(dsu.size()); + add.push_back(0); + dsu[a] = dsu.size() - 1; + dsu[b] = dsu.size() - 1; + }; + + auto get = [&](int i) { + find_p(i); + + return i == dsu[i] ? add[i] : add[i] + add[dsu[i]]; + }; + + while (m--) { + string op; + cin >> op; + + if (op == "add") { + int i; + int v; + cin >> i >> v; + + i = find_p(i - 1); + add[i] += v; + continue; + } + + if (op == "join") { + int a, b; + cin >> a >> b; + join(a - 1, b - 1); + continue; + } + + int i; + cin >> i; + + cout << get(i - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/A. Segment Tree for the Sum.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/A. Segment Tree for the Sum.cpp new file mode 100644 index 0000000..835c897 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/A. Segment Tree for the Sum.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/1/practice/contest/273169/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(0); + } + + vl seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = fds[i - n]; + } + + for (int i = n - 1; i > 0; i--) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + + function update = [&](int i, ll v) { + seg[i + n] = v; + + for (i = (i + n) >> 1; i > 0; i >>= 1) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0LL; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + while (m--) { + int op; + cin >> op; + if (op == 1) { + int i; + ll v; + cin >> i >> v; + + update(i, v); + continue; + } + + int l, r; + cin >> l >> r; + + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/B. Segment Tree for the Minimum.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/B. Segment Tree for the Minimum.cpp new file mode 100644 index 0000000..c7f266d --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/B. Segment Tree for the Minimum.cpp @@ -0,0 +1,144 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/1/practice/contest/273169/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(INT64_MAX >> 1); + } + + vl seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = fds[i - n]; + } + + for (int i = n - 1; i > 0; i--) { + seg[i] = min(seg[i * 2], seg[i * 2 + 1]); + } + + function update = [&](int i, ll v) { + seg[i + n] = v; + + for (i = (i + n) >> 1; i > 0; i >>= 1) { + seg[i] = min(seg[i * 2], seg[i * 2 + 1]); + } + }; + + function query = [&](int i, int l, int r, int tl, int tr) -> ll { + if (l > tr || r < tl) { + return INT64_MAX >> 1; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return min(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + while (m--) { + int op; + cin >> op; + if (op == 1) { + int i; + ll v; + cin >> i >> v; + + update(i, v); + continue; + } + + int l, r; + cin >> l >> r; + + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } + +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/C. Number of Minimums on a Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/C. Number of Minimums on a Segment.cpp new file mode 100644 index 0000000..49f3381 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 1/C. Number of Minimums on a Segment.cpp @@ -0,0 +1,153 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/1/practice/contest/273169/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(INT64_MAX >> 1); + } + + V> seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = {fds[i - n], 1}; + } + + auto merge = [&](pair a, pair b) -> pair { + if (a.first == b.first) { + return {a.first, a.second + b.second}; + } + + return min(a, b); + }; + + for (int i = n - 1; i > 0; i--) { + seg[i] = merge(seg[i * 2], seg[i * 2 + 1]); + } + + function update = [&](int i, ll v) { + seg[i + n].first = v; + + for (i = (i + n) >> 1; i > 0; i >>= 1) { + seg[i] = merge(seg[i * 2], seg[i * 2 + 1]); + } + }; + + function(int, int, int, int, int)> query = [&](int i, int l, int r, int tl, int tr) -> pair { + if (l > tr || r < tl) { + return {INT64_MAX >> 1, 0}; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return merge(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + while (m--) { + int op; + cin >> op; + if (op == 1) { + int i; + ll v; + cin >> i >> v; + + update(i, v); + continue; + } + + int l, r; + cin >> l >> r; + + auto ans = query(1, 0, n - 1, l, r - 1); + + cout << ans.first << ' ' << ans.second << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/B. K-th one.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/B. K-th one.cpp new file mode 100644 index 0000000..816d16e --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/B. K-th one.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/2/practice/contest/273278/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(0); + } + + vi seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = fds[i - n]; + } + + for (int i = n - 1; i > 0; i--) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + + auto update = [&](int i) { + seg[i + n] ^= 1; + + for (i = (i + n) >> 1; i > 0; i >>= 1) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + }; + + function query = [&](int i, int l, int r, int k) { + if (seg[i] < k) { + return -1; + } + + if (l == r) { + return i - n; + } + + int mid = (l + r) >> 1; + + int ans = query(i * 2, l, mid, k); + + if (ans == -1) { + return query(i * 2 + 1, mid + 1, r, k - seg[i * 2]); + } + + return ans; + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int i; + cin >> i; + update(i); + continue; + } + + int k; + cin >> k; + + cout << query(1, 0, n - 1, k + 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/C. First element at least X.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/C. First element at least X.cpp new file mode 100644 index 0000000..0a12549 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/C. First element at least X.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/2/practice/contest/273278/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(0); + } + + vl seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = fds[i - n]; + } + + for (int i = n - 1; i > 0; i--) { + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + } + + auto update = [&](int i, ll v) { + seg[i + n] = v; + + for (i = (i + n) >> 1; i > 0; i >>= 1) { + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + } + }; + + function query = [&](int i, int l, int r, ll v) { + if (seg[i] < v) { + return -1; + } + + if (l == r) { + return i - n; + } + + int mid = (l + r) >> 1; + + int ans = query(i * 2, l, mid, v); + if (ans == -1) { + return query(i * 2 + 1, mid + 1, r, v); + } + + return ans; + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int i; + ll v; + cin >> i >> v; + update(i, v); + continue; + } + + ll x; + cin >> x; + + cout << query(1, 0, n - 1, x) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/D. First element at least X - 2.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/D. First element at least X - 2.cpp new file mode 100644 index 0000000..1cf9c8e --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 2/D. First element at least X - 2.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/2/practice/contest/273278/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(0); + } + + vl seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = fds[i - n]; + } + + for (int i = n - 1; i > 0; i--) { + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + } + + auto update = [&](int i, ll v) { + seg[i + n] = v; + + for (i = (i + n) >> 1; i > 0; i >>= 1) { + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + } + }; + + function query = [&](int i, int l, int r, int tl, ll v) { + if (seg[i] < v || r < tl) { + return -1; + } + + if (l == r) { + return i - n; + } + + int mid = (l + r) >> 1; + + int ans = query(i * 2, l, mid, tl, v); + if (ans == -1) { + return query(i * 2 + 1, mid + 1, r, tl, v); + } + + return ans; + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int i; + ll v; + cin >> i >> v; + update(i, v); + continue; + } + + int l; + ll x; + cin >> x >> l; + + cout << query(1, 0, n - 1, l, x) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/A. Inversions.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/A. Inversions.cpp new file mode 100644 index 0000000..3b1856d --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/A. Inversions.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/3/practice/contest/274545/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + cin >> fds; + + int size = n + 1; + + while (__builtin_popcount(size) != 1) { + size++; + fds.push_back(0); + } + + vi seg(size * 2); + + auto update = [&](int i) { + seg[i + size]++; + + for (i = (i + size) >> 1; i > 0; i >>= 1) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + rep(i, n) { + cout << query(1, 0, size - 1, fds[i], n) << ' '; + update(fds[i]); + } + cout << '\n'; +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/B. Inversions 2.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/B. Inversions 2.cpp new file mode 100644 index 0000000..9dfa38a --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/B. Inversions 2.cpp @@ -0,0 +1,144 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/3/practice/contest/274545/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + cin >> fds; + reverse(all(fds)); + + vi ans(n); + + int size = n; + if (size > 1) { + size = 1 << (32 - __builtin_clz(size - 1)); + } + + vi seg(size * 2); + + nrep(i, size, size + n) { + seg[i] = 1; + } + + for (int i = size - 1; i > 0; i--) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + + auto update = [&](int i) { + i += size; + seg[i] = 0; + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + }; + + function query = [&](int i, int l, int r, int k) { + if (seg[i] < k) { + return -1; + } + + if (l == r) { + return i - size; + } + + int mid = (l + r) >> 1; + + int ans = query(i * 2 + 1, mid + 1, r, k); + if (ans == -1) { + return query(i * 2, l, mid, k - seg[i * 2 + 1]); + } + + return ans; + }; + + rep(i, n) { + int act = query(1, 0, size - 1, fds[i] + 1) + 1; + update(act - 1); + + ans[i] = act; + } + + for (int i = n - 1; i >= 0; i--) { + cout << ans[i] << " \n"[i == 0]; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/C. Nested Segments.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/C. Nested Segments.cpp new file mode 100644 index 0000000..aaa53d0 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/C. Nested Segments.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/3/practice/contest/274545/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi prev(n, -1); + + vi fds(n * 2); + repv(i, fds) { + cin >> i; + i--; + } + + vi ans(n); + + int size = n * 2; + if (size > 1) { + size = 1 << (32 - __builtin_clz(size)); + } + + vi seg(size * 2); + + auto update = [&](int i) { + i += size; + seg[i]++; + + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + rep(i, n * 2) { + if (prev[fds[i]] == -1) { + prev[fds[i]] = i; + continue; + } + + ans[fds[i]] = query(1, 0, size - 1, prev[fds[i]], i); + update(prev[fds[i]]); + } + + cout << ans; +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/D. Intersecting Segments.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/D. Intersecting Segments.cpp new file mode 100644 index 0000000..703a167 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/D. Intersecting Segments.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/3/practice/contest/274545/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi prev(n, -1); + + vi fds(n * 2); + repv(i, fds) { + cin >> i; + i--; + } + + vi ans(n); + + int size = n * 2; + if (size > 1) { + size = 1 << (32 - __builtin_clz(size)); + } + + vi seg(size * 2); + + auto update = [&](int i, int v) { + i += size; + seg[i] += v; + + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + rep(i, n * 2) { + if (prev[fds[i]] == -1) { + prev[fds[i]] = i; + update(i, 1); + continue; + } + + update(prev[fds[i]], -1); + ans[fds[i]] = query(1, 0, size - 1, prev[fds[i]], i); + } + + fill(all(seg), 0); + fill(all(prev), -1); + + for (int i = n * 2 - 1; i >= 0; i--) { + if (prev[fds[i]] == -1) { + prev[fds[i]] = i; + update(i, 1); + continue; + } + + update(prev[fds[i]], -1); + ans[fds[i]] += query(1, 0, size - 1, i, prev[fds[i]]); + } + + cout << ans; +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/E. Addition to Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/E. Addition to Segment.cpp new file mode 100644 index 0000000..0695961 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 3/E. Addition to Segment.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/3/practice/contest/274545/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + + vl seg(n * 2); + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + seg[i] += v; + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + }; + + auto query = [&](int i) { + ll ans = 0; + + for (i += n; i > 0; i >>= 1) { + ans += seg[i]; + } + + return ans; + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int i; + cin >> i; + + cout << query(i) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/A. Sign alternation.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/A. Sign alternation.cpp new file mode 100644 index 0000000..f90e8a5 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/A. Sign alternation.cpp @@ -0,0 +1,162 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/4/practice/contest/274684/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(0); + } + + vl seg(n * 2); + + nrep(i, n, n * 2) { + if (i & 1) { + seg[i] = -fds[i - n]; + } else { + seg[i] = fds[i - n]; + } + } + + for (int i = n - 1; i > 0; i--) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + + auto update = [&](int i, ll v) { + i += n; + + if (i & 1) { + seg[i] = -v; + } else { + seg[i] = v; + } + + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + } + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0LL; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + int m; + cin >> m; + while (m--) { + int op; + cin >> op; + + if (op == 0) { + int i; + ll v; + cin >> i >> v; + update(i - 1, v); + continue; + } + + int l, r; + cin >> l >> r; + l--, r--; + + ll ans = query(1, 0, n - 1, l, r); + + if (l & 1) { + cout << -ans << '\n'; + } else { + cout << ans << '\n'; + } + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/B. Cryptography.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/B. Cryptography.cpp new file mode 100644 index 0000000..7d8711c --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/B. Cryptography.cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/4/practice/contest/274684/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +using mat = array, 2>; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int r, n, m; + cin >> r >> n >> m; + + V fds(n); + + rep(i, n) { + rep(j, 2) { + rep(k, 2) { + cin >> fds[i][j][k]; + } + } + } + + const mat identity = {{{1, 0}, {0, 1}}}; + + while (__builtin_popcount(n) != 1) { + n++; + fds.emplace_back(identity); + } + + auto mul = [&](mat &a, mat &b, mat &ans) { + ans[0][0] = (a[0][0] * b[0][0] + a[0][1] * b[1][0]) % r; + ans[1][0] = (a[1][0] * b[0][0] + a[1][1] * b[1][0]) % r; + ans[0][1] = (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % r; + ans[1][1] = (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % r; + }; + + V seg(n * 2); + + nrep(i, n, n * 2) { + seg[i] = fds[i - n]; + } + + for (int i = n - 1; i > 0; i--) { + mul(seg[i * 2], seg[i * 2 + 1], seg[i]); + } + + function query = [&](int i, int l, int r, int tl, int tr, mat &ans) { + if (l > tr || r < tl) { + ans = identity; + return; + } + + if (l >= tl && r <= tr) { + ans = seg[i]; + return; + } + + int mid = (l + r) >> 1; + + mat ans1; + mat ans2; + + query(i * 2, l, mid, tl, tr, ans1); + query(i * 2 + 1, mid + 1, r, tl, tr, ans2); + + mul(ans1, ans2, ans); + }; + + while (m--) { + int l, r; + cin >> l >> r; + + l--, r--; + + mat res; + query(1, 0, n - 1, l, r, res); + + rep(i, 2) { + rep(j, 2) { + cout << res[i][j] << ' '; + } + cout << '\n'; + } + + if (m > 0) { + cout << '\n'; + } + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/C. Number of Inversions on Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/C. Number of Inversions on Segment.cpp new file mode 100644 index 0000000..3f1c61e --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/C. Number of Inversions on Segment.cpp @@ -0,0 +1,171 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/4/practice/contest/274684/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(40); + } + + using vet = array; + + V> seg(n * 2); + + auto mergeseg = [&](vet &a, vet &b, vet &ans) { + ll res = 0; + ll total = 0; + + ans[39] = b[39] + a[39]; + + for (int i = 38; i >= 0; i--) { + ans[i] = b[i] + a[i]; + total += a[i + 1]; + + res += b[i] * total; + } + + return res; + }; + + nrep(i, n, n * 2) { + seg[i].first = 0; + seg[i].second.fill(0); + seg[i].second[fds[i - n] - 1] = 1; + } + + for (int i = n - 1; i > 0; i--) { + seg[i].first = mergeseg(seg[i * 2].second, seg[i * 2 + 1].second, seg[i].second) + seg[i * 2].first + seg[i * 2 + 1].first; + } + + auto update = [&](int i, ll v) { + i += n; + seg[i].second[fds[i - n] - 1] = 0; + seg[i].second[v - 1] = 1; + fds[i - n] = v; + + for (i >>= 1; i > 0; i >>= 1) { + seg[i].first = mergeseg(seg[i * 2].second, seg[i * 2 + 1].second, seg[i].second) + seg[i * 2].first + seg[i * 2 + 1].first; + } + }; + + const vet empty = {0}; + + function query = [&](int i, int l, int r, int tl, int tr, vet &ans) -> ll { + if (l > tr || r < tl) { + ans = empty; + return 0; + } + + if (l >= tl && r <= tr) { + ans = seg[i].second; + return seg[i].first; + } + + vet ans1; + vet ans2; + + int mid = (l + r) >> 1; + + ll total = query(i * 2, l, mid, tl, tr, ans1) + query(i * 2 + 1, mid + 1, r, tl, tr, ans2); + total += mergeseg(ans1, ans2, ans); + + return total; + }; + + while (q--) { + int op, x, y; + cin >> op >> x >> y; + + if (op == 1) { + vet _; + cout << query(1, 0, n - 1, x - 1, y - 1, _) << '\n'; + continue; + } + + update(x - 1, y); + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/D. Number of Different on Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/D. Number of Different on Segment.cpp new file mode 100644 index 0000000..fa6a4d4 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/D. Number of Different on Segment.cpp @@ -0,0 +1,166 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/4/practice/contest/274684/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, q; + cin >> n >> q; + + vl fds(n); + cin >> fds; + + while (__builtin_popcount(n) != 1) { + n++; + fds.push_back(40); + } + + using vet = array; + + V seg(n * 2); + + auto mergeseg = [&](vet &a, vet &b, vet &ans) { + for (int i = 39; i >= 0; i--) { + ans[i] = b[i] | a[i]; + } + }; + + nrep(i, n, n * 2) { + seg[i].fill(0); + seg[i][fds[i - n] - 1] = 1; + } + + for (int i = n - 1; i > 0; i--) { + mergeseg(seg[i * 2], seg[i * 2 + 1], seg[i]); + } + + auto update = [&](int i, ll v) { + i += n; + seg[i][fds[i - n] - 1] = 0; + seg[i][v - 1] = 1; + fds[i - n] = v; + + for (i >>= 1; i > 0; i >>= 1) { + mergeseg(seg[i * 2], seg[i * 2 + 1], seg[i]); + } + }; + + const vet empty = {0}; + + function query = [&](int i, int l, int r, int tl, int tr, vet &ans) { + if (l > tr || r < tl) { + ans = empty; + return; + } + + if (l >= tl && r <= tr) { + ans = seg[i]; + return; + } + + vet ans1; + vet ans2; + + int mid = (l + r) >> 1; + + query(i * 2, l, mid, tl, tr, ans1); + query(i * 2 + 1, mid + 1, r, tl, tr, ans2); + mergeseg(ans1, ans2, ans); + }; + + while (q--) { + int op, x, y; + cin >> op >> x >> y; + + if (op == 1) { + vet ans; + query(1, 0, n - 1, x - 1, y - 1, ans); + + int count = 0; + rep(i, 40) { + count += ans[i]; + } + + cout << count << '\n'; + continue; + } + + update(x - 1, y); + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/E. Earthquakes.cpp b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/E. Earthquakes.cpp new file mode 100644 index 0000000..c6eb49a --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 1 - Step 4/E. Earthquakes.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/4/4/practice/contest/274684/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + const ll oo = INT64_MAX >> 1; + + V seg(n * 2, oo); + + auto update = [&](int i, ll v) { + i += n; + seg[i] = v; + + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = min(seg[i * 2], seg[i * 2 + 1]); + } + }; + + function query = [&](int i, int l, int r, int tl, int tr, ll v) { + if (seg[i] > v || l > tr || r < tl) { + return 0; + } + + if (l == r) { + seg[i] = oo; + return 1; + } + + int mid = (l + r) >> 1; + + int ans = query(i * 2, l, mid, tl, tr, v) + query(i * 2 + 1, mid + 1, r, tl, tr, v); + seg[i] = min(seg[i * 2], seg[i * 2 + 1]); + + return ans; + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int i; + ll v; + cin >> i >> v; + update(i, v); + continue; + } + + int l, r; + ll v; + cin >> l >> r >> v; + + cout << query(1, 0, n - 1, l, r - 1, v) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/A. Addition to Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/A. Addition to Segment.cpp new file mode 100644 index 0000000..ff326cc --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/A. Addition to Segment.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/1/practice/contest/279634/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2); + + auto propagate = [&](int i, int l, int r) { + if (lazy[i] == 0) { + return; + } + + if (i >= n) { + seg[i] += lazy[i]; + lazy[i] = 0; + return; + } + + seg[i] += lazy[i] * (r - l + 1); + + lazy[i * 2] += lazy[i]; + lazy[i * 2 + 1] += lazy[i]; + lazy[i] = 0; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] += v; + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + }; + + function search = [&](int i, int l, int r, int t) { + propagate(i, l, r); + + if (l == r) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + if (mid + 1 > t) { + return search(i * 2, l, mid, t); + } + + return search(i * 2 + 1, mid + 1, r, t); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int i; + cin >> i; + + cout << search(1, 0, n - 1, i) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/B. Applying MAX to Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/B. Applying MAX to Segment.cpp new file mode 100644 index 0000000..e8f5062 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/B. Applying MAX to Segment.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/1/practice/contest/279634/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2); + + auto propagate = [&](int i, int l, int r) { + if (lazy[i] == 0) { + return; + } + + if (i >= n) { + rmax(seg[i], lazy[i]); + lazy[i] = 0; + return; + } + + rmax(seg[i], lazy[i]); + + rmax(lazy[i * 2], lazy[i]); + rmax(lazy[i * 2 + 1], lazy[i]); + lazy[i] = 0; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] += v; + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + }; + + function search = [&](int i, int l, int r, int t) { + propagate(i, l, r); + + if (l == r) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + if (mid + 1 > t) { + return search(i * 2, l, mid, t); + } + + return search(i * 2 + 1, mid + 1, r, t); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int i; + cin >> i; + + cout << search(1, 0, n - 1, i) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/C. Assignment to Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/C. Assignment to Segment.cpp new file mode 100644 index 0000000..6cd6250 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 1/C. Assignment to Segment.cpp @@ -0,0 +1,166 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/1/practice/contest/279634/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2); + V upd(n * 2); + + auto propagate = [&](int i) { + if (!upd[i]) { + return; + } + + if (i >= n) { + seg[i] = lazy[i]; + lazy[i] = 0; + upd[i] = false; + return; + } + + lazy[i * 2] = lazy[i]; + lazy[i * 2 + 1] = lazy[i]; + upd[i * 2] = true; + upd[i * 2 + 1] = true; + lazy[i] = 0; + upd[i] = false; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + upd[i] = true; + lazy[i] = v; + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + }; + + function search = [&](int i, int l, int r, int t) { + propagate(i); + + if (l == r) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + if (t <= mid) { + return search(i * 2, l, mid, t); + } + + return search(i * 2 + 1, mid + 1, r, t); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int i; + cin >> i; + + cout << search(1, 0, n - 1, i) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/A. Addition and Minimum.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/A. Addition and Minimum.cpp new file mode 100644 index 0000000..e9f71d0 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/A. Addition and Minimum.cpp @@ -0,0 +1,167 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/2/practice/contest/279653/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2); + + auto propagate = [&](int i) { + if (lazy[i] == 0) { + return; + } + + if (i >= n) { + seg[i] += lazy[i]; + lazy[i] = 0; + return; + } + + seg[i] += lazy[i]; + + lazy[i * 2] += lazy[i]; + lazy[i * 2 + 1] += lazy[i]; + lazy[i] = 0; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] += v; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = min(seg[i * 2], seg[i * 2 + 1]); + }; + + const ll oo = INT64_MAX >> 1; + + function query = [&](int i, int l, int r, int tl, int tr) { + propagate(i); + + if (l > tr || r < tl) { + return oo; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return min(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int l, r; + cin >> l >> r; + + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/B. Multiplication and Sum.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/B. Multiplication and Sum.cpp new file mode 100644 index 0000000..2e0ddfa --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/B. Multiplication and Sum.cpp @@ -0,0 +1,167 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/2/practice/contest/279653/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2, 1); + vl lazy(n * 2, 1); + ll mod = 1e9 + 7; + + for (int i = n - 1; i > 0; i--) { + seg[i] = (seg[i * 2] + seg[i * 2 + 1]) % mod; + } + + auto propagate = [&](int i) { + if (lazy[i] == 1) { + return; + } + + if (i >= n) { + seg[i] = (seg[i] * lazy[i]) % mod; + lazy[i] = 1; + return; + } + + lazy[i * 2] = (lazy[i * 2] * lazy[i]) % mod; + lazy[i * 2 + 1] = (lazy[i * 2 + 1] * lazy[i]) % mod; + seg[i] = (seg[i] * lazy[i]) % mod; + lazy[i] = 1; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] = (lazy[i] * v) % mod; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = (seg[i * 2] + seg[i * 2 + 1]) % mod; + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0LL; + } + + propagate(i); + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return (query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr)) % mod; + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int l, r; + cin >> l >> r; + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/C. Bitwise OR and AND.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/C. Bitwise OR and AND.cpp new file mode 100644 index 0000000..349e94f --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/C. Bitwise OR and AND.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/2/practice/contest/279653/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2); + ll mod = 1e9 + 7; + + auto propagate = [&](int i) { + if (lazy[i] == 0) { + return; + } + + if (i >= n) { + seg[i] |= lazy[i]; + lazy[i] = 0; + return; + } + + lazy[i * 2] |= lazy[i]; + lazy[i * 2 + 1] |= lazy[i]; + seg[i] |= lazy[i]; + lazy[i] = 0; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] |= v; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = seg[i * 2] & seg[i * 2 + 1]; + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return -1LL; + } + + propagate(i); + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) & query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int l, r; + cin >> l >> r; + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/D. Addition and Sum.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/D. Addition and Sum.cpp new file mode 100644 index 0000000..09187b2 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/D. Addition and Sum.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/2/practice/contest/279653/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2); + ll mod = 1e9 + 7; + + auto propagate = [&](int i, int l, int r) { + if (lazy[i] == 0) { + return; + } + + if (i >= n) { + seg[i] += lazy[i] * (r - l + 1); + lazy[i] = 0; + return; + } + + lazy[i * 2] += lazy[i]; + lazy[i * 2 + 1] += lazy[i]; + seg[i] += lazy[i] * (r - l + 1); + lazy[i] = 0; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i, l, r); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] += v; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0LL; + } + + propagate(i, l, r); + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int l, r; + cin >> l >> r; + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/E. Assignment and Minimum.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/E. Assignment and Minimum.cpp new file mode 100644 index 0000000..f5450ac --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/E. Assignment and Minimum.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/2/practice/contest/279653/problem/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2, -1); + + auto propagate = [&](int i) { + if (lazy[i] == -1) { + return; + } + + if (i >= n) { + seg[i] = lazy[i]; + lazy[i] = -1; + return; + } + + lazy[i * 2] = lazy[i]; + lazy[i * 2 + 1] = lazy[i]; + seg[i] = lazy[i]; + lazy[i] = -1; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] = v; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = min(seg[i * 2], seg[i * 2 + 1]); + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + const ll oo = INT64_MAX >> 1; + if (l > tr || r < tl) { + return oo; + } + + propagate(i); + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return min(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int l, r; + cin >> l >> r; + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/F. Assignment and Sum.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/F. Assignment and Sum.cpp new file mode 100644 index 0000000..f183461 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 2/F. Assignment and Sum.cpp @@ -0,0 +1,162 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/2/practice/contest/279653/problem/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2, -1); + + auto propagate = [&](int i, int l, int r) { + if (lazy[i] == -1) { + return; + } + + if (i >= n) { + seg[i] = lazy[i] * (r - l + 1); + lazy[i] = -1; + return; + } + + lazy[i * 2] = lazy[i]; + lazy[i * 2 + 1] = lazy[i]; + seg[i] = lazy[i] * (r - l + 1); + lazy[i] = -1; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i, l, r); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] = v; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0LL; + } + + propagate(i, l, r); + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + int l, r; + cin >> l >> r; + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/A. Assignment and Maximal Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/A. Assignment and Maximal Segment.cpp new file mode 100644 index 0000000..2a1b927 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/A. Assignment and Maximal Segment.cpp @@ -0,0 +1,190 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/3/practice/contest/280799/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + int prev = n; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + struct node { + ll pref; + ll suf; + ll total; + ll ans; + }; + + V seg(n * 2); + vl lazy(n * 2); + V upd(n * 2); + + auto propagate = [&](int i, int l, int r) { + if (!upd[i]) { + return; + } + + upd[i] = false; + + if (i >= n) { + seg[i].total = lazy[i]; + seg[i].suf = max(lazy[i], 0LL); + seg[i].pref = max(lazy[i], 0LL); + seg[i].ans = max(lazy[i], 0LL); + return; + } + + lazy[i * 2] = lazy[i]; + lazy[i * 2 + 1] = lazy[i]; + upd[i * 2] = true; + upd[i * 2 + 1] = true; + + ll ans = lazy[i] * (r - l + 1); + + seg[i].total = ans; + seg[i].suf = max(ans, 0LL); + seg[i].pref = max(ans, 0LL); + seg[i].ans = max(ans, 0LL); + }; + + auto merge = [&](node &a, node &b) { + node ans; + + ans.total = a.total + b.total; + ans.pref = max(a.pref, a.total + b.pref); + ans.suf = max(b.suf, b.total + a.suf); + ans.ans = max({a.pref, b.suf, a.suf + b.pref, a.ans, b.ans}); + + return ans; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] = v; + upd[i] = true; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = merge(seg[i * 2], seg[i * 2 + 1]); + }; + + function query = [&](int i, int l, int r, int tl, int tr) -> node { + if (l > tr || r < tl) { + return {.pref=0, .suf=0, .total=0, .ans=0}; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + node left = query(i * 2, l, mid, tl, tr); + node right = query(i * 2 + 1, mid + 1, r, tl, tr); + + return merge(left, right); + }; + + while (m--) { + int l, r; + ll v; + cin >> l >> r >> v; + + update(1, 0, n - 1, l, r - 1, v); + + cout << query(1, 0, n - 1, 0, prev - 1).ans << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/B. Inverse and K-th one.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/B. Inverse and K-th one.cpp new file mode 100644 index 0000000..422d5f7 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/B. Inverse and K-th one.cpp @@ -0,0 +1,165 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/3/practice/contest/280799/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vi seg(n * 2); + vi lazy(n * 2); + + auto propagate = [&](int i, int l, int r) { + if (lazy[i] == 0) { + return; + } + + lazy[i] = 0; + + if (i >= n) { + seg[i] ^= 1; + return; + } + + lazy[i * 2] ^= 1; + lazy[i * 2 + 1] ^= 1; + + seg[i] = (r - l + 1) - seg[i]; + }; + + function update = [&](int i, int l, int r, int tl, int tr) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] = 1; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr); + update(i * 2 + 1, mid + 1, r, tl, tr); + + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + }; + + function search = [&](int i, int l, int r, int k) { + propagate(i, l, r); + if (i >= n) { + return i - n; + } + + int mid = (l + r) >> 1; + + propagate(i * 2, l, mid); + if (seg[i * 2] < k) { + return search(i * 2 + 1, mid + 1, r, k - seg[i * 2]); + } + + return search(i * 2, l, mid, k); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + cin >> l >> r; + + update(1, 0, n - 1, l, r - 1); + continue; + } + + int k; + cin >> k; + + cout << search(1, 0, n - 1, k + 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/C. Addition and First element at least X.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/C. Addition and First element at least X.cpp new file mode 100644 index 0000000..0247154 --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 3/C. Addition and First element at least X.cpp @@ -0,0 +1,176 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/3/practice/contest/280799/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazy(n * 2); + + V upd(n * 2); + + auto propagate = [&](int i) { + if (!upd[i]) { + return; + } + + upd[i] = false; + + if (i >= n) { + seg[i] += lazy[i]; + lazy[i] = 0; + return; + } + + lazy[i * 2] += lazy[i]; + lazy[i * 2 + 1] += lazy[i]; + upd[i * 2] = true; + upd[i * 2 + 1] = true; + seg[i] += lazy[i]; + lazy[i] = 0; + }; + + function update = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i); + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazy[i] += v; + upd[i] = true; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + }; + + function query = [&](int i, int l, int r, int t, ll v) { + propagate(i); + + if (r < t || seg[i] < v) { + return -1; + } + + if (i >= n) { + return i - n; + } + + int mid = (l + r) >> 1; + + int res = query(i * 2, l, mid, t, v); + + if (res != -1) { + return res; + } + return query(i * 2 + 1, mid + 1, r, t, v); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + update(1, 0, n - 1, l, r - 1, v); + continue; + } + + ll v; + int l; + cin >> v >> l; + + cout << query(1, 0, n - 1, l, v) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/A. Assignment, Addition, and Sum.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/A. Assignment, Addition, and Sum.cpp new file mode 100644 index 0000000..28f1a7b --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/A. Assignment, Addition, and Sum.cpp @@ -0,0 +1,234 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/4/practice/contest/280801/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + if (n > 1) { + n = 1 << (32 - __builtin_clz(n - 1)); + } + + vl seg(n * 2); + vl lazyadd(n * 2); + vl lazyset(n * 2); + V updadd(n * 2); + V updset(n * 2); + + auto propagateset = [&](int i, int l, int r) { + if (!updset[i]) { + return; + } + + updset[i] = false; + + if (i >= n) { + seg[i] = lazyset[i]; + return; + } + + seg[i] = lazyset[i] * (r - l + 1); + lazyset[i * 2] = lazyset[i]; + lazyset[i * 2 + 1] = lazyset[i]; + updset[i * 2] = true; + updset[i * 2 + 1] = true; + + lazyadd[i * 2] = 0; + lazyadd[i * 2 + 1] = 0; + updadd[i * 2] = false; + updadd[i * 2 + 1] = false; + }; + + auto propagateadd = [&](int i, int l, int r) { + if (!updadd[i]) { + return; + } + + updadd[i] = false; + + if (i >= n) { + seg[i] += lazyadd[i]; + lazyadd[i] = 0; + return; + } + + int mid = (l + r) >> 1; + + seg[i] += lazyadd[i] * (r - l + 1); + lazyadd[i * 2] += lazyadd[i]; + lazyadd[i * 2 + 1] += lazyadd[i]; + updadd[i * 2] = true; + updadd[i * 2 + 1] = true; + lazyadd[i] = 0; + }; + + auto propagate = [&](int i, int l, int r) { + propagateset(i, l, r); + propagateadd(i, l, r); + }; + + function updateset = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazyset[i] = v; + updset[i] = true; + updadd[i] = false; + lazyadd[i] = 0; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + updateset(i * 2, l, mid, tl, tr, v); + updateset(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + }; + + function updateadd = [&](int i, int l, int r, int tl, int tr, ll v) { + propagate(i, l, r); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + lazyadd[i] += v; + updadd[i] = true; + propagate(i, l, r); + return; + } + + int mid = (l + r) >> 1; + + updateadd(i * 2, l, mid, tl, tr, v); + updateadd(i * 2 + 1, mid + 1, r, tl, tr, v); + + seg[i] = seg[i * 2] + seg[i * 2 + 1]; + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + if (l > tr || r < tl) { + return 0LL; + } + + propagate(i, l, r); + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr); + }; + + while (m--) { + int op; + cin >> op; + + if (op == 1) { + int l, r; + ll v; + cin >> l >> r >> v; + updateset(1, 0, n - 1, l, r - 1, v); + continue; + } + + if (op == 2) { + int l, r; + ll v; + cin >> l >> r >> v; + updateadd(1, 0, n - 1, l, r - 1, v); + continue; + } + + int l, r; + cin >> l >> r; + cout << query(1, 0, n - 1, l, r - 1) << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/B. Add Arithmetic Progression On Segment.cpp b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/B. Add Arithmetic Progression On Segment.cpp new file mode 100644 index 0000000..e2c2dab --- /dev/null +++ b/ITMO Academy: pilot course - Segment Tree, part 2 - Step 4/B. Add Arithmetic Progression On Segment.cpp @@ -0,0 +1,84 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/5/4/practice/contest/280801/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + +} diff --git a/J. Journey To Stringland/J. Journey To Stringland.cpp b/J. Journey To Stringland/J. Journey To Stringland.cpp new file mode 100644 index 0000000..8462635 --- /dev/null +++ b/J. Journey To Stringland/J. Journey To Stringland.cpp @@ -0,0 +1,113 @@ +/* Problem URL: https://codeforces.com/problemset/gymProblem/105164/J */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + string fds; + cin >> fds; + + int inf = INT32_MAX >> 1; + vvvi dp(n, vvi(k + 1, vi(n, inf))); + + dp[0][0][0] = 0; + dp[0][1][0] = 0; + + nrep(i, 1, n) { + fill(all(dp[i][0]), 0); + fill(all(dp[i][1]), 0); + if (k >= 2) { + for (int j = i - 1; j >= 0; j--) { + dp[i][2][j] = fds[i] != fds[j]; + } + } + + for (int j = i - 2; j >= 0; j--) { + for (int l = 2; l <= k; l++) { + dp[i][l][j] = dp[i - 1][l][j]; + rmin(dp[i][l][j], dp[i - 1][l - 2][j + 1] + (fds[i] != fds[j])); + rmin(dp[i][l][j], dp[i][l][j + 1]); + } + } + } + + // for (auto &i : dp) { + // cout << i; + // cout << "------------------------------\n"; + // } + + cout << *min_element(all(dp[n - 1][k])) << '\n'; +} diff --git a/Manthan, Codefest 16/C. Spy Syndrome 2.cpp b/Manthan, Codefest 16/C. Spy Syndrome 2.cpp new file mode 100644 index 0000000..5eb4520 --- /dev/null +++ b/Manthan, Codefest 16/C. Spy Syndrome 2.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/problemset/problem/633/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +struct hashed_string { + static vl p; + static ll m; + static ll b; + vl hash; + + hashed_string(string &a) { + while (p.size() <= a.size()) { + p.push_back(((__int128)p.back() * b) % m); + } + + hash.resize(a.size() + 1); + + rep(i, a.size()) { + hash[i + 1] = ((__int128)hash[i] * b + tolower(a[i])) % m; + } + } + + ll gethash(int l, int r) { + return ((hash[r + 1] - (__int128)hash[l] * p[r - l + 1]) % m + m) % m; + } +}; + +vl hashed_string::p = {1}; +ll hashed_string::m = (1LL << 61) - 1; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll hashed_string::b = uniform_int_distribution(1, m - 1)(rng); + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + string a; + cin >> a; + + int m; + cin >> m; + unordered_map hashes; + V msg(m); + int v = 0; + for (auto &i : msg) { + cin >> i; + hashed_string tmp(i); + hashes[tmp.hash.back()] = v; + v++; + } + + reverse(all(a)); + hashed_string rev(a); + + vi dp(n + 1, -1); + vi aux(n + 1, -1); + dp[0] = 0; + + for (size_t i = 1; i <= n; i++) { + if (dp[i - 1] == -1) { + continue; + } + + size_t lim = min(n - i + 1, (size_t)1000); + rep(j, lim) { + ll hash = rev.gethash(n - i - j, n - i); + if (hashes.count(hash)) { + aux[i + j] = hashes[hash]; + dp[i + j] = i; + } + } + } + + stack s; + size_t i = dp.size() - 1; + while (i > 0) { + s.push(aux[i]); + i = dp[i] - 1; + } + + while (!s.empty()) { + cout << msg[s.top()] << ' '; + s.pop(); + } + cout << '\n'; +} diff --git a/OBI IV - 2024/J. A Viagem de Wevton.cpp b/OBI IV - 2024/J. A Viagem de Wevton.cpp new file mode 100644 index 0000000..d00d0a9 --- /dev/null +++ b/OBI IV - 2024/J. A Viagem de Wevton.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/group/SmO1tpanN1/contest/523051/problem/J */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, k; + cin >> n >> k; + + vvl dis(n, vl(n)); + cin >> dis; + + V frame(n); + ll ans = 0; + + function dfs = [&](int i, int pos, ll d) { + if (pos == n - 1) { + if (d + dis[i][0] == k) { + ans++; + } + return; + } + + frame[i] = true; + + rep(j, n) { + if (!frame[j]) { + dfs(j, pos + 1, d + dis[i][j]); + } + } + + frame[i] = false; + }; + + dfs(0, 0, 0); + + cout << ans << '\n'; +} diff --git a/OBI V - 2024/F. Sanduíche.cpp b/OBI V - 2024/F. Sanduíche.cpp new file mode 100644 index 0000000..cf272e2 --- /dev/null +++ b/OBI V - 2024/F. Sanduíche.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/group/SmO1tpanN1/contest/523096/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll d; + cin >> n >> d; + + vl fds(n); + cin >> fds; + + vl pref(n + 1); + rep(i, n) { + pref[i + 1] = pref[i] + fds[i]; + } + + int i = 0; + int j = 0; + int ans = 0; + while (i < n) { + while (pref[i + 1] - pref[j] > d) { + j++; + } + + if (pref[i + 1] - pref[j] == d) { + ans++; + } + + i++; + } + + if (pref.back() == d) { + cout << ans + (n - 1) << '\n'; + return 0; + } + + rep(i, n) { + int low = i + 2; + int high = n; + + while (low <= high) { + int mid = (low + high) / 2; + + if (pref.back() - (pref[mid - 1] - pref[i + 1]) == d) { + ans++; + break; + } + + if (pref.back() - (pref[mid - 1] - pref[i + 1]) > d) { + low = mid + 1; + continue; + } + + high = mid - 1; + } + } + + cout << ans << '\n'; +} diff --git a/OBI VI - 2024/H. Retângulo.cpp b/OBI VI - 2024/H. Retângulo.cpp new file mode 100644 index 0000000..0d02dbf --- /dev/null +++ b/OBI VI - 2024/H. Retângulo.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/group/SmO1tpanN1/contest/525671/problem/H */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n; + cin >> n; + + vl fds(n); + repv(i, fds) { + cin >> i; + } + + vl points = {0}; + ll count = 0; + rep(i, n - 1) { + points.push_back(points.back() + fds[i]); + count += fds[i]; + } + + n = count + fds.back(); + if (n % 2 == 1) { + cout << "N\n"; + return 0; + } + + vl diameter(points.size(), -1); + + rep(i, points.size()) { + int low = i; + int high = points.size() - 1; + + while (low <= high) { + int mid = (low + high) / 2; + + if (points[mid] - points[i] == n / 2) { + diameter[i] = mid; + diameter[mid] = i; + break; + } + + if (points[mid] - points[i] > n / 2) { + high = mid - 1; + continue; + } + + low = mid + 1; + } + } + + V vis(points.size(), false); + int c = 0; + + rep(i, points.size()) { + if (vis[i]) { + continue; + } + + if (diameter[i] != -1) { + vis[i] = true; + vis[diameter[i]] = true; + c++; + } + } + + cout << (c >= 2 ? "S\n" : "N\n"); +} diff --git a/Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)/D. Frequency of String.cpp b/Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)/D. Frequency of String.cpp new file mode 100644 index 0000000..e7ed62c --- /dev/null +++ b/Tinkoff Internship Warmup Round 2018 and Codeforces Round 475 (Div. 1)/D. Frequency of String.cpp @@ -0,0 +1,172 @@ +/* Problem URL: https://codeforces.com/problemset/problem/963/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +struct hashed_string { + vl hash; + static vl p; + static ll b; + static ll m; + + hashed_string(string &a) { + hash.resize(a.size() + 1); + + while (p.size() <= a.size()) { + p.push_back(((__int128)p.back() * b) % m); + } + + nrep(i, 1, a.size() + 1) { + hash[i] = ((__int128)hash[i - 1] * b + a[i - 1]) % m; + } + } + + ll get_hash(int l, int r) { + return ((hash[r + 1] - (__int128)hash[l] * p[r - l + 1]) % m + m) % m; + } +}; + +vl hashed_string::p = {1}; + +long long hashed_string::m = (1LL << 61) - 1; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +long long hashed_string::b = uniform_int_distribution(1, m - 1)(rng); + +int inf = INT32_MAX >> 1; + +ll get_single_hash(string &a) +{ + ll hash = 0; + + for (auto i : a) { + hash = ((__int128)hash * hashed_string::b + i) % hashed_string::m; + } + + return hash; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string a; + cin >> a; + + hashed_string hash(a); + V sizes(a.size(), false); + unordered_map memol; + unordered_map memor; + + int n; + cin >> n; + while (n--) { + int k; + string c; + cin >> k >> c; + + if (c.size() > a.size()) { + cout << "-1\n"; + continue; + } + + ll tmp = get_single_hash(c); + + size_t size = c.size(); + + if (!sizes[size - 1]) { + nrep(i, c.size() - 1, a.size()) { + ll hashed = hash.get_hash(i - size + 1, i); + memol[hashed].push_back(i - size + 1); + memor[hashed].push_back(i); + } + sizes[size - 1] = true; + } + + if (memol[tmp].size() < k) { + cout << "-1\n"; + continue; + } + + if (k == 1) { + cout << size << '\n'; + continue; + } + + int ans = inf; + auto &left = memol[tmp]; + auto &right = memor[tmp]; + + nrep(i, k - 1, left.size()) { + rmin(ans, right[i] - left[i - k + 1] + 1); + } + + cout << ans << '\n'; + } +} diff --git a/Trie - Hash/C - Watto and Mechanism.cpp b/Trie - Hash/C - Watto and Mechanism.cpp new file mode 100644 index 0000000..565e082 --- /dev/null +++ b/Trie - Hash/C - Watto and Mechanism.cpp @@ -0,0 +1,78 @@ +/* Problem URL: https://vjudge.net/contest/690768#problem/C */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + +} diff --git a/VI UnBalloon Contest Mirror/B. Boundless Deck.cpp b/VI UnBalloon Contest Mirror/B. Boundless Deck.cpp new file mode 100644 index 0000000..7210df2 --- /dev/null +++ b/VI UnBalloon Contest Mirror/B. Boundless Deck.cpp @@ -0,0 +1,99 @@ +/* Problem URL: https://codeforces.com/gym/105900/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll mod = 1e9 + 7; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, k; + cin >> n >> k; + + ll ans = 0; + if (k & 1) { + ans = 1; + } + + ll power = n; + nrep(i, 1, 31) { + if ((k >> i) & 1){ + ans += power; + ans %= mod; + } + + power *= n; + power %= mod; + } + + cout << ans << '\n'; +} diff --git a/VI UnBalloon Contest Mirror/D. Delirium at Unballoon.cpp b/VI UnBalloon Contest Mirror/D. Delirium at Unballoon.cpp new file mode 100644 index 0000000..7042521 --- /dev/null +++ b/VI UnBalloon Contest Mirror/D. Delirium at Unballoon.cpp @@ -0,0 +1,91 @@ +/* Problem URL: https://codeforces.com/gym/105900/problem/D */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const long double pi = numbers::pi; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + long double rl, hl, rc, hc; + cin >> rl >> hl >> rc >> hc; + + long double vol1 = pi * rl * rl * hl; + long double vol2 = pi * hc * rc * rc / 3; + long double vol3 = pi * rc * rc * rc * 2 / 3; + + if (vol1 > (vol2 + vol3)) { + cout << "Injusto\n"; + } else { + cout << "Justo\n"; + } +} diff --git a/VI UnBalloon Contest Mirror/E. Elementary Magical School of Words.cpp b/VI UnBalloon Contest Mirror/E. Elementary Magical School of Words.cpp new file mode 100644 index 0000000..5679d9f --- /dev/null +++ b/VI UnBalloon Contest Mirror/E. Elementary Magical School of Words.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/gym/105900/problem/E */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +ll mod = 1e9 + 7; +int s = 27; + +vvl operator*(vvl &a, vvl &b) +{ + vvl ans(s, vl(s)); + + rep(i, s) { + rep(j, s) { + rep(k, s) { + ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % mod; + } + } + } + + return ans; +} + +vvl exponential(ll n) +{ + vvl ans(s, vl(s)); + rep(i, s) { + ans[i][i] = 1; + } + + vvl expc(s, vl(s)); + // expc[0][0] = 1; + nrep(i, 1, s) { + expc[i][i] = i; + expc[i][i - 1] = 1; + } + + rep(i, 60) { + if ((n >> i) & 1) { + ans = ans * expc; + } + + expc = expc * expc; + } + + return ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n; + cin >> n; + + // vl trans(26); + // trans[0] = 1; + // nrep(i, 1, n) { + // for (int j = 25; j > 0; j--) { + // trans[j] = trans[j] * (j + 1) + trans[j - 1]; + // trans[j] %= mod; + // } + // } + // + // ll total = 0; + // repv(i, trans) { + // total += i; + // total %= mod; + // } + // + vvl ans = exponential(n); + ll total = 0; + + rep(i, s) { + total += ans[i][0]; + total %= mod; + } + + cout << total << '\n'; +} diff --git a/VI UnBalloon Contest Mirror/F. Fibonacci.cpp b/VI UnBalloon Contest Mirror/F. Fibonacci.cpp new file mode 100644 index 0000000..8bfa16b --- /dev/null +++ b/VI UnBalloon Contest Mirror/F. Fibonacci.cpp @@ -0,0 +1,195 @@ +/* Problem URL: https://codeforces.com/gym/105900/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +template tuple ext_gcd(T a, T b) { + if (!a) return {b, 0, 1}; + auto [g, x, y] = ext_gcd(b%a, a); + return {g, y - b/a*x, x}; +} + +template struct crt { + T a, m; + + crt() : a(0), m(1) {} + crt(T a_, T m_) : a(a_), m(m_) {} + crt operator * (crt C) { + auto [g, x, y] = ext_gcd(m, C.m); + if ((a - C.a) % g) a = -1; + if (a == -1 or C.a == -1) return crt(-1, 0); + T lcm = m/g*C.m; + T ans = a + (x*(C.a-a)/g % (C.m/g))*m; + return crt((ans % lcm + lcm) % lcm, lcm); + } +}; + +ll tot(int n) { + ll ret = n; + + for (int i = 2; i*i <= n; i++) if (n % i == 0) { + while (n % i == 0) n /= i; + ret -= ret / i; + } + if (n > 1) ret -= ret / n; + + return ret; +} + +ll fastpow(ll a, ll b, ll tot) +{ + ll ans = 1; + ll now = a; + + for (int i = 0; i < 31; i++) { + if ((b >> i) & 1) { + ans *= now; + ans %= tot; + } + + now *= now; + now %= tot; + } + + return ans; +} + +vvl mul(vvl &a, vvl &b, ll mod) +{ + vvl ans(a.size(), vl(b[0].size())); + + rep(i, a.size()) { + rep(j, b[0].size()) { + rep(k, a[0].size()) { + ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % mod; + } + } + } + + return ans; +} + +ll binaryexpo(ll pow, ll mod) +{ + vvl ans = { + {1, 0}, + {0, 0} + }; + + if (pow <= 1) { + return pow; + } + pow--; + + vvl now = { + {1, 1}, + {1, 0} + }; + + rep(i, 62) { + if ((pow >> i) & 1) { + ans = mul(ans, now, mod); + } + now = mul(now, now, mod); + } + + return ans[0][0]; +} + +ll factor(ll n) +{ + ll sqr = sqrt(n); + for (ll i = 2; i <= sqr; i++) { + if (n % i == 0) { + return i; + } + } + + return n; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll a, b, m; + cin >> a >> b >> m; + + ll mtot = tot(m); + if (__gcd(a, m) != 1) { + ll tmp = factor(mtot); + crt one(fastpow(a, b, tmp), tmp); + crt two(fastpow(a, b, mtot / tmp), mtot / tmp); + + mtot = (one * two).m; + } + + cout << binaryexpo(fastpow(a, b, mtot), m) << '\n'; +} diff --git a/VI UnBalloon Contest Mirror/G. Graph of Love.cpp b/VI UnBalloon Contest Mirror/G. Graph of Love.cpp new file mode 100644 index 0000000..bf425fe --- /dev/null +++ b/VI UnBalloon Contest Mirror/G. Graph of Love.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/gym/105900/problem/G */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vi fds(n); + repv(i, fds) { + cin >> i; + i--; + } + + vvi dp(n, vi(2, -1)); + + int ans = 0; + + V frame(n); + V vis(n); + vi dis(n, 0); + V loop(n); + function(int)> dfs = [&](int i) { + vis[i] = true; + if (frame[fds[i]]) { + int size = dis[i] - dis[fds[i]]; + dp[i][0] = size / 2; + dp[i][1] = size / 2; + loop[i] = true; + return make_pair(fds[i], size); + } + + frame[i] = true; + dis[fds[i]] = dis[i] + 1; + auto [cat, size] = dfs(fds[i]); + + frame[i] = false; + if (cat != -1) { + loop[i] = true; + dp[i][0] = ans / 2; + dp[i][1] = ans / 2; + if (cat == i) { + return make_pair(-1, -1); + } + return make_pair(cat, size); + } + + return make_pair(-1, -1); + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + dfs(i); + } + + function calc = [&](int i) { + if (dp[i][0] != -1) { + return; + } + + dp[i][0] = dp[fds[i]][1]; + dp[i][1] = dp[fds[i]][0] + 1; + + + }; + + rep(i, n) { + + } + + cout << dp; + + cout << ans << '\n'; +} diff --git a/VIII Maratona Mineira de Programação/J. Wasserman, Rei das Águas.cpp b/VIII Maratona Mineira de Programação/J. Wasserman, Rei das Águas.cpp new file mode 100644 index 0000000..93cdaff --- /dev/null +++ b/VIII Maratona Mineira de Programação/J. Wasserman, Rei das Águas.cpp @@ -0,0 +1,197 @@ +/* Problem URL: https://codeforces.com/group/YgJmumGtHD/contest/102223/problem/J */ + +#pragma GCC optimize(3) +#pragma GCC optimize("Ofast,unroll-loops") + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (size_t i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +struct hashed_string { + static vl p; + static ll m; + static ll b; + vl hash; + + hashed_string(string &a) { + while (p.size() <= a.size()) { + p.push_back(((__int128)p.back() * b) % m); + } + + hash.resize(a.size() + 1); + hash[0] = 0; + + rep(i, a.size()) { + hash[i + 1] = ((__int128)hash[i] * b + a[i]) % m; + } + }; + + ll gethash(int l, int r) { + return ((hash[r + 1] - (__int128)hash[l] * p[r - l + 1]) % m + m) % m; + }; +}; + +vl hashed_string::p = {1}; +ll hashed_string::m = (1LL << 61) - 1; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll hashed_string::b = uniform_int_distribution(1, m - 1)(rng); + +#define MAX_N 5 * (ll)1e4 + 10 + +// tuple msg[MAX_N]; +ll hashm[MAX_N]; +string trans[MAX_N]; +size_t sizes[MAX_N]; +int itr[MAX_N]; +map hashtmp; + +size_t hashsize; +ll hashes[MAX_N * 10]; +ll pos[MAX_N * 10]; + +pair dp[(ll)1e6]; +int aux[(ll)1e6]; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + rep(j, n) { + string tmp; + cin >> tmp; + + hashed_string act(tmp); + hashm[j] = act.hash.back(); + cin.ignore(); + + getline(cin, trans[j]); + sizes[j] = tmp.size(); + itr[j] = j; + hashtmp[hashm[j]] = j; + } + + hashsize = 0; + repv(i, hashtmp) { + hashes[hashsize] = i.first; + pos[hashsize] = i.second; + hashsize++; + } + + string tmp; + cin >> tmp; + hashed_string a(tmp); + + rep(i, tmp.size()) { + dp[i] = {-2, INT64_MAX >> 1}; + } + + rep(j, n) { + if (a.gethash(0, sizes[j] - 1) == hashm[j]) { + dp[sizes[j] - 1].second = 1; + dp[sizes[j] - 1].first = -1; + aux[sizes[j] - 1] = j; + } + } + + rep(i, tmp.size()) { + if (dp[i].first == -2) { + continue; + } + nrep(s, 1, 51) { + if (i + s < tmp.size()) { + ll tmp = a.gethash(i + 1, i + s); + // auto itr = hashes.find(tmp); + ll *itr = lower_bound(hashes, hashes + hashsize, tmp); + if (itr != hashes + hashsize && *itr == tmp && dp[i + s].second > dp[i].second + 1) { + dp[i + s].second = dp[i].second + 1; + dp[i + s].first = i; + aux[i + s] = pos[itr - hashes]; + } + } + } + } + + stack s; + + int i = tmp.size() - 1; + while (i >= 0) { + s.push(aux[i]); + i = dp[i].first; + } + + while (!s.empty()) { + string &b = trans[s.top()]; + cout << b << ' '; + s.pop(); + } + cout << '\n'; +} diff --git a/XII Maratona Mineira de Programação/B. Bares.cpp b/XII Maratona Mineira de Programação/B. Bares.cpp new file mode 100644 index 0000000..b542e39 --- /dev/null +++ b/XII Maratona Mineira de Programação/B. Bares.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/group/YgJmumGtHD/contest/105936/problem/B */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + struct bar { + int x; + int t; + + bool operator<(bar &b) { + if (t == b.t) { + return x > b.x; + } + return t > b.t; + } + }; + + V fds(n); + + repv(i, fds) { + cin >> i.x >> i.t; + } + + sortv(fds); + + map var; + + rep(i, n) { + var[fds[i].t].push_back(i); + } + + vi dp(n, -1); + vi maximal(n, 0); + + const int inf = INT32_MAX >> 1; + + function dfs = [&](int i) { + if (dp[i] != -1) { + return dp[i]; + } + + maximal[i] = fds[i].t; + + int t = fds[i].t; + int x = fds[i].x; + + dp[i] = 1; + + auto end = var.find(t); + + auto itr = prev(var.end()); + + int limit = inf; + + while (itr != end && itr->first < limit) { + repv(j, itr->second) { + if (abs(fds[j].x - x) + fds[i].t > fds[j].t) { + continue; + } + rmax(maximal[i], maximal[j]); + rmax(dp[i], dfs(j) + 1); + } + rmin(limit, maximal[i]); + + if (itr->first <= limit) { + itr--; + continue; + } + + itr = var.lower_bound(limit); + if (itr != var.begin()) { + itr--; + } + } + + return dp[i]; + }; + + int ans = 0; + + rep(i, n) { + rmax(ans, dfs(i)); + } + + cout << ans << '\n'; +} diff --git a/XII Maratona Mineira de Programação/F. Fome de Queijo.cpp b/XII Maratona Mineira de Programação/F. Fome de Queijo.cpp new file mode 100644 index 0000000..9610427 --- /dev/null +++ b/XII Maratona Mineira de Programação/F. Fome de Queijo.cpp @@ -0,0 +1,213 @@ +/* Problem URL: https://codeforces.com/group/YgJmumGtHD/contest/105936/problem/F */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k; + cin >> n >> m >> k; + + V fds(n); + cin >> fds; + + ll valids = 0; + + int ri = -1; + int rj = -1; + + rep(i, n) { + rep(j, m) { + if (fds[i][j] == 'R') { + ri = i; + rj = j; + continue; + } + + if (fds[i][j] == '.') { + valids++; + } + } + } + + ll inf = INT64_MAX >> 1; + vl discount(n * m + 1, 0); + + ll minimal_cheese = inf; + ll maximal = 0; + + auto bfs = [&](int i, int j) { + vvl dis(n, vl(m, inf)); + dis[i][j] = 0; + queue> q; + q.emplace(i, j); + + while (!q.empty()) { + auto [i, j] = q.front(); + q.pop(); + + rmax(maximal, dis[i][j]); + + if (fds[i][j] == 'Q') { + rmin(minimal_cheese, dis[i][j]); + } else { + discount[dis[i][j]]++; + } + + int add[][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; + + repv(k, add) { + int id = i + k[0]; + int jd = j + k[1]; + + if (id >= 0 && id < n && jd >= 0 && jd < m && fds[id][jd] != '#' && dis[id][jd] > dis[i][j] + 1) { + dis[id][jd] = dis[i][j] + 1; + q.emplace(id, jd); + } + } + } + }; + + bfs(ri, rj); + + vl suf(n * m + 1); + for (int i = n * m - 1; i >= 0; i--) { + suf[i] = suf[i + 1] + discount[i]; + } + + const ll mod = 998244353; + + ll divs = valids; + nrep(i, 1, k) { + divs *= (valids - i); + divs %= mod; + } + + vl fact(n * m + 1); + fact[0] = 1; + + nrep(i, 1, n * m + 1) { + fact[i] = fact[i - 1] * i; + fact[i] %= mod; + } + + auto fastpow = [&](ll a, ll b) { + ll ans = 1; + ll fds = a; + + rep(i, 31) { + if ((b >> i) & 1) { ans *= fds; + ans %= mod; + } + + fds *= fds; + fds %= mod; + } + + return ans; + }; + + auto comb = [&](ll a, ll k) { + return (fact[a] * fastpow((fact[k] * fact[a - k]) % mod, mod - 2)) % mod; + }; + + auto perm = [&](ll a, ll k) { + return (fact[a] * fastpow(fact[a - k], mod - 2)) % mod; + }; + + ll ans = 0; + + for (ll i = 1; i <= maximal && i < minimal_cheese; i++) { + if (suf[i] < k) { + break; + } + + if (suf[i + 1] < k) { + ans += perm(suf[i], k) * i; + ans %= mod; + break; + } + + // ans += ((comb(suf[i], k) - comb(suf[i + 1], k)) % mod + mod) % mod * i; + ans += ((perm(suf[i], k) - perm(suf[i + 1], k)) % mod + mod) % mod * i; + ans %= mod; + } + + if (minimal_cheese != inf && suf[minimal_cheese] >= k) { + ans += perm(suf[minimal_cheese], k) * minimal_cheese; + ans %= mod; + } + + cerr << ans << '\n'; + + cout << (ans * fastpow(divs, mod - 2)) % mod << '\n'; +} diff --git a/XII Maratona Mineira de Programação/I. Interativo.cpp b/XII Maratona Mineira de Programação/I. Interativo.cpp new file mode 100644 index 0000000..8a22dce --- /dev/null +++ b/XII Maratona Mineira de Programação/I. Interativo.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://codeforces.com/group/YgJmumGtHD/contest/105936/problem/I */ + +#include + +using namespace std; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + int first = 0; + cout << "L 1" << endl; + cin >> first; + + if (n % 2 == 0 && first == n / 2) { + cout << "C 1" << endl; + cin >> first; + cout << "L 1" << endl; + cin >> first; + } + + nrep(i, 1, n) { + cout << "L " << i + 1 << endl; + int ans; + cin >> ans; + + if (ans != first) { + cout << "L " << i + 1 << endl; + cin >> ans; + } + } + + rep(i, n) { + int ans; + cout << "C " << i + 1 << endl; + cin >> ans; + + if (ans != n) { + cout << "C " << i + 1 << endl; + cin >> ans; + } + } + + cout << "FIM" << endl; +}