From c4acd23d1968fedf2ec8c1df7faccb8a0091aa76 Mon Sep 17 00:00:00 2001 From: Segcolt <9hmbzr275@mozmail.com> Date: Mon, 15 Dec 2025 13:40:39 -0300 Subject: [PATCH] Add a bunch of other problems. --- .../G. Glad You Came.cpp | 152 ++++++++++ .../L. Lowest Latency.cpp | 211 +++++++++++++ .../M. Maximum Distance To Port.cpp | 150 ++++++++++ .../C. Trip Shopping.cpp | 152 ++++++++++ Bayan 2015 Contest Warm Up/D. CGCDSSQ.cpp | 183 ++++++++++++ .../D. Dexterina’s Lab.cpp | 122 ++++++++ .../B. Battling with Numbers.cpp | 148 ++++++++++ .../C. Alya and Permutation.cpp | 166 +++++++++++ .../D. Yet Another Real Number Problem.cpp | 190 ++++++++++++ .../D. Kevin and Competition Memories.cpp | 181 ++++++++++++ .../A. Sequence Game.cpp | 126 ++++++++ .../B. Even Modulo Pair.cpp | 120 ++++++++ .../C. Dungeon.cpp | 185 ++++++++++++ .../D. Copy String.cpp | 157 ++++++++++ .../E. Journey.cpp | 126 ++++++++ .../A. Object Identification.cpp | 142 +++++++++ .../A. Breach of Faith.cpp | 148 ++++++++++ .../B. Floor or Ceil.cpp | 128 ++++++++ .../F. Igor and Mountain.cpp | 116 +++++++- .../C. Median Splits.cpp | 180 ++++++++++++ .../A. Sports Betting.cpp | 175 +++++++++++ .../B. Sasha and the Apartment Purchase.cpp | 112 +++++++ .../C2. Hacking Numbers (Medium Version).cpp | 119 ++++++++ .../D. Fewer Batteries.cpp | 164 +++++++++++ .../B. Gellyfish and Baby's Breath.cpp | 174 +++++++++++ .../C. Make It Beautiful.cpp | 146 +++++++++ Codeforces Round 1032 (Div. 3)/G. Gangsta.cpp | 127 ++++++++ .../D. Matrix game.cpp | 137 +++++++++ .... Maple and Tree Beauty (Easy Version).cpp | 74 +++-- ...nversion Graph Coloring (Easy Version).cpp | 96 +++--- .../D. Batteries.cpp | 118 ++++++++ .../E. Beautiful Palindromes.cpp | 143 +++++++++ .../A. Souvlaki VS. Kalamaki.cpp | 119 ++++++++ .../B. Siga ta Kymata.cpp | 133 +++++++++ .../C. Monopati.cpp | 140 +++++++++ .../D1. Diadrash (Easy Version).cpp | 145 +++++++++ ...F. Rae Taylor and Trees (hard version).cpp | 148 ++++++++++ .../A. Dungeon Equilibrium.cpp | 126 ++++++++ .../B. Expansion Plan 2.cpp | 154 ++++++++++ .../C. Meximum Array 2.cpp | 158 ++++++++++ .../A. Suspension.cpp | 111 +++++++ Codeforces Round 1067 (Div. 2)/B. Split.cpp | 150 ++++++++++ .../C. Annoying Game.cpp | 147 ++++++++++ .../D. Palindrome Flipping.cpp | 186 ++++++++++++ .../A. Sleeping Through Classes.cpp | 128 ++++++++ .../B. Niko's Tactical Cards.cpp | 122 ++++++++ .../C. Kanade's Perfect Multiples.cpp | 174 +++++++++++ .../D. Taiga's Carry Chains.cpp | 215 ++++++++++++++ .../A. Operations with Inversions.cpp | 125 ++++++++ .../B. Optimal Shifts.cpp | 137 +++++++++ .../C. Odd Process.cpp | 157 ++++++++++ .../D. Fibonacci Paths.cpp | 164 +++++++++++ .../E. Tetrahedron.cpp | 145 +++++++++ .../A. The Closest Pair.cpp | 118 ++++++++ .../B. Jzzhu and Sequences.cpp | 156 ++++++++++ .../D. R2D2 and Droid Army.cpp | 160 ++++++++++ .../A. Writing Code.cpp | 150 ++++++++++ .../E. Wet Shark and Blocks.cpp | 156 ++++++++++ .../B. Maximum of Maximums of Minimums.cpp | 121 ++++++++ .../F. Tree with Maximum Cost.cpp | 160 ++++++++++ .../B. Tokitsukaze, CSL and Stone Game.cpp | 158 ++++++++++ .../A. Knapsack.cpp | 139 +++++++++ .../C. Bricks and Bags.cpp | 122 ++++++++ .../C. Almost All Multiples.cpp | 128 ++++++++ .../B. Hossam and Friends.cpp | 135 +++++++++ .../D. Friendly Spiders.cpp | 232 +++++++++++++++ .../D. Bit Guessing Game.cpp | 142 +++++++++ .../E. Vlad and a Pair of Numbers.cpp | 132 +++++++++ .../A. The Very Beautiful Blanket.cpp | 135 +++++++++ .../C. Sequence Master.cpp | 144 +++++++++ .../F. Bouncy Ball.cpp | 170 +++++++++++ .../C. Strongly Composite.cpp | 146 +++++++++ .../B. Fish Graph.cpp | 245 ++++++++++++++++ .../C. Dreaming of Freedom.cpp | 131 +++++++++ .../A. Copil Copac Draws Trees.cpp | 139 +++++++++ .../B. Long Long.cpp | 124 ++++++++ .../E. Tracking Segments.cpp | 203 +++++++++++++ .../C. Vampiric Powers, anyone?.cpp | 156 ++++++++++ .... Rudolf and Snowflakes (hard version).cpp | 155 ++++++++++ .../C. Vika and Price Tags.cpp | 147 ++++++++++ .../G. The Great Equalizer.cpp | 183 ++++++++++++ .../D. Cyclic Operations.cpp | 173 +++++++++++ Codeforces Round 899 (Div. 2)/D. Tree XOR.cpp | 172 +++++++++++ .../D. Reverse Madness.cpp | 162 ++++++++++ .../A. Anonymous Informant.cpp | 139 +++++++++ .../C. Freedom of Choice.cpp | 106 +++++++ .../F. Alex's whims.cpp | 126 ++++++++ .../B. Milena and Admirer.cpp | 123 ++++++++ .../D. Absolute Beauty.cpp | 127 ++++++++ .../F. Programming Competition.cpp | 150 ++++++++++ .../C. Partitioning the Array.cpp | 168 +++++++++++ .../E. Eat the Chip.cpp | 169 +++++++++++ .../C. Physical Education Lesson.cpp | 142 +++++++++ .../E. Vlad and an Odd Ordering.cpp | 126 ++++++++ .../B. Binary Path.cpp | 142 +++++++++ .../C. How Does the Rook Move?.cpp | 130 +++++++++ .../A. Everything Nim.cpp | 124 ++++++++ .../B. Missing Subsequence Sum.cpp | 126 ++++++++ .../A. Permutation Counting.cpp | 146 +++++++++ .../B1. Reverse Card (Easy Version).cpp | 123 ++++++++ .../E. Money Buys Happiness.cpp | 145 +++++++++ .../D. Paint the Tree.cpp | 191 ++++++++++++ .../D. GCD-sequence.cpp | 167 +++++++++++ .../E. Permutation of Rows and Columns.cpp | 145 +++++++++ .../C. Have Your Cake and Eat It Too.cpp | 154 ++++++++++ .../C. Hungry Games.cpp | 149 ++++++++++ .../F. Sakurako's Box.cpp | 137 +++++++++ ...unli's Subarray Queries (easy version).cpp | 150 ++++++++++ .../C. Gerrymandering.cpp | 158 ++++++++++ .../C. Sakurako's Field Trip.cpp | 125 ++++++++ ... Sakurako, Kosuke, and the Permutation.cpp | 130 +++++++++ .../C. Penchick and BBQ Buns.cpp | 140 +++++++++ .../E. Kachina's Favorite Binary String.cpp | 152 ++++++++++ .../B. Move Back at a Cost.cpp | 138 +++++++++ .../C. Ordered Permutations.cpp | 148 ++++++++++ .../D. Non Prime Tree.cpp | 156 ++++++++++ .../B. Take Your Places!.cpp | 151 ++++++++++ .../D. World is Mine.cpp | 138 +++++++++ .../C. Subset Multiplication.cpp | 121 ++++++++ .../E. Xor-sequences.cpp | 167 +++++++++++ .../C. Maximum Set.cpp | 171 +++++++++++ .../D. Bracket Coloring.cpp | 141 +++++++++ .../C. Ranom Numbers.cpp | 214 ++++++++++++++ .../C. Decreasing String.cpp | 154 ++++++++++ .../C. Add, Divide and Floor.cpp | 131 +++++++++ .../D. Recommendations.cpp | 180 ++++++++++++ .../D. Problem about GCD.cpp | 132 +++++++++ .../D. Palindrome Shuffle.cpp | 188 ++++++++++++ .../D. Tree Jumps.cpp | 149 ++++++++++ .../D. Array and GCD.cpp | 149 ++++++++++ .../D. Inversion Value of a Permutation.cpp | 171 +++++++++++ .... Removal of a Sequence (Easy Version).cpp | 120 ++++++++ .../A. Maximum Neighborhood.cpp | 143 +++++++++ .../B. Addition on a Segment.cpp | 129 ++++++++ .../C. Quotient and Remainder.cpp | 137 +++++++++ .../D. Almost Roman.cpp | 211 +++++++++++++ .../E. Binary Strings and Blocks.cpp | 106 +++++++ .../E. Turn Off The TV.cpp | 184 ++++++++++++ .../F. Clear The Matrix.cpp | 276 ++++++++++++++++++ .../C. Standard Free2play.cpp | 126 ++++++++ .../C. Divisor Chain.cpp | 122 ++++++++ .../C2. Game on Tree (Medium).cpp | 143 +++++++++ .../D. Kevin and Numbers.cpp | 151 ++++++++++ .../A. Suffix Array - 2.cpp | 172 +++++++++++ .../A. Substring Search.cpp | 217 ++++++++++++++ .../B. Counting Substrings.cpp | 232 +++++++++++++++ .../A. Suffix Array and LCP.cpp | 196 +++++++++++++ Matrix Exponentiation/A. Random Mood.cpp | 148 ++++++++++ Matrix Exponentiation/B. String Mood.cpp | 146 +++++++++ .../D. Two-Colored Dominoes.cpp | 164 +++++++++++ .../D. Same Count One.cpp | 156 ++++++++++ .../D. Division Versus Addition.cpp | 127 ++++++++ TODO.md | 62 +++- ...sage Transmission Error (easy version).cpp | 120 ++++++++ .../A1. Encode and Decode (Easy Version).cpp | 129 ++++++++ .../A2. Encode and Decode (Hard Version).cpp | 156 ++++++++++ .../B. Locate.cpp | 181 ++++++++++++ .../C. Intercepting Butterflies.cpp | 181 ++++++++++++ .../D. Many Perfect Squares.cpp | 162 ++++++++++ .../A. Analyzing the Race.cpp | 209 +++++++++++++ XIII UnB Contest Mirror/B. Bauru.cpp | 138 +++++++++ .../C. Creating a Playlist.cpp | 117 ++++++++ XIII UnB Contest Mirror/D. Shift Cipher.cpp | 187 ++++++++++++ .../E. Elementary Data Structure Problem.cpp | 129 ++++++++ XIII UnB Contest Mirror/F. Falatro.cpp | 161 ++++++++++ .../G. Gelatos from Goiás.cpp | 164 +++++++++++ .../I. Ivo saw the UVa.cpp | 131 +++++++++ .../J. Jolly Game Night.cpp | 109 +++++++ XIII UnB Contest Mirror/K. Kronos.cpp | 118 ++++++++ .../L. Leveling Diamonds.cpp | 129 ++++++++ XIII UnB Contest Mirror/N. Nautic Issue.cpp | 173 +++++++++++ ...Sum over all Substrings (Easy Version).cpp | 106 +++++++ 172 files changed, 25644 insertions(+), 79 deletions(-) create mode 100644 2018 Chinese Multi-University Training, BeihangU Contest/G. Glad You Came.cpp create mode 100644 2022 Benelux Algorithm Programming Contest (BAPC 22)/L. Lowest Latency.cpp create mode 100644 2025 ICPC Asia Taichung Regional Contest (Unrated, Online Mirror, ICPC Rules, Preferably Teams)/M. Maximum Distance To Port.cpp create mode 100644 Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)/C. Trip Shopping.cpp create mode 100644 Bayan 2015 Contest Warm Up/D. CGCDSSQ.cpp create mode 100644 Bubble Cup 9 - Finals [Online Mirror]/D. Dexterina’s Lab.cpp create mode 100644 COMPFEST 15 - Preliminary Online Mirror (Unrated, ICPC Rules, Teams Preferred)/B. Battling with Numbers.cpp create mode 100644 Codeforces Global Round 27/C. Alya and Permutation.cpp create mode 100644 Codeforces Global Round 27/D. Yet Another Real Number Problem.cpp create mode 100644 Codeforces Global Round 28/D. Kevin and Competition Memories.cpp create mode 100644 Codeforces Global Round 30 (Div. 1 + Div. 2)/A. Sequence Game.cpp create mode 100644 Codeforces Global Round 30 (Div. 1 + Div. 2)/B. Even Modulo Pair.cpp create mode 100644 Codeforces Global Round 30 (Div. 1 + Div. 2)/C. Dungeon.cpp create mode 100644 Codeforces Global Round 30 (Div. 1 + Div. 2)/D. Copy String.cpp create mode 100644 Codeforces Global Round 30 (Div. 1 + Div. 2)/E. Journey.cpp create mode 100644 Codeforces Round 1004 (Div. 1)/A. Object Identification.cpp create mode 100644 Codeforces Round 1008 (Div. 1)/A. Breach of Faith.cpp create mode 100644 Codeforces Round 1010 (Div. 2, Unrated)/B. Floor or Ceil.cpp create mode 100644 Codeforces Round 1019 (Div. 2)/C. Median Splits.cpp create mode 100644 Codeforces Round 1021 (Div. 1)/A. Sports Betting.cpp create mode 100644 Codeforces Round 1021 (Div. 2)/B. Sasha and the Apartment Purchase.cpp create mode 100644 Codeforces Round 1025 (Div. 2)/C2. Hacking Numbers (Medium Version).cpp create mode 100644 Codeforces Round 1026 (Div. 2)/D. Fewer Batteries.cpp create mode 100644 Codeforces Round 1028 (Div. 2)/B. Gellyfish and Baby's Breath.cpp create mode 100644 Codeforces Round 1030 (Div. 2)/C. Make It Beautiful.cpp create mode 100644 Codeforces Round 1032 (Div. 3)/G. Gangsta.cpp create mode 100644 Codeforces Round 1033 (Div. 2) and CodeNite 2025/D. Matrix game.cpp create mode 100644 Codeforces Round 1056 (Div. 2)/D. Batteries.cpp create mode 100644 Codeforces Round 1059 (Div. 3)/E. Beautiful Palindromes.cpp create mode 100644 Codeforces Round 1063 (Div. 2)/A. Souvlaki VS. Kalamaki.cpp create mode 100644 Codeforces Round 1063 (Div. 2)/B. Siga ta Kymata.cpp create mode 100644 Codeforces Round 1063 (Div. 2)/C. Monopati.cpp create mode 100644 Codeforces Round 1063 (Div. 2)/D1. Diadrash (Easy Version).cpp create mode 100644 Codeforces Round 1065 (Div. 3)/F. Rae Taylor and Trees (hard version).cpp create mode 100644 Codeforces Round 1066 (Div. 1 + Div. 2)/A. Dungeon Equilibrium.cpp create mode 100644 Codeforces Round 1066 (Div. 1 + Div. 2)/B. Expansion Plan 2.cpp create mode 100644 Codeforces Round 1066 (Div. 1 + Div. 2)/C. Meximum Array 2.cpp create mode 100644 Codeforces Round 1067 (Div. 2)/A. Suspension.cpp create mode 100644 Codeforces Round 1067 (Div. 2)/B. Split.cpp create mode 100644 Codeforces Round 1067 (Div. 2)/C. Annoying Game.cpp create mode 100644 Codeforces Round 1067 (Div. 2)/D. Palindrome Flipping.cpp create mode 100644 Codeforces Round 1068 (Div. 2)/A. Sleeping Through Classes.cpp create mode 100644 Codeforces Round 1068 (Div. 2)/B. Niko's Tactical Cards.cpp create mode 100644 Codeforces Round 1068 (Div. 2)/C. Kanade's Perfect Multiples.cpp create mode 100644 Codeforces Round 1068 (Div. 2)/D. Taiga's Carry Chains.cpp create mode 100644 Codeforces Round 1070 (Div. 2)/A. Operations with Inversions.cpp create mode 100644 Codeforces Round 1070 (Div. 2)/B. Optimal Shifts.cpp create mode 100644 Codeforces Round 1070 (Div. 2)/C. Odd Process.cpp create mode 100644 Codeforces Round 1070 (Div. 2)/D. Fibonacci Paths.cpp create mode 100644 Codeforces Round 113 (Div. 2)/E. Tetrahedron.cpp create mode 100644 Codeforces Round 185 (Div. 1)/A. The Closest Pair.cpp create mode 100644 Codeforces Round 257 (Div. 2)/B. Jzzhu and Sequences.cpp create mode 100644 Codeforces Round 291 (Div. 2)/D. R2D2 and Droid Army.cpp create mode 100644 Codeforces Round 302 (Div. 1)/A. Writing Code.cpp create mode 100644 Codeforces Round 341 (Div. 2)/E. Wet Shark and Blocks.cpp create mode 100644 Codeforces Round 440 (Div. 2, based on Technocup 2018 Elimination Round 2)/B. Maximum of Maximums of Minimums.cpp create mode 100644 Codeforces Round 527 (Div. 3)/F. Tree with Maximum Cost.cpp create mode 100644 Codeforces Round 573 (Div. 1)/B. Tokitsukaze, CSL and Stone Game.cpp create mode 100644 Codeforces Round 683 (Div. 1, by Meet IT)/A. Knapsack.cpp create mode 100644 Codeforces Round 831 (Div. 1 + Div. 2)/C. Bricks and Bags.cpp create mode 100644 Codeforces Round 836 (Div. 2)/C. Almost All Multiples.cpp create mode 100644 Codeforces Round 837 (Div. 2)/B. Hossam and Friends.cpp create mode 100644 Codeforces Round 843 (Div. 2)/D. Friendly Spiders.cpp create mode 100644 Codeforces Round 846 (Div. 2)/D. Bit Guessing Game.cpp create mode 100644 Codeforces Round 847 (Div. 3)/E. Vlad and a Pair of Numbers.cpp create mode 100644 Codeforces Round 857 (Div. 1)/A. The Very Beautiful Blanket.cpp create mode 100644 Codeforces Round 858 (Div. 2)/C. Sequence Master.cpp create mode 100644 Codeforces Round 859 (Div. 4)/F. Bouncy Ball.cpp create mode 100644 Codeforces Round 868 (Div. 2)/C. Strongly Composite.cpp create mode 100644 Codeforces Round 869 (Div. 1)/B. Fish Graph.cpp create mode 100644 Codeforces Round 870 (Div. 2)/C. Dreaming of Freedom.cpp create mode 100644 Codeforces Round 875 (Div. 1)/A. Copil Copac Draws Trees.cpp create mode 100644 Codeforces Round 881 (Div. 3)/B. Long Long.cpp create mode 100644 Codeforces Round 881 (Div. 3)/E. Tracking Segments.cpp create mode 100644 Codeforces Round 882 (Div. 2)/C. Vampiric Powers, anyone?.cpp create mode 100644 Codeforces Round 883 (Div. 3)/E2. Rudolf and Snowflakes (hard version).cpp create mode 100644 Codeforces Round 885 (Div. 2)/C. Vika and Price Tags.cpp create mode 100644 Codeforces Round 894 (Div. 3)/G. The Great Equalizer.cpp create mode 100644 Codeforces Round 897 (Div. 2)/D. Cyclic Operations.cpp create mode 100644 Codeforces Round 899 (Div. 2)/D. Tree XOR.cpp create mode 100644 Codeforces Round 900 (Div. 3)/D. Reverse Madness.cpp create mode 100644 Codeforces Round 908 (Div. 1)/A. Anonymous Informant.cpp create mode 100644 Codeforces Round 908 (Div. 1)/C. Freedom of Choice.cpp create mode 100644 Codeforces Round 909 (Div. 3)/F. Alex's whims.cpp create mode 100644 Codeforces Round 910 (Div. 2)/B. Milena and Admirer.cpp create mode 100644 Codeforces Round 910 (Div. 2)/D. Absolute Beauty.cpp create mode 100644 Codeforces Round 916 (Div. 3)/F. Programming Competition.cpp create mode 100644 Codeforces Round 919 (Div. 2)/C. Partitioning the Array.cpp create mode 100644 Codeforces Round 920 (Div. 3)/E. Eat the Chip.cpp create mode 100644 Codeforces Round 924 (Div. 2)/C. Physical Education Lesson.cpp create mode 100644 Codeforces Round 928 (Div. 4)/E. Vlad and an Odd Ordering.cpp create mode 100644 Codeforces Round 930 (Div. 2)/B. Binary Path.cpp create mode 100644 Codeforces Round 940 (Div. 2) and CodeCraft-23/C. How Does the Rook Move?.cpp create mode 100644 Codeforces Round 941 (Div. 1)/A. Everything Nim.cpp create mode 100644 Codeforces Round 941 (Div. 1)/B. Missing Subsequence Sum.cpp create mode 100644 Codeforces Round 942 (Div. 1)/A. Permutation Counting.cpp create mode 100644 Codeforces Round 942 (Div. 1)/B1. Reverse Card (Easy Version).cpp create mode 100644 Codeforces Round 946 (Div. 3)/E. Money Buys Happiness.cpp create mode 100644 Codeforces Round 947 (Div. 1 + Div. 2)/D. Paint the Tree.cpp create mode 100644 Codeforces Round 950 (Div. 3)/D. GCD-sequence.cpp create mode 100644 Codeforces Round 950 (Div. 3)/E. Permutation of Rows and Columns.cpp create mode 100644 Codeforces Round 956 (Div. 2) and ByteRace 2024/C. Have Your Cake and Eat It Too.cpp create mode 100644 Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)/C. Hungry Games.cpp create mode 100644 Codeforces Round 970 (Div. 3)/F. Sakurako's Box.cpp create mode 100644 Codeforces Round 971 (Div. 4)/G1. Yunli's Subarray Queries (easy version).cpp create mode 100644 Codeforces Round 978 (Div. 2)/C. Gerrymandering.cpp create mode 100644 Codeforces Round 981 (Div. 3)/C. Sakurako's Field Trip.cpp create mode 100644 Codeforces Round 981 (Div. 3)/E. Sakurako, Kosuke, and the Permutation.cpp create mode 100644 Codeforces Round 987 (Div. 2)/C. Penchick and BBQ Buns.cpp create mode 100644 Codeforces Round 988 (Div. 3)/E. Kachina's Favorite Binary String.cpp create mode 100644 Codeforces Round 990 (Div. 1)/B. Move Back at a Cost.cpp create mode 100644 Codeforces Round 992 (Div. 2)/C. Ordered Permutations.cpp create mode 100644 Codeforces Round 992 (Div. 2)/D. Non Prime Tree.cpp create mode 100644 Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)/B. Take Your Places!.cpp create mode 100644 EPIC Institute of Technology Round Summer 2024 (Div. 1 + Div. 2)/D. World is Mine.cpp create mode 100644 EPIC Institute of Technology Round Summer 2025 (Codeforces Round 1036, Div. 1 + Div. 2)/C. Subset Multiplication.cpp create mode 100644 Educational Codeforces Round 14/E. Xor-sequences.cpp create mode 100644 Educational Codeforces Round 144 (Rated for Div. 2)/C. Maximum Set.cpp create mode 100644 Educational Codeforces Round 149 (Rated for Div. 2)/D. Bracket Coloring.cpp create mode 100644 Educational Codeforces Round 150 (Rated for Div. 2)/C. Ranom Numbers.cpp create mode 100644 Educational Codeforces Round 156 (Rated for Div. 2)/C. Decreasing String.cpp create mode 100644 Educational Codeforces Round 158 (Rated for Div. 2)/C. Add, Divide and Floor.cpp create mode 100644 Educational Codeforces Round 172 (Rated for Div. 2)/D. Recommendations.cpp create mode 100644 Educational Codeforces Round 173 (Rated for Div. 2)/D. Problem about GCD.cpp create mode 100644 Educational Codeforces Round 174 (Rated for Div. 2)/D. Palindrome Shuffle.cpp create mode 100644 Educational Codeforces Round 175 (Rated for Div. 2)/D. Tree Jumps.cpp create mode 100644 Educational Codeforces Round 178 (Rated for Div. 2)/D. Array and GCD.cpp create mode 100644 Educational Codeforces Round 183 (Rated for Div. 2)/D. Inversion Value of a Permutation.cpp create mode 100644 Educational Codeforces Round 184 (Rated for Div. 2)/D1. Removal of a Sequence (Easy Version).cpp create mode 100644 Educational Codeforces Round 185 (Rated for Div. 2)/A. Maximum Neighborhood.cpp create mode 100644 Educational Codeforces Round 185 (Rated for Div. 2)/B. Addition on a Segment.cpp create mode 100644 Educational Codeforces Round 185 (Rated for Div. 2)/C. Quotient and Remainder.cpp create mode 100644 Educational Codeforces Round 185 (Rated for Div. 2)/D. Almost Roman.cpp create mode 100644 Educational Codeforces Round 185 (Rated for Div. 2)/E. Binary Strings and Blocks.cpp create mode 100644 Educational Codeforces Round 29/E. Turn Off The TV.cpp create mode 100644 Educational Codeforces Round 34 (Rated for Div. 2)/F. Clear The Matrix.cpp create mode 100644 Educational Codeforces Round 74 (Rated for Div. 2)/C. Standard Free2play.cpp create mode 100644 Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)/C. Divisor Chain.cpp create mode 100644 Helvetic Coding Contest 2024 online mirror (teams allowed, unrated)/C2. Game on Tree (Medium).cpp create mode 100644 IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)/D. Kevin and Numbers.cpp create mode 100644 ITMO Academy: pilot course - Suffix Array - Step 2/A. Suffix Array - 2.cpp create mode 100644 ITMO Academy: pilot course - Suffix Array - Step 3/A. Substring Search.cpp create mode 100644 ITMO Academy: pilot course - Suffix Array - Step 3/B. Counting Substrings.cpp create mode 100644 ITMO Academy: pilot course - Suffix Array - Step 4/A. Suffix Array and LCP.cpp create mode 100644 Matrix Exponentiation/A. Random Mood.cpp create mode 100644 Matrix Exponentiation/B. String Mood.cpp create mode 100644 Pinely Round 2 (Div. 1 + Div. 2)/D. Two-Colored Dominoes.cpp create mode 100644 Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)/D. Same Count One.cpp create mode 100644 Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)/D. Division Versus Addition.cpp create mode 100644 Testing Round 19 (Div. 3)/C1. Message Transmission Error (easy version).cpp create mode 100644 Testing Round 20 (Unrated, Communication Problems)/A1. Encode and Decode (Easy Version).cpp create mode 100644 Testing Round 20 (Unrated, Communication Problems)/A2. Encode and Decode (Hard Version).cpp create mode 100644 Testing Round 20 (Unrated, Communication Problems)/B. Locate.cpp create mode 100644 Testing Round 20 (Unrated, Communication Problems)/C. Intercepting Butterflies.cpp create mode 100644 VK Cup 2022 - Отборочный раунд (Engine)/D. Many Perfect Squares.cpp create mode 100644 XIII UnB Contest Mirror/A. Analyzing the Race.cpp create mode 100644 XIII UnB Contest Mirror/B. Bauru.cpp create mode 100644 XIII UnB Contest Mirror/C. Creating a Playlist.cpp create mode 100644 XIII UnB Contest Mirror/D. Shift Cipher.cpp create mode 100644 XIII UnB Contest Mirror/E. Elementary Data Structure Problem.cpp create mode 100644 XIII UnB Contest Mirror/F. Falatro.cpp create mode 100644 XIII UnB Contest Mirror/G. Gelatos from Goiás.cpp create mode 100644 XIII UnB Contest Mirror/I. Ivo saw the UVa.cpp create mode 100644 XIII UnB Contest Mirror/J. Jolly Game Night.cpp create mode 100644 XIII UnB Contest Mirror/K. Kronos.cpp create mode 100644 XIII UnB Contest Mirror/L. Leveling Diamonds.cpp create mode 100644 XIII UnB Contest Mirror/N. Nautic Issue.cpp create mode 100644 think-cell Round 1/D1. Sum over all Substrings (Easy Version).cpp diff --git a/2018 Chinese Multi-University Training, BeihangU Contest/G. Glad You Came.cpp b/2018 Chinese Multi-University Training, BeihangU Contest/G. Glad You Came.cpp new file mode 100644 index 0000000..bee0ebf --- /dev/null +++ b/2018 Chinese Multi-University Training, BeihangU Contest/G. Glad You Came.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/gym/102114/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + unsigned x, y, z; + cin >> n >> m >> x >> y >> z; + + auto rng = [&]() { + x = x ^ (x << 11); + x = x ^ (x >> 4); + x = x ^ (x << 5); + x = x ^ (x >> 14); + unsigned w = x ^ y ^ z; + x = y; + y = z; + z = w; + return z; + }; + + vvl sparse(20, vl(n)); + + while (m--) { + ll rng1 = rng(); + ll rng2 = rng(); + ll rng3 = rng(); + + int l = min(rng2 % n + 1, rng1 % n + 1); + int r = max(rng2 % n + 1, rng1 % n + 1); + ll val = rng3 & ((1LL << 30) - 1); + l--, r--; + + int log = 31 - __builtin_clz(r - l + 1); + + rmax(sparse[log][l], val); + rmax(sparse[log][r - (1 << log) + 1], val); + } + + for (int j = 19; j > 0; j--) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + rmax(sparse[j - 1][i], sparse[j][i]); + rmax(sparse[j - 1][i + (1 << (j - 1))], sparse[j][i]); + } + } + + ll ans = 0; + rep(i, n) { + ans ^= (i + 1) * sparse[0][i]; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/2022 Benelux Algorithm Programming Contest (BAPC 22)/L. Lowest Latency.cpp b/2022 Benelux Algorithm Programming Contest (BAPC 22)/L. Lowest Latency.cpp new file mode 100644 index 0000000..7eaf08e --- /dev/null +++ b/2022 Benelux Algorithm Programming Contest (BAPC 22)/L. Lowest Latency.cpp @@ -0,0 +1,211 @@ +/* Problem URL: https://codeforces.com/gym/104020/problem/L?mobile=false */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +struct point { + ll x; + ll y; + ll z; + + friend istream &operator>>(istream &is, point &a) { + is >> a.x >> a.y >> a.z; + return is; + } + + bool operator<(point b) const { + if (x == b.x) { + if (y == b.y) { + return z < b.z; + } + + return y < b.y; + } + + return x < b.x; + } +}; + +ll sqr(ll x) +{ + return x * x; +} + +long double dis(point a, point b) +{ + ll x = a.x - b.x; + ll y = a.y - b.y; + ll z = a.z - b.z; + return sqrtl(sqr(x) + sqr(y) + sqr(z)); +} + +void solve() +{ + int n; + cin >> n; + + V p(n); + cin >> p; + + sortv(p); + + long double ans = 1e16; + + struct compy { + bool operator()(point a, point b) const { + if (a.y == b.y) { + if (a.z == b.z) { + return a.x > b.x; + } + + return a.z > b.z; + } + + return a.y > b.y; + } + }; + + struct compz { + bool operator()(point a, point b) const { + if (a.z == b.z) { + if (a.x == b.x) { + return a.y > b.y; + } + + return a.x > b.x; + } + + return a.z > b.z; + } + }; + + set sy; + set sz; + + sy.emplace(p[0]); + sz.emplace(p[0]); + + int cur = 0; + + nrep(i, 1, n) { + while (p[cur].x < p[i].x - ans) { + sy.erase(p[cur]); + sz.erase(p[cur]); + cur++; + } + + auto itr = sy.lower_bound(point(1e9, p[i].y + ans, 1e9)); + while (itr != sy.end() && abs(p[i].y - itr->y) <= ans) { + rmin(ans, dis(p[i], *itr)); + itr++; + } + + itr = sz.lower_bound(point(1e9, 1e9, p[i].z + ans)); + while (itr != sz.end() && abs(p[i].z - itr->z) <= ans) { + rmin(ans, dis(p[i], *itr)); + itr++; + } + + sy.emplace(p[i]); + sz.emplace(p[i]); + } + + cout << fixed << setprecision(7) << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/2025 ICPC Asia Taichung Regional Contest (Unrated, Online Mirror, ICPC Rules, Preferably Teams)/M. Maximum Distance To Port.cpp b/2025 ICPC Asia Taichung Regional Contest (Unrated, Online Mirror, ICPC Rules, Preferably Teams)/M. Maximum Distance To Port.cpp new file mode 100644 index 0000000..1bc07a5 --- /dev/null +++ b/2025 ICPC Asia Taichung Regional Contest (Unrated, Online Mirror, ICPC Rules, Preferably Teams)/M. Maximum Distance To Port.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2172/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m, k; + cin >> n >> m >> k; + + vi ans(k); + + vi type(n); + + rep(i, n) { + int t; + cin >> t; + type[i] = t - 1; + } + + vvi graph(n); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + queue q; + vi dis(n, oo); + dis[0] = 0; + q.push(0); + + while (!q.empty()) { + auto i = q.front(); + q.pop(); + + repv(j, graph[i]) { + if (dis[j] > dis[i] + 1) { + dis[j] = dis[i] + 1; + q.push(j); + } + } + } + + rep(i, n) { + rmax(ans[type[i]], dis[i]); + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)/C. Trip Shopping.cpp b/Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)/C. Trip Shopping.cpp new file mode 100644 index 0000000..35b172f --- /dev/null +++ b/Atto Round 1 (Codeforces Round 1041, Div. 1 + Div. 2)/C. Trip Shopping.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/contest/2127/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + vl a(n); + vl b(n); + cin >> a >> b; + + rep(i, n) { + if (a[i] > b[i]) { + swap(a[i], b[i]); + } + } + + vi perm(n); + rep(i, n) { + perm[i] = i; + } + + sort(all(perm), [&](int i, int j) { + if (a[i] == a[j]) { + return b[i] > b[j]; + } + return a[i] < a[j]; + }); + + ll ans = 0; + rep(i, n) { + ans += abs(a[i] - b[i]); + } + + ll minimal = OO; + + { + ll r = b[perm[0]]; + nrep(i, 1, n) { + if (a[perm[i]] <= r) { + cout << ans << '\n'; + return; + } + + rmin(minimal, a[perm[i]] - r); + rmax(r, b[perm[i]]); + } + } + + + cout << ans + minimal * 2 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Bayan 2015 Contest Warm Up/D. CGCDSSQ.cpp b/Bayan 2015 Contest Warm Up/D. CGCDSSQ.cpp new file mode 100644 index 0000000..03331f1 --- /dev/null +++ b/Bayan 2015 Contest Warm Up/D. CGCDSSQ.cpp @@ -0,0 +1,183 @@ +/* Problem URL: https://codeforces.com/problemset/problem/475/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + // map count; + // + // V> g(n); + // g[0][a[0]] = 1; + // count[a[0]]++; + // + // nrep(i, 1, n) { + // g[i][a[i]] = 1; + // repv(j, g[i - 1]) { + // g[i][gcd(a[i], j.first)] += j.second; + // } + // + // repv(j, g[i]) { + // count[j.first] += j.second; + // } + // } + // + // int q; + // cin >> q; + // while (q--) { + // ll a; + // cin >> a; + // cout << count[a] << '\n'; + // } + + vvl sparse(20, vl(n)); + rep(i, n) { + sparse[0][i] = a[i]; + } + + nrep(j, 1, 20) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + sparse[j][i] = gcd(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]); + } + } + + auto query = [&](ll l, ll r) { + ll log = 31 - __builtin_clz(r - l + 1); + return gcd(sparse[log][l], sparse[log][r - (1 << log) + 1]); + }; + + map count; + + rep(i, n) { + ll r = i; + while (r < n) { + ll low = r; + ll high = n - 1; + ll cur = r; + while (low <= high) { + ll mid = (low + high) >> 1; + + if (query(i, r) == query(i, mid)) { + cur = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + count[query(i, cur)] += cur - r + 1; + r = cur + 1; + } + } + + int q; + cin >> q; + while (q--) { + ll x; + cin >> x; + cout << count[x] << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Bubble Cup 9 - Finals [Online Mirror]/D. Dexterina’s Lab.cpp b/Bubble Cup 9 - Finals [Online Mirror]/D. Dexterina’s Lab.cpp new file mode 100644 index 0000000..51fa33e --- /dev/null +++ b/Bubble Cup 9 - Finals [Online Mirror]/D. Dexterina’s Lab.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/717/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, x; + cin >> n >> x; + x++; + + V p(x); + cin >> p; + + V act(4); + rep(i, x) { + rep(j, 4) { + if ((i >> j) & 1) { + act[j] += p[i]; + } + } + } + + cout << act; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/COMPFEST 15 - Preliminary Online Mirror (Unrated, ICPC Rules, Teams Preferred)/B. Battling with Numbers.cpp b/COMPFEST 15 - Preliminary Online Mirror (Unrated, ICPC Rules, Teams Preferred)/B. Battling with Numbers.cpp new file mode 100644 index 0000000..73dcc65 --- /dev/null +++ b/COMPFEST 15 - Preliminary Online Mirror (Unrated, ICPC Rules, Teams Preferred)/B. Battling with Numbers.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/contest/1866/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + + vi x1(n); + vi x2(n); + cin >> x1 >> x2; + + int m; + cin >> m; + + vi y1(m); + vi y2(m); + + cin >> y1 >> y2; + + vi x(2e6 + 1); + vi y(2e6 + 1); + + rep(i, n) { + x[x1[i]] = x2[i]; + } + + rep(i, m) { + y[y1[i]] = y2[i]; + } + + rep(i, 2e6 + 1) { + if (x[i] < y[i]) { + cout << "0\n"; + return; + } + } + + ll ans = 1; + const ll mod = 998244353; + + rep(i, 2e6 + 1) { + if (x[i] > y[i]) { + ans = (ans << 1) % mod; + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Global Round 27/C. Alya and Permutation.cpp b/Codeforces Global Round 27/C. Alya and Permutation.cpp new file mode 100644 index 0000000..9d4e469 --- /dev/null +++ b/Codeforces Global Round 27/C. Alya and Permutation.cpp @@ -0,0 +1,166 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2035/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + if (n == 5) { + cout << "5\n"; + cout << "2 1 3 4 5\n"; + return; + } + + int log = (1 << (32 - __builtin_clz(n))) - 1; + + vi apt(__builtin_popcount(log) << 1); + apt[0] = 1; + apt[1] = 5; + + for (int i = 2; i < apt.size(); i += 2) { + apt[i] = 1 << (i >> 1); + apt[i + 1] = (1 << ((i >> 1)) + 1) - 1; + } + + while (apt.back() > n) { + apt.pop_back(); + } + + set all; + rep(i, n) { + all.insert(i + 1); + } + + vi perm(n); + int ans = log; + int start = n - apt.size(); + int lim = n; + if (n & 1) { + if (n != log) { + start--; + lim--; + } + + perm[n - 1] = n; + all.erase(n); + ans = n; + } + + cout << ans << '\n'; + + nrep(i, start, lim) { + perm[i] = apt[i - start]; + all.erase(perm[i]); + } + + rep(i, n) { + if (all.empty()) { + break; + } + + perm[i] = *all.begin(); + all.erase(all.begin()); + } + + cout << perm; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Global Round 27/D. Yet Another Real Number Problem.cpp b/Codeforces Global Round 27/D. Yet Another Real Number Problem.cpp new file mode 100644 index 0000000..1617c46 --- /dev/null +++ b/Codeforces Global Round 27/D. Yet Another Real Number Problem.cpp @@ -0,0 +1,190 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2035/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + ll mod = 1e9 + 7; + + vvi rem(n); + vl count(n); + + while (~a[n - 1] & 1) { + count[n - 1]++; + a[n - 1] >>= 1; + } + + stack> s; + s.emplace(a[n - 1], count[n - 1], n - 1); + + auto comp = [&](tuple al, tuple bl) { + auto [a, pa, _] = al; + auto [b, pb, __] = bl; + + if (pb >= 30) { + return false; + } + + if (pa == 0) { + return a > (b << pb); + } + + return (a << pa) + (b << pb) > a + (b << (pb + pa)); + }; + + for (int i = n - 2; i >= 0; i--) { + while (~a[i] & 1) { + count[i]++; + a[i] >>= 1; + } + + tuple tup = {a[i], count[i], i}; + auto [a, pa, _] = tup; + + while (!s.empty() && comp(tup, s.top())) { + auto [b, pb, _] = s.top(); + s.pop(); + if (!s.empty()) { + auto &[tb, tpb, __] = s.top(); + tpb += pb; + } + } + + if (s.empty()) { + s.emplace(tup); + continue; + } + + auto &[b, pb, j] = s.top(); + rem[j].push_back(i); + s.emplace(tup); + } + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + rep(i, 60) { + if ((p >> i) & 1) { + ans = (ans * a) % mod; + } + + a = (a * a) % mod; + } + + return ans; + }; + + ll total = 0; + rep(i, n) { + repv(j, rem[i]) { + total = ((total - a[j] * fpow(2, count[j])) % mod + mod) % mod; + total = (total + a[j]) % mod; + count[i] += count[j]; + } + + total = (total + a[i] * fpow(2, count[i])) % mod; + cout << total << " \n"[i == n - 1]; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Global Round 28/D. Kevin and Competition Memories.cpp b/Codeforces Global Round 28/D. Kevin and Competition Memories.cpp new file mode 100644 index 0000000..cf7a156 --- /dev/null +++ b/Codeforces Global Round 28/D. Kevin and Competition Memories.cpp @@ -0,0 +1,181 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2048/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vl a(n); + vl b(m); + cin >> a >> b; + + int ke = a[0]; + sort(all(a), greater<>()); + + while (!a.empty() && a.back() <= ke) { + a.pop_back(); + } + + if (a.empty()) { + nrep(i, 1, m + 1) { + cout << (m / i) << " \n"[i == m]; + } + return; + } + + sortv(b); + + vl act; + int i = 0; + while (i < m && b[i] <= ke) { + act.push_back(b[i]); + i++; + } + + int lim = i; + + i = m - 1; + while (i >= 0 && b[i] > ke) { + act.push_back(b[i]); + i--; + } + + swap(act, b); + + vl pref(m + 2); + + repv(j, a) { + int maxi = lower_bound(b.begin() + lim, b.end(), j, greater<>()) - b.begin(); + pref[maxi]++; + } + + nrep(i, 1, m + 2) { + pref[i] += pref[i - 1]; + } + + vvi sparse(20, vi(m)); + rep(i, m) { + sparse[0][i] = pref[i]; + } + + nrep(j, 1, 20) { + for (int i = 0; i + (1 << (j - 1)) < m; i++) { + sparse[j][i] = max(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]); + } + } + + auto query = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return max(sparse[log][l], sparse[log][r - (1 << log) + 1]); + }; + + nrep(i, 1, m + 1) { + ll ans = 0; + + for (int j = 0; j + i - 1 < m; j += i) { + ans += query(j, j + i - 1) + 1; + } + + cout << ans << " \n"[i == m]; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Global Round 30 (Div. 1 + Div. 2)/A. Sequence Game.cpp b/Codeforces Global Round 30 (Div. 1 + Div. 2)/A. Sequence Game.cpp new file mode 100644 index 0000000..2f1f53b --- /dev/null +++ b/Codeforces Global Round 30 (Div. 1 + Div. 2)/A. Sequence Game.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/contest/2164/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + ll x; + cin >> x; + + ll minimal = oo; + ll maximal = -oo; + + rep(i, n) { + rmin(minimal, a[i]); + rmax(maximal, a[i]); + } + + if (minimal <= x && maximal >= x) { + cout << "YES\n"; + return; + } + + cout << "NO\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Global Round 30 (Div. 1 + Div. 2)/B. Even Modulo Pair.cpp b/Codeforces Global Round 30 (Div. 1 + Div. 2)/B. Even Modulo Pair.cpp new file mode 100644 index 0000000..2f5bb07 --- /dev/null +++ b/Codeforces Global Round 30 (Div. 1 + Div. 2)/B. Even Modulo Pair.cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/contest/2164/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + rep(i, n - 1) { + nrep(j, i + 1, n) { + if ((a[j] % a[i] & 1) == 0) { + cout << a[i] << ' ' << a[j] << '\n'; + return; + } + } + } + + cout << "-1\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Global Round 30 (Div. 1 + Div. 2)/C. Dungeon.cpp b/Codeforces Global Round 30 (Div. 1 + Div. 2)/C. Dungeon.cpp new file mode 100644 index 0000000..7224e46 --- /dev/null +++ b/Codeforces Global Round 30 (Div. 1 + Div. 2)/C. Dungeon.cpp @@ -0,0 +1,185 @@ +/* Problem URL: https://codeforces.com/contest/2164/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + priority_queue> a; + rep(i, n) { + ll now; + cin >> now; + a.push(now); + } + V> c(m); + + rep(i, m) { + cin >> c[i].first; + } + + rep(i, m) { + cin >> c[i].second; + } + sortv(c); + + int ans = 0; + priority_queue> swo; + priority_queue> mom; + priority_queue at; + int cur = 0; + while (cur < m && !a.empty()) { + if (c[cur].second == 0) { + mom.push(c[cur].first); + cur++; + continue; + } + + if (a.top() < c[cur].first) { + if (!at.empty()) { + auto now = a.top(); + a.pop(); + a.push(max(now, at.top())); + at.pop(); + ans++; + continue; + } + + swo.push(a.top()); + a.pop(); + continue; + } + + at.push(c[cur].second); + cur++; + } + + while (!at.empty() && !a.empty()) { + auto now = a.top(); + a.pop(); + a.push(max(now, at.top())); + at.pop(); + ans++; + } + + if (a.size() > swo.size()) { + swap(a, swo); + } + + while (!a.empty()) { + swo.push(a.top()); + a.pop(); + } + + while (!swo.empty() && !mom.empty()) { + if (swo.top() < mom.top()) { + swo.pop(); + continue; + } + + ans++; + swo.pop(); + mom.pop(); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Global Round 30 (Div. 1 + Div. 2)/D. Copy String.cpp b/Codeforces Global Round 30 (Div. 1 + Div. 2)/D. Copy String.cpp new file mode 100644 index 0000000..3e10a8d --- /dev/null +++ b/Codeforces Global Round 30 (Div. 1 + Div. 2)/D. Copy String.cpp @@ -0,0 +1,157 @@ +/* Problem URL: https://codeforces.com/contest/2164/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + string s, t; + cin >> s >> t; + + if (s[0] != t[0]) { + cout << "-1\n"; + return; + } + + vvi pos(26); + rep(i, n) { + pos[s[i] - 'a'].push_back(i); + } + + int big = 0; + int minimal = n - 1; + + vi choice(n); + for (int i = n - 1; i >= 1; i--) { + int cur = t[i] - 'a'; + auto itr = upper_bound(all(pos[cur]), min(i, minimal)); + + if (itr == pos[cur].begin()) { + cout << "-1\n"; + return; + } + + itr--; + + rmax(big, i - *itr); + rmin(minimal, *itr); + choice[i] = *itr; + } + + if (big > k) { + cout << "-1\n"; + return; + } + + cout << big << '\n'; + rep(j, big) { + for (int i = n - 1; i > 0; i--) { + if (choice[i] != i) { + s[choice[i] + 1] = s[choice[i]]; + choice[i]++; + } + } + + cout << s << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Global Round 30 (Div. 1 + Div. 2)/E. Journey.cpp b/Codeforces Global Round 30 (Div. 1 + Div. 2)/E. Journey.cpp new file mode 100644 index 0000000..9b66df4 --- /dev/null +++ b/Codeforces Global Round 30 (Div. 1 + Div. 2)/E. Journey.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/contest/2164/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vl cost(m); + ll ans = 0; + + V> edges(m); + rep(i, m) { + auto &[c, u, v] = edges[i]; + cin >> u >> v >> c; + u--, v--; + ans += c; + cost[i] = c; + } + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1004 (Div. 1)/A. Object Identification.cpp b/Codeforces Round 1004 (Div. 1)/A. Object Identification.cpp new file mode 100644 index 0000000..a71a670 --- /dev/null +++ b/Codeforces Round 1004 (Div. 1)/A. Object Identification.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2066/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + set all; + rep(i, n) { + all.insert(i); + } + + vi a(n); + vi pos(n); + rep(i, n) { + cin >> a[i]; + all.erase(a[i] - 1); + pos[a[i] - 1] = i; + } + + if (all.size() > 0) { + cout << "? " << *all.begin() + 1 << ' ' << a[0] << endl; + int ans; + cin >> ans; + + cout << (ans == 0 ? "! A" : "! B") << endl; + return; + } + + int choice1 = pos[0] + 1; + int choice2 = pos[n - 1] + 1; + + cout << "? " << choice1 << ' ' << choice2 << endl; + int ans1; + cin >> ans1; + + cout << "? " << choice2 << ' ' << choice1 << endl; + int ans2; + cin >> ans2; + + cout << (ans1 == ans2 && ans1 >= n - 1 && ans2 >= n - 1 ? "! B" : "! A") << endl; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1008 (Div. 1)/A. Breach of Faith.cpp b/Codeforces Round 1008 (Div. 1)/A. Breach of Faith.cpp new file mode 100644 index 0000000..9ce31ee --- /dev/null +++ b/Codeforces Round 1008 (Div. 1)/A. Breach of Faith.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2077/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + n <<= 1; + + vl a(n); + cin >> a; + sortv(a); + + set elem; + + ll total = 0; + vl ans(n); + rep(i, n >> 1) { + ans[2 * i] = a[n - i - 1]; + ans[2 * i + 1] = a[i]; + total += ans[2 * i] - ans[2 * i + 1]; + elem.insert(ans[2 * i]); + elem.insert(ans[2 * i + 1]); + } + + if (elem.count(total)) { + ll t = a[n - 1]; + ans[1] = a[n - 2]; + total = -a[n - 2]; + nrep(i, 1, n >> 1) { + ans[2 * i] = a[i - 1]; + ans[2 * i + 1] = a[n - i - 2]; + total += ans[2 * i] - ans[2 * i + 1]; + } + cout << t; + ans[0] = t - total; + repv(i, ans) { + cout << ' ' << i; + } + cout << '\n'; + return; + } + + cout << total; + repv(i, ans) { + cout << ' ' << i; + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1010 (Div. 2, Unrated)/B. Floor or Ceil.cpp b/Codeforces Round 1010 (Div. 2, Unrated)/B. Floor or Ceil.cpp new file mode 100644 index 0000000..f84645b --- /dev/null +++ b/Codeforces Round 1010 (Div. 2, Unrated)/B. Floor or Ceil.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2082/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll x, n, m; + cin >> x >> n >> m; + + n = min(n, 31LL); + m = min(m, 31LL); + + ll one = x; + ll two = x; + + rep(i, n) { + one >>= 1; + } + + rep(j, m) { + one = (one + 1) >> 1; + two = (two + 1) >> 1; + } + + rep(i, n) { + two >>= 1; + } + + cout << two << ' ' << one << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1013 (Div. 3)/F. Igor and Mountain.cpp b/Codeforces Round 1013 (Div. 3)/F. Igor and Mountain.cpp index 06be89b..9fe3128 100644 --- a/Codeforces Round 1013 (Div. 3)/F. Igor and Mountain.cpp +++ b/Codeforces Round 1013 (Div. 3)/F. Igor and Mountain.cpp @@ -1,16 +1,22 @@ /* Problem URL: https://codeforces.com/contest/2091/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 (size_t i = 0; i < (lim); i++) -#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } @@ -69,14 +75,112 @@ auto operator>>(istream &is, vector> &vec)->istream& { return is; } +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m, d; + cin >> n >> m >> d; + + int prev = d; + d *= d; + + V a(n); + cin >> a; + + const ll mod = 998244353; + + vvl dp(n, vl(m + 1)); + + dp[n - 1][0] = 1; + + int now = n - 1; + + auto dis = [&](ll x1, ll y1, ll x2, ll y2) { + ll x = x1 - x2; + ll y = y1 - y2; + return x*x + y*y; + }; + + while (true) { + nrep(i, 1, m + 1) { + dp[now][i] = (dp[now][i] + dp[now][i - 1]) % mod; + } + + dp[now][0] = 0; + nrep(i, 1, m + 1) { + if (a[now][i - 1] == '#') { + dp[now][i] = 0; + } + } + + nrep(i, 1, m + 1) { + dp[now][i] = (dp[now][i] + dp[now][i - 1]) % mod; + } + + if (now == 0) { + break; + } + + nrep(i, 1, m + 1) { + if (a[now][i - 1] == '#') { + continue; + } + + ll sum = ((dp[now][min(i + prev, m)] - dp[now][max(i - prev - 1, 0)]) % mod + mod) % mod; + + int cur = now - 1; + int act = i - prev; + while (dis(cur, act, now, i) > d) { + act++; + } + + int other = (i << 1) - act; + + dp[cur][max(act, 0)] += sum; + dp[cur][max(act, 0)] %= mod; + if (other < m) { + dp[cur][other + 1] -= sum; + dp[cur][other + 1] += mod * (dp[cur][other + 1] < 0); + } + + cur--; + } + + now--; + } + + ll ans = 0; + nrep(i, 1, m + 1) { + if (a[0][i - 1] == '#') { + continue; + } + + ans = ((ans + dp[0][min(i + prev, m)] - dp[0][max(i - prev - 1, 0)]) % mod + mod) % mod; + } + + cout << ans << '\n'; +} + int main() { ios::sync_with_stdio(false); cin.tie(nullptr); - int n, m, d; - cin >> n >> m >> d; + pre(); - V a; - cin >> a; + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } } diff --git a/Codeforces Round 1019 (Div. 2)/C. Median Splits.cpp b/Codeforces Round 1019 (Div. 2)/C. Median Splits.cpp new file mode 100644 index 0000000..359dc20 --- /dev/null +++ b/Codeforces Round 1019 (Div. 2)/C. Median Splits.cpp @@ -0,0 +1,180 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2103/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll k; + cin >> n >> k; + vl a(n); + cin >> a; + + vl pref(n); + vl suf(n); + + pref[0] = (a[0] > k) - (a[0] <= k); + suf[n - 1] = (a[n - 1] > k) - (a[n - 1] <= k); + + nrep(i, 1, n) { + pref[i] = pref[i - 1] + (a[i] > k) - (a[i] <= k); + suf[n - i - 1] = suf[n - i] + (a[n - i - 1] > k) - (a[n - i - 1] <= k); + } + + set sav; + + nrep(i, 1, n - 1) { + if (suf[i + 1] > 0) { + sav.insert(pref[i]); + continue; + } + + if (pref[i] <= 0) { + cout << "YES\n"; + return; + } + + if (sav.lower_bound(pref[i]) != sav.end()) { + cout << "YES\n"; + return; + } + + sav.insert(pref[i]); + } + + sav.clear(); + + for (int i = n - 2; i > 0; i--) { + if (pref[i - 1] > 0) { + sav.insert(suf[i]); + continue; + } + + if (suf[i] <= 0) { + cout << "YES\n"; + return; + } + + if (sav.lower_bound(suf[i]) != sav.end()) { + cout << "YES\n"; + return; + } + + sav.insert(suf[i]); + } + + int sa1 = 0; + while (sa1 < n && pref[sa1] > 0) { + sa1++; + } + + int sa2 = n - 1; + while (sa2 >= 0 && suf[sa2] > 0) { + sa2--; + } + + if (sa1 < sa2 - 1) { + cout << "YES\n"; + return; + } + + cout << "NO\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1021 (Div. 1)/A. Sports Betting.cpp b/Codeforces Round 1021 (Div. 1)/A. Sports Betting.cpp new file mode 100644 index 0000000..21e93cd --- /dev/null +++ b/Codeforces Round 1021 (Div. 1)/A. Sports Betting.cpp @@ -0,0 +1,175 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2097/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + sortv(a); + + V> v; + int c = 1; + + nrep(i, 1, n) { + if (a[i] == a[i - 1]) { + c++; + continue; + } + + v.emplace_back(a[i - 1], c); + c = 1; + } + v.emplace_back(a.back(), c); + + int cur = -1; + + rep(i, v.size()) { + if (v[i].second >= 4) { + cout << "Yes\n"; + return; + } + + if (cur == -1 && v[i].second >= 2) { + cur = i; + } + } + + if (cur == -1) { + cout << "No\n"; + return; + } + + int tmp = v[cur].first + 1; + int choice = cur + 1; + while (choice < v.size()) { + if (v[choice].first != tmp) { + if (v[choice].second >= 2) { + cur = choice; + tmp = v[cur].first + 1; + } else { + cur = choice + 1; + if (cur < v.size()) { + tmp = v[cur].first + 1; + } + } + choice++; + continue; + } + + if (v[choice].second >= 2) { + break; + } + + tmp++; + choice++; + } + + if (choice >= v.size()) { + cout << "No\n"; + return; + } + + cout << "Yes\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1021 (Div. 2)/B. Sasha and the Apartment Purchase.cpp b/Codeforces Round 1021 (Div. 2)/B. Sasha and the Apartment Purchase.cpp new file mode 100644 index 0000000..b7e4a35 --- /dev/null +++ b/Codeforces Round 1021 (Div. 2)/B. Sasha and the Apartment Purchase.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2098/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + vl a(n); + cin >> a; + sortv(a); + + cout << a[((n + k) >> 1)] - a[((n - k - 1) >> 1)] + 1 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1025 (Div. 2)/C2. Hacking Numbers (Medium Version).cpp b/Codeforces Round 1025 (Div. 2)/C2. Hacking Numbers (Medium Version).cpp new file mode 100644 index 0000000..2167efc --- /dev/null +++ b/Codeforces Round 1025 (Div. 2)/C2. Hacking Numbers (Medium Version).cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/contest/2109/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll n; + cin >> n; + + int tmp; + cout << "mul 9" << endl; + cin >> tmp; + cout << "digit" << endl; + cin >> tmp; + cout << "digit" << endl; + cin >> tmp; + cout << "add " << n - 9 << endl; + cin >> tmp; + cout << "!" << endl; + cin >> tmp; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1026 (Div. 2)/D. Fewer Batteries.cpp b/Codeforces Round 1026 (Div. 2)/D. Fewer Batteries.cpp new file mode 100644 index 0000000..f5fe5e9 --- /dev/null +++ b/Codeforces Round 1026 (Div. 2)/D. Fewer Batteries.cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2110/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vl val(n); + cin >> val; + + V>> tmp(n); + while (m--) { + int a, b, c; + cin >> a >> b >> c; + tmp[a - 1].emplace_back(b - 1, c); + } + + V>> graph(n); + rep(i, n) { + if (tmp[i].empty()) { + continue; + } + sortv(tmp[i]); + graph[i].emplace_back(tmp[i][0]); + nrep(j, 1, tmp[i].size()) { + if (tmp[i][j].first == tmp[i][j - 1].first) { + continue; + } + + graph[i].emplace_back(tmp[i][j]); + } + } + + vl dp(n); + + ll low = 0; + ll high = 1e18; + ll ans = -1; + while (low <= high) { + ll mid = (high - low) / 2 + low; + + fill(all(dp), -OO); + + dp[0] = min(mid, val[0]); + rep(i, n) { + repv(j, graph[i]) { + if (j.second > dp[i]) { + continue; + } + dp[j.first] = max(dp[j.first], min(dp[i] + val[j.first], mid)); + } + } + + if (dp[n - 1] >= 0) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1028 (Div. 2)/B. Gellyfish and Baby's Breath.cpp b/Codeforces Round 1028 (Div. 2)/B. Gellyfish and Baby's Breath.cpp new file mode 100644 index 0000000..e4d478e --- /dev/null +++ b/Codeforces Round 1028 (Div. 2)/B. Gellyfish and Baby's Breath.cpp @@ -0,0 +1,174 @@ +/* Problem URL: https://codeforces.com/contest/2116/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi apos(n); + vi bpos(n); + + vi a(n); + vi b(n); + + rep(i, n) { + int p; + cin >> p; + apos[p] = i; + a[i] = p; + } + + rep(i, n) { + int p; + cin >> p; + bpos[p] = i; + b[i] = p; + } + + V> ans(n); + + set pos; + rep(i, n) { + pos.insert(i); + } + + for (int i = n - 1; i >= 0; i--) { + set rem; + + auto getans = [&](int now, int p, vi &other) { + auto itr = pos.lower_bound(p); + + while (itr != pos.end()) { + pair act = {now, other[*itr - p]}; + rmax(ans[*itr], act); + rem.insert(*itr); + itr++; + } + }; + + getans(a[apos[i]], apos[i], b); + getans(b[bpos[i]], bpos[i], a); + + repv(i, rem) { + pos.erase(i); + } + } + + const ll mod = 998244353; + auto fpow = [&](ll a, ll p) { + ll ans = 1; + + rep(i, 30) { + if ((p >> i) & 1) { + ans = (ans * a) % mod; + } + a = (a * a) % mod; + } + + return ans; + }; + + rep(i, n) { + cout << (fpow(2, ans[i].first) + fpow(2, ans[i].second)) % mod << " \n"[i == n - 1]; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1030 (Div. 2)/C. Make It Beautiful.cpp b/Codeforces Round 1030 (Div. 2)/C. Make It Beautiful.cpp new file mode 100644 index 0000000..49dc121 --- /dev/null +++ b/Codeforces Round 1030 (Div. 2)/C. Make It Beautiful.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/contest/2118/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +ll getcost(ll n) +{ + if (~n & 1) { + return 1; + } + return 1LL << __builtin_ctzll(~n); +} + +void solve() +{ + int n; + ll k; + cin >> n >> k; + + struct comp { + bool operator()(ll a, ll b) { + return getcost(a) > getcost(b); + } + }; + + priority_queue pq; + rep(i, n) { + int n; + cin >> n; + pq.push(n); + } + + while (getcost(pq.top()) <= k) { + ll cur = pq.top(); + pq.pop(); + ll c = getcost(cur); + cur += c; + k -= c; + pq.push(cur); + } + + int ans = 0; + while (!pq.empty()) { + ans += __builtin_popcountll(pq.top()); + pq.pop(); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1032 (Div. 3)/G. Gangsta.cpp b/Codeforces Round 1032 (Div. 3)/G. Gangsta.cpp new file mode 100644 index 0000000..a8ef8df --- /dev/null +++ b/Codeforces Round 1032 (Div. 3)/G. Gangsta.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2121/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + string a; + cin >> a; + + vl pref(n + 1); + + rep(i, n) { + pref[i + 1] = pref[i] + (a[i] == '0') - (a[i] == '1'); + } + + ll ans = 0; + rep(i, n) { + ans += ((ll)i + 1) * (n - i); + } + + sortv(pref); + rep(i, n + 1) { + ans += pref[i] * (i - (n - i)); + } + + cout << (ans >> 1) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1033 (Div. 2) and CodeNite 2025/D. Matrix game.cpp b/Codeforces Round 1033 (Div. 2) and CodeNite 2025/D. Matrix game.cpp new file mode 100644 index 0000000..0473dd1 --- /dev/null +++ b/Codeforces Round 1033 (Div. 2) and CodeNite 2025/D. Matrix game.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2120/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +ll mod = 1e9 + 7; + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll a, b, k; + cin >> a >> b >> k; + + ll n = k * (a - 1) + 1; + n %= mod; + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + rep(i, 60) { + if ((p >> i) & 1) { + ans = (ans * a) % mod; + } + a = (a * a) % mod; + } + return ans; + }; + + auto comb = [&](ll n, ll k) { + ll ans = 1; + ll div = 1; + + rep(i, k) { + ans = (ans * ((n - i) % mod + mod) % mod) % mod; + div = (div * (i + 1)) % mod; + } + + return ans * fpow(div, mod - 2) % mod; + }; + + cout << n << ' ' << ((b - 1) * k % mod * comb(n, a) % mod + 1) % mod << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1048 (Div. 1)/C1. Maple and Tree Beauty (Easy Version).cpp b/Codeforces Round 1048 (Div. 1)/C1. Maple and Tree Beauty (Easy Version).cpp index 9abf96d..c3562e7 100644 --- a/Codeforces Round 1048 (Div. 1)/C1. Maple and Tree Beauty (Easy Version).cpp +++ b/Codeforces Round 1048 (Div. 1)/C1. Maple and Tree Beauty (Easy Version).cpp @@ -91,46 +91,60 @@ void solve() int n, k; cin >> n >> k; - vi depth(n); - vi sums(n); - V leaf(n, true); - - sums[0] = 1; - + vvi graph(n); nrep(i, 1, n) { - int a; - cin >> a; - a--; - - depth[i] = depth[a] + 1; - sums[depth[i]]++; - leaf[a] = false; + int j; + cin >> j; + graph[j - 1].push_back(i); } - int maxdepth = oo; - rep(i, n) { - if (leaf[i]) { - rmin(maxdepth, depth[i]); + vi sums; + int minimal = oo; + + function calc = [&](int i, int d) { + if (d >= sums.size()) { + sums.emplace_back(); } - } - vi dp(n + 1); - dp[0] = 1; + sums[d]++; + d++; - rep(i, maxdepth) { - for (int j = n; j >= sums[i]; j--) { - dp[j] |= dp[j - sums[i]]; - } - } - - rep(i, n + 1) { - if (dp[i] && i <= k && n - i <= n - k) { - cout << maxdepth + 1 << '\n'; + if (graph[i].empty()) { + rmin(minimal, d); return; } + + for (auto j : graph[i]) { + calc(j, d); + } + }; + + calc(0, 0); + + V> dp(sums.size() + 1, V(k + 1, -1)); + dp[0][k] = n - k; + + int maximal = 0; + + nrep(i, 1, minimal + 1) { + int cost = sums[i - 1]; + + rep(j, k + 1) { + if (j + cost <= k) { + rmax(dp[i][j], dp[i - 1][j + cost]); + } + + if (dp[i - 1][j] >= cost) { + rmax(dp[i][j], dp[i - 1][j] - cost); + } + + if (dp[i][j] >= 0) { + maximal = i; + } + } } - cout << maxdepth << '\n'; + cout << maximal << '\n'; } int main() diff --git a/Codeforces Round 1051 (Div. 2)/D1. Inversion Graph Coloring (Easy Version).cpp b/Codeforces Round 1051 (Div. 2)/D1. Inversion Graph Coloring (Easy Version).cpp index a6509ac..f5bb157 100644 --- a/Codeforces Round 1051 (Div. 2)/D1. Inversion Graph Coloring (Easy Version).cpp +++ b/Codeforces Round 1051 (Div. 2)/D1. Inversion Graph Coloring (Easy Version).cpp @@ -75,53 +75,67 @@ auto operator>>(istream &is, vector> &vec)->istream& { return is; } +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + const ll mod = 1e9 + 7; + + int n; + cin >> n; + vi a(n); + cin >> a; + + vvvl dp(n + 1, vvl(n + 1, vl(n + 1))); + dp[0][0][0] = 1; + + nrep(i, 1, n + 1) { + int cur = a[i - 1]; + dp[i] = dp[i - 1]; + + rep(j, n + 1) { + rep(k, min(cur, j) + 1) { + if (cur >= j) { + dp[i][cur][k] += dp[i - 1][j][k]; + dp[i][cur][k] %= mod; + continue; + } + + dp[i][j][cur] += dp[i - 1][j][k]; + dp[i][j][cur] %= mod; + } + } + } + + ll ans = 0; + rep(i, n + 1) { + rep(j, n + 1) { + ans = (ans + dp[n][i][j]) % mod; + } + } + + cout << ans << '\n'; +} + int main() { ios::sync_with_stdio(false); cin.tie(nullptr); + pre(); + int t; - cin >> t; + (TEST && cin >> t) || (t = 1); while (t--) { - int n; - cin >> n; - - vi a(n); - cin >> a; - - ll mod = 1e9 + 7; - - vvvl memo(n, vvl(n + 1, vl(n + 1, -1))); - - function dp = [&](int i, int maximal, int mp) { - if (i >= n) { - return 1LL; - } - - ll &ans = memo[i][maximal][mp]; - if (ans != -1) { - return ans; - } - - ans = dp(i + 1, maximal, mp); - - if (a[i] < maximal && a[i] < mp) { - return ans; - } - - if (a[i] >= maximal) { - ans += dp(i + 1, a[i], mp); - ans %= mod; - } - - if (a[i] < maximal) { - ans += dp(i + 1, maximal, max(mp, a[i])); - ans %= mod; - } - - return ans; - }; - - cout << dp(0, 0, 0) << '\n'; + solve(); } } diff --git a/Codeforces Round 1056 (Div. 2)/D. Batteries.cpp b/Codeforces Round 1056 (Div. 2)/D. Batteries.cpp new file mode 100644 index 0000000..0ee549b --- /dev/null +++ b/Codeforces Round 1056 (Div. 2)/D. Batteries.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2155/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + nrep(c, 1, n + 1) { + rep(i, n - c) { + cout << i + 1 << ' ' << i + 1 + c << endl; + int x; + cin >> x; + if (x) { + return; + } + } + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1059 (Div. 3)/E. Beautiful Palindromes.cpp b/Codeforces Round 1059 (Div. 3)/E. Beautiful Palindromes.cpp new file mode 100644 index 0000000..0cadf9e --- /dev/null +++ b/Codeforces Round 1059 (Div. 3)/E. Beautiful Palindromes.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2162/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + vi a(n); + cin >> a; + + set miss; + rep(i, n) { + miss.insert(i + 1); + } + + rep(i, n) { + miss.erase(a[i]); + } + + if (miss.empty()) { + rep(i, k) { + cout << a[i] << " \n"[i == k - 1]; + } + return; + } + + int choice = -1; + rep(i, n) { + if (a[i] != a[n - 1]) { + choice = a[i]; + } + } + + vi at; + if (choice == -1) { + at = {*miss.begin(), *next(miss.begin()), a[0]}; + } else { + at = {*miss.begin(), choice, a[n - 1]}; + } + + rep(i, k) { + cout << at[i % 3] << " \n"[i == k - 1]; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1063 (Div. 2)/A. Souvlaki VS. Kalamaki.cpp b/Codeforces Round 1063 (Div. 2)/A. Souvlaki VS. Kalamaki.cpp new file mode 100644 index 0000000..dff59dc --- /dev/null +++ b/Codeforces Round 1063 (Div. 2)/A. Souvlaki VS. Kalamaki.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/contest/2163/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + sortv(a); + + rep(i, n - 1) { + if ((i & 1) && a[i] != a[i + 1]) { + cout << "NO\n"; + return; + } + } + + cout << "YES\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1063 (Div. 2)/B. Siga ta Kymata.cpp b/Codeforces Round 1063 (Div. 2)/B. Siga ta Kymata.cpp new file mode 100644 index 0000000..1a904dd --- /dev/null +++ b/Codeforces Round 1063 (Div. 2)/B. Siga ta Kymata.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/contest/2163/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi p(n); + cin >> p; + string x; + cin >> x; + + if (x[0] == '1' || x[n - 1] == '1') { + cout << "-1\n"; + return; + } + + vi pos(n); + rep(i, n) { + pos[p[i] - 1] = i + 1; + } + + if (x[pos[0] - 1] == '1' || x[pos[n - 1] - 1] == '1') { + cout << "-1\n"; + return; + } + + cout << "5\n"; + cout << "1 " << pos[0] << '\n'; + cout << "1 " << pos[n - 1] << '\n'; + cout << pos[0] << ' ' << n << '\n'; + cout << pos[n - 1] << ' ' << n << '\n'; + cout << min(pos[0], pos[n - 1]) << ' ' << max(pos[0], pos[n - 1]) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1063 (Div. 2)/C. Monopati.cpp b/Codeforces Round 1063 (Div. 2)/C. Monopati.cpp new file mode 100644 index 0000000..82c8a3f --- /dev/null +++ b/Codeforces Round 1063 (Div. 2)/C. Monopati.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/contest/2163/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vvi a(2, vi(n)); + cin >> a; + + vi maximal(n); + vi minimal(n); + + maximal[n - 1] = a[1][n - 1]; + minimal[n - 1] = a[1][n - 1]; + + for (int i = n - 2; i >= 0; i--) { + maximal[i] = max(a[1][i], maximal[i + 1]); + minimal[i] = min(a[1][i], minimal[i + 1]); + } + + int ma = 0; + int mi = oo; + vi act(n << 1, (n << 1) + 1); + + rep(i, n) { + rmax(ma, a[0][i]); + rmin(mi, a[0][i]); + + rmin(act[min(mi, minimal[i]) - 1], max(ma, maximal[i])); + } + + ll ans = (n << 1) - act.back() + 1; + for (int i = (n << 1) - 2; i >= 0; i--) { + rmin(act[i], act[i + 1]); + ans += (n << 1) - act[i] + 1; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1063 (Div. 2)/D1. Diadrash (Easy Version).cpp b/Codeforces Round 1063 (Div. 2)/D1. Diadrash (Easy Version).cpp new file mode 100644 index 0000000..a74a473 --- /dev/null +++ b/Codeforces Round 1063 (Div. 2)/D1. Diadrash (Easy Version).cpp @@ -0,0 +1,145 @@ +/* Problem URL: https://codeforces.com/contest/2163/problem/D1 */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, q; + cin >> n >> q; + + V> seg(q); + repv(i, seg) { + cin >> i.first >> i.second; + } + + sortv(seg); + vi rang(n, -1); + + repv(i, seg) { + rmax(rang[i.first - 1], i.second - 1); + } + + int ans = 0; + + int maximal = -1; + int lim = (n >> 1) + (n & 1); + + cout << "? 1 " << (n >> 1) + (n & 1) << endl; + int tmp; + cin >> tmp; + if (!tmp) { + maximal = (n >> 1) + (n & 1) - 1; + lim = n; + } + + rep(i, lim) { + if (rang[i] <= maximal) { + continue; + } + cout << "? " << i + 1 << ' ' << rang[i] + 1 << endl; + int act; + cin >> act; + rmax(ans, act); + maximal = rang[i]; + } + + cout << "! " << ans << endl; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1065 (Div. 3)/F. Rae Taylor and Trees (hard version).cpp b/Codeforces Round 1065 (Div. 3)/F. Rae Taylor and Trees (hard version).cpp new file mode 100644 index 0000000..e9fa9ad --- /dev/null +++ b/Codeforces Round 1065 (Div. 3)/F. Rae Taylor and Trees (hard version).cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2171/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi p(n); + cin >> p; + + vi pos(n); + rep(i, n) { + pos[p[i] - 1] = i; + } + + stack> s; + + pair big = {0, 0}; + + V> edges; + + for (int i = n - 1; i >= 0; i--) { + while (!s.empty() && s.top().first > pos[i]) { + edges.emplace_back(s.top().second + 1, i + 1); + s.pop(); + } + + if (big.first > pos[i]) { + edges.emplace_back(big.second + 1, i + 1); + continue; + } + + big = {pos[i], i}; + s.emplace(pos[i], i); + } + + sortv(edges); + edges.erase(unique(all(edges)), edges.end()); + + if (edges.size() != n - 1) { + cout << "No\n"; + return; + } + + cout << "Yes\n"; + repv(i, edges) { + cout << i.first << ' ' << i.second << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1066 (Div. 1 + Div. 2)/A. Dungeon Equilibrium.cpp b/Codeforces Round 1066 (Div. 1 + Div. 2)/A. Dungeon Equilibrium.cpp new file mode 100644 index 0000000..629eb99 --- /dev/null +++ b/Codeforces Round 1066 (Div. 1 + Div. 2)/A. Dungeon Equilibrium.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/contest/2157/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi count(n + 1); + rep(i, n) { + int num; + cin >> num; + count[num]++; + } + + int ans = 0; + rep(i, n + 1) { + if (count[i] < i) { + ans += count[i]; + continue; + } + + ans += count[i] - i; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1066 (Div. 1 + Div. 2)/B. Expansion Plan 2.cpp b/Codeforces Round 1066 (Div. 1 + Div. 2)/B. Expansion Plan 2.cpp new file mode 100644 index 0000000..7d36c82 --- /dev/null +++ b/Codeforces Round 1066 (Div. 1 + Div. 2)/B. Expansion Plan 2.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/contest/2157/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll x, y; + cin >> n >> x >> y; + x = abs(x); + y = abs(y); + + ll ort = min(x, y); + ll right = x - ort; + ll left = y - ort; + + rep(i, n) { + char op; + cin >> op; + + if (op == '4') { + if (right) { + right--; + continue; + } + + if (left) { + left--; + continue; + } + + if (ort) { + ort--; + right++; + } + + continue; + } + + if (ort) { + ort--; + continue; + } + + if (right) { + right--; + continue; + } + + if (left) { + left--; + } + } + + cout << (ort == 0 && left == 0 && right == 0 ? "YES\n" : "NO\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1066 (Div. 1 + Div. 2)/C. Meximum Array 2.cpp b/Codeforces Round 1066 (Div. 1 + Div. 2)/C. Meximum Array 2.cpp new file mode 100644 index 0000000..e747d39 --- /dev/null +++ b/Codeforces Round 1066 (Div. 1 + Div. 2)/C. Meximum Array 2.cpp @@ -0,0 +1,158 @@ +/* Problem URL: https://codeforces.com/contest/2157/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k, q; + cin >> n >> k >> q; + + V valid(n); + V inv(n); + V> adj; + while (q--) { + int c, l, r; + cin >> c >> l >> r; + l--, r--; + + if (c == 1) { + nrep(i, l, r + 1) { + inv[i] = true; + } + continue; + } + + adj.emplace_back(l, r); + + nrep(i, l, r + 1) { + valid[i] = true; + } + } + + sortv(adj); + + vi ans(n); + rep(i, n) { + if (!valid[i]) { + ans[i] = k; + continue; + } + + if (valid[i] && inv[i]) { + ans[i] = n + 1; + } + } + + repv(i, adj) { + vi us; + nrep(j, i.first, i.second + 1) { + if (!inv[j]) { + us.push_back(j); + } + } + + nrep(j, 1, k) { + ans[us[j]] = (ans[us[j - 1]] + 1) % k; + } + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1067 (Div. 2)/A. Suspension.cpp b/Codeforces Round 1067 (Div. 2)/A. Suspension.cpp new file mode 100644 index 0000000..6a75126 --- /dev/null +++ b/Codeforces Round 1067 (Div. 2)/A. Suspension.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/contest/2158/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + int y, r; + cin >> y >> r; + + cout << min(y / 2 + r, n) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1067 (Div. 2)/B. Split.cpp b/Codeforces Round 1067 (Div. 2)/B. Split.cpp new file mode 100644 index 0000000..923a453 --- /dev/null +++ b/Codeforces Round 1067 (Div. 2)/B. Split.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/contest/2158/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + n <<= 1; + vi a(n); + cin >> a; + + sortv(a); + vi count = {1}; + nrep(i, 1, n) { + if (a[i] != a[i - 1]) { + count.emplace_back(); + } + + count.back()++; + } + + if (count.size() == 1) { + cout << (((n >> 1) & 1) << 1) << '\n'; + return; + } + + int ans = 0; + int c = 0; + rep(i, count.size()) { + if (count[i] & 1) { + ans++; + continue; + } + + c++; + } + + if (ans > 0) { + c <<= 1; + } else { + if (((n >> 1) & 1)) { + c -= ~c & 1; + c <<= 1; + } else { + c -= c & 1; + c <<= 1; + } + } + + cout << ans + c << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1067 (Div. 2)/C. Annoying Game.cpp b/Codeforces Round 1067 (Div. 2)/C. Annoying Game.cpp new file mode 100644 index 0000000..0b90d98 --- /dev/null +++ b/Codeforces Round 1067 (Div. 2)/C. Annoying Game.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/contest/2158/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + vl a(n); + vl b(n); + cin >> a >> b; + + if (n == 1) { + cout << a[0] + b[0] * (k & 1) << '\n'; + return; + } + + vl pref(n); + vl suf(n); + + ll kanp = 0; + ll kans = 0; + + ll ans = -OO; + + rep(i, n) { + kanp += a[i]; + kans += a[n - i - 1]; + + ans = max({ans, kanp, kans}); + + rmax(kanp, 0LL); + rmax(kans, 0LL); + + pref[i] = kanp; + suf[n - i - 1] = kans; + } + + if (k & 1) { + rmax(ans, suf[1] + b[0] + a[0]); + rmax(ans, pref[n - 2] + b[n - 1] + a[n - 1]); + nrep(i, 1, n - 1) { + rmax(ans, pref[i - 1] + suf[i + 1] + a[i] + b[i]); + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1067 (Div. 2)/D. Palindrome Flipping.cpp b/Codeforces Round 1067 (Div. 2)/D. Palindrome Flipping.cpp new file mode 100644 index 0000000..4d085df --- /dev/null +++ b/Codeforces Round 1067 (Div. 2)/D. Palindrome Flipping.cpp @@ -0,0 +1,186 @@ +/* Problem URL: https://codeforces.com/contest/2158/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +map>, char>> ops; +map>> getlast[2]; +void pre() +{ + ops["0000"] = {{}, '0'}; + ops["0001"] = {{{1, 3}}, '1'}; + ops["0010"] = {{{1, 2}, {1, 3}}, '0'}; + ops["0011"] = {{{1, 2}}, '1'}; + ops["0100"] = {{{3, 4}, {2, 4}}, '0'}; + ops["0101"] = {{{1, 3}, {3, 4}, {2, 4}}, '1'}; + ops["0110"] = {{{2, 3}}, '0'}; + ops["0111"] = {{{2, 4}}, '0'}; + ops["1000"] = {{{2, 4}}, '1'}; + ops["1001"] = {{{2, 3}}, '1'}; + ops["1010"] = {{{1, 3}, {3, 4}, {2, 4}}, '0'}; + ops["1011"] = {{{3, 4}, {2, 4}}, '1'}; + ops["1100"] = {{{1, 2}}, '0'}; + ops["1101"] = {{{1, 2}, {1, 3}}, '1'}; + ops["1110"] = {{{1, 3}}, '0'}; + ops["1111"] = {{}, '1'}; + + getlast[0]["0000"] = {}; + getlast[0]["0001"] = {{1, 4}, {1, 3}}; + getlast[0]["0010"] = {{1, 3}, {1, 2}}; + getlast[0]["0011"] = {{3, 4}}; + getlast[0]["0100"] = {{2, 4}, {3, 4}}; + getlast[0]["0101"] = {{1, 3}, {1, 2}, {2, 4}}; + getlast[0]["0110"] = {{2, 3}}; + getlast[0]["0111"] = {{2, 4}}; + getlast[0]["1000"] = {{1, 4}, {2, 4}}; + getlast[0]["1001"] = {{2, 3}, {1, 4}}; + getlast[0]["1010"] = {{2, 4}, {3, 4}, {1, 3}}; + getlast[0]["1011"] = {{1, 4}, {2, 4}, {3, 4}}; + getlast[0]["1100"] = {{1, 2}}; + getlast[0]["1101"] = {{1, 4}, {1, 3}, {1, 2}}; + getlast[0]["1110"] = {{1, 3}}; + getlast[0]["1111"] = {{1, 4}}; + + getlast[1]["1111"] = {}; + getlast[1]["1110"] = {{1, 4}, {1, 3}}; + getlast[1]["1101"] = {{1, 3}, {1, 2}}; + getlast[1]["1100"] = {{3, 4}}; + getlast[1]["1011"] = {{2, 4}, {3, 4}}; + getlast[1]["1010"] = {{1, 3}, {1, 2}, {2, 4}}; + getlast[1]["1001"] = {{2, 3}}; + getlast[1]["1000"] = {{2, 4}}; + getlast[1]["0111"] = {{1, 4}, {2, 4}}; + getlast[1]["0110"] = {{2, 3}, {1, 4}}; + getlast[1]["0101"] = {{2, 4}, {3, 4}, {1, 3}}; + getlast[1]["0100"] = {{1, 4}, {2, 4}, {3, 4}}; + getlast[1]["0011"] = {{1, 2}}; + getlast[1]["0010"] = {{1, 4}, {1, 3}, {1, 2}}; + getlast[1]["0001"] = {{1, 3}}; + getlast[1]["0000"] = {{1, 4}}; +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + string a, b; + cin >> a >> b; + + auto [ans, cur] = ops[a.substr(0, 4)]; + + nrep(i, 4, n) { + if (a[i] != cur) { + ans.emplace_back(1, i); + cur = a[i]; + } + } + + for (int i = n - 1; i > 3; i--) { + if (b[i] != cur) { + ans.emplace_back(1, i + 1); + cur = b[i]; + } + } + + auto bf = getlast[cur - '0'][b.substr(0, 4)]; + repv(i, bf) { + ans.emplace_back(i); + } + + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i.first << ' ' << i.second << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1068 (Div. 2)/A. Sleeping Through Classes.cpp b/Codeforces Round 1068 (Div. 2)/A. Sleeping Through Classes.cpp new file mode 100644 index 0000000..fb2014a --- /dev/null +++ b/Codeforces Round 1068 (Div. 2)/A. Sleeping Through Classes.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/contest/2173/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + string a; + cin >> a; + + int c = 0; + int ans = 0; + rep(i, n) { + if (c == 0 && a[i] == '0') { + ans++; + continue; + } + + if (a[i] == '1') { + c = k; + continue; + } + + c--; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1068 (Div. 2)/B. Niko's Tactical Cards.cpp b/Codeforces Round 1068 (Div. 2)/B. Niko's Tactical Cards.cpp new file mode 100644 index 0000000..0357d4b --- /dev/null +++ b/Codeforces Round 1068 (Div. 2)/B. Niko's Tactical Cards.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/2173/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + vl b(n); + cin >> a >> b; + + vvl dp(n, vl(2)); + dp[0][0] = max(-a[0], b[0]); + dp[0][1] = min(-a[0], b[0]); + + nrep(i, 1, n) { + dp[i][0] = max(b[i] - dp[i - 1][1], dp[i - 1][0] - a[i]); + dp[i][1] = min(b[i] - dp[i - 1][0], dp[i - 1][1] - a[i]); + } + + cout << dp[n - 1][0] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1068 (Div. 2)/C. Kanade's Perfect Multiples.cpp b/Codeforces Round 1068 (Div. 2)/C. Kanade's Perfect Multiples.cpp new file mode 100644 index 0000000..9788165 --- /dev/null +++ b/Codeforces Round 1068 (Div. 2)/C. Kanade's Perfect Multiples.cpp @@ -0,0 +1,174 @@ +/* Problem URL: https://codeforces.com/contest/2173/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll k; + cin >> n >> k; + + vl a(n); + cin >> a; + + sortv(a); + a.erase(unique(all(a)), a.end()); + n = a.size(); + + ll lim = (k + n - 1) / n / 2; + if (a[0] < lim) { + cout << "-1\n"; + return; + } + + set all; + rep(i, n) { + all.insert(a[i]); + } + + set ans; + rep(i, n) { + auto test = [&]() { + for (ll j = a[i]; j <= k; j += a[i]) { + if (!all.count(j)) { + return false; + } + } + + return true; + }; + + if (test()) { + ans.insert(a[i]); + } + } + + auto itr = ans.begin(); + while (itr != ans.end()) { + bool valid = false; + + for (ll j = *itr; j <= k; j += *itr) { + auto tmp = all.find(j); + if (tmp != all.end()) { + all.erase(tmp); + valid = true; + } + } + + if (!valid) { + itr = ans.erase(itr); + continue; + } + + itr++; + } + + if (!all.empty()) { + cout << "-1\n"; + return; + } + + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i << ' '; + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1068 (Div. 2)/D. Taiga's Carry Chains.cpp b/Codeforces Round 1068 (Div. 2)/D. Taiga's Carry Chains.cpp new file mode 100644 index 0000000..56f35fb --- /dev/null +++ b/Codeforces Round 1068 (Div. 2)/D. Taiga's Carry Chains.cpp @@ -0,0 +1,215 @@ +/* Problem URL: https://codeforces.com/contest/2173/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll n, k; + cin >> n >> k; + + if (k == 0) { + cout << "0\n"; + return; + } + + // vi count(n); + // int c = 0; + // nrep(i, 1, 30) { + // if (~(n >> i) & 1) { + // count.push_back(c); + // c = 0; + // continue; + // } + // + // c++; + // } + // + // count.push_back(c); + // sort(all(count), greater<>()); + // + // ll ans = 0; + // + + int act = min(k, 30LL); + + // vvvl memo(31, vvl(act + 1, vl(2, -1))); + // + // function dp = [&](int i, int k, int c) { + // if (i >= 31) { + // return 0LL; + // } + // + // ll &ans = memo[i][k][c]; + // if (ans != -1) { + // return ans; + // } + // + // if (c == 1 && ((n >> i) & 1)) { + // return ans = dp(i + 1, k, 1) + 1; + // } + // + // if (k == 0) { + // return ans = 0LL; + // } + // + // if (((n >> i) & 1) || c == 1) { + // return ans = max(dp(i + 1, k, 0), dp(i + 1, k - 1, 1) + 1); + // } + // + // return ans = dp(i + 1, k, 0); + // }; + + // dp(0, act, 0); + + vvvl dp(31, vvl(act + 1, vl(2, -OO))); + dp[0][0][0] = 0; + + if (n & 1) { + dp[0][1][1] = 1; + } + + nrep(i, 1, 31) { + dp[i][0][0] = dp[i - 1][0][0]; + if ((n >> i) & 1) { + dp[i][1][1] = max(dp[i - 1][1][1], dp[i - 1][0][0]) + 1; + } else { + dp[i][1][0] = max(dp[i - 1][1][1], dp[i - 1][0][0]); + } + + nrep(j, 1, act + 1) { + dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]); + + if ((n >> i) & 1) { + dp[i][j][1] = max(dp[i - 1][j - 1][0], dp[i - 1][j][1]) + 1; + continue; + } + + dp[i][j][1] = dp[i - 1][j - 1][1] + 1; + if (j > 1) { + rmax(dp[i][j][1], dp[i - 1][j - 2][0] + 1); + } + } + } + + ll ans = 0; + rep(i, 31) { + rep(j, act + 1) { + rmax(ans, dp[i][j][0] + k - j); + rmax(ans, dp[i][j][1] + k - j); + } + } + + cout << ans << '\n'; + + // rep(i, 30) { + // rep(j, act + 1) { + // if (memo[i][j][0] != -1) { + // rmax(ans, memo[i][j][0] + k - j); + // } + // + // if (memo[i][j][1] != -1) { + // rmax(ans, memo[i][j][1] + k - j); + // } + // } + // } + // + // cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1070 (Div. 2)/A. Operations with Inversions.cpp b/Codeforces Round 1070 (Div. 2)/A. Operations with Inversions.cpp new file mode 100644 index 0000000..bb6d954 --- /dev/null +++ b/Codeforces Round 1070 (Div. 2)/A. Operations with Inversions.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://codeforces.com/contest/2176/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + int now = n; + int ans = 0; + while (now > 1) { + rep(i, a.size()) { + if (a[i] == now) { + while (i < a.size() - 1 && a[i + 1] < a[i]) { + a.erase(a.begin() + i + 1); + ans++; + } + } + } + now--; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1070 (Div. 2)/B. Optimal Shifts.cpp b/Codeforces Round 1070 (Div. 2)/B. Optimal Shifts.cpp new file mode 100644 index 0000000..5b631eb --- /dev/null +++ b/Codeforces Round 1070 (Div. 2)/B. Optimal Shifts.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/contest/2176/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + string a; + cin >> n >> a; + + int maximal = 0; + + int first = a.find('1'); + int prev = first; + + nrep(i, 1, n) { + if (a[(first + i) % n] == '1') { + prev = (first + i) % n; + continue; + } + + rmax(maximal, (((first + i) % n - prev) % n + n) % n); + } + + if (maximal == 0) { + cout << "0\n"; + return; + } + + int ans = 0; + int cur = 1; + while (cur > maximal) { + maximal -= cur; + ans += cur; + cur <<= 1; + } + + cout << ans + maximal << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1070 (Div. 2)/C. Odd Process.cpp b/Codeforces Round 1070 (Div. 2)/C. Odd Process.cpp new file mode 100644 index 0000000..b119456 --- /dev/null +++ b/Codeforces Round 1070 (Div. 2)/C. Odd Process.cpp @@ -0,0 +1,157 @@ +/* Problem URL: https://codeforces.com/contest/2176/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + cin >> a; + + vl even; + vl odd; + + rep(i, n) { + if (a[i] & 1) { + odd.push_back(a[i]); + } else { + even.push_back(a[i]); + } + } + + if (odd.empty()) { + rep(i, n) { + cout << "0 "; + } + cout << '\n'; + return; + } + + sortv(even); + sortv(odd); + + ll ans = odd.back(); + ll cur = 0; + cout << ans; + rep(i, even.size()) { + cur += even[even.size() - i - 1]; + cout << ' ' << ans + cur; + } + + int tmp = n - 1 - even.size(); + + for (int i = 1; tmp > 1; tmp -= 2, i++) { + if (even.size() >= 1) { + cout << ' ' << odd.back() + cur - even[0] << ' '; + } else { + cout << " 0 "; + } + cout << odd.back() + cur; + } + + if (tmp == 1) { + cout << " 0"; + } + + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1070 (Div. 2)/D. Fibonacci Paths.cpp b/Codeforces Round 1070 (Div. 2)/D. Fibonacci Paths.cpp new file mode 100644 index 0000000..8032f3f --- /dev/null +++ b/Codeforces Round 1070 (Div. 2)/D. Fibonacci Paths.cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/contest/2176/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + vl a(n); + cin >> a; + + const ll mod = 998244353; + + V>> graph(n); + vvl inv(n); + + while (m--) { + int u, v; + cin >> u >> v; + u--, v--; + graph[u].emplace_back(a[v], v); + inv[v].push_back(a[u]); + } + + V> act(n); + + rep(now, n) { + sortv(graph[now]); + sortv(inv[now]); + inv[now].erase(unique(all(inv[now])), inv[now].end()); + + repv(j, inv[now]) { + auto itr = lower_bound(all(graph[now]), make_pair(j + a[now], -1)); + + while (itr != graph[now].end() && itr->first == j + a[now]) { + act[now][j].push_back(itr->second); + itr++; + } + } + } + + V> memo(n); + + function dp = [&](int i, ll prev) { + auto itr = memo[i].find(prev); + if (itr != memo[i].end()) { + return itr->second; + } + + ll &ans = memo[i][prev]; + + repv(j, act[i][prev]) { + ans = (ans + dp(j, a[i]) + 1) % mod; + } + + return ans; + }; + + ll ans = 0; + rep(i, n) { + repv(j, graph[i]) { + ans = (ans + dp(j.second, a[i]) + 1) % mod; + } + } + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 113 (Div. 2)/E. Tetrahedron.cpp b/Codeforces Round 113 (Div. 2)/E. Tetrahedron.cpp new file mode 100644 index 0000000..2877586 --- /dev/null +++ b/Codeforces Round 113 (Div. 2)/E. Tetrahedron.cpp @@ -0,0 +1,145 @@ +/* Problem URL: https://codeforces.com/contest/166/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + ll n; + cin >> n; + + const ll mod = 1e9 + 7; + + auto mul = [&](vvl &a, vvl &b) { + vvl c(a.size(), vl(b[0].size())); + + rep(i, c.size()) { + rep(j, c[0].size()) { + rep(k, b.size()) { + c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod; + } + } + } + + return c; + }; + + auto fpow = [&]() { + vvl ans(4, vl(4)); + vvl mu(4, vl(4, 1)); + + rep(i, 4) { + ans[i][i] = 1; + mu[i][i] = 0; + } + + rep(i, 30) { + if ((n >> i) & 1) { + ans = mul(ans, mu); + } + + mu = mul(mu, mu); + } + + return ans; + }; + + cout << fpow()[0][0] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 185 (Div. 1)/A. The Closest Pair.cpp b/Codeforces Round 185 (Div. 1)/A. The Closest Pair.cpp new file mode 100644 index 0000000..bd2ffaa --- /dev/null +++ b/Codeforces Round 185 (Div. 1)/A. The Closest Pair.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/problemset/problem/311/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + ll n, k; + cin >> n >> k; + + ll full = n * (n - 1) / 2; + + if (full <= k) { + cout << "no solution\n"; + return; + } + + rep(i, n) { + cout << "0 " << i << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 257 (Div. 2)/B. Jzzhu and Sequences.cpp b/Codeforces Round 257 (Div. 2)/B. Jzzhu and Sequences.cpp new file mode 100644 index 0000000..41cde9b --- /dev/null +++ b/Codeforces Round 257 (Div. 2)/B. Jzzhu and Sequences.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/contest/450/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + ll x, y, n; + cin >> x >> y >> n; + + const ll mod = 1e9 + 7; + + if (n == 1) { + cout << x + mod * (x < 0) << '\n'; + return; + } + + if (n == 2) { + cout << y + mod * (y < 0) << '\n'; + return; + } + n -= 2; + + auto mul = [&](vvl &a, vvl &b) { + vvl c(a.size(), vl(b[0].size())); + + rep(i, c.size()) { + rep(j, c[0].size()) { + rep(k, b.size()) { + c[i][j] = ((c[i][j] + a[i][k] * b[k][j] % mod) % mod + mod) % mod; + } + } + } + + return c; + }; + + auto fpow = [&]() { + vvl ans = {{x}, {y}}; + vvl mu(2, vl(2)); + mu[0][1] = 1; + mu[1][0] = mod - 1; + mu[1][1] = 1; + + rep(i, 31) { + if ((n >> i) & 1) { + ans = mul(mu, ans); + } + + mu = mul(mu, mu); + } + + return ans; + }; + + auto ans = fpow(); + + cout << ans[1][0] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 291 (Div. 2)/D. R2D2 and Droid Army.cpp b/Codeforces Round 291 (Div. 2)/D. R2D2 and Droid Army.cpp new file mode 100644 index 0000000..08bb409 --- /dev/null +++ b/Codeforces Round 291 (Div. 2)/D. R2D2 and Droid Army.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/problemset/problem/514/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m; + ll k; + cin >> n >> m >> k; + + vvl droid(n, vl(m)); + cin >> droid; + + vvvl sparse(m, vvl(20, vl(n))); + + rep(i, n) { + rep(j, m) { + sparse[j][0][i] = droid[i][j]; + } + } + + rep(i, m) { + nrep(j, 1, 20) { + for (int k = 0; k + (1 << (j - 1)) < n; k++) { + sparse[i][j][k] = max(sparse[i][j - 1][k], sparse[i][j - 1][k + (1 << (j - 1))]); + } + } + } + + auto query = [&](int i, int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return max(sparse[i][log][l], sparse[i][log][r - (1 << log) + 1]); + }; + + auto calc = [&](int l, int r) { + int ans = 0; + rep(j, m) { + ans += query(j, l, r); + } + return ans; + }; + + int ans = 0; + vi act(m); + + int l = 0; + rep(i, n) { + while (l <= i && calc(l, i) > k) { + l++; + } + + if (i - l + 1 > ans) { + ans = i - l + 1; + + rep(j, m) { + act[j] = query(j, l, i); + } + } + } + + cout << act; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 302 (Div. 1)/A. Writing Code.cpp b/Codeforces Round 302 (Div. 1)/A. Writing Code.cpp new file mode 100644 index 0000000..143ad86 --- /dev/null +++ b/Codeforces Round 302 (Div. 1)/A. Writing Code.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/contest/543/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m, b; + ll mod; + cin >> n >> m >> b >> mod; + + vi a(n); + cin >> a; + + // vvvl dp(n + 1, vvl(m + 1, vl(b + 1))); + // dp[0][0][0] = 1; + // + // nrep(i, 1, n + 1) { + // int now = a[i - 1]; + // + // rep(j, m + 1) { + // nrep(k, j, m + 1) { + // nrep(l, now * j, b + 1) { + // dp[i][k][l] += dp[i - 1][k - j][l - now * j]; + // dp[i][k][l] %= mod; + // } + // } + // } + // } + + + vvl dp(m + 1, vl(b + 1)); + dp[0][0] = 1; + + nrep(i, 1, n + 1) { + int now = a[i - 1]; + + rep(j, m) { + rep(l, b + 1 - now) { + dp[j + 1][l + now] += dp[j][l]; + dp[j + 1][l + now] %= mod; + } + } + } + + ll ans = 0; + rep(i, b + 1) { + ans += dp[m][i]; + ans %= mod; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 341 (Div. 2)/E. Wet Shark and Blocks.cpp b/Codeforces Round 341 (Div. 2)/E. Wet Shark and Blocks.cpp new file mode 100644 index 0000000..c480967 --- /dev/null +++ b/Codeforces Round 341 (Div. 2)/E. Wet Shark and Blocks.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/contest/621/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, k, x; + ll b; + cin >> n >> b >> k >> x; + + vi dig(n); + cin >> dig; + + const ll mod = 1e9 + 7; + + vvl ans(x, vl(x)); + repv(j, dig) { + ans[j%x][j%x]++; + } + + vvl adj(x, vl(x)); + rep(i, x) { + repv(j, dig) { + adj[i][(10 * i + j) % x]++; + } + } + + b--; + + auto mul = [&](vvl &a, vvl &b) { + vvl c(a.size(), vl(b[0].size())); + + rep(i, c.size()) { + rep(j, c[0].size()) { + rep(k, b.size()) { + c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod; + } + } + } + + return c; + }; + + rep(i, 30) { + if ((b >> i) & 1) { + ans = mul(ans, adj); + } + + adj = mul(adj, adj); + } + + ll sum = 0; + rep(i, x) { + sum = (sum + ans[i][k]) % mod; + } + + cout << sum << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 440 (Div. 2, based on Technocup 2018 Elimination Round 2)/B. Maximum of Maximums of Minimums.cpp b/Codeforces Round 440 (Div. 2, based on Technocup 2018 Elimination Round 2)/B. Maximum of Maximums of Minimums.cpp new file mode 100644 index 0000000..4121f50 --- /dev/null +++ b/Codeforces Round 440 (Div. 2, based on Technocup 2018 Elimination Round 2)/B. Maximum of Maximums of Minimums.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/problemset/problem/872/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, k; + cin >> n >> k; + vl a(n); + cin >> a; + + if (k == 1) { + cout << *min_element(all(a)) << '\n'; + return; + } + + if (k == 2) { + cout << max(a[0], a[n - 1]) << '\n'; + return; + } + + cout << *max_element(all(a)) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 527 (Div. 3)/F. Tree with Maximum Cost.cpp b/Codeforces Round 527 (Div. 3)/F. Tree with Maximum Cost.cpp new file mode 100644 index 0000000..447c842 --- /dev/null +++ b/Codeforces Round 527 (Div. 3)/F. Tree with Maximum Cost.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/contest/1092/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + + vl cost(n); + cin >> cost; + + 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); + } + + vl c(n); + vl dp(n); + + function dfs = [&](int i, int p) { + dp[i] = 0; + c[i] = cost[i]; + repv(j, graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i); + + c[i] += c[j]; + dp[i] += dp[j] + c[j]; + } + }; + + dfs(0, 0); + + ll ans = 0; + function reroot = [&](int i, int p) { + rmax(ans, dp[i]); + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + dp[j] = dp[i] + c[i] - c[j] * 2; + c[j] = c[i]; + + reroot(j, i); + } + }; + + reroot(0, 0); + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 573 (Div. 1)/B. Tokitsukaze, CSL and Stone Game.cpp b/Codeforces Round 573 (Div. 1)/B. Tokitsukaze, CSL and Stone Game.cpp new file mode 100644 index 0000000..e15aa84 --- /dev/null +++ b/Codeforces Round 573 (Div. 1)/B. Tokitsukaze, CSL and Stone Game.cpp @@ -0,0 +1,158 @@ +/* Problem URL: https://codeforces.com/contest/1190/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + const string win[] = { + "cslnb\n", + "sjfnb\n" + }; + + if (n == 1) { + cout << win[a[0] & 1]; + return; + } + + sortv(a); + if (a[0] == 0 && a[1] == 0) { + cout << win[0]; + return; + } + + map c; + + rep(i, n) { + c[a[i]]++; + } + + auto itr = c.begin(); + int count = 0; + while (itr != c.end()) { + if (itr->second >= 3 || (itr != c.begin() && itr->second == 2 && prev(itr)->first == itr->first - 1)) { + cout << win[0]; + return; + } + + if (itr->second == 2) { + count++; + } + + if (count > 1) { + cout << win[0]; + return; + } + + itr++; + } + + ll diff = 0; + rep(i, n) { + diff += a[i] - i; + } + + cout << win[diff & 1]; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 683 (Div. 1, by Meet IT)/A. Knapsack.cpp b/Codeforces Round 683 (Div. 1, by Meet IT)/A. Knapsack.cpp new file mode 100644 index 0000000..3326cae --- /dev/null +++ b/Codeforces Round 683 (Div. 1, by Meet IT)/A. Knapsack.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/contest/1446/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll w; + cin >> n >> w; + + vl a(n); + cin >> a; + + ll total = 0; + vi ans; + rep(i, n) { + if (a[i] >= ((w + 1) >> 1) && a[i] <= w) { + cout << "1\n"; + cout << i + 1 << '\n'; + return; + } + + if (a[i] > w) { + continue; + } + + total += a[i]; + ans.push_back(i + 1); + if (total >= ((w + 1) >> 1)) { + break; + } + } + + if (total < ((w + 1) >> 1)) { + cout << "-1\n"; + return; + } + + cout << ans.size() << '\n'; + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 831 (Div. 1 + Div. 2)/C. Bricks and Bags.cpp b/Codeforces Round 831 (Div. 1 + Div. 2)/C. Bricks and Bags.cpp new file mode 100644 index 0000000..d969999 --- /dev/null +++ b/Codeforces Round 831 (Div. 1 + Div. 2)/C. Bricks and Bags.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1740/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + sortv(a); + ll ans = 0; + + nrep(i, 2, n) { + rmax(ans, a[i] - a[i - 1] + a[i] - a[0]); + } + + rep(i, n - 2) { + rmax(ans, a[i + 1] - a[i] + a[n - 1] - a[i]); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 836 (Div. 2)/C. Almost All Multiples.cpp b/Codeforces Round 836 (Div. 2)/C. Almost All Multiples.cpp new file mode 100644 index 0000000..6efb0d4 --- /dev/null +++ b/Codeforces Round 836 (Div. 2)/C. Almost All Multiples.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/contest/1758/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, x; + cin >> n >> x; + + if (n % x) { + cout << "-1\n"; + return; + } + + vi ans(n); + iota(all(ans), 1); + ans[0] = x; + ans.back() = 1; + + while (x < n) { + for (int i = x * 2; i <= n; i += x) { + if (n % i == 0) { + ans[x - 1] = i; + x = i; + break; + } + } + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 837 (Div. 2)/B. Hossam and Friends.cpp b/Codeforces Round 837 (Div. 2)/B. Hossam and Friends.cpp new file mode 100644 index 0000000..5abe87b --- /dev/null +++ b/Codeforces Round 837 (Div. 2)/B. Hossam and Friends.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1771/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + V> nfriend(n); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + if (a < b) { + swap(a, b); + } + nfriend[a].insert(b); + } + + int l = 0; + ll ans = 0; + rep(i, n) { + auto inv = [&]() { + auto itr = nfriend[i].lower_bound(l); + return itr == nfriend[i].end(); + }; + + while (!inv()) { + l++; + } + + ans += i - l + 1; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 843 (Div. 2)/D. Friendly Spiders.cpp b/Codeforces Round 843 (Div. 2)/D. Friendly Spiders.cpp new file mode 100644 index 0000000..580fdea --- /dev/null +++ b/Codeforces Round 843 (Div. 2)/D. Friendly Spiders.cpp @@ -0,0 +1,232 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1775/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +const int MAXN = 3e5 + 1; + +vi primes; +int di[MAXN]; + +void pre() +{ + fill(di, di + MAXN, 1); + + nrep(i, 2, MAXN) { + if (di[i] != 1) { + continue; + } + + for (int j = i; j < MAXN; j += i) { + di[j] = i; + } + } +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + + vi a(n); + cin >> a; + + int s, t; + cin >> s >> t; + s--, t--; + + if (s == t) { + cout << "1\n"; + cout << s + 1 << '\n'; + return; + } + + if (a[s] == 1 || a[t] == 1) { + cout << "-1\n"; + return; + } + + if (a[s] == a[t]) { + cout << "2\n"; + cout << s + 1 << ' ' << t + 1 << '\n'; + return; + } + + V used(3e5 + 1); + used[a[s]] = true; + used[a[t]] = true; + + vvi graph(MAXN + n); + auto getgraph = [&](int i) { + int now = a[i]; + while (now > 1) { + int prev = di[now]; + while (di[now] == prev) { + now /= di[now]; + } + graph[prev].push_back(i + MAXN); + graph[i + MAXN].push_back(prev); + } + }; + + getgraph(s); + getgraph(t); + + rep(i, n) { + // if ((i != s && a[i] == a[s]) || (i != t && a[i] == a[t])) { + // continue; + // } + // pos[a[i]] = i; + + if (used[a[i]]) { + continue; + } + + used[a[i]] = true; + getgraph(i); + } + + // nrep(i, 2, 3e5 + 1) { + // if (pos[i] == -1) { + // continue; + // } + // + // for (int j = i * 2; j <= 3e5; j += i) { + // if (pos[j] == -1) { + // continue; + // } + // + // graph[pos[i]].push_back(pos[j]); + // graph[pos[j]].push_back(pos[i]); + // } + // } + + vi dis(MAXN + n, oo); + vi parent(MAXN + n, -1); + queue q; + dis[s + MAXN] = 0; + q.push(s + MAXN); + + while (!q.empty()) { + auto i = q.front(); + q.pop(); + + for (auto j : graph[i]) { + if (dis[j] > dis[i] + 1) { + dis[j] = dis[i] + 1; + parent[j] = i; + q.push(j); + } + } + } + + if (dis[t + MAXN] == oo) { + cout << "-1\n"; + return; + } + + vi ans; + int now = t + MAXN; + while (parent[now] != -1) { + if (now >= MAXN) { + ans.push_back(now - MAXN + 1); + } + now = parent[now]; + } + + ans.push_back(s + 1); + reverse(all(ans)); + cout << ans.size() << '\n'; + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 846 (Div. 2)/D. Bit Guessing Game.cpp b/Codeforces Round 846 (Div. 2)/D. Bit Guessing Game.cpp new file mode 100644 index 0000000..419a883 --- /dev/null +++ b/Codeforces Round 846 (Div. 2)/D. Bit Guessing Game.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1780/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int cnt; + cin >> cnt; + + int c = 0; + int t = cnt; + + int ans = 0; + rep(i, 30) { + if (c == t) { + break; + } + + if (cnt == 0) { + break; + } + + cout << "- " << (1 << i) << endl; + int tmp; + cin >> tmp; + + if (tmp >= cnt) { + int diff = tmp - cnt + 1; + while (diff--) { + i++; + } + ans |= 1 << i; + cnt = tmp; + c++; + continue; + } + + ans |= 1 << i; + cnt = tmp; + c++; + } + + cout << "! " << ans << endl; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 847 (Div. 3)/E. Vlad and a Pair of Numbers.cpp b/Codeforces Round 847 (Div. 3)/E. Vlad and a Pair of Numbers.cpp new file mode 100644 index 0000000..128d80e --- /dev/null +++ b/Codeforces Round 847 (Div. 3)/E. Vlad and a Pair of Numbers.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1790/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll x; + cin >> x; + ll prev = x; + + ll a = x; + ll b = 0; + + int log = 31 - __builtin_clz(x); + + for (int i = log; i >= 0; i--) { + int bit1 = (a >> i) & 1; + + if (bit1 || (2LL << i) > x) { + continue; + } + + a |= 1LL << i; + b |= 1LL << i; + x -= 2LL << i; + } + + if (((a + b) >> 1) != prev || (a^b) != prev) { + cout << "-1\n"; + return; + } + + cout << a << ' ' << b << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 857 (Div. 1)/A. The Very Beautiful Blanket.cpp b/Codeforces Round 857 (Div. 1)/A. The Very Beautiful Blanket.cpp new file mode 100644 index 0000000..3b02a39 --- /dev/null +++ b/Codeforces Round 857 (Div. 1)/A. The Very Beautiful Blanket.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1801/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + cout << n * m << '\n'; + + vvl ans(n, vl(m)); + ll cur = 0; + for (int i = 0; i < n; i += 2) { + int now = 0; + for (int j = 0; j < m; j += 2) { + ans[i][j] = now + cur; + if (j < m - 1) { + ans[i][j + 1] = now + cur + 1; + } + + if (i < n - 1) { + ans[i + 1][j] = now + cur + 2; + } + + if (i < n - 1 && j < m - 1) { + ans[i + 1][j + 1] = now + cur + 3; + } + + now += 4; + } + + cur += 1 << 30; + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 858 (Div. 2)/C. Sequence Master.cpp b/Codeforces Round 858 (Div. 2)/C. Sequence Master.cpp new file mode 100644 index 0000000..ac93172 --- /dev/null +++ b/Codeforces Round 858 (Div. 2)/C. Sequence Master.cpp @@ -0,0 +1,144 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1806/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + n <<= 1; + vl p(n); + cin >> p; + + if (n == 2) { + cout << abs(p[0] - p[1]) << '\n'; + return; + } + + ll ans = OO; + + if (n == 4) { + ans = 0; + rep(i, n) { + ans += abs(p[i] - 2); + } + } + + ll total = 0; + rep(i, n) { + total += abs(p[i]); + } + + rmin(ans, total); + if (~n & 2) { + ll act = 0; + rep(i, n) { + act += abs(p[i] + 1); + } + + rep(i, n) { + ll tmp = act - abs(p[i] + 1); + rmin(ans, tmp + abs(p[i] - (n >> 1))); + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 859 (Div. 4)/F. Bouncy Ball.cpp b/Codeforces Round 859 (Div. 4)/F. Bouncy Ball.cpp new file mode 100644 index 0000000..3ca11ce --- /dev/null +++ b/Codeforces Round 859 (Div. 4)/F. Bouncy Ball.cpp @@ -0,0 +1,170 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1807/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m, i1, j1, i2, j2; + string d; + cin >> n >> m >> i1 >> j1 >> i2 >> j2 >> d; + i1--, j1--, i2--, j2--; + + int mask = ((d[0] == 'D') << 1) | (d[1] == 'R'); + + vvvi dis(n, vvi(m, vi(4, oo))); + dis[i1][j1][mask] = 0; + + queue> q; + q.emplace(i1, j1, mask); + + while (!q.empty()) { + auto [i, j, cur] = q.front(); + q.pop(); + + int bounce = 0; + + int prev = dis[i][j][cur]; + + if (cur & 2) { + i++; + } else { + i--; + } + + if (cur & 1) { + j++; + } else { + j--; + } + + if (i < 0) { + i = 1; + cur ^= 2; + bounce |= 1; + } + + if (i >= n) { + i = n - 2; + cur ^= 2; + bounce |= 1; + } + + if (j < 0) { + j = 1; + cur ^= 1; + bounce |= 1; + } + + if (j >= m) { + j = m - 2; + cur ^= 1; + bounce |= 1; + } + + if (dis[i][j][cur] > prev + bounce) { + dis[i][j][cur] = prev + bounce; + q.emplace(i, j, cur); + } + } + + int ans = *min_element(all(dis[i2][j2])); + cout << (ans == oo ? -1 : ans) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 868 (Div. 2)/C. Strongly Composite.cpp b/Codeforces Round 868 (Div. 2)/C. Strongly Composite.cpp new file mode 100644 index 0000000..5eff894 --- /dev/null +++ b/Codeforces Round 868 (Div. 2)/C. Strongly Composite.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/contest/1823/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +const int MAXN = 1e7 + 1; +int divs[MAXN]; + +void pre() +{ + fill(divs, divs + MAXN, 1); + + nrep(i, 1, MAXN) { + if (divs[i] != 1) { + continue; + } + + for (int j = i; j < MAXN; j += i) { + divs[j] = i; + } + } +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + map var; + int maximal = 0; + rep(i, n) { + int no; + cin >> no; + + while (divs[no] != 1) { + var[divs[no]]++; + rmax(maximal, var[divs[no]]); + no /= divs[no]; + } + } + + if (var.size() <= 2 && maximal < 2) { + cout << "0\n"; + return; + } + + int k = 0; + int c = 0; + repv(i, var) { + k += i.second / 2; + c += i.second & 1; + } + + cout << k + c / 3 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 869 (Div. 1)/B. Fish Graph.cpp b/Codeforces Round 869 (Div. 1)/B. Fish Graph.cpp new file mode 100644 index 0000000..b696529 --- /dev/null +++ b/Codeforces Round 869 (Div. 1)/B. Fish Graph.cpp @@ -0,0 +1,245 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1817/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vvi graph(n); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + V vis(n); + + function find = [&](int i, int t, int p) { + vis[i] = true; + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + if (j == t) { + return true; + } + + if (vis[j]) { + continue; + } + + if (find(j, t, i)) { + return true; + } + } + + return false; + }; + + rep(i, n) { + if (graph[i].size() < 4) { + continue; + } + + fill(all(vis), false); + + if (!find(i, i, i)) { + continue; + } + + V> ans; + V loop(n); + + function()> get = [&]() -> pair { + rep(l1, graph[i].size() - 1) { + nrep(l2, l1 + 1, graph[i].size()) { + fill(all(vis), false); + int one = graph[i][l1]; + int two = graph[i][l2]; + function test = [&](int j, int p) { + vis[j] = true; + repv(k, graph[j]) { + if (k == one || k == two) { + continue; + } + + if (k == p) { + continue; + } + + if (k == i) { + loop[j] = true; + return true; + } + + if (vis[k]) { + continue; + } + + if (test(k, j)) { + loop[j] = true; + return true; + } + } + + return false; + }; + + if (test(i, i)) { + return {one, two}; + } + } + } + + return {-1, -1}; + }; + + auto [one, two] = get(); + + ans.emplace_back(i + 1, one + 1); + ans.emplace_back(i + 1, two + 1); + + fill(all(vis), false); + int now = -1; + int p = -1; + int next = i; + while (next != -1) { + now = next; + vis[now] = true; + next = -1; + repv(j, graph[now]) { + if (j == p || !loop[j]) { + continue; + } + + if (j == i) { + ans.emplace_back(i + 1, now + 1); + break; + } + + if (vis[j]) { + continue; + } + + p = now; + next = j; + ans.emplace_back(now + 1, j + 1); + break; + } + } + + cout << "YES\n"; + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i.first << ' ' << i.second << '\n'; + } + return; + } + + cout << "NO\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 870 (Div. 2)/C. Dreaming of Freedom.cpp b/Codeforces Round 870 (Div. 2)/C. Dreaming of Freedom.cpp new file mode 100644 index 0000000..8bdc714 --- /dev/null +++ b/Codeforces Round 870 (Div. 2)/C. Dreaming of Freedom.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/contest/1826/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +const int MAXN = 1e6 + 1; +vi divs[MAXN]; + +void pre() +{ + V prime(MAXN, true); + prime[1] = false; + + nrep(i, 2, MAXN) { + if (!prime[i]) { + continue; + } + + for (int j = i; j < MAXN; j += i) { + prime[j] = false; + divs[j].push_back(i); + } + } +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + repv(i, divs[n]) { + if (i <= m) { + cout << "NO\n"; + return; + } + } + + cout << "YES\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 875 (Div. 1)/A. Copil Copac Draws Trees.cpp b/Codeforces Round 875 (Div. 1)/A. Copil Copac Draws Trees.cpp new file mode 100644 index 0000000..87c1b43 --- /dev/null +++ b/Codeforces Round 875 (Div. 1)/A. Copil Copac Draws Trees.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1830/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + V>> graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].emplace_back(b, i); + graph[b].emplace_back(a, i); + } + + function dfs = [&](int i, int p, int t) { + int ans = 0; + + repv(no, graph[i]) { + auto [j, c] = no; + + if (j == p) { + continue; + } + + if (t > c) { + rmax(ans, dfs(j, i, c) + 1); + } else { + rmax(ans, dfs(j, i, c)); + } + } + + return ans; + }; + + cout << dfs(0, 0, -1) + 1 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 881 (Div. 3)/B. Long Long.cpp b/Codeforces Round 881 (Div. 3)/B. Long Long.cpp new file mode 100644 index 0000000..c9164c5 --- /dev/null +++ b/Codeforces Round 881 (Div. 3)/B. Long Long.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/contest/1843/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + ll sum = 0; + int ans = 0; + int neg = 0; + rep(i, n) { + if (a[i] > 0) { + ans += neg; + neg = 0; + } else { + neg = neg | (a[i] < 0); + } + sum += abs(a[i]); + } + + cout << sum << ' ' << ans + neg << endl; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 881 (Div. 3)/E. Tracking Segments.cpp b/Codeforces Round 881 (Div. 3)/E. Tracking Segments.cpp new file mode 100644 index 0000000..a21800b --- /dev/null +++ b/Codeforces Round 881 (Div. 3)/E. Tracking Segments.cpp @@ -0,0 +1,203 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1843/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +const int MAX = 1e5+10, UPD = 1e5+10, LOG = 18; +const int MAXS = 2*MAX+UPD*LOG; + +namespace perseg { + ll seg[MAXS]; + int rt[UPD], L[MAXS], R[MAXS], cnt, t; + int n, *v; + + ll build(int p, int l, int r) { + if (l == r) return seg[p] = v[l]; + L[p] = cnt++, R[p] = cnt++; + int m = (l+r)/2; + return seg[p] = build(L[p], l, m) + build(R[p], m+1, r); + } + void build(int n2, int* v2) { + n = n2, v = v2; + rt[0] = cnt++; + build(0, 0, n-1); + } + ll query(int a, int b, int p, int l, int r) { + if (b < l or r < a) return 0; + if (a <= l and r <= b) return seg[p]; + int m = (l+r)/2; + return query(a, b, L[p], l, m) + query(a, b, R[p], m+1, r); + } + ll query(int a, int b, int tt) { + return query(a, b, rt[tt], 0, n-1); + } + ll update(int a, int x, int lp, int p, int l, int r) { + if (l == r) return seg[p] = seg[lp]+x; + int m = (l+r)/2; + if (a <= m) + return seg[p] = update(a, x, L[lp], L[p]=cnt++, l, m) + seg[R[p]=R[lp]]; + return seg[p] = seg[L[p]=L[lp]] + update(a, x, R[lp], R[p]=cnt++, m+1, r); + } + int update(int a, int x, int tt=t) { + update(a, x, rt[tt], rt[++t]=cnt++, 0, n-1); + return t; + } +}; + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + perseg::t = 0; + perseg::cnt = 0; + + int n, m; + cin >> n >> m; + + V> p(m); + repv(i, p) { + cin >> i.first >> i.second; + i.first--, i.second--; + } + + vi v2(n); + perseg::build(n, v2.data()); + + int q; + cin >> q; + vi que(q); + vi pref(n + 1); + repv(i, que) { + cin >> i; + i--; + perseg::update(i, 1); + pref[i + 1]++; + } + + rep(i, n) { + pref[i + 1] += pref[i]; + } + + int ans = oo; + repv(seg, p) { + int count = pref[seg.second + 1] - pref[seg.first]; + int lim = ((seg.second - seg.first + 1) >> 1) + 1; + if (count < lim) { + continue; + } + + int low = 1; + int high = q; + int act = q; + while (low <= high) { + int mid = (low + high) >> 1; + + int cur = perseg::query(seg.first, seg.second, mid); + if (cur >= lim) { + act = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + rmin(ans, act); + } + + cout << (ans == oo ? -1 : ans) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 882 (Div. 2)/C. Vampiric Powers, anyone?.cpp b/Codeforces Round 882 (Div. 2)/C. Vampiric Powers, anyone?.cpp new file mode 100644 index 0000000..53431fa --- /dev/null +++ b/Codeforces Round 882 (Div. 2)/C. Vampiric Powers, anyone?.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1847/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + vvi trie(1, vi(2)); + + auto add = [&](int i) { + int now = 0; + + for (int j = 7; j >= 0; j--) { + int bit = (i >> j) & 1; + + if (trie[now][bit] == 0) { + trie[now][bit] = trie.size(); + trie.emplace_back(2); + } + + now = trie[now][bit]; + } + }; + + auto get = [&](int i) { + int ans = 0; + int now = 0; + + for (int j = 7; j >=0; j--) { + int bit = (i >> j) & 1; + + if (trie[now][bit^1] == 0) { + now = trie[now][bit]; + continue; + } + + ans |= 1 << j; + now = trie[now][bit^1]; + } + + return ans; + }; + + int ans = 0; + int xo = 0; + + rep(i, n) { + add(xo); + xo ^= a[i]; + rmax(ans, get(xo)); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 883 (Div. 3)/E2. Rudolf and Snowflakes (hard version).cpp b/Codeforces Round 883 (Div. 3)/E2. Rudolf and Snowflakes (hard version).cpp new file mode 100644 index 0000000..6a4eeec --- /dev/null +++ b/Codeforces Round 883 (Div. 3)/E2. Rudolf and Snowflakes (hard version).cpp @@ -0,0 +1,155 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1846/E2 */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +const int sz = 1e6 + 1; +vl p[sz]; + +void pre() +{ + for (__int128 i = 2; i < sz; i++) { + __int128 cur = i * i; + __int128 sum = i + i * i; + + while (sum <= (ll)1e18) { + p[i].push_back(sum); + cur *= i; + sum += cur; + } + } +} + +#define TEST 1 + +void solve() +{ + ll n; + cin >> n; + + if (n == 1) { + cout << "NO\n"; + return; + } + + n--; + + __int128 at = sqrtl((n * 4 + 1)) / 2.0L - 0.5L; + + if (at > 1 && at + at * at == n) { + cout << "YES\n"; + return; + } + + rep(i, 63) { + int low = 2; + int high = 1e6; + int ans = 2; + while (low <= high) { + int mid = (low + high) >> 1; + + if (p[mid].size() > i && p[mid][i] <= n) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + if (p[ans].size() > i && p[ans][i] == n) { + cout << "YES\n"; + return; + } + } + + cout << "NO\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 885 (Div. 2)/C. Vika and Price Tags.cpp b/Codeforces Round 885 (Div. 2)/C. Vika and Price Tags.cpp new file mode 100644 index 0000000..a2ae231 --- /dev/null +++ b/Codeforces Round 885 (Div. 2)/C. Vika and Price Tags.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/contest/1848/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + vl b(n); + cin >> a >> b; + + V> mo(n); + + rep(i, n) { + if (a[i] == 0 && b[i] == 0) { + mo[i][0] = true; + mo[i][1] = true; + mo[i][2] = true; + continue; + } + + if (a[i] == 0) { + mo[i][0] = true; + continue; + } + + if (b[i] == 0) { + mo[i][2] = true; + continue; + } + + ll d = gcd(a[i], b[i]); + int x = (a[i] / d) & 1; + int y = (b[i] / d) & 1; + + mo[i][x + !y] = true; + } + + array ans = {true, true, true}; + rep(i, n) { + ans[0] = ans[0] && mo[i][0]; + ans[1] = ans[1] && mo[i][1]; + ans[2] = ans[2] && mo[i][2]; + } + + cout << (ans[0] || ans[1] || ans[2] ? "YES\n" : "NO\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 894 (Div. 3)/G. The Great Equalizer.cpp b/Codeforces Round 894 (Div. 3)/G. The Great Equalizer.cpp new file mode 100644 index 0000000..8198fb6 --- /dev/null +++ b/Codeforces Round 894 (Div. 3)/G. The Great Equalizer.cpp @@ -0,0 +1,183 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1862/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + cin >> a; + + map act; + multiset diff; + diff.insert(0); + + repv(i, a) { + act[i]++; + } + + { + auto itr = next(act.begin()); + auto prev = act.begin(); + while (itr != act.end()) { + diff.insert(itr->first - prev->first); + itr++; + prev++; + } + } + + int q; + cin >> q; + while (q--) { + int i; + ll x; + cin >> i >> x; + i--; + + ll p = a[i]; + a[i] = x; + + auto itr = act.find(p); + itr->second--; + + if (itr->second == 0) { + auto ne = next(itr); + + if (itr != act.begin() && ne != act.end()) { + diff.insert(ne->first - prev(itr)->first); + } + + if (itr != act.begin()) { + diff.erase(diff.find(itr->first - prev(itr)->first)); + } + + if (ne != act.end()) { + diff.erase(diff.find(ne->first - itr->first)); + } + + act.erase(itr); + } + + act[x]++; + + itr = act.find(x); + + if (itr->second == 1) { + auto ne = next(itr); + + if (ne != act.end() && itr != act.begin()) { + diff.erase(diff.find(ne->first - prev(itr)->first)); + } + + if (itr != act.begin()) { + diff.insert(itr->first - prev(itr)->first); + } + + if (ne != act.end()) { + diff.insert(ne->first - itr->first); + } + } + + cout << prev(act.end())->first + *prev(diff.end()) << " \n"[q == 0]; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 897 (Div. 2)/D. Cyclic Operations.cpp b/Codeforces Round 897 (Div. 2)/D. Cyclic Operations.cpp new file mode 100644 index 0000000..834356a --- /dev/null +++ b/Codeforces Round 897 (Div. 2)/D. Cyclic Operations.cpp @@ -0,0 +1,173 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1867/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + vi a(n); + repv(i, a) { + cin >> i; + i--; + } + + if (k == 1) { + rep(i, n) { + if (a[i] != i) { + cout << "NO\n"; + return; + } + } + + cout << "YES\n"; + return; + } + + V vis(n); + V frame(n); + + auto findloop = [&](int i) { + int hare = a[a[i]]; + int tortoise = a[i]; + + while (tortoise != hare && !vis[tortoise]) { + hare = a[a[hare]]; + tortoise = a[tortoise]; + } + + if (vis[tortoise]) { + while (!vis[i]) { + vis[i] = true; + i = a[i]; + } + return k; + } + + int count = 1; + hare = a[hare]; + while (hare != tortoise) { + count++; + hare = a[hare]; + } + + while (!vis[i]) { + vis[i] = true; + i = a[i]; + } + + return count; + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + if (findloop(i) != k) { + cout << "NO\n"; + return; + } + } + + cout << "YES\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 899 (Div. 2)/D. Tree XOR.cpp b/Codeforces Round 899 (Div. 2)/D. Tree XOR.cpp new file mode 100644 index 0000000..2c3265f --- /dev/null +++ b/Codeforces Round 899 (Div. 2)/D. Tree XOR.cpp @@ -0,0 +1,172 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1882/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi a(n); + cin >> a; + + 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> dp(n); + vl count(n, 1); + function calc = [&](int i, int p) { + repv(j, graph[i]) { + if (j == p) { + continue; + } + + calc(j, i); + count[i] += count[j]; + + rep(k, 20) { + dp[i][k] += dp[j][k]; + if (((a[i] >> k) & 1) != ((a[j] >> k) & 1)) { + dp[i][k] += (1LL << k) * count[j]; + continue; + } + } + } + }; + + calc(0, 0); + + vl ans(n); + + function reroot = [&](int i, int p) { + ans[i] = accumulate(all(dp[i]), 0LL); + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + count[i] = n - count[j]; + + rep(k, 20) { + if (((a[i] >> k) & 1) != ((a[j] >> k) & 1)) { + dp[j][k] = dp[i][k] + count[i] * (1LL << k) - count[j] * (1LL << k); + continue; + } + + dp[j][k] = dp[i][k]; + } + + reroot(j, i); + } + }; + + reroot(0, 0); + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 900 (Div. 3)/D. Reverse Madness.cpp b/Codeforces Round 900 (Div. 3)/D. Reverse Madness.cpp new file mode 100644 index 0000000..63f6144 --- /dev/null +++ b/Codeforces Round 900 (Div. 3)/D. Reverse Madness.cpp @@ -0,0 +1,162 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1878/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + string s; + cin >> s; + + vi l(k); + vi r(k); + cin >> l >> r; + + vi other(n); + V start(n); + int cur = 0; + rep(i, n) { + while (r[cur] < i + 1) { + cur++; + } + + start[i] = i == l[cur] - 1; + other[i] = r[cur] - i + l[cur] - 2; + other[r[cur] - i + l[cur] - 2] = i; + } + + vi act(n + 1); + + int q; + cin >> q; + while (q--) { + int x; + cin >> x; + + int pos = upper_bound(all(l), x) - l.begin() - 1; + int a = min(x, l[pos] + r[pos] - x) - 1; + int b = max(x, l[pos] + r[pos] - x) - 1; + + if (a == b) { + continue; + } + + act[a] ^= 1; + if (b != r[pos] - 1) { + act[b + 1] ^= 1; + } + } + + cur = 0; + rep(i, n) { + if (start[i]) { + cur = 0; + } + cur ^= act[i]; + if (cur) { + cout << s[other[i]]; + } else { + cout << s[i]; + } + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 908 (Div. 1)/A. Anonymous Informant.cpp b/Codeforces Round 908 (Div. 1)/A. Anonymous Informant.cpp new file mode 100644 index 0000000..85a40df --- /dev/null +++ b/Codeforces Round 908 (Div. 1)/A. Anonymous Informant.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1893/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + vi b(n); + cin >> b; + + vi pos(n, -1); + set> t; + rep(i, n) { + t.insert(b[i]); + if (b[i] > n) { + continue; + } + b[i]--; + // pos[i - b[i] + n * ((i - b[i]) < 0)].push_back(b[i] + 1); + int p = b[i] - i + n * ((b[i] - i) < 0); + b[i]++; + pos[p - b[i] + n * (p - b[i] < 0)] = b[i]; + } + + if ((t.size() == 1 && *t.begin() <= n) || *t.begin() == n) { + cout << "Yes\n"; + return; + } + + int cur = 0; + int c = 0; + int lim = min(k, n); + while (pos[cur] != -1 && c < lim) { + cur += pos[cur]; + cur %= n; + c++; + } + + cout << (c < lim ? "No\n" : "Yes\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 908 (Div. 1)/C. Freedom of Choice.cpp b/Codeforces Round 908 (Div. 1)/C. Freedom of Choice.cpp new file mode 100644 index 0000000..d0a1cdb --- /dev/null +++ b/Codeforces Round 908 (Div. 1)/C. Freedom of Choice.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1893/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 909 (Div. 3)/F. Alex's whims.cpp b/Codeforces Round 909 (Div. 3)/F. Alex's whims.cpp new file mode 100644 index 0000000..c0b4825 --- /dev/null +++ b/Codeforces Round 909 (Div. 3)/F. Alex's whims.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1899/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, q; + cin >> n >> q; + + rep(i, n - 1) { + cout << i + 1 << ' ' << i + 2 << '\n'; + } + + int prev = n - 1; + + while (q--) { + int d; + cin >> d; + + if (d == prev) { + cout << "-1 -1 -1\n"; + continue; + } + + cout << n << ' ' << prev << ' ' << d << '\n'; + prev = d; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 910 (Div. 2)/B. Milena and Admirer.cpp b/Codeforces Round 910 (Div. 2)/B. Milena and Admirer.cpp new file mode 100644 index 0000000..4a95e25 --- /dev/null +++ b/Codeforces Round 910 (Div. 2)/B. Milena and Admirer.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/1898/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + if (n == 1) { + cout << "0\n"; + return; + } + + ll ans = 0; + for (int i = n - 2; i >= 0; i--) { + ll k = (a[i] + a[i + 1] - 1) / a[i + 1]; + ans += k - 1; + a[i] = a[i] / k; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 910 (Div. 2)/D. Absolute Beauty.cpp b/Codeforces Round 910 (Div. 2)/D. Absolute Beauty.cpp new file mode 100644 index 0000000..e94416c --- /dev/null +++ b/Codeforces Round 910 (Div. 2)/D. Absolute Beauty.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1898/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + vl b(n); + cin >> a >> b; + rep(i, n) { + if (a[i] > b[i]) { + swap(a[i], b[i]); + } + } + + ll total = 0; + ll mi = OO; + ll ma = 0; + rep(i, n) { + total += b[i] - a[i]; + rmax(ma, a[i]); + rmin(mi, b[i]); + } + + cout << total + max(ma - mi, 0LL) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 916 (Div. 3)/F. Programming Competition.cpp b/Codeforces Round 916 (Div. 3)/F. Programming Competition.cpp new file mode 100644 index 0000000..50f5a4f --- /dev/null +++ b/Codeforces Round 916 (Div. 3)/F. Programming Competition.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1914/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vvi graph(n); + rep(i, n - 1) { + int now; + cin >> now; + graph[now - 1].push_back(i + 1); + } + + vi size(n + 1); + + function getsize = [&](int i) { + size[i] = 1; + repv(j, graph[i]) { + size[i] += getsize(j); + } + + return size[i]; + }; + + getsize(0); + + function getans = [&](int i, int rem) { + if (size[i] == 1) { + return 0; + } + + int choice = n; + int total = size[i] - 1; + + repv(j, graph[i]) { + if (size[j] > size[choice]) { + choice = j; + } + } + + if (size[choice] * 2 - rem <= total) { + return (total - rem) >> 1; + } + + return total - size[choice] + getans(choice, max(0, rem + total - size[choice] - 1)); + }; + + cout << getans(0, 0) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 919 (Div. 2)/C. Partitioning the Array.cpp b/Codeforces Round 919 (Div. 2)/C. Partitioning the Array.cpp new file mode 100644 index 0000000..c2e88c9 --- /dev/null +++ b/Codeforces Round 919 (Div. 2)/C. Partitioning the Array.cpp @@ -0,0 +1,168 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1920/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +const int MAXN = 2e5 + 1; +vvi divs(MAXN); + +void pre() +{ + nrep(i, 2, MAXN) { + for (int j = i; j < MAXN; j += i) { + divs[j].push_back(i); + } + } +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + vi mods(n); + + int ans = 1; + repv(i, divs[n]) { + int size = n / i; + + rep(j, size) { + int cur = abs(a[j] - a[j + size]); + if (cur == 0) { + cur = -1; + } + + for (int k = j + size; k + size < n; k += size) { + int now = abs(a[k] - a[k + size]); + if (now == 0) { + now = -1; + } + + if (cur == -1 && now != -1) { + cur = now; + continue; + } + + if (cur != -1 && now != -1) { + cur = gcd(cur, now); + } + } + + mods[j] = cur; + } + + int prev = -1; + rep(j, size) { + if (prev == -1) { + prev = mods[j]; + continue; + } + + if (mods[j] == -1) { + continue; + } + + prev = gcd(prev, mods[j]); + } + + if (prev != 1) { + ans++; + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 920 (Div. 3)/E. Eat the Chip.cpp b/Codeforces Round 920 (Div. 3)/E. Eat the Chip.cpp new file mode 100644 index 0000000..cdfcd35 --- /dev/null +++ b/Codeforces Round 920 (Div. 3)/E. Eat the Chip.cpp @@ -0,0 +1,169 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1921/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll h, w, xa, ya, xb, yb; + cin >> h >> w >> xa >> ya >> xb >> yb; + + if (xb <= xa) { + cout << "Draw\n"; + return; + } + + ll diff = xb - xa; + + if (diff & 1) { + int d; + if (yb <= ya) { + d = -1; + } else { + d = 1; + } + + while (xa < xb) { + if (xb == xa + 1 && abs(ya - yb) <= 1) { + cout << "Alice\n"; + return; + } + + xa++; + ya += d; + rmax(ya, 1LL); + rmin(ya, w); + + xb--; + yb += d; + rmax(yb, 1LL); + rmin(yb, w); + } + + cout << "Draw\n"; + return; + } + + int d; + if (yb >= ya) { + d = -1; + } else { + d = 1; + } + + while (xa < xb) { + xa++; + ya += d; + rmax(ya, 1LL); + rmin(ya, w); + + if (xb == xa + 1 && abs(ya - yb) <= 1) { + cout << "Bob\n"; + return; + } + + xb--; + yb += d; + rmax(yb, 1LL); + rmin(yb, w); + } + + cout << "Draw\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 924 (Div. 2)/C. Physical Education Lesson.cpp b/Codeforces Round 924 (Div. 2)/C. Physical Education Lesson.cpp new file mode 100644 index 0000000..88bc80a --- /dev/null +++ b/Codeforces Round 924 (Div. 2)/C. Physical Education Lesson.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1928/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll n, x; + cin >> n >> x; + + set ans; + + ll now = n - x; + if (~now & 1) { + now >>= 1; + + for (ll i = 1; i * i <= now; i++) { + if (now % i == 0) { + ans.insert(i + 1); + ans.insert(now / i + 1); + } + } + } + + now = n + x - 2; + if (~now & 1) { + now >>= 1; + + for (ll i = 1; i * i <= now; i++) { + if (now % i == 0) { + ans.insert(i + 1); + ans.insert(now / i + 1); + } + } + } + + ll act = 0; + repv(i, ans) { + if (i >= x) { + act++; + } + } + + cout << act << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 928 (Div. 4)/E. Vlad and an Odd Ordering.cpp b/Codeforces Round 928 (Div. 4)/E. Vlad and an Odd Ordering.cpp new file mode 100644 index 0000000..068f3f3 --- /dev/null +++ b/Codeforces Round 928 (Div. 4)/E. Vlad and an Odd Ordering.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1926/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll n, k; + cin >> n >> k; + + ll c = (n + 1) >> 1; + if (c >= k) { + cout << (k << 1) - 1 << '\n'; + return; + } + k -= c + 1; + + rep(i, 32) { + ll now = 2LL << i; + + ll tmp = (n - now) / (now << 1); + if (tmp >= k) { + cout << now + (now << 1) * k << '\n'; + return; + } + + k -= tmp + 1; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 930 (Div. 2)/B. Binary Path.cpp b/Codeforces Round 930 (Div. 2)/B. Binary Path.cpp new file mode 100644 index 0000000..13f571b --- /dev/null +++ b/Codeforces Round 930 (Div. 2)/B. Binary Path.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/contest/1937/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + V a(2); + cin >> a; + + string best; + + int cur = 0; + while (cur < n - 1) { + best.push_back(a[0][cur]); + + if (a[0][cur + 1] > a[1][cur]) { + break; + } + cur++; + } + + if (cur == n - 1) { + best.push_back(a[0][n - 1]); + } + + while (cur < n) { + best.push_back(a[1][cur]); + cur++; + } + + vi valid(n); + for (int i = n - 1; i >= 0 && a[1][i] == best[i + 1]; i--) { + valid[i] = 1; + } + + int ans = 0; + for (int i = 0; i < n && a[0][i] == best[i]; i++) { + ans += valid[i]; + } + + cout << best << '\n' << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 940 (Div. 2) and CodeCraft-23/C. How Does the Rook Move?.cpp b/Codeforces Round 940 (Div. 2) and CodeCraft-23/C. How Does the Rook Move?.cpp new file mode 100644 index 0000000..01803a1 --- /dev/null +++ b/Codeforces Round 940 (Div. 2) and CodeCraft-23/C. How Does the Rook Move?.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1957/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +const ll mod = 1e9 + 7; +const int MAXN = 3e5 + 1; +ll dp[MAXN]; + +void pre() +{ + dp[0] = 1; + dp[1] = 1; + nrep(i, 2, MAXN) { + dp[i] = dp[i - 1] + (i - 1) * dp[i - 2] * 2; + dp[i] %= mod; + } + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + int total = n; + + while (k--) { + int r, c; + cin >> r >> c; + + total--; + if (r != c) { + total--; + } + } + + cout << dp[total] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 941 (Div. 1)/A. Everything Nim.cpp b/Codeforces Round 941 (Div. 1)/A. Everything Nim.cpp new file mode 100644 index 0000000..ed045fd --- /dev/null +++ b/Codeforces Round 941 (Div. 1)/A. Everything Nim.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1965/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + int now = 0; + sortv(a); + a.erase(unique(all(a)), a.end()); + int c = 0; + rep(i, a.size()) { + c += a[i] == i + 1; + } + + if (c == a.size()) { + cout << ((a.size() & 1) ? "Alice\n" : "Bob\n"); + return; + } + + cout << ((c & 1) ? "Bob\n" : "Alice\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 941 (Div. 1)/B. Missing Subsequence Sum.cpp b/Codeforces Round 941 (Div. 1)/B. Missing Subsequence Sum.cpp new file mode 100644 index 0000000..dde875d --- /dev/null +++ b/Codeforces Round 941 (Div. 1)/B. Missing Subsequence Sum.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/contest/1965/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + vl ans; + ll now = 1; + while ((now << 1) <= k) { + ans.push_back(now); + now <<= 1; + } + + ans.push_back(k + 1); + ans.push_back(k - now); + ans.push_back(k + now + 1); + now <<= 1; + while (now <= 1e6) { + ans.push_back(now); + now <<= 1; + } + + cout << ans.size() << '\n'; + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 942 (Div. 1)/A. Permutation Counting.cpp b/Codeforces Round 942 (Div. 1)/A. Permutation Counting.cpp new file mode 100644 index 0000000..429eb3e --- /dev/null +++ b/Codeforces Round 942 (Div. 1)/A. Permutation Counting.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1967/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll n; + ll k; + cin >> n >> k; + vl a(n); + cin >> a; + + ll low = 1; + ll high = 1e14; + ll ans = 0; + while (low <= high) { + ll mid = (low + high) >> 1; + ll act = mid - 1; + + ll cost = 0; + ll valid = 0; + ll overflow = 0; + rep(i, n) { + if (cost > k) { + break; + } + + if (a[i] >= mid) { + overflow++; + continue; + } + + cost += act - a[i]; + valid++; + } + + if (cost <= k) { + rmax(ans, act * n + min(k - cost, valid) + overflow - n + 1); + low = mid + 1; + continue; + } + + high = mid - 1; + } + + cout << ans << '\n'; + // cout << ans * n - n + 1 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 942 (Div. 1)/B1. Reverse Card (Easy Version).cpp b/Codeforces Round 942 (Div. 1)/B1. Reverse Card (Easy Version).cpp new file mode 100644 index 0000000..e8e1522 --- /dev/null +++ b/Codeforces Round 942 (Div. 1)/B1. Reverse Card (Easy Version).cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1967/B1 */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +const int MAXN = 2e6 + 1; +vvl d(MAXN); + +void pre() +{ + nrep(i, 1, MAXN) { + for (int j = i; j < MAXN; j += i) { + if ((j + i) % ((ll)i * i) == 0) { + d[j].push_back(i); + } + } + } +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + ll ans = 0; + + nrep(i, 1, n + 1) { + ans += upper_bound(all(d[i]), m) - d[i].begin(); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 946 (Div. 3)/E. Money Buys Happiness.cpp b/Codeforces Round 946 (Div. 3)/E. Money Buys Happiness.cpp new file mode 100644 index 0000000..2bd426b --- /dev/null +++ b/Codeforces Round 946 (Div. 3)/E. Money Buys Happiness.cpp @@ -0,0 +1,145 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1974/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int m; + ll x; + cin >> m >> x; + + V> act(m); + ll maximal = 0; + repv(i, act) { + cin >> i.first >> i.second; + maximal += i.second; + } + + vvl dp(maximal + 1, vl(2, OO)); + dp[0][1] = 0; + + int now = 0; + ll ans = 0; + nrep(i, 1, m + 1) { + int prev = now^1; + + rep(j, maximal + 1) { + if (j < act[i - 1].second) { + dp[j][now] = dp[j][prev]; + } else { + dp[j][now] = min(dp[j][prev], dp[j - act[i - 1].second][prev] + act[i - 1].first); + } + } + + ll cur = x * (i - 1); + for (ll j = maximal; j > 0; j--) { + if (cur >= dp[j][now]) { + rmax(ans, j); + } else { + dp[j][now] = OO; + } + } + + now = prev; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 947 (Div. 1 + Div. 2)/D. Paint the Tree.cpp b/Codeforces Round 947 (Div. 1 + Div. 2)/D. Paint the Tree.cpp new file mode 100644 index 0000000..907c6c9 --- /dev/null +++ b/Codeforces Round 947 (Div. 1 + Div. 2)/D. Paint the Tree.cpp @@ -0,0 +1,191 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1975/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, a, b; + cin >> n >> a >> b; + a--, b--; + + vvi graph(n); + rep(i, n - 1) { + int u, v; + cin >> u >> v; + u--, v--; + graph[u].push_back(v); + graph[v].push_back(u); + } + + auto bfs = [&](vi &dis, int a) { + queue q; + dis[a] = 0; + q.push(a); + + while (!q.empty()) { + int i = q.front(); + q.pop(); + + repv(j, graph[i]) { + if (dis[j] > dis[i] + 1) { + dis[j] = dis[i] + 1; + q.push(j); + } + } + } + }; + + vi disa(n, oo); + vi disb(n, oo); + + bfs(disa, a); + bfs(disb, b); + + vi dp(n); + function calc = [&](int i, int p) { + repv(j, graph[i]) { + if (j == p) { + continue; + } + + rmax(dp[i], calc(j, i) + 1); + } + + return dp[i]; + }; + + calc(0, 0); + + ll ans = OO; + function reroot = [&](int i, int p) { + int first = 0; + int second = 0; + repv(j, graph[i]) { + if (dp[j] >= first) { + second = first; + first = dp[j] + 1; + continue; + } + + rmax(second, dp[j] + 1); + } + + rmin(ans, max(disb[i], disa[i]) + (disa[i] > disb[i] && (disb[i] & 1) != (disa[i] & 1)) + (ll)(n - 1) * 2 - first); + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + if (dp[j] == first - 1) { + dp[i] = second; + } else { + dp[i] = first; + } + + reroot(j, i); + } + }; + + reroot(0, 0); + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 950 (Div. 3)/D. GCD-sequence.cpp b/Codeforces Round 950 (Div. 3)/D. GCD-sequence.cpp new file mode 100644 index 0000000..c174ab2 --- /dev/null +++ b/Codeforces Round 950 (Div. 3)/D. GCD-sequence.cpp @@ -0,0 +1,167 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1980/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + int wrong = -1; + nrep(i, 1, n - 1) { + ll cur = gcd(a[i], a[i + 1]); + ll prev = gcd(a[i - 1], a[i]); + + if (cur < prev) { + wrong = i; + break; + } + } + + if (wrong != -1) { + vl one(n - 1); + vl two(n - 1); + vl three(n - 1); + + int cur1 = 0; + int cur2 = 0; + int cur3 = 0; + + rep(i, n) { + if (i != wrong - 1) { + one[cur1] = a[i]; + cur1++; + } + + if (i != wrong) { + two[cur2] = a[i]; + cur2++; + } + + if (i != wrong + 1) { + three[cur3] = a[i]; + cur3++; + } + } + + auto test = [&](vl &now) { + nrep(i, 1, now.size() - 1) { + ll cur = gcd(now[i], now[i + 1]); + ll prev = gcd(now[i], now[i - 1]); + + if (prev > cur) { + return false; + } + } + + return true; + }; + + if (!test(one) && !test(two) && !test(three)) { + cout << "NO\n"; + return; + } + } + + cout << "YES\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 950 (Div. 3)/E. Permutation of Rows and Columns.cpp b/Codeforces Round 950 (Div. 3)/E. Permutation of Rows and Columns.cpp new file mode 100644 index 0000000..a9c8a37 --- /dev/null +++ b/Codeforces Round 950 (Div. 3)/E. Permutation of Rows and Columns.cpp @@ -0,0 +1,145 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1980/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vvi a(n, vi(m)); + vvi b(n, vi(m)); + cin >> a >> b; + + vi ar(n * m); + vi br(n * m); + vi ac(n * m); + vi bc(n * m); + + rep(i, n) { + rep(j, m) { + ar[a[i][j] - 1] = i; + br[b[i][j] - 1] = i; + ac[a[i][j] - 1] = j; + bc[b[i][j] - 1] = j; + } + } + + vi actr(n, -1); + vi actc(m, -1); + rep(i, n * m) { + if (actr[ar[i]] == -1) { + actr[ar[i]] = br[i]; + } else if (actr[ar[i]] != br[i]) { + cout << "NO\n"; + return; + } + + if (actc[ac[i]] == -1) { + actc[ac[i]] = bc[i]; + } else if (actc[ac[i]] != bc[i]) { + cout << "NO\n"; + return; + } + } + + cout << "YES\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 956 (Div. 2) and ByteRace 2024/C. Have Your Cake and Eat It Too.cpp b/Codeforces Round 956 (Div. 2) and ByteRace 2024/C. Have Your Cake and Eat It Too.cpp new file mode 100644 index 0000000..2db0afa --- /dev/null +++ b/Codeforces Round 956 (Div. 2) and ByteRace 2024/C. Have Your Cake and Eat It Too.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1983/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vvl p(3, vl(n)); + cin >> p[0] >> p[1] >> p[2]; + ll tot = accumulate(all(p[0]), 0LL); + + vvi perm = { + {0, 1, 2}, + {1, 0, 2}, + {1, 2, 0}, + {2, 1, 0}, + {2, 0, 1}, + {0, 2, 1} + }; + + V> act(3); + + auto getans = [&](vi &perm) { + act[perm[0]].first = 1; + + ll now = 0; + int cur = 0; + rep(i, n) { + now += p[perm[cur]][i]; + if (now >= (tot + 2) / 3) { + act[perm[cur]].second = i + 1; + cur++; + now = 0; + if (cur == 3) { + act[perm[cur - 1]].second = n; + break; + } + + act[perm[cur]].first = i + 2; + } + } + + return cur == 3; + }; + + repv(k, perm) { + if (getans(k)) { + cout << act[0].first << ' ' << act[0].second << ' ' << act[1].first << ' ' << act[1].second << ' ' << act[2].first << ' ' << act[2].second << '\n'; + return; + } + } + + cout << "-1\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)/C. Hungry Games.cpp b/Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)/C. Hungry Games.cpp new file mode 100644 index 0000000..1885163 --- /dev/null +++ b/Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)/C. Hungry Games.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1994/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll x; + cin >> n >> x; + vl a(n); + cin >> a; + + vl pref(n + 1); + rep(i, n) { + pref[i + 1] = pref[i] + a[i]; + } + + vl dp(n + 1); + dp[n - 1] = a[n - 1] <= x; + + for (int i = n - 2; i >= 0; i--) { + if (a[i] > x) { + dp[i] = dp[i + 1]; + continue; + } + + int low = i + 1; + int high = n - 1; + int ans = n; + while (low <= high) { + int mid = (low + high) >> 1; + + if (pref[mid + 1] - pref[i] > x) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + if (ans == n) { + dp[i] = n - i; + continue; + } + + dp[i] = dp[ans + 1] + ans - i; + } + + cout << accumulate(all(dp), 0LL) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 970 (Div. 3)/F. Sakurako's Box.cpp b/Codeforces Round 970 (Div. 3)/F. Sakurako's Box.cpp new file mode 100644 index 0000000..d7abbbe --- /dev/null +++ b/Codeforces Round 970 (Div. 3)/F. Sakurako's Box.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2008/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll n; + cin >> n; + vl a(n); + cin >> a; + + const ll mod = 1e9 + 7; + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + + rep(i, 30) { + if ((p >> i) & 1) { + ans = (ans * a) % mod; + } + + a = (a * a) % mod; + } + + return ans; + }; + + ll sum = 0; + ll ans = 0; + + rep(i, n) { + ans += sum * a[i]; + ans %= mod; + sum += a[i]; + sum %= mod; + } + + cout << ans * fpow(n * (n - 1) / 2 % mod, mod - 2) % mod << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 971 (Div. 4)/G1. Yunli's Subarray Queries (easy version).cpp b/Codeforces Round 971 (Div. 4)/G1. Yunli's Subarray Queries (easy version).cpp new file mode 100644 index 0000000..0eb0634 --- /dev/null +++ b/Codeforces Round 971 (Div. 4)/G1. Yunli's Subarray Queries (easy version).cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2009/G1 */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k, q; + cin >> n >> k >> q; + vi a(n); + cin >> a; + + rep(i, n) { + a[i] -= i + 1 - n; + } + + int tmp = (n + 1) << 1; + + int size = 1; + if (tmp > 1) { + size = 1 << (32 - __builtin_clz(tmp - 1)); + } + + vi seg(size << 1); + + auto update = [&](int i, int v) { + i += size; + seg[i] += v; + + for (i >>= 1; i > 0; i >>= 1) { + seg[i] = max(seg[i * 2], seg[i * 2 + 1]); + } + }; + + vi ans(n); + rep(i, k) { + update(a[i], 1); + } + + ans[0] = k - seg[1]; + + nrep(i, k, n) { + update(a[i - k], -1); + update(a[i], 1); + ans[i - k + 1] = k - seg[1]; + } + + while (q--) { + int l, r; + cin >> l >> r; + cout << ans[l - 1] << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 978 (Div. 2)/C. Gerrymandering.cpp b/Codeforces Round 978 (Div. 2)/C. Gerrymandering.cpp new file mode 100644 index 0000000..6976484 --- /dev/null +++ b/Codeforces Round 978 (Div. 2)/C. Gerrymandering.cpp @@ -0,0 +1,158 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2022/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + string a, b; + cin >> a >> b; + + vvi act(2, vi(n)); + rep(i, n) { + act[0][i] += a[i] == 'A'; + act[1][i] += b[i] == 'A'; + } + + vvi memo(n, vi(3, -1)); + + function dp = [&](int i, int s) { + if (i >= n - 1) { + if (s == 1) { + return (act[0][n - 1] + act[0][n - 2] + act[1][n - 1] + 1) / 3; + } + + if (s == 2) { + return (act[0][n - 1] + act[1][n - 1] + act[1][n - 2] + 1) / 3; + } + + return 0; + } + + int &ans = memo[i][s]; + if (ans != -1) { + return ans; + } + + if (s == 1) { + int one = (act[0][i] + act[0][i - 1] + act[0][i + 1] + 1) / 3 + (act[1][i] + act[1][i + 1] + act[1][i + 2] + 1) / 3; + int two = (act[0][i] + act[0][i - 1] + act[1][i] + 1) / 3; + + return ans = max(one + dp(i + 3, s), two + dp(i + 1, 0)); + } + + if (s == 2) { + int one = (act[1][i] + act[1][i - 1] + act[1][i + 1] + 1) / 3 + (act[0][i] + act[0][i + 1] + act[0][i + 2] + 1) / 3; + int two = (act[1][i] + act[1][i - 1] + act[0][i] + 1) / 3; + + return ans = max(one + dp(i + 3, s), two + dp(i + 1, 0)); + } + + int one = (act[0][i] + act[1][i] + act[1][i + 1] + 1) / 3; + int two = (act[1][i] + act[0][i] + act[0][i + 1] + 1) / 3; + int three = (act[0][i] + act[0][i + 1] + act[0][i + 2] + 1) / 3 + (act[1][i] + act[1][i + 1] + act[1][i + 2] + 1) / 3; + + return ans = max({one + dp(i + 2, 1), two + dp(i + 2, 2), three + dp(i + 3, 0)}); + }; + + cout << dp(0, 0) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 981 (Div. 3)/C. Sakurako's Field Trip.cpp b/Codeforces Round 981 (Div. 3)/C. Sakurako's Field Trip.cpp new file mode 100644 index 0000000..d3e4b32 --- /dev/null +++ b/Codeforces Round 981 (Div. 3)/C. Sakurako's Field Trip.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2033/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + vvi dp(n >> 1, vi(2)); + + nrep(i, 1, n >> 1) { + dp[i][0] = min(dp[i - 1][0] + (a[i] == a[i - 1]) + (a[n - i - 1] == a[n - i]), dp[i - 1][1] + (a[i] == a[n - i]) + (a[n - i - 1] == a[i - 1])); + dp[i][1] = min(dp[i - 1][0] + (a[n - i - 1] == a[i - 1]) + (a[i] == a[n - i]), dp[i - 1][1] + (a[n - i - 1] == a[n - i]) + (a[i] == a[i - 1])); + } + + int add = 0; + if (n & 1) { + add = (a[n >> 1] == a[(n >> 1) + 1]) + (a[n >> 1] == a[(n >> 1) - 1]); + } else { + add = (a[n >> 1] == a[(n >> 1) - 1]); + } + + cout << min(dp.back()[0], dp.back()[1]) + add << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 981 (Div. 3)/E. Sakurako, Kosuke, and the Permutation.cpp b/Codeforces Round 981 (Div. 3)/E. Sakurako, Kosuke, and the Permutation.cpp new file mode 100644 index 0000000..818b0c4 --- /dev/null +++ b/Codeforces Round 981 (Div. 3)/E. Sakurako, Kosuke, and the Permutation.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2033/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + vi pos(n); + rep(i, n) { + a[i]--; + pos[a[i]] = i; + } + + int ans = 0; + rep(i, n) { + if (a[i] == i || a[a[i]] == i) { + continue; + } + + int prev = pos[i]; + int nex = pos[a[a[i]]]; + swap(pos[i], pos[a[a[i]]]); + swap(a[prev], a[nex]); + ans++; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 987 (Div. 2)/C. Penchick and BBQ Buns.cpp b/Codeforces Round 987 (Div. 2)/C. Penchick and BBQ Buns.cpp new file mode 100644 index 0000000..75bd341 --- /dev/null +++ b/Codeforces Round 987 (Div. 2)/C. Penchick and BBQ Buns.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/contest/2031/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + if (n & 1 && n < 27) { + cout << "-1\n"; + return; + } + + if (~n & 1) { + rep(i, n >> 1) { + cout << (i + 1) << ' ' << (i + 1) << ' '; + } + cout << '\n'; + return; + } + + vi ans(n); + ans[0] = 1; + ans[9] = 1; + ans[25] = 1; + ans[22] = 1e6; + ans[23] = 1e6 - 1; + ans[24] = 1e6 - 1; + ans[26] = 1e6; + nrep(i, 1, 25) { + if (ans[i - 1] == 0 && ans[i] == 0) { + ans[i - 1] = i + 1; + ans[i] = i + 1; + } + } + for (int i = 28; i < n; i += 2) { + ans[i] = i + 1; + ans[i - 1] = i + 1; + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 988 (Div. 3)/E. Kachina's Favorite Binary String.cpp b/Codeforces Round 988 (Div. 3)/E. Kachina's Favorite Binary String.cpp new file mode 100644 index 0000000..243d8e8 --- /dev/null +++ b/Codeforces Round 988 (Div. 3)/E. Kachina's Favorite Binary String.cpp @@ -0,0 +1,152 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2037/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + auto ask = [&](int a, int b) { + cout << "? " << a << ' ' << b << endl; + ll ans; + cin >> ans; + return ans; + }; + + auto answer = [&](const string &a) { + cout << "! " << a << endl; + }; + + string ans(n, '1'); + + int choice = -1; + ll tmp; + nrep(i, 2, n + 1) { + tmp = ask(1, i); + if (tmp != 0) { + choice = i; + break; + } + } + + if (choice == -1) { + answer("IMPOSSIBLE"); + return; + } + + for (int i = choice - 2; choice - i - 1 <= tmp; i--) { + ans[i] = '0'; + } + + ll prev = tmp; + nrep(i, choice + 1, n + 1) { + tmp = ask(1, i); + if (prev == tmp) { + ans[i - 1] = '0'; + continue; + } + + prev = tmp; + } + + answer(ans); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 990 (Div. 1)/B. Move Back at a Cost.cpp b/Codeforces Round 990 (Div. 1)/B. Move Back at a Cost.cpp new file mode 100644 index 0000000..25e537a --- /dev/null +++ b/Codeforces Round 990 (Div. 1)/B. Move Back at a Cost.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2046/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + vl minimal(n); + minimal.back() = a.back(); + + for (int i = n - 2; i >= 0; i--) { + minimal[i] = min(minimal[i + 1], a[i]); + } + + multiset rec; + vl ans; + + rep(i, n) { + if (!rec.empty()) { + rmin(minimal[i], *rec.begin()); + } + + if (a[i] == minimal[i]) { + ans.push_back(a[i]); + continue; + } + + rec.insert(a[i] + 1); + } + + repv(i, rec) { + ans.push_back(i); + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 992 (Div. 2)/C. Ordered Permutations.cpp b/Codeforces Round 992 (Div. 2)/C. Ordered Permutations.cpp new file mode 100644 index 0000000..22dd9ab --- /dev/null +++ b/Codeforces Round 992 (Div. 2)/C. Ordered Permutations.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/contest/2040/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll k; + cin >> n >> k; + + // if (k == 1) { + // rep(i, n) { + // cout << i + 1 << " \n"[i == n - 1]; + // } + // return; + // } + + if (n < 61 && (1LL << (n - 1)) < k) { + cout << "-1\n"; + return; + } + + vi ans(n); + ordered_set ord; + rep(i, n) { + ord.insert(i + 1); + } + + for (int i = n - 1; i >= 0; i--) { + int now = i; + ll cur = 1; + bool at = false; + while (cur < k) { + now--; + k -= cur; + if (at) { + cur <<= 1; + continue; + } + + at = true; + } + + auto itr = ord.find_by_order(now); + ans[i] = *itr; + ord.erase(itr); + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 992 (Div. 2)/D. Non Prime Tree.cpp b/Codeforces Round 992 (Div. 2)/D. Non Prime Tree.cpp new file mode 100644 index 0000000..b70093e --- /dev/null +++ b/Codeforces Round 992 (Div. 2)/D. Non Prime Tree.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2040/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +const int MAXN = 4e5 + 1; +int prime[MAXN]; + +void pre() +{ + fill(prime, prime + MAXN, 1); + prime[1] = 0; + prime[0] = 0; + + nrep(i, 2, MAXN) { + if (!prime[i]) { + continue; + } + + for (int j = i * 2; j < MAXN; j += i) { + prime[j] = 0; + } + } +} + +#define TEST 1 + +void solve() +{ + 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 now = 2; + vi ans(n); + ans[0] = 1; + + function dfs = [&](int i, int p) { + repv(j, graph[i]) { + if (j == p) { + continue; + } + + while (prime[now - ans[i]]) { + now++; + } + + ans[j] = now; + now++; + dfs(j, i); + } + }; + + dfs(0, 0); + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)/B. Take Your Places!.cpp b/Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)/B. Take Your Places!.cpp new file mode 100644 index 0000000..6343367 --- /dev/null +++ b/Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)/B. Take Your Places!.cpp @@ -0,0 +1,151 @@ +/* Problem URL: https://codeforces.com/contest/1556/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi a(n); + cin >> a; + + vi odd; + vi even; + + rep(i, n) { + if (a[i] & 1) { + odd.push_back(i); + } else { + even.push_back(i); + } + } + + if (abs((int)odd.size() - (int)even.size()) >= 2) { + cout << "-1\n"; + return; + } + + auto getans = [&](vi &act) { + ll cur = 0; + + int now = act.size() >= ((n + 1) >> 1) ? 0 : 1; + repv(i, act) { + cur += abs(i - now); + now += 2; + } + + if ((~n & 1) && act.size() == (n >> 1)) { + now = (now & 1)^1; + ll cur2 = 0; + repv(i, act) { + cur2 += abs(i - now); + now += 2; + } + + return min(cur, cur2); + } + + return cur; + }; + + cout << getans(odd) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/EPIC Institute of Technology Round Summer 2024 (Div. 1 + Div. 2)/D. World is Mine.cpp b/EPIC Institute of Technology Round Summer 2024 (Div. 1 + Div. 2)/D. World is Mine.cpp new file mode 100644 index 0000000..cfab9d7 --- /dev/null +++ b/EPIC Institute of Technology Round Summer 2024 (Div. 1 + Div. 2)/D. World is Mine.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1987/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + vi count(n + 1); + rep(i, n) { + count[a[i]]++; + } + + vvi dp(n + 1, vi(n + 1, oo)); + dp[0][0] = 0; + + nrep(i, 1, n + 1) { + dp[i][0] = 0; + + nrep(j, 1, n + 1) { + if (dp[i - 1][j - 1] + count[i] < i) { + dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + count[i] + 1); + } else { + dp[i][j] = dp[i - 1][j]; + } + } + } + + int ans = n; + rep(i, n + 1) { + if (dp[n][i] != oo) { + ans = n - i; + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/EPIC Institute of Technology Round Summer 2025 (Codeforces Round 1036, Div. 1 + Div. 2)/C. Subset Multiplication.cpp b/EPIC Institute of Technology Round Summer 2025 (Codeforces Round 1036, Div. 1 + Div. 2)/C. Subset Multiplication.cpp new file mode 100644 index 0000000..4e86658 --- /dev/null +++ b/EPIC Institute of Technology Round Summer 2025 (Codeforces Round 1036, Div. 1 + Div. 2)/C. Subset Multiplication.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/2124/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + ll ans = 1; + + nrep(i, 1, n) { + if (a[i] % a[i - 1] == 0) { + continue; + } + + ans = lcm(ans, a[i - 1] / __gcd(a[i], a[i - 1])); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 14/E. Xor-sequences.cpp b/Educational Codeforces Round 14/E. Xor-sequences.cpp new file mode 100644 index 0000000..27bcbe8 --- /dev/null +++ b/Educational Codeforces Round 14/E. Xor-sequences.cpp @@ -0,0 +1,167 @@ +/* Problem URL: https://codeforces.com/contest/691/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + ll k; + cin >> n >> k; + vl a(n); + cin >> a; + + ll mod = 1e9 + 7; + + vvl adj(n, vl(n)); + rep(i, n) { + adj[i][i] = 1; + } + + rep(i, n - 1) { + nrep(j, i + 1, n) { + if (__builtin_popcountll(a[i] ^ a[j]) % 3 == 0) { + adj[i][j] = 1; + adj[j][i] = 1; + } + } + } + + auto mul = [&](vvl &a, vvl &b) { + vvl c(a.size(), vl(b[0].size())); + + rep(i, c.size()) { + rep(j, c[0].size()) { + rep(k, b.size()) { + c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod; + } + } + } + + return c; + }; + + k--; + auto fpow = [&]() { + vvl ans(n, vl(n)); + rep(i, n) { + ans[i][i] = 1; + } + + rep(i, 61) { + if ((k >> i) & 1) { + ans = mul(ans, adj); + } + adj = mul(adj, adj); + } + + return ans; + }; + + auto vet = fpow(); + ll ans = 0; + rep(i, n) { + rep(j, n) { + ans = (ans + vet[i][j]) % mod; + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 144 (Rated for Div. 2)/C. Maximum Set.cpp b/Educational Codeforces Round 144 (Rated for Div. 2)/C. Maximum Set.cpp new file mode 100644 index 0000000..e6a819d --- /dev/null +++ b/Educational Codeforces Round 144 (Rated for Div. 2)/C. Maximum Set.cpp @@ -0,0 +1,171 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1796/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int l, r; + cin >> l >> r; + + ll mod = 998244353; + + ll pot = 1; + int count = 1; + while (l * pot * 2 <= r) { + pot <<= 1; + count++; + } + + if (count == 1) { + cout << "1 " << r - l + 1 << '\n'; + return; + } + + // while (l * pot <= r) { + // ll cur = l * pot >> 1; + // + // ans++; + // if (cur * 3 <= r) { + // ans += count - 1; + // } + // ans %= mod; + // l++; + // } + + ll tmppot = pot >> 1; + + ll low = l; + ll high = r; + ll act = 0; + while (low <= high) { + ll mid = (low + high) >> 1; + + if (mid * tmppot * 3 <= r) { + act = mid - l + 1; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + ll ans = act * (count - 1) % mod; + + low = l; + high = r; + act = 0; + while (low <= high) { + ll mid = (low + high) >> 1; + + if (mid * pot <= r) { + act = mid - l + 1; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + ans += act; + ans %= mod; + + cout << count << ' ' << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 149 (Rated for Div. 2)/D. Bracket Coloring.cpp b/Educational Codeforces Round 149 (Rated for Div. 2)/D. Bracket Coloring.cpp new file mode 100644 index 0000000..05eb35b --- /dev/null +++ b/Educational Codeforces Round 149 (Rated for Div. 2)/D. Bracket Coloring.cpp @@ -0,0 +1,141 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1837/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + string s; + cin >> n >> s; + array c = {}; + repv(i, s) { + i = i == '(' ? 0 : 1; + c[i]++; + } + + if (c[0] != c[1]) { + cout << "-1\n"; + return; + } + + vi ans(n, 1); + int one = s[0]; + int tmp = 0; + bool save = true; + rep(i, n) { + if (s[i] == one) { + if (tmp < 0) { + ans[i] = 2; + } + tmp++; + continue; + } + + tmp--; + if (tmp < 0) { + ans[i] = 2; + save = false; + } + } + + cout << (save ? "1\n" : "2\n"); + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 150 (Rated for Div. 2)/C. Ranom Numbers.cpp b/Educational Codeforces Round 150 (Rated for Div. 2)/C. Ranom Numbers.cpp new file mode 100644 index 0000000..a56a8ee --- /dev/null +++ b/Educational Codeforces Round 150 (Rated for Div. 2)/C. Ranom Numbers.cpp @@ -0,0 +1,214 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1841/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + string a; + cin >> a; + int n = a.size(); + + vvi support(n, vi(4)); + + map var; + + vl val = {1, 10, 100, 1000, 10000}; + + vvi count(n + 1, vi(5)); + vi cul(5); + + vvi valid(5); + vvl rem(n, vl(5)); + + vl cur(n); + + ll total = 0; + + for (int i = n - 1; i >= 0; i--) { + int pos = a[i] - 'A'; + + count[i] = count[i + 1]; + count[i][pos]++; + cul[pos] = i; + + ll calc = accumulate(count[i].begin() + pos + 1, count[i].end(), 0); + if (calc == 0) { + valid[pos].push_back(i); + total += val[pos]; + cur[i] = val[pos]; + continue; + } + + if (calc == 1) { + int it = find(count[i].begin() + pos + 1, count[i].end(), 1) - count[i].begin(); + rem[cul[it]][pos]++; + total -= val[pos]; + cur[i] = -val[pos]; + continue; + } + + total -= val[pos]; + cur[i] = -val[pos]; + } + + rep(i, 4) { + reverse(all(valid[i])); + } + + ll ans = total; + rep(i, n) { + count[i][a[i] - 'A']--; + auto testa = [&]() { + ll now = total - cur[i] + rem[i][0] * 2 + rem[i][1] * 20 + rem[i][2] * 200 + rem[i][3] * 2000; + if (accumulate(count[i].begin() + 1, count[i].end(), 0) > 0) { + return now - 1; + } + return now + 1; + }; + + auto testb = [&]() { + ll now = total - cur[i] + rem[i][1] * 20 + rem[i][2] * 200 + rem[i][3] * 2000; + now -= lower_bound(all(valid[0]), i) - valid[0].begin(); + if (accumulate(count[i].begin() + 2, count[i].end(), 0) > 0) { + return now - 10; + } + return now + 10; + }; + + auto testc = [&]() { + ll now = total - cur[i] + rem[i][2] * 200 + rem[i][3] * 2000; + now -= lower_bound(all(valid[0]), i) - valid[0].begin(); + now -= (lower_bound(all(valid[1]), i) - valid[1].begin()) * 20; + if (accumulate(count[i].begin() + 3, count[i].end(), 0) > 0) { + return now - 100; + } + return now + 100; + }; + + auto testd = [&]() { + ll now = total - cur[i] + rem[i][3] * 2000; + now -= lower_bound(all(valid[0]), i) - valid[0].begin(); + now -= (lower_bound(all(valid[1]), i) - valid[1].begin()) * 20; + now -= (lower_bound(all(valid[2]), i) - valid[2].begin()) * 200; + if (accumulate(count[i].begin() + 4, count[i].end(), 0) > 0) { + return now - 1000; + } + return now + 1000; + }; + + auto teste = [&]() { + ll now = total - cur[i] + 10000; + now -= lower_bound(all(valid[0]), i) - valid[0].begin(); + now -= (lower_bound(all(valid[1]), i) - valid[1].begin()) * 20; + now -= (lower_bound(all(valid[2]), i) - valid[2].begin()) * 200; + now -= (lower_bound(all(valid[3]), i) - valid[3].begin()) * 2000; + return now; + }; + + rmax(ans, testa()); + rmax(ans, testb()); + rmax(ans, testc()); + rmax(ans, testd()); + rmax(ans, teste()); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 156 (Rated for Div. 2)/C. Decreasing String.cpp b/Educational Codeforces Round 156 (Rated for Div. 2)/C. Decreasing String.cpp new file mode 100644 index 0000000..b1532de --- /dev/null +++ b/Educational Codeforces Round 156 (Rated for Div. 2)/C. Decreasing String.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1886/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + string s; + ll pos; + cin >> s >> pos; + pos--; + + int cur = s.size(); + int rem = 0; + while (cur <= pos) { + rem++; + pos -= cur; + cur--; + } + + V r(s.size()); + + vi next; + + stack> t; + rep(i, s.size()) { + while (!t.empty() && t.top().first > s[i]) { + next.push_back(t.top().second); + t.pop(); + } + + t.emplace(s[i], i); + } + + while (!t.empty()) { + next.push_back(t.top().second); + t.pop(); + } + + rep(i, rem) { + r[next[i]] = true; + } + + int now = 0; + int i = 0; + while (now <= pos && i < s.size()) { + if (r[i]) { + i++; + continue; + } + now++; + i++; + } + + cout << s[i - 1]; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } + cout << '\n'; +} diff --git a/Educational Codeforces Round 158 (Rated for Div. 2)/C. Add, Divide and Floor.cpp b/Educational Codeforces Round 158 (Rated for Div. 2)/C. Add, Divide and Floor.cpp new file mode 100644 index 0000000..fb5041a --- /dev/null +++ b/Educational Codeforces Round 158 (Rated for Div. 2)/C. Add, Divide and Floor.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1901/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + ll ma = 0; + ll mi = OO; + rep(i, n) { + rmax(ma, a[i]); + rmin(mi, a[i]); + } + + vl ans; + + while (ma != mi) { + ans.push_back(mi & 1); + ma = (ma + (mi & 1)) >> 1; + mi = (mi + (mi & 1)) >> 1; + } + + cout << ans.size() << '\n'; + if (ans.size() <= n) { + rep(i, ans.size()) { + cout << ans[i] << " \n"[i == ans.size() - 1]; + } + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 172 (Rated for Div. 2)/D. Recommendations.cpp b/Educational Codeforces Round 172 (Rated for Div. 2)/D. Recommendations.cpp new file mode 100644 index 0000000..58a040f --- /dev/null +++ b/Educational Codeforces Round 172 (Rated for Div. 2)/D. Recommendations.cpp @@ -0,0 +1,180 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2042/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + V> fds(n); + rep(i, n) { + auto &[l, r, j] = fds[i]; + cin >> l >> r; + j = i; + } + + sort(all(fds), [&](tuple &a, tuple &b){ + auto [la, ra, ia] = a; + auto [lb, rb, ib] = b; + + if (la == lb) { + return ra > rb; + } + return la < lb; + }); + + set lesser; + lesser.insert(get<1>(fds[0])); + + vl ans(n); + + + nrep(i, 1, n) { + auto [l, r, j] = fds[i]; + + if (i < n - 1 && get<0>(fds[i + 1]) == l && get<1>(fds[i + 1]) == r) { + lesser.insert(r); + continue; + } + + auto itr = lesser.lower_bound(r); + lesser.insert(r); + if (itr == lesser.end()) { + continue; + } + + ans[j] = *itr - r; + } + + sort(all(fds), [&](tuple &a, tuple &b){ + auto [la, ra, ia] = a; + auto [lb, rb, ib] = b; + + if (ra == rb) { + return la < lb; + } + return ra > rb; + }); + + set> greater; + greater.insert(get<0>(fds[0])); + + nrep(i, 1, n) { + auto [l, r, j] = fds[i]; + + if (i < n - 1 && get<0>(fds[i + 1]) == l && get<1>(fds[i + 1]) == r) { + greater.insert(l); + continue; + } + + auto itr = greater.lower_bound(l); + greater.insert(l); + if (itr == greater.end()) { + continue; + } + + ans[j] += l - *itr; + } + + rep(i, n) { + cout << ans[i] << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 173 (Rated for Div. 2)/D. Problem about GCD.cpp b/Educational Codeforces Round 173 (Rated for Div. 2)/D. Problem about GCD.cpp new file mode 100644 index 0000000..8f37e83 --- /dev/null +++ b/Educational Codeforces Round 173 (Rated for Div. 2)/D. Problem about GCD.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2043/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll l, r, g; + cin >> l >> r >> g; + + ll a = (l + g - 1) / g; + ll b = r / g; + + if (b < a) { + cout << "-1 -1\n"; + return; + } + + rep(i, 30) { + if (i > b - a) { + break; + } + + rep(j, i + 1) { + ll acta = a + j; + ll actb = b - (i - j); + + if (gcd(acta, actb) == 1) { + cout << acta * g << ' ' << actb * g << '\n'; + return; + } + } + } + + cout << "-1 -1\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 174 (Rated for Div. 2)/D. Palindrome Shuffle.cpp b/Educational Codeforces Round 174 (Rated for Div. 2)/D. Palindrome Shuffle.cpp new file mode 100644 index 0000000..842e221 --- /dev/null +++ b/Educational Codeforces Round 174 (Rated for Div. 2)/D. Palindrome Shuffle.cpp @@ -0,0 +1,188 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2069/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + string a; + cin >> a; + + int itr = 0; + while (itr < (a.size() >> 1) && a[itr] == a[a.size() - itr - 1]) { + itr++; + } + + if (itr == (a.size() >> 1)) { + cout << "0\n"; + return; + } + + string s = a.substr(itr, a.size() - (itr << 1)); + + vvi pref(s.size() + 1, vi(26)); + vvi suf(s.size() + 1, vi(26)); + + rep(i, s.size()) { + pref[i + 1][s[i] - 'a']++; + + rep(j, 26) { + pref[i + 1][j] += pref[i][j]; + } + } + + for (int i = s.size() - 1; i >= 0; i--) { + suf[i][s[i] - 'a']++; + + rep(j, 26) { + suf[i][j] += suf[i + 1][j]; + } + } + + int ans = s.size(); + + rep(i, s.size()) { + auto check1 = [&]() { + rep(j, 26) { + if (pref[i + 1][j] < suf[i + 1][j]) { + return false; + } + } + return true; + }; + + auto check2 = [&]() { + rep(j, 26) { + if (suf[i][j] < pref[i][j]) { + return false; + } + } + return true; + }; + + if (check1()) { + rmin(ans, i + 1); + } + + if (check2()) { + rmin(ans, (int)s.size() - i); + } + } + + int choice = s.size() >> 1; + while (s[choice] == s[s.size() - choice - 1]) { + choice--; + } + + auto check = [&]() { + rep(i, 26) { + if (pref[choice + 1][i] != suf[s.size() - choice - 1][i]) { + return false; + } + } + return true; + }; + + if (check()) { + rmin(ans, choice + 1); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 175 (Rated for Div. 2)/D. Tree Jumps.cpp b/Educational Codeforces Round 175 (Rated for Div. 2)/D. Tree Jumps.cpp new file mode 100644 index 0000000..4ace5ab --- /dev/null +++ b/Educational Codeforces Round 175 (Rated for Div. 2)/D. Tree Jumps.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2070/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vvi graph(n); + nrep(i, 1, n) { + int p; + cin >> p; + graph[p - 1].push_back(i); + } + + vvi dep; + + function calc = [&](int i, int d) { + if (d >= dep.size()) { + dep.emplace_back(); + } + dep[d].push_back(i); + + repv(j, graph[i]) { + calc(j, d + 1); + } + }; + + calc(0, 0); + + ll mod = 998244353; + vl dp(n, 1); + vl depans(dep.size()); + depans.back() = dep.back().size(); + + for (int i = dep.size() - 2; i > 0; i--) { + repv(j, dep[i]) { + ll sum = 0; + repv(k, graph[j]) { + sum += dp[k]; + sum %= mod; + } + + dp[j] = ((depans[i + 1] - sum + 1) % mod + mod) % mod; + depans[i] = (depans[i] + dp[j]) % mod; + } + } + + cout << (depans[1] + 1) % mod << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 178 (Rated for Div. 2)/D. Array and GCD.cpp b/Educational Codeforces Round 178 (Rated for Div. 2)/D. Array and GCD.cpp new file mode 100644 index 0000000..3829126 --- /dev/null +++ b/Educational Codeforces Round 178 (Rated for Div. 2)/D. Array and GCD.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/contest/2104/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +const int MAXN = 8e6; + +vi primes; +bool nprime[MAXN]; + +void pre() +{ + for (int i = 2; i < MAXN; i++) { + if (nprime[i]) { + continue; + } + + primes.push_back(i); + + for (int j = i * 2; j < MAXN; j += i) { + nprime[j] = true; + } + } +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + cin >> a; + + sort(all(a), greater<>()); + + ll coins = 0; + int ans = 0; + rep(i, n) { + ll diff = a[i] - primes[i]; + + if (diff >= 0) { + coins += diff; + continue; + } + + diff = -diff; + + if (coins < diff) { + ans++; + continue; + } + + coins -= diff; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 183 (Rated for Div. 2)/D. Inversion Value of a Permutation.cpp b/Educational Codeforces Round 183 (Rated for Div. 2)/D. Inversion Value of a Permutation.cpp new file mode 100644 index 0000000..28662a0 --- /dev/null +++ b/Educational Codeforces Round 183 (Rated for Div. 2)/D. Inversion Value of a Permutation.cpp @@ -0,0 +1,171 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2145/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + + vi perm(n); + rep(i, n) { + perm[i] = i + 1; + } + + V>> dp(n + 1, V>(n + 1, V(n * (n + 1) / 2 + 1))); + dp[0][0][0] = true; + V> tmp(n + 1, V(n * (n + 1) / 2 + 1)); + tmp[0][0] = true; + + nrep(i, 1, n + 1) { + for (int j = i; j > 0; j--) { + int sz = i - j + 1; + int sum = sz * (sz + 1) / 2; + + nrep(k, sum, n * (n + 1) / 2 + 1) { + if (tmp[j - 1][k - sum]) { + dp[j][i][k] = true; + tmp[i][k] = true; + } + } + } + } + + int t = n * (n + 1) / 2 - k; + vi sizes; + int l = n; + int r = n; + while (l > 0) { + if (dp[l][r][t]) { + int sz = r - l + 1; + int sum = sz * (sz + 1) / 2; + t -= sum; + sizes.push_back(sz); + l--; + r = l; + continue; + } + + l--; + } + + if (t > 0) { + cout << "0\n"; + return; + } + + int cur = 0; + int c = n; + vi ans(n); + + while (cur < n) { + int now = sizes.back(); + sizes.pop_back(); + + for (int i = now - 1; i >= 0; i--) { + ans[cur + i] = c; + c--; + } + cur += now; + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 184 (Rated for Div. 2)/D1. Removal of a Sequence (Easy Version).cpp b/Educational Codeforces Round 184 (Rated for Div. 2)/D1. Removal of a Sequence (Easy Version).cpp new file mode 100644 index 0000000..ab34458 --- /dev/null +++ b/Educational Codeforces Round 184 (Rated for Div. 2)/D1. Removal of a Sequence (Easy Version).cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/contest/2169/problem/D1 */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + ll x, y, k; + cin >> x >> y >> k; + + ll prev = 0; + rep(i, x + 1) { + ll now = k / y - prev; + k += k / y; + } + + if (k > 1e12) { + cout << "-1\n"; + return; + } + + cout << k << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 185 (Rated for Div. 2)/A. Maximum Neighborhood.cpp b/Educational Codeforces Round 185 (Rated for Div. 2)/A. Maximum Neighborhood.cpp new file mode 100644 index 0000000..b29db17 --- /dev/null +++ b/Educational Codeforces Round 185 (Rated for Div. 2)/A. Maximum Neighborhood.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/contest/2170/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vvi fds(n, vi(n)); + int now = 1; + rep(i, n) { + rep(j, n) { + fds[i][j] = now; + now++; + } + } + + int ans = 0; + rep(i, n) { + rep(j, n) { + int cur = 0; + cur = fds[i][j]; + if (i > 0) { + cur += fds[i - 1][j]; + } + + if (i < n - 1) { + cur += fds[i + 1][j]; + } + + if (j > 0) { + cur += fds[i][j - 1]; + } + + if (j < n - 1) { + cur += fds[i][j + 1]; + } + + rmax(ans, cur); + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 185 (Rated for Div. 2)/B. Addition on a Segment.cpp b/Educational Codeforces Round 185 (Rated for Div. 2)/B. Addition on a Segment.cpp new file mode 100644 index 0000000..b86fb7f --- /dev/null +++ b/Educational Codeforces Round 185 (Rated for Div. 2)/B. Addition on a Segment.cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/contest/2170/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi b(n); + cin >> b; + + vi count(n + 1); + int lim = 0; + rep(i, n) { + count[b[i]]++; + rmax(lim, b[i]); + } + + int ops = n; + while (ops > lim) { + count[lim - 1]++; + count[lim]--; + ops--; + if (count[lim] == 0) { + lim--; + } + } + + cout << n - count[0] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 185 (Rated for Div. 2)/C. Quotient and Remainder.cpp b/Educational Codeforces Round 185 (Rated for Div. 2)/C. Quotient and Remainder.cpp new file mode 100644 index 0000000..4084471 --- /dev/null +++ b/Educational Codeforces Round 185 (Rated for Div. 2)/C. Quotient and Remainder.cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/contest/2170/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll k; + cin >> n >> k; + + vl q(n); + vl r(n); + cin >> q >> r; + + sortv(q); + + // int i = 0; + // int j = 0; + // while (i < n && j < n) { + // ll low = 0; + // ll high = 0 + // } + + multiset s; + rep(i, n) { + s.insert((k - r[i]) / (r[i] + 1)); + } + + int ans = 0; + rep(i, n) { + auto itr = s.lower_bound(q[i]); + if (itr != s.end()) { + s.erase(itr); + ans++; + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 185 (Rated for Div. 2)/D. Almost Roman.cpp b/Educational Codeforces Round 185 (Rated for Div. 2)/D. Almost Roman.cpp new file mode 100644 index 0000000..ceb5e05 --- /dev/null +++ b/Educational Codeforces Round 185 (Rated for Div. 2)/D. Almost Roman.cpp @@ -0,0 +1,211 @@ +/* Problem URL: https://codeforces.com/contest/2170/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, q; + cin >> n >> q; + string a; + cin >> a; + + int cur = 0; + vi mo(n); + int co = 0; + + int ma = 0; + + rep(i, n) { + if (a[i] == '?') { + co++; + if (i > 0 && a[i - 1] == 'I') { + mo[i] |= 1; + ma++; + } + + if (i < n - 1 && a[i + 1] != 'I' && a[i + 1] != '?') { + mo[i] |= 2; + } + + continue; + } + + if (a[i] == 'I') { + cur++; + continue; + } + + if (i > 0 && a[i - 1] == 'I') { + cur -= 2; + } + + if (a[i] == 'X') { + cur += 10; + continue; + } + + cur += 5; + } + + vi po(n + 1); + po[0] = ma * 2; + int cu = 1; + + rep(i, n) { + if (mo[i] == 2) { + po[cu] = po[cu - 1] + 2; + cu++; + } + } + + rep(i, n) { + if (a[i] == '?' && a[i - 1] == '?' && mo[i] == 0 && mo[i - 1] == 0) { + mo[i - 1] |= 4; + mo[i] |= 8; + po[cu] = po[cu - 1] + 2; + cu++; + } + } + + rep(i, n) { + if (mo[i] == 3 || (a[i] == '?' && mo[i] == 0)) { + po[cu] = po[cu - 1]; + cu++; + } + } + + rep(i, n) { + if (mo[i] == 8) { + po[cu] = po[cu - 1] - 2; + cu++; + } + } + + rep(i, n) { + if (mo[i] == 1) { + po[cu] = po[cu - 1] - 2; + cu++; + } + } + + while (cu <= n) { + if (cu > 0) { + po[cu] = po[cu - 1]; + } + cu++; + } + + while (q--) { + int a, b, c; + cin >> c >> b >> a; + + int rem = po[a]; + int tot = co; + + int ans = min(tot, a) + cur; + tot -= min(tot, a); + ans += min(tot, b) * 5; + tot -= min(tot, b); + ans += min(tot, c) * 10; + + cout << ans - rem << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 185 (Rated for Div. 2)/E. Binary Strings and Blocks.cpp b/Educational Codeforces Round 185 (Rated for Div. 2)/E. Binary Strings and Blocks.cpp new file mode 100644 index 0000000..1dac049 --- /dev/null +++ b/Educational Codeforces Round 185 (Rated for Div. 2)/E. Binary Strings and Blocks.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/contest/2170/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 29/E. Turn Off The TV.cpp b/Educational Codeforces Round 29/E. Turn Off The TV.cpp new file mode 100644 index 0000000..e72ad10 --- /dev/null +++ b/Educational Codeforces Round 29/E. Turn Off The TV.cpp @@ -0,0 +1,184 @@ +/* Problem URL: https://codeforces.com/contest/863/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + + V> query(n); + for (auto &[l, r] : query) { + cin >> l >> r; + } + + vi p(n); + rep(i, n) { + p[i] = i; + } + + sort(all(p), [&](int ai, int bi){ + auto [al, ar] = query[ai]; + auto [bl, br] = query[bi]; + + if (al == bl) { + return ar > br; + } + return al < bl; + }); + + vvl sparse(20, vl(n)); + + rep(i, n) { + sparse[0][i] = query[p[i]].second; + } + + nrep(j, 1, 20) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + sparse[j][i] = max(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]); + } + } + + auto mquery = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return max(sparse[log][l], sparse[log][r - (1 << log) + 1]); + }; + + int ans = -1; + + nrep(i, 1, n) { + auto [l, r] = query[p[i]]; + int lim = mquery(0, i - 1); + + if (lim >= r) { + ans = p[i] + 1; + break; + } + + lim++; + + int low = i + 1; + int high = n - 1; + int act = -1; + while (low <= high) { + int mid = (low + high) >> 1; + + if (query[p[mid]].first <= lim) { + act = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + if (act == -1) { + continue; + } + + if (mquery(i + 1, act) >= r) { + ans = p[i] + 1; + break; + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 34 (Rated for Div. 2)/F. Clear The Matrix.cpp b/Educational Codeforces Round 34 (Rated for Div. 2)/F. Clear The Matrix.cpp new file mode 100644 index 0000000..c08a3b7 --- /dev/null +++ b/Educational Codeforces Round 34 (Rated for Div. 2)/F. Clear The Matrix.cpp @@ -0,0 +1,276 @@ +/* Problem URL: https://codeforces.com/contest/903/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + + ll a1, a2, a3, a4; + cin >> a1 >> a2 >> a3 >> a4; + + V a(4); + cin >> a; + + vvi c(4, vi(n)); + + rep(i, 4) { + rep(j, n) { + c[i][j] += a[i][j] == '*'; + + if (i > 0) { + c[i][j] += c[i - 1][j]; + } + + if (j > 0) { + c[i][j] += c[i][j - 1]; + } + + if (i > 0 && j > 0) { + c[i][j] -= c[i - 1][j - 1]; + } + } + } + + auto get = [&](int x1, int y1, int x2, int y2) { + int sum = c[y2][x2]; + + if (x1 > 0) { + sum -= c[y2][x1 - 1]; + } + + if (y1 > 0) { + sum -= c[y1 - 1][x2]; + } + + if (x1 > 0 && y1 > 0) { + sum += c[y1 - 1][x1 - 1]; + } + + return sum; + }; + + auto case1 = [&](int i) { + int sum1 = get(i, 0, i, 0) > 0; + int sum2 = get(i, 1, i, 1) > 0; + int sum3 = get(i, 2, i, 2) > 0; + int sum4 = get(i, 3, i, 3) > 0; + + return a1 * (sum1 + sum2 + sum3 + sum4); + }; + + auto case2 = [&](int i) { + int sum1 = get(i, 0, i + 1, 1) > 0; + int sum2 = get(i, 2, i + 1, 3) > 0; + + return a2 * (sum1 + sum2); + }; + + auto case3 = [&](int i) { + int sum1 = get(i, 0, i + 1, 1) > 0; + int sum2 = get(i, 2, i, 2) > 0; + int sum3 = get(i + 1, 2, i + 1, 2) > 0; + int sum4 = get(i, 3, i, 3) > 0; + int sum5 = get(i + 1, 3, i + 1, 3) > 0; + + return a2 * sum1 + a1 * (sum2 + sum3 + sum4 + sum5); + }; + + auto case4 = [&](int i) { + int sum1 = get(i, 2, i + 1, 3) > 0; + int sum2 = get(i, 0, i, 0) > 0; + int sum3 = get(i + 1, 0, i + 1, 0) > 0; + int sum4 = get(i, 1, i, 1) > 0; + int sum5 = get(i + 1, 1, i + 1, 1) > 0; + + return a2 * sum1 + a1 * (sum2 + sum3 + sum4 + sum5); + }; + + auto case5 = [&](int i) { + int sum1 = get(i, 0, i + 2, 2) > 0; + int sum2 = get(i, 3, i, 3) > 0; + int sum3 = get(i + 1, 3, i + 1, 3) > 0; + int sum4 = get(i + 2, 3, i + 2, 3) > 0; + + return a3 * sum1 + a1 * (sum2 + sum3 + sum4); + }; + + auto case6 = [&](int i) { + int sum1 = get(i, 1, i + 2, 3) > 0; + int sum2 = get(i, 0, i, 0) > 0; + int sum3 = get(i + 1, 0, i + 1, 0) > 0; + int sum4 = get(i + 2, 0, i + 2, 0) > 0; + + return a3 * sum1 + a1 * (sum2 + sum3 + sum4); + }; + + auto case7 = [&](int i) { + int sum1 = get(i, 0, i + 2, 2) > 0; + int sum2 = get(i, 3, i + 1, 3) > 0; + int sum3 = get(i + 2, 3, i + 2, 3) > 0; + + return sum1 * a3 + sum2 * a2 + sum3 * min(a1, a2); + }; + + auto case8 = [&](int i) { + int sum1 = get(i, 0, i + 2, 2) > 0; + int sum2 = get(i + 1, 3, i + 2, 3) > 0; + int sum3 = get(i, 3, i, 3) > 0; + + return sum1 * a3 + sum2 * a2 + sum3 * min(a1, a2); + }; + + auto case9 = [&](int i) { + int sum1 = get(i, 1, i + 2, 3) > 0; + int sum2 = get(i, 0, i + 1, 0) > 0; + int sum3 = get(i + 2, 0, i + 2, 0) > 0; + + return sum1 * a3 + sum2 * a2 + sum3 * min(a1, a2); + }; + + auto case10 = [&](int i) { + int sum1 = get(i, 1, i + 2, 3) > 0; + int sum2 = get(i + 1, 0, i + 2, 0) > 0; + int sum3 = get(i, 0, i, 0) > 0; + + return sum1 * a3 + sum2 * a2 + sum3 * min(a1, a2); + }; + + auto case11 = [&](int i) { + int sum1 = get(i, 0, i + 2, 2) > 0; + int sum2 = get(i, 3, i + 2, 3) > 0; + + return a3 * (sum1 + sum2); + }; + + auto case12 = [&](int i) { + return a4 * (get(i, 0, i + 3, 3) > 0); + }; + + vl dp(n + 1, OO); + dp[0] = 0; + + nrep(i, 1, n + 1) { + rmin(dp[i], dp[i - 1] + case1(i - 1)); + + if (i >= 2) { + rmin(dp[i], dp[i - 2] + case2(i - 2)); + rmin(dp[i], dp[i - 2] + case3(i - 2)); + rmin(dp[i], dp[i - 2] + case4(i - 2)); + } + + if (i >= 3) { + rmin(dp[i], dp[i - 3] + case5(i - 3)); + rmin(dp[i], dp[i - 3] + case6(i - 3)); + rmin(dp[i], dp[i - 3] + case7(i - 3)); + rmin(dp[i], dp[i - 3] + case8(i - 3)); + rmin(dp[i], dp[i - 3] + case9(i - 3)); + rmin(dp[i], dp[i - 3] + case10(i - 3)); + } + + if (i >= 4) { + rmin(dp[i], dp[i - 4] + case11(i - 4)); + } + } + + cout << dp[n] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 74 (Rated for Div. 2)/C. Standard Free2play.cpp b/Educational Codeforces Round 74 (Rated for Div. 2)/C. Standard Free2play.cpp new file mode 100644 index 0000000..43923aa --- /dev/null +++ b/Educational Codeforces Round 74 (Rated for Div. 2)/C. Standard Free2play.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1238/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int h, n; + cin >> h >> n; + + vi p(n); + cin >> p; + + p.push_back(0); + + int ans = 0; + int c = 0; + nrep(i, 1, n + 1) { + if (p[i] == p[i - 1] - 1) { + c++; + continue; + } + + ans += c & 1; + c = 1; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)/C. Divisor Chain.cpp b/Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)/C. Divisor Chain.cpp new file mode 100644 index 0000000..81bd188 --- /dev/null +++ b/Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)/C. Divisor Chain.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/1864/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int x; + cin >> x; + vi ans; + ans.push_back(x); + + while (__builtin_popcount(x) != 1) { + x -= x&(-x); + ans.push_back(x); + } + + while (x > 1) { + x >>= 1; + ans.push_back(x); + } + + cout << ans.size() << '\n'; + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Helvetic Coding Contest 2024 online mirror (teams allowed, unrated)/C2. Game on Tree (Medium).cpp b/Helvetic Coding Contest 2024 online mirror (teams allowed, unrated)/C2. Game on Tree (Medium).cpp new file mode 100644 index 0000000..7b0b9c3 --- /dev/null +++ b/Helvetic Coding Contest 2024 online mirror (teams allowed, unrated)/C2. Game on Tree (Medium).cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1970/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, t; + cin >> n >> t; + + vvi graph(n); + rep(i, n - 1) { + int u, v; + cin >> u >> v; + u--, v--; + + graph[u].push_back(v); + graph[v].push_back(u); + } + + int s; + cin >> s; + + vi memo(n, -1); + + function dp = [&](int i, int p) { + int &ans = memo[i]; + if (ans != -1) { + return ans; + } + + for (auto j : graph[i]) { + if (j == p) { + continue; + } + + if (!dp(j, i)) { + return ans = 1; + } + } + + return ans = 0; + }; + + cout << (dp(s - 1, -1) ? "Ron\n" : "Hermione\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)/D. Kevin and Numbers.cpp b/IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)/D. Kevin and Numbers.cpp new file mode 100644 index 0000000..b8eafbc --- /dev/null +++ b/IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)/D. Kevin and Numbers.cpp @@ -0,0 +1,151 @@ +/* Problem URL: https://codeforces.com/contest/2061/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vl a(n); + vl b(m); + cin >> a >> b; + + map ca; + + rep(i, n) { + ca[a[i]]++; + } + + function dfs = [&](int now) { + if (ca[now] > 0) { + ca[now]--; + return true; + } + + if (now == 1) { + return false; + } + + int one = now >> 1; + int two = (now + 1) >> 1; + + return dfs(one) && dfs(two); + }; + + sort(all(b), greater<>()); + + repv(i, b) { + if (!dfs(i)) { + cout << "No\n"; + return; + } + } + + repv(i, ca) { + if (i.second > 0) { + cout << "No\n"; + return; + } + } + + cout << "Yes\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/ITMO Academy: pilot course - Suffix Array - Step 2/A. Suffix Array - 2.cpp b/ITMO Academy: pilot course - Suffix Array - Step 2/A. Suffix Array - 2.cpp new file mode 100644 index 0000000..0b35de8 --- /dev/null +++ b/ITMO Academy: pilot course - Suffix Array - Step 2/A. Suffix Array - 2.cpp @@ -0,0 +1,172 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/2/2/practice/contest/269103/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string a; + cin >> a; + + a.push_back('$'); + + int n = a.size(); + + vi p(n); + vi c(max(256, n)); + rep(i, n) { + c[a[i]]++; + } + + nrep(i, 1, c.size()) { + c[i] += c[i - 1]; + } + + rep(i, n) { + c[a[i]]--; + p[c[a[i]]] = i; + } + + vi suf(n); + suf[p[0]] = 0; + nrep(i, 1, n) { + suf[p[i]] = suf[p[i - 1]]; + if (a[p[i]] != a[p[i - 1]]) { + suf[p[i]]++; + } + } + + vi pn(n); + vi cn(c.size()); + for (int j = 0; (1 << j) < n; j++) { + rep(i, n) { + pn[i] = p[i] - (1 << j); + pn[i] += n * (pn[i] < 0); + } + + fill(all(c), 0); + + rep(i, n) { + c[suf[pn[i]]]++; + } + + nrep(i, 1, c.size()) { + c[i] += c[i - 1]; + } + + for (int i = n - 1; i >= 0; i--) { + c[suf[pn[i]]]--; + p[c[suf[pn[i]]]] = pn[i]; + } + + cn[p[0]] = 0; + nrep(i, 1, n) { + pair cur = {suf[p[i]], suf[(p[i] + (1 << j)) % n]}; + pair prev = {suf[p[i - 1]], suf[(p[i - 1] + (1 << j)) % n]}; + cn[p[i]] = cn[p[i - 1]]; + if (cur != prev) { + cn[p[i]]++; + } + } + swap(suf, cn); + } + + cout << p; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/ITMO Academy: pilot course - Suffix Array - Step 3/A. Substring Search.cpp b/ITMO Academy: pilot course - Suffix Array - Step 3/A. Substring Search.cpp new file mode 100644 index 0000000..8ffe069 --- /dev/null +++ b/ITMO Academy: pilot course - Suffix Array - Step 3/A. Substring Search.cpp @@ -0,0 +1,217 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/2/3/practice/contest/269118/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string s; + cin >> s; + s.push_back('$'); + + int n = s.size(); + + vi cnt(max(n, 256)); + vi p(n); + + rep(i, n) { + cnt[s[i]]++; + } + + nrep(i, 1, cnt.size()) { + cnt[i] += cnt[i - 1]; + } + + rep(i, n) { + cnt[s[i]]--; + p[cnt[s[i]]] = i; + } + + vi c(n); + nrep(i, 1, n) { + c[p[i]] = c[p[i - 1]]; + if (s[p[i]] != s[p[i - 1]]) { + c[p[i]]++; + } + } + + vi pn(n); + vi cn(n); + for (int j = 0; (1 << j) < n; j++) { + rep(i, n) { + pn[i] = p[i] - (1 << j); + pn[i] += n * (pn[i] < 0); + } + + fill(all(cnt), 0); + + rep(i, n) { + cnt[c[pn[i]]]++; + } + + nrep(i, 1, cnt.size()) { + cnt[i] += cnt[i - 1]; + } + + for (int i = n - 1; i >= 0; i--) { + cnt[c[pn[i]]]--; + p[cnt[c[pn[i]]]] = pn[i]; + } + + cn[p[0]] = 0; + nrep(i, 1, n) { + pair cur = {c[p[i]], c[(p[i] + (1 << j)) % n]}; + pair prev = {c[p[i - 1]], c[(p[i - 1] + (1 << j)) % n]}; + cn[p[i]] = cn[p[i - 1]]; + if (cur != prev) { + cn[p[i]]++; + } + } + + swap(c, cn); + } + + int q; + cin >> q; + while (q--) { + string a; + cin >> a; + + int low = 0; + int high = n - 1; + int ans = -1; + while (low <= high) { + int mid = (low + high) >> 1; + + auto test = [&](string &a) { + int i = 0; + while (i < a.size() && i < s.size() && a[i] == s[(i + p[mid]) % n]) { + i++; + } + + if (i == a.size()) { + return 0; + } + + if (i < s.size() && a[i] > s[(i + p[mid]) % n]) { + return 1; + } + + return -1; + }; + + int now = test(a); + + if (now == 0) { + ans = mid; + break; + } + + if (now == 1) { + low = mid + 1; + continue; + } + + high = mid - 1; + } + + cout << (ans == -1 ? "No\n" : "Yes\n"); + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/ITMO Academy: pilot course - Suffix Array - Step 3/B. Counting Substrings.cpp b/ITMO Academy: pilot course - Suffix Array - Step 3/B. Counting Substrings.cpp new file mode 100644 index 0000000..081a460 --- /dev/null +++ b/ITMO Academy: pilot course - Suffix Array - Step 3/B. Counting Substrings.cpp @@ -0,0 +1,232 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/2/3/practice/contest/269118/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string s; + cin >> s; + + s.push_back('$'); + + int n = s.size(); + + vi cnt(max(n, 256)); + vi p(n); + + rep(i, n) { + cnt[s[i]]++; + } + + nrep(i, 1, cnt.size()) { + cnt[i] += cnt[i - 1]; + } + + rep(i, n) { + cnt[s[i]]--; + p[cnt[s[i]]] = i; + } + + vi c(n); + nrep(i, 1, n) { + c[p[i]] = c[p[i - 1]]; + if (s[p[i]] != s[p[i - 1]]) { + c[p[i]]++; + } + } + + vi cn(n); + vi pn(n); + for (int j = 0; (1 << j) < n; j++) { + rep(i, n) { + pn[i] = p[i] - (1 << j); + pn[i] += n * (pn[i] < 0); + } + + fill(all(cnt), 0); + + rep(i, n) { + cnt[c[pn[i]]]++; + } + + nrep(i, 1, cnt.size()) { + cnt[i] += cnt[i - 1]; + } + + for (int i = n - 1; i >= 0; i--) { + cnt[c[pn[i]]]--; + p[cnt[c[pn[i]]]] = pn[i]; + } + + cn[p[0]] = 0; + nrep(i, 1, n) { + pair cur = {c[p[i]], c[(p[i] + (1 << j)) % n]}; + pair prev = {c[p[i - 1]], c[(p[i - 1] + (1 << j)) % n]}; + + cn[p[i]] = cn[p[i - 1]]; + if (cur != prev) { + cn[p[i]]++; + } + } + + swap(cn, c); + } + + int q; + cin >> q; + while (q--) { + string a; + cin >> a; + + auto comp = [&](int pos) { + int i = 0; + while (i < a.size() && i < s.size() && a[i] == s[(i + p[pos]) % n]) { + i++; + } + + if (i == a.size()) { // equal + return 0; + } + + if (i == s.size() || a[i] > s[(i + p[pos]) % n]) { // bigger + return 1; + } + + return -1; // lesser + }; + + int low = 0; + int high = n - 1; + int ans1 = n; + while (low <= high) { + int mid = (low + high) >> 1; + + int test = comp(mid); + + if (test <= 0) { + ans1 = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + low = 0; + high = n - 1; + int ans2 = n; + while (low <= high) { + int mid = (low + high) >> 1; + + int test = comp(mid); + + if (test < 0) { + ans2 = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans2 - ans1 << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/ITMO Academy: pilot course - Suffix Array - Step 4/A. Suffix Array and LCP.cpp b/ITMO Academy: pilot course - Suffix Array - Step 4/A. Suffix Array and LCP.cpp new file mode 100644 index 0000000..f01b5a3 --- /dev/null +++ b/ITMO Academy: pilot course - Suffix Array - Step 4/A. Suffix Array and LCP.cpp @@ -0,0 +1,196 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/2/4/practice/contest/269119/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string a; + cin >> a; + a.push_back('$'); + int n = a.size(); + + vi suf(n); + vi cnt(max(256, n)); + vi pos(n); + + rep(i, n) { + cnt[a[i]]++; + } + + nrep(i, 1, cnt.size()) { + cnt[i] += cnt[i - 1]; + } + + rep(i, n) { + cnt[a[i]]--; + pos[cnt[a[i]]] = i; + } + + nrep(i, 1, n) { + suf[pos[i]] = suf[pos[i - 1]]; + if (a[pos[i]] != a[pos[i - 1]]) { + suf[pos[i]]++; + } + } + + vi tsuf(n); + vi tpos(n); + for (int j = 0; (1 << j) < n; j++) { + rep(i, n) { + tpos[i] = pos[i] - (1 << j); + tpos[i] += n * (tpos[i] < 0); + } + + fill(all(cnt), 0); + + rep(i, n) { + cnt[suf[tpos[i]]]++; + } + + nrep(i, 1, cnt.size()) { + cnt[i] += cnt[i - 1]; + } + + for (int i = n - 1; i >= 0; i--) { + cnt[suf[tpos[i]]]--; + pos[cnt[suf[tpos[i]]]] = tpos[i]; + } + + tsuf[pos[0]] = 0; + nrep(i, 1, n) { + pair cur = {suf[pos[i]], suf[(pos[i] + (1 << j)) % n]}; + pair prev = {suf[pos[i - 1]], suf[(pos[i - 1] + (1 << j)) % n]}; + + tsuf[pos[i]] = tsuf[pos[i - 1]]; + if (cur != prev) { + tsuf[pos[i]]++; + } + } + + swap(tsuf, suf); + } + + vi inv(n); + rep(i, n) { + inv[pos[i]] = i; + } + + vi lcp(n - 1); + vi skip(n + 1); + rep(i, n - 1) { + int j = inv[i]; + if (j == n - 1) { + continue; + } + j = pos[j + 1]; + + int k = skip[i]; + while (a[i + k] == a[j + k]) { + k++; + } + + lcp[inv[i]] = k; + skip[i + 1] = max(0, k - 1); + } + + cout << pos; + cout << lcp; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Matrix Exponentiation/A. Random Mood.cpp b/Matrix Exponentiation/A. Random Mood.cpp new file mode 100644 index 0000000..44fbc65 --- /dev/null +++ b/Matrix Exponentiation/A. Random Mood.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/gym/102644/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + ll n; + long double p; + cin >> n >> p; + + // long double h = 1; + // long double s = 1; + // + // rep(i, n) { + // long double th = h * (1 - p) + s * p; + // long double ts = h * p + s * (p - 1); + // h = th; + // s = ts; + // } + + V> mat = {{1}, + {0}}; + + auto mul = [&](V> &a, V> &b) { + V> c(a.size(), V(b[0].size())); + + rep(i, c.size()) { + rep(j, c[0].size()) { + rep(k, b.size()) { + c[i][j] += a[i][k] * b[k][j]; + } + } + } + + return c; + }; + + V> prob = {{1 - p, p}, + {p, 1 - p}}; + + rep(i, 30) { + if ((n >> i) & 1) { + mat = mul(prob, mat); + } + + prob = mul(prob, prob); + } + + cout << fixed << setprecision(10) << mat[0][0] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Matrix Exponentiation/B. String Mood.cpp b/Matrix Exponentiation/B. String Mood.cpp new file mode 100644 index 0000000..4d740c9 --- /dev/null +++ b/Matrix Exponentiation/B. String Mood.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/gym/102644/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + const ll mod = 1e9 + 7; + + ll n; + cin >> n; + + // ll happy = 1; + // ll sad = 0; + // + // rep(i, n) { + // ll nhappy = (happy * 19 + sad * 6) % mod; + // ll nsad = (happy * 7 + sad * 20) % mod; + // happy = nhappy; + // sad = nsad; + // } + + auto mul = [&](vvl &a, vvl &b) { + vvl c(a.size(), vl(b[0].size())); + + rep(i, c.size()) { + rep(j, c[0].size()) { + rep(k, b.size()) { + c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod; + } + } + } + + return c; + }; + + vvl mat = {{1}, {0}}; + vvl tr = {{19, 6}, {7, 20}}; + + rep(i, 63) { + if ((n >> i) & 1) { + mat = mul(tr, mat); + } + + tr = mul(tr, tr); + } + + cout << mat[0][0] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Pinely Round 2 (Div. 1 + Div. 2)/D. Two-Colored Dominoes.cpp b/Pinely Round 2 (Div. 1 + Div. 2)/D. Two-Colored Dominoes.cpp new file mode 100644 index 0000000..e85fc52 --- /dev/null +++ b/Pinely Round 2 (Div. 1 + Div. 2)/D. Two-Colored Dominoes.cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1863/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + V a(n); + cin >> a; + + rep(i, n) { + int c = 0; + rep(j, m) { + c += a[i][j] != '.'; + } + + if (c & 1) { + cout << "-1\n"; + return; + } + } + + rep(i, m) { + int c = 0; + rep(j, n) { + c += a[j][i] != '.'; + } + + if (c & 1) { + cout << "-1\n"; + return; + } + } + + V ans(n, string(m, '.')); + + array op = {'W', 'B'}; + + rep(i, n) { + int now = 0; + rep(j, m) { + if (a[i][j] == 'U') { + ans[i][j] = op[now]; + ans[i + 1][j] = op[now ^ 1]; + now ^= 1; + } + } + } + + rep(i, m) { + int now = 0; + rep(j, n) { + if (a[j][i] == 'L') { + ans[j][i] = op[now]; + ans[j][i + 1] = op[now ^ 1]; + now ^= 1; + } + } + } + + repv(i, ans) { + cout << i << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)/D. Same Count One.cpp b/Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)/D. Same Count One.cpp new file mode 100644 index 0000000..e5f2b73 --- /dev/null +++ b/Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)/D. Same Count One.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1774/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + vvi a(n, vi(m)); + cin >> a; + + vi count(n); + + int c = 0; + int prev = 0; + rep(i, n) { + rep(j, m) { + c += a[i][j]; + } + + count[i] = c - prev; + prev = c; + } + + if (c % n != 0) { + cout << "-1\n"; + return; + } + + int lim = c / n; + + V> ans; + + rep(i, m) { + vi high; + vi low; + rep(j, n) { + if (count[j] > lim && a[j][i] == 1) { + high.push_back(j); + } + if (count[j] < lim && a[j][i] == 0) { + low.push_back(j); + } + } + + rep(j, min(high.size(), low.size())) { + count[high[j]]--; + count[low[j]]++; + ans.emplace_back(high[j] + 1, low[j] + 1, i + 1); + } + } + + cout << ans.size() << '\n'; + repv(i, ans) { + auto [u, v, x] = i; + cout << u << ' ' << v << ' ' << x << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)/D. Division Versus Addition.cpp b/Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)/D. Division Versus Addition.cpp new file mode 100644 index 0000000..7f81361 --- /dev/null +++ b/Squarepoint Challenge (Codeforces Round 1055, Div. 1 + Div. 2)/D. Division Versus Addition.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2152/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, q; + cin >> n >> q; + + vl a(n); + cin >> a; + + vl pref(n + 1); + vi valid(n + 1); + rep(i, n) { + auto ispot = __builtin_popcount(a[i]) == 1; + auto ispotp = __builtin_popcount(a[i]^1) == 1; + pref[i + 1] = pref[i] + (31 - __builtin_clz(a[i])) + (!ispot && !ispotp); + valid[i + 1] = valid[i] + (!ispot && ispotp); + } + + while (q--) { + int l, r; + cin >> l >> r; + l--, r--; + + cout << pref[r + 1] - pref[l] + ((valid[r + 1] - valid[l]) >> 1) << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/TODO.md b/TODO.md index 271ad6a..1c5e2fa 100644 --- a/TODO.md +++ b/TODO.md @@ -37,9 +37,24 @@ Official divs from codeforces Another problem that I didn't have time to finish... + - [ ] [D. Smithing Skill](https://codeforces.com/problemset/problem/1989/D) + + Yet another problem I couldn't solve on time. + + - [ ] [D1. Removal of a Sequence (Easy Version)](https://codeforces.com/contest/2169/problem/D1) + + A josephus problem, I'm gonna need to remember how it works to maka this. + +- Global + + - [ ] [E. Journey](https://codeforces.com/contest/2164/problem/E) + + A problem from the global round that I couldn't do during the contest, it's seems to be about + reconstruction tree, I'll have to see about it. + - Div 1/2 - - [ ] [D. Division Versus Addition](https://codeforces.com/contest/2152/problem/D) + - [X] [D. Division Versus Addition](https://codeforces.com/contest/2152/problem/D) The idea is simple, but I have to think through it better. @@ -72,6 +87,24 @@ Official divs from codeforces - [ ] [D1. Hot Start Up (easy version)](https://codeforces.com/contest/1799/problem/D1) DP, but couldn't do during the simulation... + + - [ ] [D. Birthday Gift](https://codeforces.com/problemset/problem/1946/D) + + Another interesting problem that I couldn't do on time. + + - [X] [C. How Does the Rook Move?](https://codeforces.com/problemset/problem/1957/C) + + Combinatory problem, managed to get close to the answer but apparently the equation is still + wrong, need to find the error. + + - [X] [C2. Hacking Numbers (Medium Version)](https://codeforces.com/contest/2109/problem/C2) + + Math problem, I feel like I'm closer to an answer. + + - [ ] [C. Freedom of Choice](https://codeforces.com/problemset/problem/1893/C) + + Doesn't look impossible, but I'm not sure what to do on it, I'll check it out + in the future. - Div 3 @@ -82,7 +115,16 @@ Official divs from codeforces - [ ] [XORificator 3000](https://codeforces.com/problemset/problem/2036/F) I had an idea, but didn't have time to implement it. + + - [X] [F. Programming Competition](https://codeforces.com/problemset/problem/1914/F) + Lack of time... Again. + + - [ ] [F. Rae Taylor and Trees (hard version)](https://codeforces.com/problemset/problem/2171/F) + + I tried doing it during a simulation but my head didn't want to help me, + I have to do it eventually when I feel better. + - Div 4 - [ ] [F. Gravity Falls](https://codeforces.com/contest/2148/problem/F) @@ -106,7 +148,7 @@ Gym from simulations or contests that I did - [ ] L. Last Guess - I think I can solve it with flow. + I think I can solve it with max flow. - USP Try-outs @@ -124,8 +166,24 @@ Gym from simulations or contests that I did I'll be honest, I just got bored in the middle. +- 2022-2023 ICPC, NERC, Northern Eurasia Onsite (Unrated, Online Mirror, ICPC Rules, Teams Preferred) + + - [ ] [A. Amazing Trick](https://codeforces.com/problemset/problem/1773/A) + + Once again, didn't finish in time, I didn't even read it to be honest. + +- Bubble Cup 9 - Finals [Online Mirror] + + - [ ] [D. Dexterina’s Lab](https://codeforces.com/contest/717/problem/D) + + Got recomended to me. + ## Atcoder - [ ] [F. Operate K](https://atcoder.jp/contests/abc386/tasks/abc386_f) DP with optimization. + +- [ ] [F - Pyramid Alignment](https://atcoder.jp/contests/abc428/tasks/abc428_f) + + Looks like a good implementation problem. diff --git a/Testing Round 19 (Div. 3)/C1. Message Transmission Error (easy version).cpp b/Testing Round 19 (Div. 3)/C1. Message Transmission Error (easy version).cpp new file mode 100644 index 0000000..6589ab4 --- /dev/null +++ b/Testing Round 19 (Div. 3)/C1. Message Transmission Error (easy version).cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2010/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string t; + cin >> t; + + nrep(i, 1, (t.size() + 1) >> 1) { + string a = t.substr(i, t.size() - i); + string b = t.substr(0, t.size() - i); + + if (a == b) { + cout << "YES\n"; + cout << a << '\n'; + return; + } + } + + cout << "NO\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Testing Round 20 (Unrated, Communication Problems)/A1. Encode and Decode (Easy Version).cpp b/Testing Round 20 (Unrated, Communication Problems)/A1. Encode and Decode (Easy Version).cpp new file mode 100644 index 0000000..da904e7 --- /dev/null +++ b/Testing Round 20 (Unrated, Communication Problems)/A1. Encode and Decode (Easy Version).cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/contest/2168/problem/A1 */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string op; + cin >> op; + + if (op[0] == 'f') { + int n; + cin >> n; + vi a(n); + cin >> a; + + string act; + rep(i, n) { + act.push_back(a[i] + 'a' - 1); + } + cout << act << '\n'; + return; + } + + string act; + cin >> act; + + cout << act.size() << '\n'; + rep(i, act.size()) { + cout << act[i] - 'a' + 1 << " \n"[i == act.size() - 1]; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Testing Round 20 (Unrated, Communication Problems)/A2. Encode and Decode (Hard Version).cpp b/Testing Round 20 (Unrated, Communication Problems)/A2. Encode and Decode (Hard Version).cpp new file mode 100644 index 0000000..6328de8 --- /dev/null +++ b/Testing Round 20 (Unrated, Communication Problems)/A2. Encode and Decode (Hard Version).cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/contest/2168/problem/A2 */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string op; + cin >> op; + if (op[0] == 'f') { + int n; + cin >> n; + + string act; + rep(i, n) { + int now; + cin >> now; + + string tmp; + + while (now > 0) { + tmp.push_back(now % 10 + 'a'); + now /= 10; + } + + reverse(all(tmp)); + act += tmp; + if (tmp.size() < 10) { + act += 'z'; + } + } + + cout << act << '\n'; + return; + } + + string act; + cin >> act; + + vi ans; + + int i = 0; + while (i < act.size()) { + int now = 0; + int tmp = i; + while (act[i] != 'z' && i - tmp < 10) { + now *= 10; + now += act[i] - 'a'; + i++; + } + ans.push_back(now); + if (i - tmp < 10) { + i++; + } + } + + cout << ans.size() << '\n'; + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Testing Round 20 (Unrated, Communication Problems)/B. Locate.cpp b/Testing Round 20 (Unrated, Communication Problems)/B. Locate.cpp new file mode 100644 index 0000000..de20c2f --- /dev/null +++ b/Testing Round 20 (Unrated, Communication Problems)/B. Locate.cpp @@ -0,0 +1,181 @@ +/* Problem URL: https://codeforces.com/contest/2168/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve1() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + int pos1; + int pos2; + + rep(i, n) { + if (a[i] == 1) { + pos1 = i; + continue; + } + + if (a[i] == n) { + pos2 = i; + } + } + + cout << (pos1 < pos2) << '\n'; +} + +void solve2() +{ + int n, x; + cin >> n >> x; + + int low = 0; + int high = n - 1; + int l = 1; + while (low <= high) { + int mid = (low + high) >> 1; + + cout << "? " << mid + 1 << ' ' << n << endl; + int ans; + cin >> ans; + + if (ans == n - 1) { + l = mid + 1; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + low = 0; + high = n - 1; + int r = n; + while (low <= high) { + int mid = (low + high) >> 1; + + cout << "? 1 " << mid + 1 << endl; + int ans; + cin >> ans; + + if (ans == n - 1) { + r = mid + 1; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + if (x) { + cout << "! " << r << endl; + return; + } + + cout << "! " << l << endl; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + void (*solve[])() = {solve1, solve2}; + string op; + cin >> op; + bool s = op[0] != 'f'; + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve[s](); + } +} diff --git a/Testing Round 20 (Unrated, Communication Problems)/C. Intercepting Butterflies.cpp b/Testing Round 20 (Unrated, Communication Problems)/C. Intercepting Butterflies.cpp new file mode 100644 index 0000000..39905de --- /dev/null +++ b/Testing Round 20 (Unrated, Communication Problems)/C. Intercepting Butterflies.cpp @@ -0,0 +1,181 @@ +/* Problem URL: https://codeforces.com/contest/2168/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve1() +{ + int x; + cin >> x; + x--; + + int xo = 0; + set all; + rep(i, 15) { + int bit = (x >> i) & 1; + if (bit) { + all.insert(i + 1); + xo ^= i + 1; + } + } + + int pa = __builtin_popcount(xo) & 1; + rep(j, 4) { + if ((xo >> j) & 1) { + all.insert(j + 16); + } + } + + if (pa) { + all.insert(20); + } + + cout << all.size() << '\n'; + repv(i, all) { + cout << i << ' '; + } + cout << '\n'; +} + +void solve2() +{ + int n; + cin >> n; + + int act = 0; + int xo = 0; + int tmp = 0; + int pa = 0; + rep(i, n) { + int bit; + cin >> bit; + if (bit <= 15) { + act |= (1 << (bit - 1)); + tmp ^= bit; + continue; + } + + if (bit < 20) { + xo |= (1 << (bit - 16)); + continue; + } + + pa = 1; + } + + if ((__builtin_popcount(xo) & 1) != pa) { + cout << act + 1 << '\n'; + return; + } + + if ((tmp ^ xo) == 0) { + cout << act + 1 << '\n'; + return; + } + + act ^= 1 << ((tmp ^ xo) - 1); + cout << act + 1 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + string op; + cin >> op; + bool c = op[0] != 'f'; + + void (*solve[])() = {solve1, solve2}; + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve[c](); + } +} diff --git a/VK Cup 2022 - Отборочный раунд (Engine)/D. Many Perfect Squares.cpp b/VK Cup 2022 - Отборочный раунд (Engine)/D. Many Perfect Squares.cpp new file mode 100644 index 0000000..56cec3b --- /dev/null +++ b/VK Cup 2022 - Отборочный раунд (Engine)/D. Many Perfect Squares.cpp @@ -0,0 +1,162 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1781/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + set cand; + + auto getcand = [&](ll a, ll b) { + ll diff = b - a; + for (ll i = 1; i * i <= diff; i++) { + if (diff % i == 0) { + ll p2 = (diff / i - i); + ll q2 = (diff / i + i); + + if (p2 < 0 || q2 < 0 || (p2 & 1) || (q2 & 1)) { + continue; + } + + p2 >>= 1; + q2 >>= 1; + + cand.insert(p2 * p2 - a); + cand.insert(q2 * q2 - b); + } + } + }; + + rep(i, n - 1) { + nrep(j, i + 1, n) { + getcand(a[i], a[j]); + } + } + + cand.insert(0); + + auto issquare = [&](ll a) { + ll lim = sqrtl(a); + return lim * lim == a; + }; + + int ans = 1; + repv(now, cand) { + if (now < 0) { + continue; + } + + int cur = 0; + rep(j, n) { + if (issquare(a[j] + now)) { + cur++; + } + } + + rmax(ans, cur); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/A. Analyzing the Race.cpp b/XIII UnB Contest Mirror/A. Analyzing the Race.cpp new file mode 100644 index 0000000..b167e0c --- /dev/null +++ b/XIII UnB Contest Mirror/A. Analyzing the Race.cpp @@ -0,0 +1,209 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +ll mod = 998244353; + +#define TEST 0 + +void solve() +{ + ll n, m; + cin >> n >> m; + string s; + cin >> s; + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + rep(i, 61) { + if ((p >> i) & 1) { + ans *= a; + ans %= mod; + } + + a *= a; + a %= mod; + } + + return ans; + }; + + // ll total = fpow(26, n - m) * (n - m + 1) % mod; + // // cout << fpow(26, n - m) * (n - m + 1) % mod << '\n'; + // // cout << fpow(26, n - m) << '\n'; + // + // nrep(i, 1, m - 1) { + // if (s.substr(i, m - 1) != s.substr(0, m - i)) { + // continue; + // } + // + // string tmp1 = s.substr(0, i); + // string tmp2 = s.substr(m - i); + // reverse(all(tmp2)); + // if (tmp1 != tmp2) { + // continue; + // } + // + // int size = m + i; + // if (size > n) { + // break; + // } + // + // total -= fpow(26, n - size) * (n - size + 1) % mod; + // total += mod * (total < 0); + // } + // + // if (2 * m > n) { + // cout << total << '\n'; + // return; + // } + // + // ll rem = fpow(26, n - m * 2); + // + // + // cout << total << '\n'; + + // ll total = fpow(26, n); + // rep(i, m) { + // ll now = fpow(26, i); + // ll cur = n - i; + // + // ll inv = (cur + m - 1) / m % mod; + // ll val = (cur / m * (m - 1) + max(cur % m - 1, 0LL)) % mod; + // + // total -= now * fpow(25, inv) % mod * fpow(26, val) % mod; + // total += mod * (total < 0); + // } + // + // cout << total << '\n'; + + vl fact(101); + fact[0] = 1; + nrep(i, 1, 101) { + fact[i] = (fact[i - 1] * i) % mod; + } + + vl inv(101); + inv[0] = 1; + inv[1] = 1; + nrep(i, 2, 101) { + inv[i] = (mod - mod / i) * inv[mod % i] % mod; + } + nrep(i, 2, 101) { + inv[i] = (inv[i] * inv[i - 1]) % mod; + } + + auto comb = [&](ll n, ll k) { + return fact[n] * inv[k] % mod * inv[n - k] % mod; + }; + + auto comb2 = [&](ll n, ll k) { + ll ans = n; + ll div = 1; + nrep(i, 2, k + 1) { + ans = (ans * (n - i + 1)) % mod; + div = (div * i) % mod; + } + + return ans * fpow(div, mod - 2) % mod; + }; + + ll rem = 0; + ll cur = (n + m - 1) / m; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/B. Bauru.cpp b/XIII UnB Contest Mirror/B. Bauru.cpp new file mode 100644 index 0000000..ced0061 --- /dev/null +++ b/XIII UnB Contest Mirror/B. Bauru.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + string tmp; + cin >> tmp; + + int ans = 0; + string a; + string b; + + rep(i, tmp.size() - 1) { + string tmpa = tmp.substr(0, i + 1); + string tmpb = tmp.substr(i + 1); + + if (tmpa != to_string(stoi(tmpa)) || tmpb != to_string(stoi(tmpb))) { + continue; + } + + int ta = stoi(tmpa); + int tb = stoi(tmpb); + + if (ta <= 0 || ta > 99 || tb <= 0 || tb > 99) { + continue; + } + + ans++; + a = tmpa; + b = tmpb; + } + + if (ans > 1) { + cout << "-1\n"; + return; + } + + cout << a << ' ' << b << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/C. Creating a Playlist.cpp b/XIII UnB Contest Mirror/C. Creating a Playlist.cpp new file mode 100644 index 0000000..5c229ea --- /dev/null +++ b/XIII UnB Contest Mirror/C. Creating a Playlist.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, k; + cin >> n >> k; + vl a(n); + cin >> a; + + vl dp(n + 1); + + for (int i = n - 1; i >= 0; i--) { + dp[i] = max(dp[i + 1], dp[min(i + k, n)] + a[i]); + } + + cout << dp[0] << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/D. Shift Cipher.cpp b/XIII UnB Contest Mirror/D. Shift Cipher.cpp new file mode 100644 index 0000000..e2896fd --- /dev/null +++ b/XIII UnB Contest Mirror/D. Shift Cipher.cpp @@ -0,0 +1,187 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +struct hashing { + static vl p; + static ll m; + static ll b; + vl hash; + + hashing(vi &now) { + while (p.size() <= now.size()) { + p.push_back(((__int128)p.back() * b % m)); + } + + hash.push_back(0); + rep(i, now.size()) { + hash.push_back(((__int128)hash.back() * b % m + now[i]) % m); + } + } + + ll gethash(int l, int r) { + return (((__int128)hash[r + 1] - (__int128)hash[l] * p[r - l + 1]) % m + m) % m; + } +}; + +vl hashing::p = {1}; +ll hashing::m = (1LL << 61) - 1; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll hashing::b = uniform_int_distribution(1000, m - 1)(rng); + +void solve() +{ + int n, m; + cin >> n >> m; + + vi a(n); + vi b(m); + cin >> a >> b; + + vi diffa(n - 1); + vi diffb(m - 1); + + rep(i, n - 1) { + if (a[i] <= a[i + 1]) { + diffa[i] = a[i + 1] - a[i]; + } else { + diffa[i] = a[i + 1] - a[i] + 10000; + } + } + + rep(i, m - 1) { + if (b[i] <= b[i + 1]) { + diffb[i] = b[i + 1] - b[i]; + } else { + diffb[i] = b[i + 1] - b[i] + 10000; + } + } + + vi all; + hashing ah(diffa); + hashing bh(diffb); + + rep(i, n - m + 1) { + if (bh.gethash(0, m - 2) == ah.gethash(i, i + m - 2)) { + all.push_back(i); + } + } + + vi ans(10000); + repv(i, all) { + if (a[i] <= b[0]) { + ans[b[0] - a[i]]++; + continue; + } + ans[b[0] - a[i] + 10000]++; + } + + int maximal = 0; + int choice = 0; + rep(i, 10000) { + if (ans[i] > maximal) { + maximal = ans[i]; + choice = i; + } + } + + cout << choice << ' ' << maximal << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/E. Elementary Data Structure Problem.cpp b/XIII UnB Contest Mirror/E. Elementary Data Structure Problem.cpp new file mode 100644 index 0000000..21c634a --- /dev/null +++ b/XIII UnB Contest Mirror/E. Elementary Data Structure Problem.cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int q; + cin >> q; + + vvi a; + + while (q--) { + int op; + cin >> op; + if (op == 1) { + a.emplace_back(); + continue; + } + + if (op == 2) { + int i, j; + cin >> i >> j; + a[i - 1].push_back(j); + continue; + } + + int i, j; + cin >> i >> j; + cout << a[i - 1][j - 1] << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/F. Falatro.cpp b/XIII UnB Contest Mirror/F. Falatro.cpp new file mode 100644 index 0000000..f786fef --- /dev/null +++ b/XIII UnB Contest Mirror/F. Falatro.cpp @@ -0,0 +1,161 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m; + cin >> n >> m; + + vvi graph(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + + graph[a].push_back(b); + graph[b].push_back(a); + } + + ll ans = 0; + rep(i, n) { + ll cur = OO; + + repv(j, graph[i]) { + auto bfs = [&]() { + queue q; + q.push(j); + vl dis(n, OO); + dis[j] = 1; + + while (!q.empty()) { + auto k = q.front(); + q.pop(); + + repv(l, graph[k]) { + if (k == j && l == i) { + continue; + } + + if (dis[l] > dis[k] + 1) { + dis[l] = dis[k] + 1; + q.push(l); + } + } + } + + return dis[i]; + }; + + rmin(cur, bfs()); + } + + if (cur == OO) { + ans += i + 1; + continue; + } + + ans += (i + 1) * cur; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/G. Gelatos from Goiás.cpp b/XIII UnB Contest Mirror/G. Gelatos from Goiás.cpp new file mode 100644 index 0000000..c851cda --- /dev/null +++ b/XIII UnB Contest Mirror/G. Gelatos from Goiás.cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + + vl a(n); + vi p(n); + cin >> a >> p; + + vvi graph(n); + rep(i, n) { + int u = i; + int v = p[i] - 1; + if (a[u] > a[v]) { + swap(u, v); + } + + graph[u].push_back(v); + } + + // vi group(n); + // V vis(n); + // V act(n); + // vi rem; + // vi add; + // vi size; + // int g = -1; + // + // int total = 0; + // + // function dfs = [&](int i) { + // act[i] = true; + // vis[i] = true; + // group[i] = g; + // add[g]++; + // size[g]++; + // + // repv(j, graph[i]) { + // if (vis[j]) { + // continue; + // } + // + // dfs(j); + // } + // }; + // + // rep(i, n) { + // if (vis[i]) { + // continue; + // } + // + // g++; + // rem.emplace_back(); + // add.emplace_back(); + // size.emplace_back(); + // dfs(i); + // total += size[g] >> 1; + // } + + V activated(n); + +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/I. Ivo saw the UVa.cpp b/XIII UnB Contest Mirror/I. Ivo saw the UVa.cpp new file mode 100644 index 0000000..d435e1f --- /dev/null +++ b/XIII UnB Contest Mirror/I. Ivo saw the UVa.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m; + cin >> m >> n; + + V act(n); + cin >> act; + + int ans = 0; + int add1[][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; + // int add2[][2] = {{0, -2}, {0, 2}, {-2, 0}, {2, 0}, {-2, -2, {-2, 2}}} + + rep(i, n) { + rep(j, m) { + repv(k, add1) { + int i1 = i + k[0]; + int j1 = j + k[1]; + int i2 = i + k[0] * 2; + int j2 = j + k[1] * 2; + + if (i1 >= 0 && i1 < n && j1 >= 0 && j1 < m && i2 >= 0 && i2 < n && j2 >= 0 && j2 < m && toupper(act[i][j]) == 'U' && toupper(act[i1][j1]) == 'V' && toupper(act[i2][j2]) == 'A') { + ans++; + } + } + } + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/J. Jolly Game Night.cpp b/XIII UnB Contest Mirror/J. Jolly Game Night.cpp new file mode 100644 index 0000000..eba86be --- /dev/null +++ b/XIII UnB Contest Mirror/J. Jolly Game Night.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/gym/106159/problem/J */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m; + cin >> n >> m; + + cout << (((n * m) & 1) ? "W\n" : "P\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/K. Kronos.cpp b/XIII UnB Contest Mirror/K. Kronos.cpp new file mode 100644 index 0000000..789e4a7 --- /dev/null +++ b/XIII UnB Contest Mirror/K. Kronos.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/gym/106159/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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, q; + cin >> n >> q; + + vi a(n); + cin >> a; + sortv(a); + + while (q--) { + int l, r; + cin >> l >> r; + + cout << upper_bound(all(a), r) - lower_bound(all(a), l) << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/L. Leveling Diamonds.cpp b/XIII UnB Contest Mirror/L. Leveling Diamonds.cpp new file mode 100644 index 0000000..039a227 --- /dev/null +++ b/XIII UnB Contest Mirror/L. Leveling Diamonds.cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/gym/106159/problem/L */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + ll n; + cin >> n; + + ll mod = 1e9 + 7; + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + rep(i, 61) { + if ((p >> i) & 1) { + ans *= a; + ans %= mod; + } + + a *= a; + a %= mod; + } + + return ans; + }; + + ll part1 = 4 * ((__int128)n * (n + 1) % mod * (2 * n + 1) % mod * fpow(6, mod - 2)) % mod; + ll part2 = 4 * ((__int128)n * (n + 1) % mod * fpow(2, mod - 2)) % mod; + + cout << ((part1 + part2) % mod + n) % mod << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/XIII UnB Contest Mirror/N. Nautic Issue.cpp b/XIII UnB Contest Mirror/N. Nautic Issue.cpp new file mode 100644 index 0000000..ace0906 --- /dev/null +++ b/XIII UnB Contest Mirror/N. Nautic Issue.cpp @@ -0,0 +1,173 @@ +/* Problem URL: https://codeforces.com/gym/106159/problem/N */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + ll d, tmpy; + cin >> d >> tmpy; + __int128 y = tmpy; + + int size = 62; + V<__int128> mov(size); + mov[0] = 0; + mov[1] = 1; + mov[2] = 3; + nrep(i, 3, size) { + mov[i] = mov[i - 1] * 2; + } + + V<__int128> pref(size + 1); + rep(i, size) { + pref[i + 1] = pref[i] + mov[i]; + } + + int low = 0; + int high = size; + int an = -1; + while (low <= high) { + int mid = (low + high) >> 1; + + if (pref[mid] >= d) { + an = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + int now = ~an & 1; + __int128 cost = d - pref[an - 1]; + __int128 x = y + cost * (now ? -1 : 1); + now ^= 1; + an--; + while (an-- > 0) { + __int128 next = x + mov[an] * (now ? -1 : 1); + if ((y >= x && y <= next) || (y >= next && y <= x)) { + cout << "Nao\n"; + return; + } + x = next; + now ^= 1; + } + + string ans; + if (x == 0) { + ans = "0"; + } else { + bool neg = false; + if (x < 0) { + x = -x; + neg = true; + } + while (x > 0) { + ans.push_back(x % 10 + '0'); + x /= 10; + } + if (neg) { + ans.push_back('-'); + } + } + reverse(all(ans)); + + cout << "Sim\n" << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/think-cell Round 1/D1. Sum over all Substrings (Easy Version).cpp b/think-cell Round 1/D1. Sum over all Substrings (Easy Version).cpp new file mode 100644 index 0000000..a5a76fb --- /dev/null +++ b/think-cell Round 1/D1. Sum over all Substrings (Easy Version).cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1930/D1 */ + +#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; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +}