├── .gitignore ├── 1000A └── codehorses-t-shirts.cpp ├── 1000B └── light-it-up.cpp ├── 1003A └── polycarps-pockets.cpp ├── 1003B └── binary-string-constructing.cpp ├── 1003C └── intense-heat.cpp ├── 1006A └── adjacent-replacements.cpp ├── 1006B └── polycarps-practice.cpp ├── 1006C └── three-parts-of-the-array.cpp ├── 1008A └── romaji.cpp ├── 1008B └── turn-the-rectangles.cpp ├── 1008C └── reorder-the-array.cpp ├── 1009A └── game-shopping.cpp ├── 1009B └── minimum-ternary-string.cpp ├── 1013A └── piles-with-stones.cpp ├── 1013B └── and.cpp ├── 1013C └── photo-of-the-sky.cpp ├── 1015A └── points-in-segments.cpp ├── 1015B └── obtaining-the-string.cpp ├── 1015C └── songs-compression.cpp ├── 1015D └── walking-between-houses.cpp ├── 1015E1 └── stars-drawing.cpp ├── 1017A └── the-rank.cpp ├── 1017B └── the-bits.cpp ├── 1017C └── the-phone-number.cpp ├── 1020A └── new-building-for-sis.cpp ├── 1020B └── badge.cpp ├── 1027A └── palindromic-twist.cpp ├── 1027B └── numbers-on-the-chessboard.cpp ├── 1028A └── find-square.cpp ├── 1028B └── unnatural-conditions.py ├── 1029A └── many-equal-substrings.cpp ├── 1029B └── creating-the-contest.cpp ├── 1036A └── function-height.cpp ├── 1036B └── diagonal-walking-v2.cpp ├── 1037A └── packets.cpp ├── 1037B └── reach-median.cpp ├── 1037C └── equalize.cpp ├── 1037D └── valid-bfs.cpp ├── 1038A └── equality.cpp ├── 1038B └── non-coprime-partition.cpp ├── 1038C └── gambling.cpp ├── 1040A └── palindrome-dance.cpp ├── 1040B └── shashlik-cooking.cpp ├── 1041A └── heist.cpp ├── 1041B └── buying-a-tv-set.cpp ├── 1041C └── coffee-break.cpp ├── 1042A └── benches.cpp ├── 1042B └── vitamins.cpp ├── 1042C └── array-product.cpp ├── 1043A └── elections.cpp ├── 1043B └── lost-array.cpp ├── 1043C └── smallest-word.cpp ├── 1047A └── little-c-loves-3-I.cpp ├── 1047B └── cover-points.cpp ├── 1055A └── metro.cpp ├── 1055B └── alice-and-hairdresser.cpp ├── 1061A └── coins.cpp ├── 1061B └── views-matter.cpp ├── 1062A └── a-prank.cpp ├── 1062B └── math.cpp ├── 1062C └── banh-mi.cpp ├── 1068A └── birthday.cpp ├── 1068B └── lcm.cpp ├── 1073A └── diverse-substring.cpp ├── 1073B └── vasya-and-books.cpp ├── 1073D └── berland-fair.cpp ├── 1076A └── minimizing-the-string.cpp ├── 1076B └── divisor-subtraction.cpp ├── 1076C └── meme-problem.cpp ├── 1077A └── frog-jumping.cpp ├── 1077B └── disturbed-people.cpp ├── 1077C └── good-array.cpp ├── 1077D └── cutting-out.cpp ├── 1080A └── petya-and-origami.cpp ├── 1080B └── margarite-and-the-best-present.cpp ├── 1080C └── masha-and-two-friends.cpp ├── 1081A └── definite-game.cpp ├── 1081B └── farewell-party.cpp ├── 1082A └── vasya-and-book.cpp ├── 1082B └── vova-and-trophies.cpp ├── 1084A └── the-fair-nut-and-elevator.cpp ├── 1084B └── kvass-and-the-fair-nut.cpp ├── 1087A └── right-left-cipher.cpp ├── 1087B └── div-times-mod.cpp ├── 1091A └── new-year-and-the-christmas-ornament.cpp ├── 1091B └── new-year-and-the-treasure-geolocation.cpp ├── 1091C └── new-year-and-the-sphere-transmission.cpp ├── 1092A └── uniform-string.cpp ├── 1092B └── teams-forming.cpp ├── 1092C └── prefixes-and-suffixes.cpp ├── 1093A └── dice-rolling.cpp ├── 1093B └── letters-rearranging.cpp ├── 1093C └── mishka-and-the-last-exam.cpp ├── 1093D └── beautiful-graph.cpp ├── 1095A └── repeating-cipher.cpp ├── 1095B └── array-stabilization.cpp ├── 1095C └── powers-of-two.cpp ├── 1096A └── find-divisible.cpp ├── 1096B └── substring-removal.cpp ├── 1096C └── polygon-for-the-angle.cpp ├── 1097A └── gennady-and-a-card-game.cpp ├── 1097B └── petr-and-a-combination-lock.cpp ├── 1097C └── yuhao-and-a-parenthesis.cpp ├── 1099A └── snowball.cpp ├── 1099B └── squares-and-segments.cpp ├── 1099C └── postcard.cpp ├── 1101A └── minimum-integer.cpp ├── 1101B └── accordion.cpp ├── 1101C └── division-and-union.cpp ├── 1101E └── polycarps-new-job.cpp ├── 1102A └── integer-sequence-dividing.cpp ├── 1102B └── array-k-coloring.cpp ├── 1102C └── doors-breaking-and-repairing.cpp ├── 1102D └── balanced-ternary-string.cpp ├── 1106A └── lunar-new-year-and-cross-counting.cpp ├── 1106B └── lunar-new-year-and-food-ordering.cpp ├── 1106C └── lunar-new-year-and-number-division.cpp ├── 1106D └── lunar-new-year-and-a-wander.cpp ├── 1110A └── parity.cpp ├── 1110B └── tape.cpp ├── 1110C └── meaningless-operations.cpp ├── 1110D └── jongmah.cpp ├── 1110E └── magic-stones.cpp ├── 1113A └── sasha-and-his-trip.cpp ├── 1113B └── sasha-and-magnetic-machines.cpp ├── 1113C └── sasha-and-a-bit-of-relax.cpp ├── 1114A └── got-any-grapes.cpp ├── 1114B └── yet-another-array-partitioning-task.cpp ├── 1114C └── trailing-loves-or-loeufs.cpp ├── 1114D └── flood-fill.cpp ├── 1118A └── water-buying.cpp ├── 1118B └── tanya-and-candies.cpp ├── 1118C └── palindromic-matrix.cpp ├── 1118D1 └── coffee-and-coursework-easy.cpp ├── 1118D2 └── coffee-and-coursework-hard.cpp ├── 1118E └── yet-another-ball-problem.cpp ├── 1118F1 └── tree-cutting-easy-version.cpp ├── 1141A └── game-23.cpp ├── 1141B └── maximal-continuous-rest.cpp ├── 1141C └── polycarp-restores-permutation.cpp ├── 1141D └── colored-boots.cpp ├── 1171A └── diceRolling.kt ├── 1171B └── newYearAndTheChristmasOrnament.kt ├── 1171C └── lettersRearranging.kt ├── 1171D └── gotAnyGrapes.kt ├── 1171E └── doggoRecoloring.kt ├── 1171F └── divisionAndUnion.kt ├── 1178A └── prime-minister.cpp ├── 1178B └── wow-factor.cpp ├── 1178C └── tiles.cpp ├── 1191A └── tokitsukaze-and-enhancement.cpp ├── 1191B └── tokitsukaze-and-mahjong.cpp ├── 1191C └── tokitsukaze-and-discard-items.cpp ├── 1191D └── tokitsukaze-csl-and-stone-game.cpp ├── 1194A └── remove-a-progression.cpp ├── 1194B └── yet-another-crosses-problem.cpp ├── 1194C └── from-s-to-t.cpp ├── 1195A └── drinks-choosing.cpp ├── 1195B └── sport-mafia.cpp ├── 1195C └── basketball-exercise.cpp ├── 1195D1 └── submarine-in-the-rybinsk-sea-easy-edition.py ├── 1196A └── three-piles-of-candies.cpp ├── 1196B └── odd-sum-segments.cpp ├── 1196D1 └── rgb-substring-easy-version.cpp ├── 1197A └── diy-wooden-ladder.cpp ├── 1197B └── pillars.cpp ├── 1197C └── array-splitting.cpp ├── 1199A └── city-day.cpp ├── 1199B └── water-lily.cpp ├── 1199C └── mp3.cpp ├── 1200A └── hotelier.cpp ├── 1200B └── block-adventure.cpp ├── 1201A └── important-exam.cpp ├── 1201B └── zero-array.cpp ├── 1202A └── you-are-given-two-binary-strings.cpp ├── 1202D └── print-a-1337-string.cpp ├── 1203A └── circle-of-students.cpp ├── 1203B └── equal-rectangles.cpp ├── 1203C └── common-divisors.cpp ├── 1203D1 └── remove-the-substring-easy-version.cpp ├── 1203E └── boxers.cpp ├── 1204A └── bowwow-and-the-timetable.cpp ├── 1204B └── mislove-has-lost-an-array.cpp ├── 1204C └── anna-svyatoslav-and-maps.cpp ├── 1206A └── choose-two-numbers.cpp ├── 1206B └── make-product-equal-one.cpp ├── 1206C └── almost-equal.cpp ├── 1206D └── shortest-cycle.cpp ├── 1207A └── there-are-two-types-of-burgers.cpp ├── 1207B └── square-filling.cpp ├── 1207C └── gas-pipeline.cpp ├── 1208A └── xorinacci.cpp ├── 1208B └── uniqueness.cpp ├── 1208C └── magic-grid.cpp ├── 1213A └── chips-moving.cpp ├── 1213B └── bad-prices.cpp ├── 1213C └── book-reading.cpp ├── 1213D1 └── equalizing-by-division-easy-version.cpp ├── 1213D2 └── equalizing-by-division-hard-version.cpp ├── 1213E └── two-small-strings.cpp ├── 1217A └── creating-a-character.cpp ├── 1217B └── zmei-gorynich.cpp ├── 1217C └── the-number-of-good-substrings.cpp ├── 1237A └── balanced-rating-changes.cpp ├── 1237B └── balanced-tunnel.cpp ├── 1237C1 └── balanced-removals-easier.cpp ├── 1237C2 └── balanced-removals-harder.cpp ├── 1335B └── construct-the-string.cpp ├── 1352F └── binary-string-reconstruction.cpp ├── 1409A └── yet-another-two-integers-problem.cpp ├── 1409B └── minimum-product.cpp ├── 1409C └── yet-another-array-restoration.cpp ├── 1426A └── floor-number.cpp ├── 1426B └── symmetric-matrix.cpp ├── 1426C └── increase-and-copy.cpp ├── 1426D └── non-zero-segments.cpp ├── 1440A └── buy-the-string.cpp ├── 1440B └── sum-of-medians.cpp ├── 1440C1 └── binary-table-easy-version.cpp ├── 1440C2 └── binary-table-hard-version.cpp ├── 1447A └── add-candies.cpp ├── 1447B └── numbers-box.cpp ├── 1451A └── subtract-or-divide.cpp ├── 1451B └── non-substring-subsequence.cpp ├── 1451C └── string-equality.cpp ├── 1454A └── special-permutation.cpp ├── 1454B └── unique-bid-auction.cpp ├── 1454C └── sequence-transformation.cpp ├── 1454D └── number-into-sequence.cpp ├── 1469A └── regular-bracket-sequence.cpp ├── 1469B └── red-and-blue.cpp ├── 1669A └── division.cpp ├── 1669B └── triple.cpp ├── 1669C └── odd-even-increments.cpp ├── 1669D └── colorful-stamp.cpp ├── 1669E ├── 2-letter-strings-v2.cpp └── 2-letter-strings.cpp ├── 1669F ├── eating-candies-v2.cpp └── eating-candies.cpp ├── 1669G └── fall-down.cpp ├── 1669H └── maximal-and.cpp ├── 1676A └── lucky.cpp ├── 1676B └── equal-candies.cpp ├── 1676C └── most-similar-words.cpp ├── 1676D └── x-sum.cpp ├── 1676E └── eating-queries.cpp ├── 1676F ├── longest-strike-v2.cpp └── longest-strike.cpp ├── 1676G └── white-black-balanced-subtrees.cpp ├── 1676H1 └── maximum-crossings-easy-version.cpp ├── 1676H2 └── maximum-crossings-hard-version.cpp ├── 1692E ├── binary-deque-v2.cpp ├── binary-deque.cpp ├── generator.cpp ├── naive.cpp └── stress.sh ├── 1692F ├── 3sum-v2.cpp └── 3sum.cpp ├── 764A └── taymyr-is-calling-you.cpp ├── 764B └── timofey-and-cubes.cpp ├── 765A └── neverending-competitions.py ├── 765B └── code-obfuscation.py ├── 765C └── table-tennis-game-2.py ├── 766A └── mahmoud-and-longest-uncommon-subsequence.py ├── 766B └── mahmoud-and-a-triangle.cpp ├── 767A └── snacktower.cpp ├── 767B └── the-queue.cpp ├── 768A └── oath-of-the-nights-watch.py ├── 768B └── code-for-1.cpp ├── 768C └── jon-snow-and-his-favourite-number.cpp ├── 769A └── year-of-university-entrance.cpp ├── 769B └── news-about-credit.cpp ├── 769C └── cycle-in-maze.cpp ├── 769D ├── k-interesting-pairs-of-integers-v1.cpp └── k-interesting-pairs-of-integers-v2.cpp ├── 770A └── new-password.cpp ├── 770B └── maximize-sum-of-digits.cpp ├── 770C └── online-courses-in-bsu.cpp ├── 770D └── draw-brackets.py ├── 771A └── bear-and-friendship-condition.cpp ├── 771B └── bear-and-different-names.cpp ├── 776A └── a-serial-killer.py ├── 776B └── sherlock-and-his-girlfriend.py ├── 777A └── shell-game.cpp ├── 777B └── game-of-credit-cards.cpp ├── 777C └── alyona-and-spreadsheet.cpp ├── 779A └── pupils-redistribution.py ├── 779B └── weird-rounding.py ├── 779C ├── dishonest-sellers.cpp └── dishonest-sellers.py ├── 785A └── anton-and-polyhedrons.py ├── 785B └── anton-and-classes.py ├── 785C └── anton-and-fairy-tale.cpp ├── 787A └── the-monster.cpp ├── 787B └── not-afraid.py ├── 789A └── anastasia-and-pebbles.cpp ├── 789B └── masha-and-geometric-depression.cpp ├── 795C └── Main.java ├── 795D └── lie-or-truth.py ├── 795K └── stepan-and-vowels.cpp ├── 798A └── mike-and-palindrome.cpp ├── 798B └── mike-and-strings.cpp ├── 805A └── fake-np.cpp ├── 805B └── 3-palindrome.cpp ├── 805C └── find-amir.cpp ├── 805D └── minimum-number-of-steps.cpp ├── 816A └── karen-and-morning.cpp ├── 816B └── karen-and-coffee.cpp ├── 816C └── karen-and-game.cpp ├── 820A └── mister-b-and-book-reading.cpp ├── 820B └── mister-b-and-angle-in-polygon.cpp ├── 820C └── mister-b-and-boring-game.cpp ├── 821A └── okabe-and-future-gadget-laboratory.cpp ├── 821B └── okabe-and-banana-trees.cpp ├── 821C └── okabe-and-boxes.cpp ├── 834A └── the-useless-toy.cpp ├── 834B └── the-festive-evening.cpp ├── 834C └── the-meaningless-game.cpp ├── 835A └── key-races.cpp ├── 835B └── the-number-on-the-board.cpp ├── 835C └── star-sky.cpp ├── 839A └── arya-and-bran.cpp ├── 839C └── journey.cpp ├── 842A └── kirill-and-the-game.cpp ├── 842B └── gleb-and-pizza.cpp ├── 842C └── ilya-and-the-tree.cpp ├── 849A └── odds-and-ends.cpp ├── 849B └── tell-your-world.cpp ├── 854A └── fraction.cpp ├── 854B └── maxim-buys-an-apartment.cpp ├── 864A └── fair-game.cpp ├── 864B └── polycarp-and-letters.cpp ├── 864C └── bus.cpp ├── 864D └── make-a-permutation.cpp ├── 867A └── between-the-offices.cpp ├── 869A └── the-artful-expedient.cpp ├── 869B └── the-eternal-immortality.cpp ├── 869C └── the-intriguing-obsession.cpp ├── 879A └── boryas-diagnosis.cpp ├── 879B └── table-tennis.cpp ├── 887A └── div-64.cpp ├── 887B └── cubes-for-masha.cpp ├── 887C └── solution-for-cube.cpp ├── 893A └── chess-for-three.cpp ├── 893B └── beautiful-divisors.cpp ├── 893C └── rumor.cpp ├── 894A └── qaq.cpp ├── 894B └── ralph-and-his-magic-field.cpp ├── 916A └── jamie-and-alarm-snooze.cpp ├── 922A └── cloning-toys.cpp ├── 922B └── magic-forest.cpp ├── 922C └── cave-painting.cpp ├── 922D └── robot-vacuum-cleaner.cpp ├── 939A └── love-triangle.cpp ├── 939B └── hamster-farm.cpp ├── 939C └── convenient-for-everybody.cpp ├── 954A └── diagonal-walking.cpp ├── 954B └── string-typing.cpp ├── 954C └── matrix-walk.cpp ├── 954D └── fight-against-traffic.cpp ├── 959A └── mahmoud-and-ehab-and-the-even-odd-game.cpp ├── 959B └── mahmoud-and-ehab-and-the-message.cpp ├── 959C └── mahmoud-and-ehab-and-the-wrong-algorithm.cpp ├── 962A └── equator.cpp ├── 962B └── students-in-railway-carriage.cpp ├── 962C └── make-a-square.cpp ├── 964A └── splits.cpp ├── 964B └── messages.cpp ├── 965A └── paper-airplanes.cpp ├── 965B └── battleship.cpp ├── 965C └── greedy-arkady.cpp ├── 977A └── wrong-subtraction.cpp ├── 977B └── two-gram.cpp ├── 977C └── less-or-equal.cpp ├── 977D └── divide-by-three-multiply-by-two.cpp ├── 978A └── remove-duplicates.cpp ├── 978B └── file-name.cpp ├── 978C └── letters.cpp ├── 978D └── almost-arithmetic-progression.cpp ├── 978E └── bus-video-system.cpp ├── 979A └── pizza-pizza-pizza.cpp ├── 979B └── treasure-hunt.cpp ├── 979C └── kuro-and-walking-route.cpp ├── 988A └── diverse-team.cpp ├── 988B └── substrings-sort.cpp ├── 988C └── equal-sums.cpp ├── 988E └── divisibility-by-25.cpp ├── 990A └── commentary-boxes.cpp ├── 990B └── micro-world.cpp ├── 991A └── if-at-first-you-dont-succeed.cpp ├── 991B └── getting-an-a.cpp ├── 991C └── candies.cpp ├── 996A └── hit-the-lottery.cpp ├── 996B └── world-cup.cpp ├── 996D └── suit-and-tie.cpp ├── 996E └── leaving-the-bar.cpp ├── 999A └── mishka-and-contest.cpp ├── 999B └── reversing-encryption.cpp ├── 999C └── alphabetic-removals.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # macOS 2 | .DS_Store 3 | 4 | # C/C++ 5 | *.out 6 | 7 | # Java 8 | *.class 9 | 10 | # Vim 11 | *.swp 12 | 13 | # IDE 14 | .idea 15 | -------------------------------------------------------------------------------- /1000A/codehorses-t-shirts.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool compare(string a, string b) { 5 | if (a.size() == b.size()) { 6 | return a < b; 7 | } 8 | return a.size() < b.size(); 9 | } 10 | 11 | int main() { 12 | int n; 13 | cin >> n; 14 | vector prv(n); 15 | for (int i = 0; i < n; i++) { 16 | cin >> prv[i]; 17 | } 18 | vector cur(n); 19 | for (int i = 0; i < n; i++) { 20 | cin >> cur[i]; 21 | } 22 | for (int i = 0; i < n; i++) { 23 | for (int j = 0; j < n; j++) { 24 | if (prv[i] == cur[j]) { 25 | prv[i] = ""; 26 | cur[j] = ""; 27 | break; 28 | } 29 | } 30 | } 31 | sort(prv.begin(), prv.end(), compare); 32 | sort(cur.begin(), cur.end(), compare); 33 | int ans = 0; 34 | for (int i = 0; i < n; i++) { 35 | if (prv[i] != cur[i]) { 36 | ans++; 37 | } 38 | } 39 | cout << ans << "\n"; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /1003A/polycarps-pockets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(100); 8 | int ans = 0; 9 | for (int i = 0; i < n; i++) { 10 | int j; 11 | cin >> j; 12 | a[j - 1]++; 13 | ans = max(ans, a[j - 1]); 14 | } 15 | cout << ans << "\n"; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /1003B/binary-string-constructing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a, b, x; 6 | cin >> a >> b >> x; 7 | string s; 8 | bool turn = a > b; 9 | while (x >= 0) { 10 | if (turn) { 11 | if (a > 0) { 12 | s += "0"; 13 | a--; 14 | } 15 | } else { 16 | if (b > 0) { 17 | s += "1"; 18 | b--; 19 | } 20 | } 21 | turn = !turn; 22 | x--; 23 | } 24 | while (a > 0) { 25 | for (int i = 0; i < s.size(); i++) { 26 | if (s[i] == '0') { 27 | s.insert(i, 1, '0'); 28 | break; 29 | } 30 | } 31 | a--; 32 | } 33 | while (b > 0) { 34 | for (int i = 0; i < s.size(); i++) { 35 | if (s[i] == '1') { 36 | s.insert(i, 1, '1'); 37 | break; 38 | } 39 | } 40 | b--; 41 | } 42 | cout << s << "\n"; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /1003C/intense-heat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | double ans = 0.0; 12 | for (int j = k; j < n + 1; j++) { 13 | double sum = 0.0; 14 | for (int i = 0; i < j; i++) { 15 | sum += a[i]; 16 | } 17 | ans = max(ans, sum / j); 18 | for (int i = 1; i + j - 1 < n; i++) { 19 | sum -= a[i - 1]; 20 | sum += a[i + j - 1]; 21 | ans = max(ans, sum / j); 22 | } 23 | } 24 | cout << fixed << setprecision(7); 25 | cout << ans << "\n"; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1006A/adjacent-replacements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | for (int i = 0; i < n; i++) { 12 | if (a[i] % 2 == 0) { 13 | cout << a[i] - 1 << " "; 14 | } else { 15 | cout << a[i] << " "; 16 | } 17 | } 18 | cout << "\n"; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1006C/three-parts-of-the-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector d(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> d[i]; 10 | } 11 | if (n == 1) { 12 | cout << 0 << "\n"; 13 | } else { 14 | long long s1 = 0, s3 = 0; 15 | long long ans = 0; 16 | int i = 0, j = n - 1; 17 | s1 += d[i]; 18 | s3 += d[j]; 19 | while (i < j) { 20 | if (s1 == s3) { 21 | ans = max(ans, s1); 22 | i++, j--; 23 | s1 += d[i]; 24 | s3 += d[j]; 25 | } else if (s1 < s3) { 26 | i++; 27 | s1 += d[i]; 28 | } else { 29 | j--; 30 | s3 += d[j]; 31 | } 32 | } 33 | cout << ans << "\n"; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /1008A/romaji.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool is_consonant(char c) { 5 | return c != 'a' and c != 'o' and c != 'u' and c != 'i' and c != 'e'; 6 | } 7 | 8 | int main() { 9 | string s; 10 | cin >> s; 11 | char last = s.back(); 12 | if (is_consonant(last) and last != 'n') { 13 | cout << "NO" << "\n"; 14 | return 0; 15 | } 16 | bool ok = true; 17 | for (int i = 0; ok and i < s.size() - 1; i++) { 18 | if (s[i] != 'n' and is_consonant(s[i]) and is_consonant(s[i + 1])) { 19 | ok = false; 20 | break; 21 | } 22 | } 23 | if (ok) { 24 | cout << "YES" << "\n"; 25 | } else { 26 | cout << "NO" << "\n"; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1008C/reorder-the-array.cpp: -------------------------------------------------------------------------------- 1 | // TLE 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | multiset> ms; 9 | for (int i = 0; i < n; i++) { 10 | int a; 11 | cin >> a; 12 | ms.insert(a); 13 | } 14 | int ans = 0; 15 | bool found = true; 16 | while (found) { 17 | found = false; 18 | auto it = ms.begin(); 19 | if (it == ms.end()) { 20 | break; 21 | } 22 | int curr = *it; 23 | it = ms.erase(it); 24 | while (it != ms.end()) { 25 | if (curr > *it) { 26 | found = true; 27 | ans++; 28 | curr = *it; 29 | it = ms.erase(it); 30 | } else { 31 | it++; 32 | } 33 | } 34 | } 35 | cout << ans << "\n"; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /1009A/game-shopping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m; 6 | cin >> n >> m; 7 | vector c(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> c[i]; 10 | } 11 | queue q; 12 | while (m--) { 13 | int a; 14 | cin >> a; 15 | q.push(a); 16 | } 17 | int ans = 0; 18 | for (int i = 0; i < n; i++) { 19 | if (q.empty()) { 20 | break; 21 | } 22 | int front = q.front(); 23 | if (front >= c[i]) { 24 | q.pop(); 25 | ans++; 26 | } 27 | } 28 | cout << ans << "\n"; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1013A/piles-with-stones.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | long long xs = 0; 8 | for (int i = 0; i < n; i++) { 9 | int x; 10 | cin >> x; 11 | xs += x; 12 | } 13 | long long ys = 0; 14 | for (int i = 0; i < n; i++) { 15 | int y; 16 | cin >> y; 17 | ys += y; 18 | } 19 | if (xs >= ys) { 20 | cout << "Yes" << "\n"; 21 | } else { 22 | cout << "No" << "\n"; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1013C/photo-of-the-sky.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | long long rect(int x1, int x2, int y1, int y2) { 6 | return abs(x1 - x2) * abs(y1 - y2); 7 | } 8 | 9 | int main() { 10 | int n; 11 | cin >> n; 12 | vector a(2 * n); 13 | for (int i = 0; i < 2 * n; i++) { 14 | cin >> a[i]; 15 | } 16 | sort(a.begin(), a.end()); 17 | vector x(n), y(n); 18 | for (int i = 0; i < n; i++) { 19 | x[i] = a[i]; 20 | y[i] = a[2 * n - i - 1]; 21 | } 22 | long long ans = rect(x[0], x[n - 1], y[0], y[n - 1]); 23 | for (int i = 0; i < 2 * n; i += 2) { 24 | x[i] = a[i]; 25 | y[i] = a[i + 1]; 26 | } 27 | ans = max(ans, rect(x[0], x[n - 1], y[0], y[n - 1])); 28 | cout << ans << "\n"; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1015A/points-in-segments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m; 6 | cin >> n >> m; 7 | vector s(m); 8 | for (int i = 0; i < n; i++) { 9 | int l, r; 10 | cin >> l >> r; 11 | for (int j = l - 1; j < r; j++) { 12 | s[j]++; 13 | } 14 | } 15 | int cnt = 0; 16 | for (int i = 0; i < m; i++) { 17 | if (s[i] == 0) { 18 | cnt++; 19 | } 20 | } 21 | cout << cnt << "\n"; 22 | for (int i = 0; i < m; i++) { 23 | if (s[i] == 0) { 24 | cout << i + 1 << " "; 25 | } 26 | } 27 | cout << "\n"; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1015B/obtaining-the-string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s, t; 8 | cin >> s >> t; 9 | if (s == t) { 10 | cout << 0 << "\n"; 11 | return 0; 12 | } 13 | vector moves; 14 | for (int i = 0; i < n; i++) { 15 | if (s[i] != t[i]) { 16 | int pos = -1; 17 | for (int j = i + 1; j < n; j++) { 18 | if (s[j] == t[i]) { 19 | pos = j; 20 | break; 21 | } 22 | } 23 | if (pos == -1) { 24 | cout << -1 << "\n"; 25 | return 0; 26 | } 27 | for (int j = pos; j > i; j--) { 28 | swap(s[j], s[j - 1]); 29 | moves.push_back(j); 30 | } 31 | } 32 | } 33 | cout << moves.size() << "\n"; 34 | for (int i = 0; i < moves.size(); i++) { 35 | cout << moves[i] << " "; 36 | } 37 | cout << "\n"; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /1015C/songs-compression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m; 6 | cin >> n >> m; 7 | vector a(n), b(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i] >> b[i]; 10 | } 11 | long long sum = 0; 12 | for (int i = 0; i < n; i++) { 13 | sum += b[i]; 14 | } 15 | if (sum > m) { 16 | cout << -1 << "\n"; 17 | return 0; 18 | } 19 | sum = 0; 20 | for (int i = 0; i < n; i++) { 21 | sum += a[i]; 22 | } 23 | vector> diff(n); 24 | for (int i = 0; i < n; i++) { 25 | diff[i] = {a[i] - b[i], i}; 26 | } 27 | sort(diff.rbegin(), diff.rend()); 28 | int ans = 0; 29 | for (int i = 0; i < n and sum > m; i++) { 30 | sum -= diff[i].first; 31 | ans++; 32 | } 33 | cout << ans << "\n"; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /1015D/walking-between-houses.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, k, s; 6 | cin >> n >> k >> s; 7 | if (k * (n - 1) < s) { 8 | cout << "NO" << "\n"; 9 | return 0; 10 | } 11 | if (s < k) { 12 | cout << "NO" << "\n"; 13 | return 0; 14 | } 15 | cout << "YES" << "\n"; 16 | long long q = s / k; 17 | long long r = s % k; 18 | long long cur = 1; 19 | for (int i = 0; i < k; i++) { 20 | int step = q; 21 | if (r > 0) { 22 | step++; 23 | r--; 24 | } 25 | if (i % 2 == 0) { 26 | cur += step; 27 | } else { 28 | cur -= step; 29 | } 30 | cout << cur << " "; 31 | } 32 | cout << "\n"; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /1017A/the-rank.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector> s(n); 8 | int tom = 0; 9 | for (int i = 0; i < n; i++) { 10 | int a, b, c, d; 11 | cin >> a >> b >> c >> d; 12 | s[i] = {a + b + c + d, i + 1}; 13 | if (i == 0) { 14 | tom = a + b + c + d; 15 | } 16 | } 17 | sort(s.rbegin(), s.rend()); 18 | for (int i = 0; i < n; i++) { 19 | if (s[i].first == tom) { 20 | cout << i + 1 << "\n"; 21 | break; 22 | } 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1017B/the-bits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string a, b; 8 | cin >> a >> b; 9 | long long zeros = 0, ones = 0, oz = 0; 10 | for (int i = 0; i < n; i++) { 11 | if (a[i] == '1') { 12 | ones++; 13 | } else { 14 | zeros++; 15 | } 16 | if (a[i] == '1' and b[i] == '0') { 17 | oz++; 18 | } 19 | } 20 | long long ans = 0; 21 | for (int i = 0; i < n; i++) { 22 | if (a[i] == '1' and b[i] == '0') { 23 | ans += zeros; 24 | } else if (a[i] == '0' and b[i] == '0') { 25 | ans += ones - oz; 26 | } 27 | } 28 | cout << ans << "\n"; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1017C/the-phone-number.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | vector a; 9 | for (int i = n / 2; i < n; i++) { 10 | a.push_back(i + 1); 11 | } 12 | for (int i = 0; i < n / 2; i++) { 13 | a.push_back(i + 1); 14 | } 15 | for (int i = 0; i < n - 1; i++) { 16 | if (a[i] == n) { 17 | swap(a[i], a[i + 1]); 18 | break; 19 | } 20 | } 21 | for (int i = 0; i < n; i++) { 22 | cout << a[i] << " "; 23 | } 24 | cout << "\n"; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1020A/new-building-for-sis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, h, a, b, k; 6 | cin >> n >> h >> a >> b >> k; 7 | while (k--) { 8 | int ta, fa, tb, fb; 9 | cin >> ta >> fa >> tb >> fb; 10 | if (ta == tb) { 11 | cout << abs(fa - fb) << endl; 12 | } else if (fa >= a and fa <= b) { 13 | cout << abs(tb - ta) + abs(fa - fb) << endl; 14 | } else { 15 | long long ans = 0; 16 | if (abs(fa - a) < abs(fa - b)) { 17 | ans += abs(fa - a); 18 | ans += abs(tb - ta); 19 | ans += abs(fb - a); 20 | } else { 21 | ans += abs(fa - b); 22 | ans += abs(tb - ta); 23 | ans += abs(fb - b); 24 | } 25 | cout << ans << endl; 26 | } 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1020B/badge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector p(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> p[i]; 10 | } 11 | for (int i = 0; i < n; i++) { 12 | vector h(n, 0); 13 | int j = i; 14 | while (h[j] < 2) { 15 | h[j]++; 16 | j = p[j] - 1; 17 | } 18 | cout << j + 1 << " "; 19 | } 20 | cout << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /1027A/palindromic-twist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while (t--) { 8 | int n; 9 | cin >> n; 10 | string s; 11 | cin >> s; 12 | bool found = true; 13 | for (int i = 0; i < n / 2; i++) { 14 | int diff = abs(s[i] - s[n - 1 - i]); 15 | if (diff != 0 and diff != 2) { 16 | found = false; 17 | break; 18 | } 19 | } 20 | if (found) { 21 | cout << "YES" << endl; 22 | } else { 23 | cout << "NO" << endl; 24 | } 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1027B/numbers-on-the-chessboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, q; 6 | cin >> n >> q; 7 | while (q--) { 8 | long long x, y; 9 | cin >> x >> y; 10 | long long ans = n * (x - 1) + y; 11 | ans = (ans + 1) / 2; 12 | long long sum = x + y; 13 | ans += (sum % 2 == 1) ? (n * n + 1) / 2 : 0; 14 | cout << ans << endl; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /1028A/find-square.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m; 6 | cin >> n >> m; 7 | char A[n][m]; 8 | for (int r = 0; r < n; r++) { 9 | for (int c = 0; c < m; c++) { 10 | cin >> A[r][c]; 11 | } 12 | } 13 | int br = -1, bc = -1; 14 | int len = 0; 15 | for (int r = 0; r < n; r++) { 16 | for (int c = 0; c < m; c++) { 17 | if (A[r][c] == 'B') { 18 | if (br == -1 and bc == -1) { 19 | br = r; 20 | bc = c; 21 | } 22 | len++; 23 | } 24 | } 25 | if (br != -1 and bc != -1) { 26 | break; 27 | } 28 | } 29 | br += len / 2 + 1; 30 | bc += len / 2 + 1; 31 | cout << br << " " << bc << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /1028B/unnatural-conditions.py: -------------------------------------------------------------------------------- 1 | n, m = map(int, input().split()) 2 | a = 1 3 | for i in range(n - 1): 4 | a *= 10 5 | a += 1 6 | b = 10 ** n - a 7 | print(a) 8 | print(b) 9 | -------------------------------------------------------------------------------- /1029A/many-equal-substrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | string t; 8 | cin >> t; 9 | if (n == 1) { 10 | while (k--) { 11 | cout << t; 12 | } 13 | } else { 14 | int pos = 0; 15 | for (int i = 1; i < n; i++) { 16 | if (t[i] != t[0]) { 17 | continue; 18 | } 19 | for (int j = i; j < n; j++) { 20 | if (t[j] == t[j - i]) { 21 | pos++; 22 | } else { 23 | pos = 0; 24 | break; 25 | } 26 | } 27 | i = (pos > 0) ? i + pos - 1 : i; 28 | } 29 | cout << t; 30 | for (int i = 0; i < k - 1; i++) { 31 | for (int j = pos; j < n; j++) { 32 | cout << t[j]; 33 | } 34 | } 35 | } 36 | cout << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /1029B/creating-the-contest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | int ans = 1; 12 | for (int i = 0; i < n; i++) { 13 | int j = i; 14 | while (j + 1 < n and a[j + 1] <= a[j] * 2) { 15 | j++; 16 | } 17 | ans = max(ans, j - i + 1); 18 | i = j; 19 | } 20 | cout << ans << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /1036A/function-height.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double n, k; 6 | cin >> n >> k; 7 | // BTDT: explicit cast is required 8 | long long ans = ceil(k / n); 9 | cout << ans << endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /1037A/packets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int ans = log2(n) + 1; 8 | cout << ans << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /1037B/reach-median.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, s; 6 | cin >> n >> s; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | long long ans = 0; 12 | sort(a.begin(), a.end()); 13 | auto lower = lower_bound(a.begin(), a.end(), s); 14 | int j = lower - a.begin(); 15 | if (j > n / 2) { 16 | for (int i = n / 2; i < j; i++) { 17 | ans += s - a[i]; 18 | } 19 | } else if (j < n / 2) { 20 | for (int i = j; i < n / 2 + 1; i++) { 21 | ans += a[i] - s; 22 | } 23 | } else { 24 | ans += abs(a[j] - s); 25 | } 26 | cout << ans << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /1037C/equalize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string a, b; 8 | cin >> a >> b; 9 | int ans = 0; 10 | for (int i = 0; i < n - 1; i++) { 11 | if (a[i] != b[i] and a[i + 1] != b[i + 1] and a[i] != a[i + 1]) { 12 | a[i] = b[i]; 13 | a[i + 1] = b[i + 1]; 14 | ans++; 15 | } 16 | } 17 | for (int i = 0; i < n; i++) { 18 | if (a[i] != b[i]) { 19 | a[i] = b[i]; 20 | ans++; 21 | } 22 | } 23 | cout << ans << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1038A/equality.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | string s; 8 | cin >> s; 9 | vector f(k, 0); 10 | for (int i = 0; i < s.size(); i++) { 11 | int l = s[i] - 'A'; 12 | if (l < k) { 13 | f[l]++; 14 | } 15 | } 16 | int ans = n; 17 | for (int i = 0; i < k; i++) { 18 | ans = min(ans, f[i]); 19 | } 20 | cout << ans * k << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /1038C/gambling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | sort(a.rbegin(), a.rend()); 12 | vector b(n); 13 | for (int i = 0; i < n; i++) { 14 | cin >> b[i]; 15 | } 16 | sort(b.rbegin(), b.rend()); 17 | long long sa = 0, sb = 0; 18 | int i = 0, j = 0; 19 | int turn = 0; 20 | while (i != n or j != n) { 21 | if (turn % 2 == 0) { 22 | if (j == n or (i < n and a[i] > b[j])) { 23 | sa += a[i]; 24 | i = min(n, i + 1); 25 | } else { 26 | j = min(n, j + 1); 27 | } 28 | } else { 29 | if (i == n or (j < n and b[j] > a[i])) { 30 | sb += b[j]; 31 | j = min(n, j + 1); 32 | } else { 33 | i = min(n, i + 1); 34 | } 35 | } 36 | turn++; 37 | } 38 | cout << sa - sb << endl; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /1041A/heist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | sort(a.begin(), a.end()); 12 | cout << a[n - 1] - a[0] + 1 - n << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /1041B/buying-a-tv-set.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long gcd(long long a, long long b) { 5 | return b == 0 ? a : gcd(b, a % b); 6 | } 7 | 8 | int main() { 9 | long long a, b, x, y; 10 | cin >> a >> b >> x >> y; 11 | long long d = gcd(x, y); 12 | x /= d; 13 | y /= d; 14 | cout << min(a / x, b / y) << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /1041C/coffee-break.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m, d; 6 | cin >> n >> m >> d; 7 | vector a(n); 8 | map memo; 9 | set s; 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | memo[a[i]] = i; 13 | s.insert(a[i]); 14 | } 15 | vector color(n); 16 | int ans = 1; 17 | for (int i = 0; i < n and s.size() > 0; i++) { 18 | int curr = *s.begin(); 19 | color[memo[curr]] = ans; 20 | s.erase(curr); 21 | curr += d + 1; 22 | while (true) { 23 | auto lower = s.lower_bound(curr); 24 | if (lower == s.end()) { 25 | break; 26 | } 27 | color[memo[*lower]] = ans; 28 | curr = *lower + d + 1; 29 | s.erase(lower); 30 | } 31 | ans++; 32 | } 33 | cout << ans - 1 << endl; 34 | for (int i = 0; i < n; i++) { 35 | cout << color[i] << " "; 36 | } 37 | cout << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /1042A/benches.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m; 6 | cin >> n >> m; 7 | vector a(n); 8 | double sum = 0; 9 | int mx = 0; 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | sum += a[i]; 13 | mx = max(mx, a[i]); 14 | } 15 | sum += m; 16 | cout << max(mx, (int) ceil(sum / n)) << " "; 17 | cout << mx + m << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1042B/vitamins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | vector c(n); 9 | vector s(n); 10 | map memo; 11 | memo["A"] = memo["B"] = memo["C"] = INF; 12 | memo["AB"] = memo["AC"] = memo["BC"] = INF; 13 | memo["ABC"] = INF; 14 | for (int i = 0; i < n; i++) { 15 | cin >> c[i] >> s[i]; 16 | sort(s[i].begin(), s[i].end()); 17 | memo[s[i]] = min(memo[s[i]], c[i]); 18 | } 19 | int ans = memo["A"] + memo["B"] + memo["C"]; 20 | ans = min(ans, memo["AB"] + memo["C"]); 21 | ans = min(ans, memo["AC"] + memo["B"]); 22 | ans = min(ans, memo["BC"] + memo["A"]); 23 | ans = min(ans, memo["AB"] + memo["AC"]); 24 | ans = min(ans, memo["AB"] + memo["BC"]); 25 | ans = min(ans, memo["AC"] + memo["BC"]); 26 | ans = min(ans, memo["ABC"]); 27 | if (ans == INF) { 28 | ans = -1; 29 | } 30 | cout << ans << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1043A/elections.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | int sum = 0; 9 | int mx = -1; 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | sum += a[i]; 13 | mx = max(mx, a[i]); 14 | } 15 | int ans = (2 * sum + n) / n; 16 | cout << max(mx, ans) << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /1043B/lost-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n + 1); 8 | a[0] = 0; 9 | for (int i = 1; i < n + 1; i++) { 10 | cin >> a[i]; 11 | } 12 | set s; 13 | for (int k = 1; k < n + 1; k++) { 14 | map memo; 15 | bool ok = true; 16 | for (int i = 1; i < n + 1; i++) { 17 | int index = (i - 1) % k; 18 | if (memo.count(index) and memo[index] != a[i] - a[i - 1]) { 19 | ok = false; 20 | break; 21 | } 22 | memo[index] = a[i] - a[i - 1]; 23 | } 24 | if (ok) { 25 | s.insert(k); 26 | } 27 | } 28 | cout << s.size() << endl; 29 | for (auto val: s) { 30 | cout << val << " "; 31 | } 32 | cout << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /1043C/smallest-word.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string s; 6 | cin >> s; 7 | int n = s.size(); 8 | vector flip(n); 9 | for (int i = 0; i < n; i++) { 10 | if (i == n - 1) { 11 | flip[i] = s[i] == 'a' ? 1 : 0; 12 | } else { 13 | flip[i] = s[i] != s[i + 1] ? 1 : 0; 14 | } 15 | } 16 | for (int i = 0; i < n; i++) { 17 | cout << flip[i] << " "; 18 | } 19 | cout << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1047A/little-c-loves-3-I.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | if (n % 3 == 2) { 8 | cout << 1 << " " << 2 << " " << n - 3 << endl; 9 | } else { 10 | cout << 1 << " " << 1 << " " << n - 2 << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /1047B/cover-points.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int ans = -1; 8 | for (int i = 0; i < n; i++) { 9 | int x, y; 10 | cin >> x >> y; 11 | ans = max(ans, x + y); 12 | } 13 | cout << ans << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /1061A/coins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, s; 6 | cin >> n >> s; 7 | long long ans = s / n; 8 | if (s % n > 0) { 9 | ans++; 10 | } 11 | cout << ans << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /1061B/views-matter.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, m; 7 | cin >> n >> m; 8 | vector s(n); 9 | long long sum = 0; 10 | int mx = 1; 11 | for (int i = 0; i < n; i++) { 12 | cin >> s[i]; 13 | sum += s[i]; 14 | mx = max(mx, s[i]); 15 | } 16 | sort(s.begin(), s.end()); 17 | int ans = sum - max(mx, n); 18 | if (mx >= n and n > 1 and s[1] == 1) { 19 | ans--; 20 | } 21 | cout << ans << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /1062A/a-prank.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | vector color(n); 12 | for (int i = 0; i < n - 2; i++) { 13 | if (a[i + 1] - a[i] == 1 and a[i + 2] - a[i + 1] == 1) { 14 | color[i + 1] = 1; 15 | } 16 | } 17 | color[0] = (a[1] == 2) ? 1 : 0; 18 | color[n - 1] = (a[n - 2] == 999) ? 1 : 0; 19 | int ans = 0, cur = 0; 20 | for (int i = 0; i < n; i++) { 21 | cur = color[i] == 1 ? cur + 1 : 0; 22 | ans = max(ans, cur); 23 | } 24 | cout << ans << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1062B/math.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | if (n == 1) { 8 | cout << "1 0" << endl; 9 | return 0; 10 | } 11 | long long ans = 1; 12 | vector p(n); 13 | for (int i = 2; i < n + 1; i++) { 14 | if (n % i == 0) { 15 | ans *= i; 16 | while (n % i == 0) { 17 | p[i]++; 18 | n /= i; 19 | } 20 | } 21 | } 22 | int mp = 1; 23 | for (int i = 0; i < p.size(); i++) { 24 | mp = max(mp, p[i]); 25 | } 26 | bool ok = (mp & (mp - 1)) == 0; 27 | for (int i = 0; i < p.size(); i++) { 28 | if (p[i] > 0 and p[i] != mp) { 29 | ok = false; 30 | break; 31 | } 32 | } 33 | int steps = ceil(log2(mp)); 34 | if (!ok) { 35 | steps++; 36 | } 37 | cout << ans << " " << steps << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /1068A/birthday.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, m, k, l; 6 | cin >> n >> m >> k >> l; 7 | long long ans = -1; 8 | if (m <= n) { 9 | long long cnt = (l + k) / m; 10 | while (cnt * m <= n) { 11 | if (cnt * m - k >= l) { 12 | ans = cnt; 13 | break; 14 | } 15 | cnt++; 16 | } 17 | } 18 | cout << ans << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1068B/lcm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long b; 6 | cin >> b; 7 | set s; 8 | for (long long i = 1; i * i <= b; i++) { 9 | if (b % i == 0) { 10 | s.insert(i); 11 | s.insert(b / i); 12 | } 13 | } 14 | cout << s.size() << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /1073A/diverse-substring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s; 8 | cin >> s; 9 | bool found = false; 10 | string ans; 11 | for (int i = 0; i < n - 1; i++) { 12 | if (s[i] != s[i + 1]) { 13 | found = true; 14 | ans += s[i]; 15 | ans += s[i + 1]; 16 | break; 17 | } 18 | } 19 | if (found) { 20 | cout << "YES" << endl; 21 | cout << ans << endl; 22 | } else { 23 | cout << "NO" << endl; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1073B/vasya-and-books.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | map memo; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | memo[a[i]] = i; 12 | } 13 | vector b(n); 14 | for (int i = 0; i < n; i++) { 15 | cin >> b[i]; 16 | } 17 | stack s; 18 | for (int i = n - 1; i > -1; i--) { 19 | s.push(a[i]); 20 | } 21 | for (int i = 0; i < n; i++) { 22 | int cnt = 0; 23 | while (!s.empty()) { 24 | if (memo[b[i]] < memo[s.top()]) { 25 | break; 26 | } 27 | s.pop(); 28 | cnt++; 29 | } 30 | cout << cnt << " "; 31 | } 32 | cout << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /1073D/berland-fair.cpp: -------------------------------------------------------------------------------- 1 | // TLE 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | long long t; 8 | cin >> n >> t; 9 | vector a(n); 10 | list b; 11 | long long sum = 0; 12 | int mina = 1e9; 13 | for (int i = 0; i < n; i++) { 14 | cin >> a[i]; 15 | b.push_back(a[i]); 16 | sum += a[i]; 17 | mina = min(mina, a[i]); 18 | } 19 | long long ans = (t / sum) * n; 20 | t -= (t / sum) * sum; 21 | while (b.size() > 0 and t >= mina) { 22 | for (auto it = b.begin(); it != b.end(); it++) { 23 | if (t >= *it) { 24 | t -= *it; 25 | ans++; 26 | } else { 27 | sum -= *it; 28 | b.erase(it); 29 | } 30 | } 31 | } 32 | cout << ans << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /1076A/minimizing-the-string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s; 8 | cin >> s; 9 | string ans = s; 10 | for (int i = 0; i < s.size() - 1; i++) { 11 | if (s[i] > s[i + 1]) { 12 | ans = s.substr(0, i) + s.substr(i + 1); 13 | break; 14 | } 15 | } 16 | if (ans == s) { 17 | ans = ans.substr(0, n - 1); 18 | } 19 | cout << ans << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1076B/divisor-subtraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n; 6 | cin >> n; 7 | long long ans = 0; 8 | bool found = false; 9 | for (int d = 2; d < sqrt(n) + 1; d++) { 10 | if (n % d == 0) { 11 | found = true; 12 | n -= d; 13 | ans++; 14 | break; 15 | } 16 | } 17 | if (found) { 18 | ans += n / 2; 19 | } else { 20 | ans = 1; 21 | } 22 | cout << ans << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /1076C/meme-problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while (t--) { 8 | int d; 9 | cin >> d; 10 | double a, b; 11 | bool found = false; 12 | if (d == 0) { 13 | found = true; 14 | a = 0; 15 | b = 0; 16 | } else if (d * d - 4 * d >= 0) { 17 | found = true; 18 | b = (-d + sqrt(d * d - 4 * d)) / -2.0; 19 | a = d / b; 20 | } 21 | if (found) { 22 | cout << fixed << setprecision(9); 23 | cout << "Y " << a << " " << b << endl; 24 | } else { 25 | cout << "N" << endl; 26 | } 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1077A/frog-jumping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while (t--) { 8 | long long a, b, k; 9 | cin >> a >> b >> k; 10 | long long ans; 11 | if (k % 2 == 0) { 12 | ans = (k / 2) * a - (k / 2) * b; 13 | } else { 14 | ans = (k / 2 + 1) * a - (k / 2) * b; 15 | } 16 | cout << ans << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1077B/disturbed-people.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int a[n]; 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | int ans = 0; 12 | for (int i = 0; i < n - 2; i++) { 13 | if (a[i] == 1 and a[i + 1] == 0 and a[i + 2] == 1) { 14 | a[i + 2] = 0; 15 | ans++; 16 | } 17 | } 18 | cout << ans << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1077C/good-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | long long sum = 0; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | sum += a[i]; 12 | } 13 | multiset> ms(a.begin(), a.end()); 14 | vector ans; 15 | for (int i = 0; i < n; i++) { 16 | sum -= a[i]; 17 | auto it = ms.find(a[i]); 18 | if (it != ms.end()) { 19 | ms.erase(it); 20 | } 21 | int m = *ms.begin(); 22 | if (sum - m == m) { 23 | ans.push_back(i + 1); 24 | } 25 | sum += a[i]; 26 | ms.insert(a[i]); 27 | } 28 | cout << ans.size() << endl; 29 | for (auto id: ans) { 30 | cout << id << " "; 31 | } 32 | cout << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /1077D/cutting-out.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector s(n); 8 | map memo; 9 | for (int i = 0; i < n; i++) { 10 | cin >> s[i]; 11 | memo[s[i]]++; 12 | } 13 | multimap> rmemo; 14 | for (auto m: memo) { 15 | rmemo.insert(make_pair(m.second, m.first)); 16 | } 17 | long long sum = 0, mx = 0; 18 | int kc = k; 19 | for (auto m: rmemo) { 20 | if (kc == 0) { 21 | break; 22 | } 23 | sum += m.first; 24 | if (min(sum / k, (long long) m.first) > mx) { 25 | mx = min(sum / k, (long long) m.first); 26 | } 27 | kc--; 28 | } 29 | vector t; 30 | for (auto m: rmemo) { 31 | int cnt = m.first / mx; 32 | for (int i = 0; i < cnt and t.size() < k; i++) { 33 | t.push_back(m.second); 34 | } 35 | } 36 | for (int i = 0; i < k; i++) { 37 | cout << t[i] << " "; 38 | } 39 | cout << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /1080A/petya-and-origami.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | int ans = ceil(2.0 * n / k) + ceil(5.0 * n / k) + ceil(8.0 * n / k); 8 | cout << ans << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /1080B/margarite-and-the-best-present.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int q; 6 | cin >> q; 7 | while (q--) { 8 | int l, r; 9 | cin >> l >> r; 10 | int ans; 11 | if (l == r) { 12 | ans = pow(-1, l % 2) * l; 13 | } else { 14 | int nl = pow(-1, l % 2) * l; 15 | int nr = pow(-1, r % 2) * r; 16 | double s = (nl + nr) / 2.0; 17 | if (s > 0) { 18 | ans = ceil(s); 19 | } else { 20 | ans = floor(s); 21 | } 22 | } 23 | cout << ans << endl; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1081A/definite-game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int ans = (n == 2) ? 2 : 1; 8 | cout << ans << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /1082A/vasya-and-book.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while (t--) { 8 | int n, x, y, d; 9 | cin >> n >> x >> y >> d; 10 | int diff = abs(y - x); 11 | int ans = -1; 12 | if (diff % d == 0) { 13 | ans = diff / d; 14 | } else { 15 | int d1 = y - 1; 16 | int a1 = -1; 17 | if (d1 % d == 0) { 18 | a1 = ceil(1.0 * x / d) + d1 / d; 19 | } 20 | int d2 = n - y; 21 | int a2 = -1; 22 | if (d2 % d == 0) { 23 | a2 = ceil(1.0 * (n - x) / d) + d2 / d; 24 | } 25 | if (a1 == -1 or a2 == -1) { 26 | ans = max(a1, a2); 27 | } else { 28 | ans = min(a1, a2); 29 | } 30 | } 31 | cout << ans << endl; 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /1084A/the-fair-nut-and-elevator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | vector a(n); 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | } 12 | int ans = INF; 13 | for (int x = 1; x < n + 1; x++) { 14 | int sum = 0; 15 | for (int i = 1; i < n + 1; i++) { 16 | sum += a[i - 1] * 2 * (abs(x - i) + x + i - 2); 17 | } 18 | ans = min(ans, sum); 19 | } 20 | cout << ans << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /1084B/kvass-and-the-fair-nut.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | long long s; 7 | cin >> n >> s; 8 | vector v(n); 9 | long long sum = 0; 10 | for (int i = 0; i < n; i++) { 11 | cin >> v[i]; 12 | sum += v[i]; 13 | } 14 | int ans = -1; 15 | if (sum >= s) { 16 | sort(v.begin(), v.end()); 17 | long long tmp = 0; 18 | for (int i = 1; i < n; i++) { 19 | int diff = v[i] - v[0]; 20 | tmp += diff; 21 | } 22 | if (tmp < s) { 23 | int cnt = ceil(1.0 * (s - tmp) / n); 24 | ans = v[0] - cnt; 25 | } else { 26 | ans = v[0]; 27 | } 28 | } 29 | cout << ans << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1087A/right-left-cipher.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string t; 6 | cin >> t; 7 | string s; 8 | int l = 0, r = t.size() - 1; 9 | if (t.size() % 2 == 0) { 10 | s += t[r]; 11 | r--; 12 | } 13 | bool turn = true; 14 | while (l <= r) { 15 | if (turn) { 16 | s = t[l] + s; 17 | l++; 18 | } else { 19 | s = t[r] + s; 20 | r--; 21 | } 22 | turn = !turn; 23 | } 24 | cout << s << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1087B/div-times-mod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector ans; 8 | for (int i = 1; i < k; i++) { 9 | if (n % i == 0) { 10 | int x = i; 11 | while (true) { 12 | if (x / k * (x % k) == n) { 13 | ans.push_back(x); 14 | break; 15 | } 16 | x += k; 17 | } 18 | } 19 | } 20 | sort(ans.begin(), ans.end()); 21 | cout << ans[0] << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /1091A/new-year-and-the-christmas-ornament.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int y, b, r; 6 | cin >> y >> b >> r; 7 | int ans; 8 | for (int i = 1; i <= y; i++) { 9 | bool ok = (i + 1) <= b and (i + 2) <= r; 10 | if (ok) { 11 | ans = 3 * i + 3; 12 | } 13 | } 14 | cout << ans << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /1091B/new-year-and-the-treasure-geolocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector> coords(2 * n); 8 | for (int i = 0; i < coords.size(); i++) { 9 | cin >> coords[i].first >> coords[i].second; 10 | } 11 | sort(coords.begin(), coords.end()); 12 | auto c1 = coords[0], c2 = coords[2 * n - 1]; 13 | int tx = c1.first + c2.first; 14 | int ty = c1.second + c2.second; 15 | cout << tx << " " << ty << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /1091C/new-year-and-the-sphere-transmission.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n; 6 | cin >> n; 7 | set fun; 8 | fun.insert(1); 9 | fun.insert(n * (n + 1) / 2); 10 | for (int k = 2; k * k <= n; k++) { 11 | if (n % k == 0) { 12 | long long m = n / k; 13 | long long t1 = m * (2 + (m - 1) * k) / 2; 14 | long long t2 = k * (2 + (k - 1) * m) / 2; 15 | fun.insert(t1); 16 | fun.insert(t2); 17 | } 18 | } 19 | for (auto val: fun) { 20 | cout << val << " "; 21 | } 22 | cout << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /1092A/uniform-string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while (t--) { 8 | int n, k; 9 | cin >> n >> k; 10 | string s(n, ' '); 11 | for (int i = 0; i < n; i++) { 12 | s[i] = 'a' + (i % k); 13 | } 14 | cout << s << endl; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /1092B/teams-forming.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | sort(a.begin(), a.end()); 12 | int ans = 0; 13 | for (int i = 0; i < n; i += 2) { 14 | ans += a[i + 1] - a[i]; 15 | } 16 | cout << ans << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /1093A/dice-rolling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while (t--) { 8 | int x; 9 | cin >> x; 10 | cout << x / 2 << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /1093B/letters-rearranging.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while (t--) { 8 | string s; 9 | cin >> s; 10 | sort(s.begin(), s.end()); 11 | string ans = s.front() == s.back() ? "-1" : s; 12 | cout << ans << endl; 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /1093C/mishka-and-the-last-exam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n), b(n / 2); 8 | for (int i = 0; i < n / 2; i++) { 9 | cin >> b[i]; 10 | } 11 | long long l = -1, r = -1; 12 | for (int i = 0; i < n / 2; i++) { 13 | if (l == -1 and r == -1) { 14 | l = 0; 15 | r = b[i]; 16 | } else if (b[i] - l > r) { 17 | l = b[i] - r; 18 | } else if (b[i] - l < r) { 19 | r = b[i] - l; 20 | } 21 | a[i] = l; 22 | a[n - i - 1] = r; 23 | } 24 | for (int i = 0; i < n; i++) { 25 | cout << a[i] << " "; 26 | } 27 | cout << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1095A/repeating-cipher.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s; 8 | cin >> s; 9 | string ans; 10 | int skip = 0; 11 | for (int i = 0; i < n; i += skip) { 12 | ans += s[i]; 13 | skip++; 14 | } 15 | cout << ans << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /1095B/array-stabilization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | sort(a.begin(), a.end()); 12 | int ans = min({ 13 | a[n - 1] - a[0], 14 | a[n - 2] - a[0], 15 | a[n - 1] - a[1] 16 | }); 17 | cout << ans << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1095C/powers-of-two.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | priority_queue pq; 8 | int curr = n; 9 | while (curr > 0) { 10 | int p = log2(curr); 11 | curr -= pow(2, p); 12 | pq.push(p); 13 | } 14 | bool ok = k >= pq.size() and k <= n; 15 | if (ok) { 16 | k -= pq.size(); 17 | while (k--) { 18 | int p = pq.top(); 19 | pq.pop(); 20 | pq.push(p - 1); 21 | pq.push(p - 1); 22 | } 23 | } 24 | if (ok) { 25 | cout << "YES" << endl; 26 | while (!pq.empty()) { 27 | cout << (int) pow(2, pq.top()) << " "; 28 | pq.pop(); 29 | } 30 | cout << endl; 31 | } else { 32 | cout << "NO" << endl; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /1096A/find-divisible.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while (t--) { 8 | int l, r; 9 | cin >> l >> r; 10 | cout << l << " " << 2 * l << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /1096B/substring-removal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define P 998244353 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | string s; 9 | cin >> s; 10 | long long rf = 1; 11 | for (int i = 1; i < n; i++) { 12 | if (s[i] == s[i - 1]) { 13 | rf++; 14 | } else { 15 | break; 16 | } 17 | } 18 | long long rb = 1; 19 | for (int i = n - 1; i > 0; i--) { 20 | if (s[i] == s[i - 1]) { 21 | rb++; 22 | } else { 23 | break; 24 | } 25 | } 26 | long long ans = (rf + rb + 1) % P; 27 | if (s[0] == s[n - 1]) { 28 | ans = (ans + (rf * rb) % P) % P; 29 | } 30 | cout << ans << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1096C/polygon-for-the-angle.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int t; 7 | cin >> t; 8 | while (t--) { 9 | int angle; 10 | cin >> angle; 11 | int n = 3; 12 | while (true) { 13 | if (n == 998244354) { 14 | n = -1; 15 | break; 16 | } 17 | double abc = (n - 2) * 180.0 / n; 18 | abc = (180 - abc) / 2; 19 | int iabc = abc; 20 | bool c1 = ceil(abc) == floor(abc); 21 | bool c2 = angle % iabc == 0 and angle <= (n - 2) * 180.0 / n; 22 | if (c1 and c2) { 23 | break; 24 | } 25 | n++; 26 | } 27 | cout << n << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1097A/gennady-and-a-card-game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string s; 6 | cin >> s; 7 | vector h(5); 8 | for (int i = 0; i < h.size(); i++) { 9 | cin >> h[i]; 10 | } 11 | bool ok = false; 12 | for (int i = 0; i < h.size(); i++) { 13 | if (h[i][0] == s[0] or h[i][1] == s[1]) { 14 | ok = true; 15 | break; 16 | } 17 | } 18 | if (ok) { 19 | cout << "YES" << endl; 20 | } else { 21 | cout << "NO" << endl; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /1097B/petr-and-a-combination-lock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | bool ok = false; 12 | for (int i = 0; i < (1 << n); i++) { 13 | bitset<16> b(i); 14 | int total = 0; 15 | for (int j = 0; j < n; j++) { 16 | total += (b[j] == 1) ? a[j] : -a[j]; 17 | total %= 360; 18 | } 19 | if (total == 0) { 20 | ok = true; 21 | break; 22 | } 23 | } 24 | if (ok) { 25 | cout << "YES" << endl; 26 | } else { 27 | cout << "NO" << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1099A/snowball.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int w, h; 6 | cin >> w >> h; 7 | int u1, d1, u2, d2; 8 | cin >> u1 >> d1 >> u2 >> d2; 9 | while (h > 0) { 10 | w += h; 11 | if (h == d1) { 12 | w = max(w - u1, 0); 13 | } 14 | if (h == d2) { 15 | w = max(w - u2, 0); 16 | } 17 | h--; 18 | } 19 | cout << w << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1099B/squares-and-segments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int h = sqrt(n); 8 | int w = h; 9 | if (h * w < n) { 10 | h++; 11 | } 12 | if (h * w < n) { 13 | w++; 14 | } 15 | cout << h + w << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /1101A/minimum-integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int q; 8 | cin >> q; 9 | while (q--) { 10 | long long l, r, d; 11 | cin >> l >> r >> d; 12 | long long ans = 1; 13 | if (l <= d) { 14 | ans = r / d + 1; 15 | } 16 | cout << ans * d << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1101E/polycarps-new-job.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool fit(int h, int w, int x, int y) { 5 | return (x <= h and y <= w) or (y <= h and x <= w); 6 | } 7 | 8 | int main() { 9 | ios::sync_with_stdio(false); 10 | cin.tie(nullptr); 11 | int n; 12 | cin >> n; 13 | int mx = -1, my = -1; 14 | while (n--) { 15 | char t; 16 | cin >> t; 17 | if (t == '+') { 18 | int x, y; 19 | cin >> x >> y; 20 | if (x < y) { 21 | swap(x, y); 22 | } 23 | mx = max(mx, x); 24 | my = max(my, y); 25 | } else if (t == '?') { 26 | int h, w; 27 | cin >> h >> w; 28 | string ans = fit(h, w, mx, my) ? "YES" : "NO"; 29 | cout << ans << endl; 30 | } 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /1102A/integer-sequence-dividing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n; 6 | cin >> n; 7 | long long ans = n * (n + 1) / 2; 8 | cout << ans % 2 << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /1102B/array-k-coloring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector> a(n); 8 | map freq; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i].first; 11 | a[i].second = i; 12 | freq[a[i].first]++; 13 | } 14 | sort(a.begin(), a.end()); 15 | vector color(n); 16 | int cur = 0; 17 | for (int i = 0; i < n; i++) { 18 | color[a[i].second] = cur; 19 | cur = (cur + 1) % k; 20 | } 21 | bool ok = true; 22 | for (auto f: freq) { 23 | if (f.second > k) { 24 | ok = false; 25 | break; 26 | } 27 | } 28 | if (ok) { 29 | cout << "YES" << endl; 30 | for (int i = 0; i < n; i++) { 31 | cout << color[i] + 1 << " "; 32 | } 33 | cout << endl; 34 | } else { 35 | cout << "NO" << endl; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /1102C/doors-breaking-and-repairing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, x, y; 6 | cin >> n >> x >> y; 7 | vector a(n); 8 | int lte = 0; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | lte += a[i] <= x; 12 | } 13 | if (x > y) { 14 | cout << n << endl; 15 | } else { 16 | cout << ceil(lte / 2.0) << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1106A/lunar-new-year-and-cross-counting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | char M[n][n]; 10 | for (int i = 0; i < n; i++) { 11 | for (int j = 0; j < n; j++) { 12 | cin >> M[i][j]; 13 | } 14 | } 15 | int ans = 0; 16 | for (int r = 0; r < n; r++) { 17 | for (int c = 0; c < n; c++) { 18 | bool ok = ( 19 | 0 < r and r < n - 1 and 20 | 0 < c and c < n - 1 and 21 | M[r][c] == 'X' and 22 | M[r][c] == M[r - 1][c - 1] and 23 | M[r][c] == M[r - 1][c + 1] and 24 | M[r][c] == M[r + 1][c - 1] and 25 | M[r][c] == M[r + 1][c + 1] 26 | ); 27 | ans += ok; 28 | } 29 | } 30 | cout << ans << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1106C/lunar-new-year-and-number-division.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | long long ans = 0; 14 | sort(a.begin(), a.end()); 15 | for (int i = 0; i < n / 2; i++) { 16 | ans += (a[i] + a[n - 1 - i]) * (a[i] + a[n - 1 - i]); 17 | } 18 | cout << ans << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1106D/lunar-new-year-and-a-wander.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, m; 8 | cin >> n >> m; 9 | vector> g(n); 10 | vector color(n); 11 | while (m--) { 12 | int u, v; 13 | cin >> u >> v; 14 | u--, v--; 15 | g[u].insert(v); 16 | g[v].insert(u); 17 | } 18 | vector path; 19 | priority_queue pq; 20 | int s = 0; 21 | pq.push(-s); 22 | color[s] = 1; 23 | while (!pq.empty()) { 24 | int u = -pq.top(); pq.pop(); 25 | path.push_back(u); 26 | for (auto v: g[u]) { 27 | if (color[v] == 0) { 28 | pq.push(-v); 29 | color[v] = 1; 30 | } 31 | } 32 | } 33 | for (auto u: path) { 34 | cout << u + 1 << " "; 35 | } 36 | cout << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /1110A/parity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int mpow(int x, int y) { 5 | int ans = 1; 6 | while (y > 0) { 7 | if (y % 2 == 0) { 8 | ans = (ans * ans) % 2; 9 | y /= 2; 10 | } else { 11 | ans = (ans * x) % 2; 12 | y--; 13 | } 14 | } 15 | return ans; 16 | } 17 | 18 | int main() { 19 | ios::sync_with_stdio(false); 20 | cin.tie(nullptr); 21 | int b, k; 22 | cin >> b >> k; 23 | vector a(k); 24 | for (int i = 0; i < k; i++) { 25 | cin >> a[i]; 26 | } 27 | int ans = 0; 28 | for (int i = k - 1; i > -1; i--) { 29 | ans += a[i] * mpow(b, k - 1 - i) % 2; 30 | } 31 | if (ans % 2 == 0) { 32 | cout << "even" << endl; 33 | } else { 34 | cout << "odd" << endl; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /1110B/tape.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | long long n, m, k; 8 | cin >> n >> m >> k; 9 | vector b(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> b[i]; 12 | } 13 | long long ans = n; 14 | priority_queue pq; 15 | for (int i = 1; i < n; i++) { 16 | long long diff = b[i] - b[i - 1] + 1; 17 | pq.push(-diff); 18 | } 19 | long long used = n; 20 | while (used > k) { 21 | long long diff = -pq.top(); pq.pop(); 22 | ans = ans - 2 + diff; 23 | used--; 24 | } 25 | cout << ans << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1113A/sasha-and-his-trip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, v; 8 | cin >> n >> v; 9 | int ans = min(n - 1, v); 10 | v = min(n - 1, v); 11 | for (int i = 1; i < n; i++) { 12 | if (n - i > v) { 13 | ans += i + 1; 14 | v++; 15 | } 16 | v--; 17 | } 18 | cout << ans << "\n"; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1113B/sasha-and-magnetic-machines.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | int sum = 0; 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | sum += a[i]; 14 | } 15 | sort(a.begin(), a.end()); 16 | int ans = sum; 17 | for (int i = n - 1; i > 0; i--) { 18 | int tmp = a[i]; 19 | for (int j = 1; j * j <= tmp; j++) { 20 | if (tmp % j == 0) { 21 | int s1 = sum - a[0] - a[i] + a[0] * j + a[i] / j; 22 | int s2 = sum - a[0] - a[i] + a[0] * (tmp / j) + a[i] * (tmp / j); 23 | ans = min({ ans, s1, s2 }); 24 | } 25 | } 26 | } 27 | cout << ans << "\n"; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1113C/sasha-and-a-bit-of-relax.cpp: -------------------------------------------------------------------------------- 1 | // MLE 2 | #include 3 | using namespace std; 4 | 5 | int n; 6 | vector a; 7 | map> memo; 8 | 9 | int solve(int l, int r) { 10 | if (memo[l][r] > 0) { 11 | return memo[l][r]; 12 | } 13 | int m = (l + r) / 2; 14 | return solve(l, m) xor solve(m + 1, r); 15 | } 16 | 17 | int main() { 18 | ios::sync_with_stdio(false); 19 | cin.tie(nullptr); 20 | cin >> n; 21 | a.resize(n); 22 | for (int i = 0; i < n; i++) { 23 | cin >> a[i]; 24 | memo[i][i] = a[i]; 25 | } 26 | int ans = 0; 27 | for (int j = 1; j < n; j += 2) { 28 | for (int i = 0; i + j < n; i++) { 29 | int l = i, r = i + j; 30 | memo[l][r] = solve(l, r); 31 | if (memo[l][r] == 0) { 32 | ans++; 33 | } 34 | } 35 | } 36 | cout << ans << "\n"; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /1114A/got-any-grapes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int x, y, z; 8 | cin >> x >> y >> z; 9 | int a, b, c; 10 | cin >> a >> b >> c; 11 | bool c1 = a >= x; 12 | bool c2 = a - x + b >= y; 13 | bool c3 = a + b - x - y + c >= z; 14 | if (c1 and c2 and c3) { 15 | cout << "YES" << endl; 16 | } else { 17 | cout << "NO" << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1114C/trailing-loves-or-loeufs.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | long long n, b; 9 | cin >> n >> b; 10 | long long ans = 0; 11 | long long i = 1; 12 | while (b * i <= n) { 13 | ans++; 14 | i++; 15 | } 16 | cout << ans + 1 << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /1114D/flood-fill.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | #define MAXN 5005 4 | using namespace std; 5 | 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | int n; 10 | cin >> n; 11 | vector c(n); 12 | for (int i = 0; i < n; i++) { 13 | cin >> c[i]; 14 | } 15 | vector diff; 16 | diff.push_back(c[0]); 17 | for (int i = 1; i < n; i++) { 18 | if (c[i] != c[i - 1]) { 19 | diff.push_back(c[i]); 20 | } 21 | } 22 | int mf = -1, mn = -1; 23 | vector f(MAXN); 24 | for (auto val: diff) { 25 | f[val]++; 26 | if (f[val] > mf) { 27 | mf = f[val]; 28 | mn = val; 29 | } 30 | } 31 | int ans = 0; 32 | for (auto val: diff) { 33 | if (val != mn) { 34 | ans++; 35 | } 36 | } 37 | cout << ans << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /1118A/water-buying.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int q; 8 | cin >> q; 9 | while (q--) { 10 | long long n, a, b; 11 | cin >> n >> a >> b; 12 | long long ans = min(n * a, (n / 2) * b + (n % 2) * a); 13 | cout << ans << endl; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /1118B/tanya-and-candies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | int sum = 0; 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | sum += a[i]; 14 | } 15 | int e = 0, o = 0; 16 | for (int i = 0; i < n; i++) { 17 | if (i % 2 == 0) { 18 | e += a[i]; 19 | } else { 20 | o += a[i]; 21 | } 22 | } 23 | int ne = 0, no = 0; 24 | int ans = 0; 25 | for (int i = 0; i < n; i++) { 26 | if (i % 2 == 0) { 27 | ans += (ne + o - no) * 2 == sum - a[i]; 28 | ne += a[i]; 29 | } else { 30 | ans += (no + e - ne) * 2 == sum - a[i]; 31 | no += a[i]; 32 | } 33 | } 34 | cout << ans << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /1118D1/coffee-and-coursework-easy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, m; 5 | vector a; 6 | 7 | bool possible(int d) { 8 | int total = 0; 9 | for (int i = 0; i < n; i++) { 10 | total += max(a[i] - i / d, 0); 11 | } 12 | return total >= m; 13 | } 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); 17 | cin.tie(nullptr); 18 | cin >> n >> m; 19 | a.resize(n); 20 | int total = 0; 21 | for (int i = 0; i < n; i++) { 22 | cin >> a[i]; 23 | total += a[i]; 24 | } 25 | if (total < m) { 26 | cout << -1 << endl; 27 | } else { 28 | sort(a.rbegin(), a.rend()); 29 | for (int d = 1; d <= n; d++) { 30 | if (possible(d)) { 31 | cout << d << endl; 32 | break; 33 | } 34 | } 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /1118D2/coffee-and-coursework-hard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, m; 5 | vector a; 6 | 7 | bool possible(int d) { 8 | int total = 0; 9 | for (int i = 0; i < n; i++) { 10 | total += max(a[i] - i / d, 0); 11 | } 12 | return total >= m; 13 | } 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); 17 | cin.tie(nullptr); 18 | cin >> n >> m; 19 | a.resize(n); 20 | int total = 0; 21 | for (int i = 0; i < n; i++) { 22 | cin >> a[i]; 23 | total += a[i]; 24 | } 25 | if (total < m) { 26 | cout << -1 << endl; 27 | } else { 28 | sort(a.rbegin(), a.rend()); 29 | int l = 1, h = n; 30 | while (l < h) { 31 | int m = (l + h) / 2; 32 | if (possible(m)) { 33 | h = m; 34 | } else { 35 | l = m + 1; 36 | } 37 | } 38 | cout << l << endl; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /1118E/yet-another-ball-problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | long long n, k; 8 | cin >> n >> k; 9 | long long p = k * (k - 1); 10 | if (n > p) { 11 | cout << "NO" << endl; 12 | } else { 13 | cout << "YES" << endl; 14 | for (int i = 1; i <= k and n > 0; i++) { 15 | for (int j = i + 1; j <= k and n > 0; j++) { 16 | cout << i << " " << j << endl; 17 | if (n > 1) { 18 | cout << j << " " << i << endl; 19 | } 20 | n -= 2; 21 | } 22 | } 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1141A/game-23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | long long n, m; 8 | cin >> n >> m; 9 | if (m % n == 0) { 10 | long long d = m / n; 11 | if (d == 1 or d % 2 == 0 or d % 3 == 0) { 12 | int ans = 0; 13 | while (d % 2 == 0) { 14 | d /= 2; 15 | ans++; 16 | } 17 | while (d % 3 == 0) { 18 | d /= 3; 19 | ans++; 20 | } 21 | if (d == 1) { 22 | cout << ans << endl; 23 | } else { 24 | cout << -1 << endl; 25 | } 26 | } else { 27 | cout << -1 << endl; 28 | } 29 | } else { 30 | cout << -1 << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /1141B/maximal-continuous-rest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | int front = 0; 14 | for (int i = 0; i < n; i++) { 15 | if (a[i] == 1) { 16 | front++; 17 | } else { 18 | break; 19 | } 20 | } 21 | int back = 0; 22 | for (int i = n - 1; i > -1; i--) { 23 | if (a[i] == 1) { 24 | back++; 25 | } else { 26 | break; 27 | } 28 | } 29 | int ans = 0, cnt = 0; 30 | for (int i = 0; i < n; i++) { 31 | if (a[i] == 1) { 32 | cnt++; 33 | } else { 34 | cnt = 0; 35 | } 36 | ans = max(ans, cnt); 37 | } 38 | cout << max(ans, front + back) << endl; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /1141C/polycarp-restores-permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | long long n; 8 | cin >> n; 9 | vector q(n - 1); 10 | for (int i = 0; i < n - 1; i++) { 11 | cin >> q[i]; 12 | } 13 | long long qsum = 0, total = 0; 14 | for (int i = n - 2; i > -1; i--) { 15 | qsum += q[i]; 16 | total += qsum; 17 | } 18 | vector p(n); 19 | p[n - 1] = (n * (n + 1) / 2 + total) / n; 20 | for (int i = n - 2; i > -1; i--) { 21 | p[i] = p[i + 1] - q[i]; 22 | } 23 | vector sp(p); 24 | sort(sp.begin(), sp.end()); 25 | bool ok = true; 26 | for (int i = 0; i < n; i++) { 27 | if (sp[i] != i + 1) { 28 | ok = false; 29 | break; 30 | } 31 | } 32 | if (ok) { 33 | for (auto val: p) { 34 | cout << val << " "; 35 | } 36 | cout << endl; 37 | } else { 38 | cout << -1 << endl; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /1171A/diceRolling.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val t = readLine()!!.toInt() 3 | for (i in 1..t) { 4 | val x = readLine()!!.toInt() 5 | println(x / 2) 6 | } 7 | } -------------------------------------------------------------------------------- /1171B/newYearAndTheChristmasOrnament.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val (y, b, r) = readLine()!!.split(" ").map { it.toInt() } 3 | var ans = 6 4 | for (i in y downTo 2) { 5 | if (b > i && r > i + 1) { 6 | ans = 3 * i + 3 7 | break 8 | } 9 | } 10 | println(ans) 11 | } 12 | -------------------------------------------------------------------------------- /1171C/lettersRearranging.kt: -------------------------------------------------------------------------------- 1 | import java.util.Collections 2 | 3 | fun main() { 4 | val t = readLine()!!.toInt() 5 | for (i in 1..t) { 6 | val s = readLine()!! 7 | var ans = if (s == s.reversed()) "-1" else s 8 | if (ans == "-1") { 9 | for (j in s.length - 1 downTo 1) { 10 | if (s[0] != s[j]) { 11 | val sl = s.toMutableList() 12 | Collections.swap(sl, 0, j) 13 | ans = sl.joinToString("") 14 | break 15 | } 16 | } 17 | } 18 | println(ans) 19 | } 20 | } -------------------------------------------------------------------------------- /1171D/gotAnyGrapes.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val (x, y, z) = readLine()!!.split(" ").map { it.toInt() } 3 | val (a, b, c) = readLine()!!.split(" ").map { it.toInt() } 4 | val andrew = a >= x 5 | val dmitry = (a - x + b) >= y 6 | val michal = (a - x + b - y + c) >= z 7 | val ans = if (andrew && dmitry && michal) "YES" else "NO" 8 | println(ans) 9 | } -------------------------------------------------------------------------------- /1171E/doggoRecoloring.kt: -------------------------------------------------------------------------------- 1 | fun main() { 2 | val n = readLine()!!.toInt() 3 | val s = readLine()!! 4 | val freq = s.groupingBy { it }.eachCount() 5 | var ok = false 6 | for (f in freq) { 7 | if (f.value > 1) { 8 | ok = true 9 | break 10 | } 11 | } 12 | val ans = if (n == 1 || ok) "Yes" else "No" 13 | println(ans) 14 | } -------------------------------------------------------------------------------- /1178A/prime-minister.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | int sum = 0; 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | sum += a[i]; 14 | } 15 | vector clt = {0}; 16 | int total = a[0]; 17 | for (int i = 1; total <= sum / 2 and i < n; i++) { 18 | if (a[0] / 2 >= a[i]) { 19 | clt.push_back(i); 20 | total += a[i]; 21 | } 22 | } 23 | if (total > sum / 2) { 24 | cout << clt.size() << endl; 25 | for (auto id: clt) { 26 | cout << id + 1 << " "; 27 | } 28 | cout << endl; 29 | } else { 30 | cout << 0 << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /1178B/wow-factor.cpp: -------------------------------------------------------------------------------- 1 | // TLE 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | string s; 9 | cin >> s; 10 | vector ws, os; 11 | for (int i = 0; i < s.size() - 1; i++) { 12 | if (s[i] == 'v' and s[i + 1] == 'v') { 13 | ws.push_back(i); 14 | } 15 | } 16 | for (int i = 0; i < s.size(); i++) { 17 | if (s[i] == 'o') { 18 | os.push_back(i); 19 | } 20 | } 21 | long long ans = 0; 22 | for (int i = 0; i < ws.size(); i++) { 23 | auto nxo = upper_bound(os.begin(), os.end(), ws[i]); 24 | if (nxo == os.end()) continue; 25 | for (auto it = nxo; it != os.end(); it++) { 26 | auto nxw = upper_bound(ws.begin(), ws.end(), *it); 27 | if (nxw == ws.end()) continue; 28 | ans += ws.end() - nxw; 29 | } 30 | } 31 | cout << ans << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /1178C/tiles.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | #define M 998244353 4 | using namespace std; 5 | 6 | int main() { 7 | ios::sync_with_stdio(false); 8 | cin.tie(nullptr); 9 | long long w, h; 10 | cin >> w >> h; 11 | long long ans = (w * h) % M; 12 | cout << (ans * ans) % M << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /1191A/tokitsukaze-and-enhancement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int x; 8 | cin >> x; 9 | if (x % 4 == 0) { 10 | cout << "1 A" << endl; 11 | } else if (x % 4 == 1) { 12 | cout << "0 A" << endl; 13 | } else if (x % 4 == 2) { 14 | cout << "1 B" << endl; 15 | } else { 16 | cout << "2 A" << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1191C/tokitsukaze-and-discard-items.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | long long n, m, k; 8 | cin >> n >> m >> k; 9 | vector p(m); 10 | for (int i = 0; i < m; i++) { 11 | cin >> p[i]; 12 | } 13 | int ans = 0; 14 | int i = 0; 15 | while (i < m) { 16 | int j = i + 1; 17 | while (j < m) { 18 | long long gi = ceil(1.0 * (p[i] - i) / k); 19 | long long gj = ceil(1.0 * (p[j] - i) / k); 20 | if (gi != gj) { 21 | break; 22 | } 23 | j++; 24 | } 25 | i = j; 26 | ans++; 27 | } 28 | cout << ans << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1191D/tokitsukaze-csl-and-stone-game.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int n; 9 | cin >> n; 10 | vector a(n); 11 | int xsum = 0, sum = 0; 12 | for (int i = 0; i < n; i++) { 13 | cin >> a[i]; 14 | xsum ^= a[i]; 15 | sum += a[i]; 16 | } 17 | if (sum == 0) { 18 | cout << "cslnb" << endl; 19 | } else if (xsum == 0) { 20 | cout << "sjfnb" << endl; 21 | } else { 22 | if (n % 2 == 0) { 23 | cout << "cslnb" << endl; 24 | } else { 25 | cout << "sjfnb" << endl; 26 | } 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1194A/remove-a-progression.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, x; 11 | cin >> n >> x; 12 | cout << 2 * x << endl; 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /1194C/from-s-to-t.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int q; 8 | cin >> q; 9 | while (q--) { 10 | string s, t, p; 11 | cin >> s >> t >> p; 12 | int i = 0; 13 | while (i < t.size() and s.size() < t.size()) { 14 | if (s[i] != t[i]) { 15 | auto pos = p.find(t[i]); 16 | if (pos == string::npos) { 17 | break; 18 | } 19 | s.insert(i, 1, p[pos]); 20 | p.erase(pos, 1); 21 | } 22 | i++; 23 | } 24 | if (s == t) { 25 | cout << "YES" << endl; 26 | } else { 27 | cout << "NO" << endl; 28 | } 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1195A/drinks-choosing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, k; 8 | cin >> n >> k; 9 | map cnt; 10 | for (int i = 0; i < n; i++) { 11 | int a; 12 | cin >> a; 13 | cnt[a]++; 14 | } 15 | int full = 0, left = 0; 16 | for (auto [key, value]: cnt) { 17 | full += value / 2; 18 | left += value % 2; 19 | } 20 | cout << 2 * full + (left + 1) / 2 << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /1195B/sport-mafia.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(0); 7 | long long n, k; 8 | cin >> n >> k; 9 | long long l = 0, h = n; 10 | while (l < h) { 11 | long long m = (l + h) / 2; 12 | long long candies = m * (m + 1) / 2; 13 | if (candies + m < k + n) { 14 | l = m + 1; 15 | } else { 16 | h = m; 17 | } 18 | } 19 | cout << n - l << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1195D1/submarine-in-the-rybinsk-sea-easy-edition.py: -------------------------------------------------------------------------------- 1 | # TLE 2 | def f(l, r): 3 | ls = str(l) 4 | rs = str(r) 5 | fs = "" 6 | for i in range(len(ls)): 7 | fs += ls[i] 8 | fs += rs[i] 9 | return int(fs) % M 10 | 11 | 12 | M = 998244353 13 | n = int(input()) 14 | a = [int(x) for x in input().split()] 15 | 16 | ans = 0 17 | for i in range(n): 18 | for j in range(n): 19 | ans += f(a[i], a[j]) 20 | ans %= M 21 | 22 | print(ans) 23 | -------------------------------------------------------------------------------- /1196A/three-piles-of-candies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int q; 8 | cin >> q; 9 | while (q--) { 10 | long long a, b, c; 11 | cin >> a >> b >> c; 12 | long long ans = (a + b + c) / 2; 13 | cout << ans << endl; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /1196B/odd-sum-segments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int q; 8 | cin >> q; 9 | while (q--) { 10 | int n, k; 11 | cin >> n >> k; 12 | vector a(n); 13 | for (int i = 0; i < n; i++) { 14 | cin >> a[i]; 15 | } 16 | vector r; 17 | long long tmp = 0; 18 | int i = 0; 19 | while (i < n - 1 and r.size() < k - 1) { 20 | tmp += a[i]; 21 | if (tmp % 2 == 1) { 22 | r.push_back(i); 23 | tmp = 0; 24 | } 25 | i++; 26 | } 27 | for (int j = i; j < n; j++) { 28 | tmp += a[j]; 29 | } 30 | if (r.size() == k - 1 and tmp % 2 == 1) { 31 | r.push_back(n - 1); 32 | cout << "YES" << endl; 33 | for (auto ri: r) { 34 | cout << ri + 1 << " "; 35 | } 36 | cout << endl; 37 | } else { 38 | cout << "NO" << endl; 39 | } 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /1197A/diy-wooden-ladder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | vector a(n); 13 | for (int i = 0; i < n; i++) { 14 | cin >> a[i]; 15 | } 16 | sort(a.begin(), a.end()); 17 | int ans = (n > 2) ? min(n - 2, a[n - 2] - 1) : 0; 18 | cout << ans << endl; 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1197B/pillars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | int mx = 0, mxi = 0; 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | if (a[i] > mx) { 14 | mx = a[i]; 15 | mxi = i; 16 | } 17 | } 18 | int i = mxi - 1, j = mxi + 1; 19 | int nxt = mx - 1; 20 | bool ok = true; 21 | while (ok) { 22 | ok = false; 23 | if (i >= 0 and a[i] == nxt) { 24 | ok = true; 25 | nxt--; 26 | i--; 27 | } 28 | if (j < n and a[j] == nxt) { 29 | ok = true; 30 | nxt--; 31 | j++; 32 | } 33 | } 34 | if (nxt == 0) { 35 | cout << "YES" << endl; 36 | } else { 37 | cout << "NO" << endl; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /1199A/city-day.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, x, y; 8 | cin >> n >> x >> y; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | int ans = -1; 14 | for (int i = 0; i < n; i++) { 15 | bool before = true; 16 | for (int j = i - 1, cnt = x; j > 0 and cnt > 0; j--, cnt--) { 17 | if (a[i] > a[j]) { 18 | before = false; 19 | break; 20 | } 21 | } 22 | bool after = true; 23 | for (int j = i + 1, cnt = y; j < n and cnt > 0; j++, cnt--) { 24 | if (a[i] > a[j]) { 25 | after = false; 26 | break; 27 | } 28 | } 29 | if (before and after) { 30 | ans = i + 1; 31 | break; 32 | } 33 | } 34 | cout << ans << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /1199B/water-lily.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | double h, l; 8 | cin >> h >> l; 9 | double ans = (l * l - h * h) / (2 * h); 10 | cout << fixed << setprecision(10); 11 | cout << ans << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /1199C/mp3.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int n, I; 6 | vector a; 7 | map freq; 8 | 9 | bool possible(int K) { 10 | int k = ceil(log2(1.0 * K)); 11 | return n * k <= 8 * I; 12 | } 13 | 14 | int main() { 15 | ios::sync_with_stdio(false); 16 | cin.tie(nullptr); 17 | cin >> n >> I; 18 | a.resize(n); 19 | for (int i = 0; i < n; i++) { 20 | cin >> a[i]; 21 | freq[a[i]]++; 22 | } 23 | set s(a.begin(), a.end()); 24 | a.assign(s.begin(), s.end()); 25 | long long ans = 0; 26 | int l = 0, r = a.size() - 1; 27 | while (l <= r) { 28 | if (possible(r - l + 1)) { 29 | break; 30 | } 31 | int fl = freq[a[l]]; 32 | int fr = freq[a[r]]; 33 | if (fl < fr) { 34 | ans += fl; 35 | l++; 36 | } else { 37 | ans += fr; 38 | r--; 39 | } 40 | } 41 | cout << ans << endl; 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /1200A/hotelier.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | string s; 10 | cin >> s; 11 | vector h(10); 12 | for (int i = 0; i < s.size(); i++) { 13 | if (s[i] == 'L') { 14 | for (int i = 0; i < 10; i++) { 15 | if (h[i] == 0) { 16 | h[i] = 1; 17 | break; 18 | } 19 | } 20 | } else if (s[i] == 'R') { 21 | for (int i = 9; i > -1; i--) { 22 | if (h[i] == 0) { 23 | h[i] = 1; 24 | break; 25 | } 26 | } 27 | } else { 28 | h[s[i] - '0'] = 0; 29 | } 30 | } 31 | for (int i = 0; i < 10; i++) { 32 | cout << h[i]; 33 | } 34 | cout << endl; 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /1200B/block-adventure.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | long long n, m, k; 12 | cin >> n >> m >> k; 13 | vector h(n); 14 | for (int i = 0; i < n; i++) { 15 | cin >> h[i]; 16 | } 17 | bool win = true; 18 | for (int i = 0; i < n - 1; i++) { 19 | long long cur = h[i]; 20 | long long nxt = h[i + 1]; 21 | if (cur < nxt) { 22 | if (cur + m + k < nxt) { 23 | win = false; 24 | break; 25 | } else { 26 | m -= nxt - cur - k; 27 | } 28 | } else { 29 | m += min(cur, cur - nxt + k); 30 | } 31 | } 32 | if (win) { 33 | cout << "YES" << endl; 34 | } else { 35 | cout << "NO" << endl; 36 | } 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /1201A/important-exam.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, m; 8 | cin >> n >> m; 9 | vector s(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> s[i]; 12 | } 13 | vector a(m); 14 | for (int i = 0; i < m; i++) { 15 | cin >> a[i]; 16 | } 17 | vector mf(m); 18 | for (int j = 0; j < m; j++) { 19 | map cnt; 20 | for (int i = 0; i < n; i++) { 21 | cnt[s[i][j]]++; 22 | } 23 | int mxc = 0; 24 | for (auto [key, value]: cnt) { 25 | if (value > mxc) { 26 | mf[j] = key; 27 | mxc = value; 28 | } 29 | } 30 | } 31 | int ans = 0; 32 | for (int i = 0; i < n; i++) { 33 | for (int j = 0; j < m; j++) { 34 | if (s[i][j] == mf[j]) { 35 | ans += a[j]; 36 | } 37 | } 38 | } 39 | cout << ans << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /1201B/zero-array.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int n; 9 | cin >> n; 10 | priority_queue pq; 11 | for (int i = 0; i < n; i++) { 12 | int a; 13 | cin >> a; 14 | pq.push(a); 15 | } 16 | bool possible = true; 17 | while (true) { 18 | auto first = pq.top(); pq.pop(); 19 | auto second = pq.top(); pq.pop(); 20 | if (first == 0 or second == 0) { 21 | possible = first == 0 and second == 0; 22 | break; 23 | } 24 | int common = min(first, second); 25 | pq.push(first - common); 26 | pq.push(second - common); 27 | } 28 | if (possible) { 29 | cout << "YES" << endl; 30 | } else { 31 | cout << "NO" << endl; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /1202D/print-a-1337-string.cpp: -------------------------------------------------------------------------------- 1 | // TLE 2 | #include 3 | using namespace std; 4 | 5 | long long C2(long long n) { 6 | return n * (n - 1) / 2; 7 | } 8 | 9 | int main() { 10 | ios::sync_with_stdio(false); 11 | cin.tie(nullptr); 12 | int t; 13 | cin >> t; 14 | while (t--) { 15 | long long n; 16 | cin >> n; 17 | long long y = 2; 18 | long long tmp = 3; 19 | while (C2(tmp) <= n) { 20 | if (n % C2(tmp) == 0) { 21 | y = tmp; 22 | } 23 | tmp++; 24 | } 25 | long long x = n / C2(y); 26 | for (auto i = 0; i < x; i++) { 27 | cout << "1"; 28 | } 29 | for (auto i = 0; i < y; i++) { 30 | cout << "3"; 31 | } 32 | cout << "7" << endl; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /1203B/equal-rectangles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int q; 8 | cin >> q; 9 | while (q--) { 10 | int n; 11 | cin >> n; 12 | int m = 4 * n; 13 | vector a(m); 14 | for (int i = 0; i < m; i++) { 15 | cin >> a[i]; 16 | } 17 | sort(a.begin(), a.end()); 18 | bool ok = true; 19 | int area = -1; 20 | for (int i = 0; i < m; i += 2) { 21 | if (a[i] != a[i + 1] or a[m - 1 - i] != a[m - 2 - i]) { 22 | ok = false; 23 | break; 24 | } 25 | int tmp = a[i] * a[m - 1 - i]; 26 | if (area == -1) { 27 | area = tmp; 28 | } 29 | if (area != tmp) { 30 | ok = false; 31 | break; 32 | } 33 | } 34 | if (ok) { 35 | cout << "YES" << endl; 36 | } else { 37 | cout << "NO" << endl; 38 | } 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /1203C/common-divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | long long cmn = 0; 14 | for (int i = 0; i < n; i++) { 15 | cmn = gcd(cmn, a[i]); 16 | } 17 | set factors; 18 | for (long long i = 1; i * i <= cmn; i++) { 19 | if (cmn % i == 0) { 20 | factors.insert(i); 21 | factors.insert(cmn / i); 22 | } 23 | } 24 | cout << factors.size() << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1203D1/remove-the-substring-easy-version.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool subseq(string& s, string& t) { 5 | int i = 0, j = 0; 6 | while (i < s.size() and j < t.size()) { 7 | if (s[i] == t[j]) { 8 | j++; 9 | } 10 | i++; 11 | } 12 | return j == t.size(); 13 | } 14 | 15 | int main() { 16 | ios::sync_with_stdio(false); 17 | cin.tie(nullptr); 18 | string s, t; 19 | cin >> s >> t; 20 | int ans = 0; 21 | for (int l = 0; l < s.size(); l++) { 22 | for (int r = 0; r < s.size(); r++) { 23 | string ns = s.substr(0, l) + s.substr(r + 1); 24 | if (subseq(ns, t)) { 25 | ans = max(ans, r - l + 1); 26 | } 27 | } 28 | } 29 | cout << ans << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1203E/boxers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | sort(a.begin(), a.end()); 14 | set s; 15 | for (int i = 0; i < n; i++) { 16 | int old = s.size(); 17 | if (a[i] - 1 > 0) { 18 | s.insert(a[i] - 1); 19 | } 20 | if (s.size() == old) { 21 | s.insert(a[i]); 22 | } 23 | if (s.size() == old) { 24 | s.insert(a[i] + 1); 25 | } 26 | } 27 | cout << s.size() << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1204A/bowwow-and-the-timetable.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | string s; 8 | cin >> s; 9 | bool pot = s.find('1', 1) == string::npos; 10 | int ans = pot ? s.size() / 2 : (s.size() + 1) / 2; 11 | cout << ans << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /1204B/mislove-has-lost-an-array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, l, r; 8 | cin >> n >> l >> r; 9 | int mns = (1 << l) - 1 + (n - l); 10 | int mxs = (1 << r) - 1 + (n - r) * (1 << r >> 1); 11 | cout << mns << " " << mxs << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /1206A/choose-two-numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | int amx = -1; 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | amx = max(amx, a[i]); 14 | } 15 | int m; 16 | cin >> m; 17 | vector b(m); 18 | int bmx = -1; 19 | for (int i = 0; i < m; i++) { 20 | cin >> b[i]; 21 | bmx = max(bmx, b[i]); 22 | } 23 | cout << amx << " " << bmx << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1206B/make-product-equal-one.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | sort(a.begin(), a.end()); 14 | long long ans = 0; 15 | for (int i = 0; i < n - 1; i += 2) { 16 | long long s1 = abs(a[i] + 1) + abs(a[i + 1] + 1); 17 | long long s2 = abs(a[i] - 1) + abs(a[i + 1] - 1); 18 | ans += min(s1, s2); 19 | } 20 | if (n % 2 == 1) { 21 | ans += abs(a.back() - 1); 22 | } 23 | cout << ans << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1206C/almost-equal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector p(2 * n); 10 | for (int i = 0; i < n; i++) { 11 | p[i] = 2 * i; 12 | p[i + n] = 2 * i + 1; 13 | if (i % 2 == 1) { 14 | swap(p[i], p[i + n]); 15 | } 16 | } 17 | if (n % 2 == 1) { 18 | cout << "YES" << endl; 19 | for (auto pi: p) { 20 | cout << pi + 1 << " "; 21 | } 22 | cout << endl; 23 | } else { 24 | cout << "NO" << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1207A/there-are-two-types-of-burgers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int b, p, f; 11 | cin >> b >> p >> f; 12 | int h, c; 13 | cin >> h >> c; 14 | int ans = 0; 15 | if (h > c) { 16 | ans += h * min(b / 2, p); 17 | b -= 2 * min(b / 2, p); 18 | ans += c * min(b / 2, f); 19 | b -= 2 * min(b / 2, f); 20 | } else { 21 | ans += c * min(b / 2, f); 22 | b -= 2 * min(b / 2, f); 23 | ans += h * min(b / 2, p); 24 | b -= 2 * min(b / 2, p); 25 | } 26 | cout << ans << endl; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1207C/gas-pipeline.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (long long) 1e18 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | long long n, a, b; 12 | cin >> n >> a >> b; 13 | string s; 14 | cin >> s; 15 | long long memo[n + 1][2]; 16 | for (int i = 0; i < n + 1; i++) { 17 | memo[i][0] = memo[i][1] = INF; 18 | } 19 | memo[0][0] = b; 20 | for (int i = 0; i < n; i++) { 21 | if (s[i] == '0') { 22 | memo[i + 1][0] = min(memo[i][0] + a + b, memo[i][1] + 2 * a + b); 23 | memo[i + 1][1] = min(memo[i][1] + a + 2 * b, memo[i][0] + 2 * a + 2 * b); 24 | } else { 25 | memo[i + 1][1] = memo[i][1] + a + 2 * b; 26 | } 27 | } 28 | cout << memo[n][0] << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1208A/xorinacci.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | long long a, b, n; 11 | cin >> a >> b >> n; 12 | if (n % 3 == 0) { 13 | cout << a << endl; 14 | } else if (n % 3 == 1) { 15 | cout << b << endl; 16 | } else { 17 | cout << (a xor b) << endl; 18 | } 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1208B/uniqueness.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | int ans = n; 14 | set s; 15 | for (auto ai: a) { 16 | if (s.count(ai) > 0) { 17 | break; 18 | } 19 | s.insert(ai); 20 | } 21 | // If a prefix of size L has a suffix of size R, 22 | // then a prefix of size L - 1 will have a suffix of size ≥ R. 23 | int l = s.size(), r = n; 24 | for (int i = l; i > -1; i--) { 25 | while (r > i) { 26 | if (s.count(a[r - 1]) > 0) { 27 | break; 28 | } 29 | s.insert(a[r - 1]); 30 | r--; 31 | } 32 | ans = min(ans, r - i); 33 | if (i > 0) { 34 | s.erase(a[i - 1]); 35 | } 36 | } 37 | cout << ans << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /1208C/magic-grid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | int g[n][n]; 10 | int nxt = 0; 11 | for (int r = 0; r < n; r += 4) { 12 | for (int c = 0; c < n; c += 4) { 13 | for (int i = 0; i < 4; i++) { 14 | for (int j = 0; j < 4; j++) { 15 | g[r + i][c + j] = nxt; 16 | nxt++; 17 | } 18 | } 19 | } 20 | } 21 | for (int r = 0; r < n; r++) { 22 | for (int c = 0; c < n; c++) { 23 | cout << g[r][c] << " "; 24 | } 25 | cout << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /1213A/chips-moving.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | int even = 0, odd = 0; 10 | for (int i = 0; i < n; i++) { 11 | int x; 12 | cin >> x; 13 | if (x % 2 == 0) { 14 | even++; 15 | } else { 16 | odd++; 17 | } 18 | } 19 | cout << min(even, odd) << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1213B/bad-prices.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n; 12 | cin >> n; 13 | vector a(n); 14 | for (int i = 0; i < n; i++) { 15 | cin >> a[i]; 16 | } 17 | vector mns(n); 18 | int mn = INF; 19 | for (int i = n - 1; i > -1; i--) { 20 | mn = min(mn, a[i]); 21 | mns[i] = mn; 22 | } 23 | int ans = 0; 24 | for (int i = 0; i < n - 1; i++) { 25 | if (a[i] > mns[i + 1]) { 26 | ans++; 27 | } 28 | } 29 | cout << ans << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1213D1/equalizing-by-division-easy-version.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int n, k; 9 | cin >> n >> k; 10 | vector a(n); 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | } 14 | sort(a.begin(), a.end()); 15 | map> kit; 16 | for (int i = 0; i < n; i++) { 17 | kit[a[i]].push_back(0); 18 | for (int j = 1; a[i] > 0; j++) { 19 | a[i] /= 2; 20 | kit[a[i]].push_back(j); 21 | } 22 | } 23 | int ans = INF; 24 | for (auto [_, it]: kit) { 25 | if (it.size() >= k) { 26 | int sum = accumulate(it.begin(), it.begin() + k, 0); 27 | ans = min(ans, sum); 28 | } 29 | } 30 | cout << ans << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1213D2/equalizing-by-division-hard-version.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int n, k; 9 | cin >> n >> k; 10 | vector a(n); 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | } 14 | sort(a.begin(), a.end()); 15 | map> kit; 16 | for (int i = 0; i < n; i++) { 17 | kit[a[i]].push_back(0); 18 | for (int j = 1; a[i] > 0; j++) { 19 | a[i] /= 2; 20 | kit[a[i]].push_back(j); 21 | } 22 | } 23 | int ans = INF; 24 | for (auto [_, it]: kit) { 25 | if (it.size() >= k) { 26 | int sum = accumulate(it.begin(), it.begin() + k, 0); 27 | ans = min(ans, sum); 28 | } 29 | } 30 | cout << ans << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1217A/creating-a-character.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | long long s, i, e; 11 | cin >> s >> i >> e; 12 | long long ans = 0; 13 | if (s + e > i) { 14 | if (i >= s) { 15 | e -= i - s + 1; 16 | s = i + 1; 17 | } 18 | long long diff = s + e - i; 19 | ans = min(e + 1, diff / 2 + diff % 2); 20 | } 21 | cout << ans << endl; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /1217B/zmei-gorynich.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, x; 11 | cin >> n >> x; 12 | vector d(n), h(n); 13 | int mxd = 0, mxdh = 0; 14 | for (int i = 0; i < n; i++) { 15 | cin >> d[i] >> h[i]; 16 | mxd = max(mxd, d[i]); 17 | mxdh = max(mxdh, d[i] - h[i]); 18 | } 19 | int ans = x <= mxd ? 1 : -1; 20 | if (x > mxd and mxdh > 0) { 21 | ans = ceil(1.0 * (x - mxd) / mxdh) + 1; 22 | } 23 | cout << ans << endl; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1217C/the-number-of-good-substrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | string s; 11 | cin >> s; 12 | int n = s.size(); 13 | vector zbefore(n); 14 | int z = 0; 15 | for (int i = 0; i < n; i++) { 16 | zbefore[i] = z; 17 | z = s[i] == '0' ? z + 1 : 0; 18 | } 19 | long long ans = 0; 20 | for (int i = 0; i < n; i++) { 21 | if (s[i] == '1') { 22 | long long num = 0; 23 | int j = i; 24 | while (j < n) { 25 | num = 2 * num + (s[j] - '0'); 26 | if (j - i + 1 + zbefore[i] < num) { 27 | break; 28 | } 29 | ans++; 30 | j++; 31 | } 32 | } 33 | } 34 | cout << ans << endl; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /1237A/balanced-rating-changes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | bool debt = true; 10 | while (n--) { 11 | int a; 12 | cin >> a; 13 | int b; 14 | if (a % 2 == 0) { 15 | b = a / 2; 16 | } else { 17 | if (debt) { 18 | b = ceil(a / 2.0); 19 | } else { 20 | b = floor(a / 2.0); 21 | } 22 | debt = !debt; 23 | } 24 | cout << b << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1237B/balanced-tunnel.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | vector b(n); 14 | for (int i = 0; i < n; i++) { 15 | cin >> b[i]; 16 | } 17 | set s; 18 | int i = n - 1, j = n - 1; 19 | while (i >= 0 and j >= 0) { 20 | if (s.count(b[j]) > 0) { 21 | j--; 22 | } else if (a[i] == b[j]) { 23 | i--, j--; 24 | } else { 25 | s.insert(a[i]); 26 | i--; 27 | } 28 | } 29 | cout << s.size() << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1335B/construct-the-string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, a, b; 11 | cin >> n >> a >> b; 12 | string s; 13 | for (int i = 0, j = 0; i < n; i++, j++) { 14 | s += (j % b) + 'a'; 15 | } 16 | cout << s << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1352F/binary-string-reconstruction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n0, n1, n2; 11 | cin >> n0 >> n1 >> n2; 12 | string s; 13 | if (n0 > 0) { 14 | s += string(n0 + 1, '0'); 15 | } 16 | if (n2 > 0) { 17 | s += string(n2 + 1, '1'); 18 | } 19 | if (n0 > 0 and n2 > 0) { 20 | n1--; 21 | } 22 | if (n0 == 0 and n2 == 0) { 23 | n1++; 24 | } 25 | int nxt = s.back() == '0' ? 1 : 0; 26 | for (int i = 0; i < n1; i++) { 27 | s += '0' + nxt; 28 | nxt = !nxt; 29 | } 30 | cout << s << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /1409A/yet-another-two-integers-problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int a, b; 11 | cin >> a >> b; 12 | int ans = (abs(a - b) + 9) / 10; 13 | cout << ans << endl; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /1409B/minimum-product.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (long long) 1e18 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | long long a, b, x, y, n; 12 | cin >> a >> b >> x >> y >> n; 13 | if (max(a - n, x) > max(b - n, y)) { 14 | swap(a, b); 15 | swap(x, y); 16 | } 17 | auto na = max(a - n, x); 18 | auto sa = a - na; 19 | n = max(n - sa, 0LL); 20 | a = max(na, x); 21 | auto nb = max(b - n, y); 22 | auto sb = b - nb; 23 | n = max(n - sb, 0LL); 24 | b = max(nb, y); 25 | cout << a * b << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /1409C/yet-another-array-restoration.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, x, y; 11 | cin >> n >> x >> y; 12 | int pos, dif; 13 | for (int i = 1; i < n; i++) { 14 | if ((y - x) % (n - i) == 0 and (y - x) / (n - i) > 0) { 15 | dif = (y - x) / (n - i); 16 | pos = i - 1; 17 | break; 18 | } 19 | } 20 | int mn = x - pos * dif; 21 | while (mn <= 0) { 22 | mn += dif; 23 | } 24 | int mx = mn + (n - 1) * dif; 25 | for (int val = mn; val <= mx; val += dif) { 26 | cout << val << " "; 27 | } 28 | cout << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1426A/floor-number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, x; 11 | cin >> n >> x; 12 | int f = 0; 13 | while (n > f * x + 2) { 14 | f++; 15 | } 16 | cout << f + 1 << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1426B/symmetric-matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, m; 11 | cin >> n >> m; 12 | bool symmetric = false; 13 | for (int i = 0; i < n; i++) { 14 | int tl, tr, bl, br; 15 | cin >> tl >> tr >> bl >> br; 16 | if (tr == bl) { 17 | symmetric = true; 18 | } 19 | } 20 | if (m % 2 == 0 and symmetric) { 21 | cout << "YES" << endl; 22 | } else { 23 | cout << "NO" << endl; 24 | } 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1426C/increase-and-copy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | int ans = 2 * sqrt(n) - 1; 13 | int rn = sqrt(n); 14 | if (rn * rn == n) { 15 | ans--; 16 | } 17 | cout << ans << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1426D/non-zero-segments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | int ans = 0; 14 | long long ps = 0; 15 | set s; 16 | s.insert(0); 17 | for (int i = 0; i < n; i++) { 18 | ps += a[i]; 19 | if (s.count(ps) > 0) { 20 | s.clear(); 21 | s.insert(0); 22 | ps = a[i]; 23 | ans++; 24 | } 25 | s.insert(ps); 26 | } 27 | cout << ans << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1440A/buy-the-string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, c0, c1, h; 11 | cin >> n >> c0 >> c1 >> h; 12 | string s; 13 | cin >> s; 14 | int ans = 0; 15 | for (int i = 0; i < n; i++) { 16 | if (s[i] == '0') { 17 | ans += min(c0, c1 + h); 18 | } else { 19 | ans += min(c1, c0 + h); 20 | } 21 | } 22 | cout << ans << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1440B/sum-of-medians.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, k; 11 | cin >> n >> k; 12 | vector a(n * k); 13 | for (int i = 0; i < n * k; i++) { 14 | cin >> a[i]; 15 | } 16 | long long ans = 0; 17 | int l = ceil(n / 2.0) - 1; 18 | int r = n - l; 19 | int i = 0, j = n * k; 20 | while (i < j) { 21 | i += l; 22 | j -= r; 23 | ans += a[j]; 24 | } 25 | cout << ans << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /1447A/add-candies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | cout << n - 1 << endl; 13 | for (int i = 2; i <= n; i++) { 14 | cout << i << " "; 15 | } 16 | cout << endl; 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /1447B/numbers-box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(nullptr); 8 | int t; 9 | cin >> t; 10 | while (t--) { 11 | int n, m; 12 | cin >> n >> m; 13 | int ans = 0; 14 | int mn = INF, ncnt = 0; 15 | for (int r = 0; r < n; r++) { 16 | for (int c = 0; c < m; c++) { 17 | int a; 18 | cin >> a; 19 | if (a < 0) { 20 | ncnt++; 21 | } 22 | mn = min(mn, abs(a)); 23 | ans += abs(a); 24 | } 25 | } 26 | if (ncnt % 2 == 1) { 27 | ans -= 2 * mn; 28 | } 29 | cout << ans << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1451A/subtract-or-divide.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | int ans = 0; 13 | if (n <= 3) { 14 | ans = n - 1; 15 | } else if (n % 2 == 0) { 16 | ans = 2; 17 | } else { 18 | ans = 3; 19 | } 20 | cout << ans << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /1451B/non-substring-subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, q; 11 | cin >> n >> q; 12 | string s; 13 | cin >> s; 14 | for (int i = 0; i < q; i++) { 15 | int l, r; 16 | cin >> l >> r; 17 | l--, r--; 18 | bool found = false; 19 | for (int i = 0; i < l; i++) { 20 | if (s[i] == s[l]) { 21 | found = true; 22 | break; 23 | } 24 | } 25 | for (int i = r + 1; i < s.size(); i++) { 26 | if (s[i] == s[r]) { 27 | found = true; 28 | break; 29 | } 30 | } 31 | if (found) { 32 | cout << "YES" << endl; 33 | } else { 34 | cout << "NO" << endl; 35 | } 36 | } 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /1451C/string-equality.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, k; 11 | cin >> n >> k; 12 | string a, b; 13 | cin >> a >> b; 14 | vector ac(26), bc(26); 15 | for (int i = 0; i < n; i++) { 16 | ac[a[i] - 'a']++; 17 | bc[b[i] - 'a']++; 18 | } 19 | bool ok = true; 20 | for (int i = 0; i < 26; i++) { 21 | int diff = ac[i] - bc[i]; 22 | if (diff < 0 or diff % k > 0) { 23 | ok = false; 24 | break; 25 | } 26 | if (i < 25) { 27 | ac[i + 1] += diff; 28 | } 29 | } 30 | if (ok) { 31 | cout << "Yes" << endl; 32 | } else { 33 | cout << "No" << endl; 34 | } 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /1454A/special-permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | vector p(n); 13 | for (int i = 0; i < n; i++) { 14 | p[i] = n - i; 15 | } 16 | swap(p[n / 2], p[n - 1]); 17 | for (int i = 0; i < n; i++) { 18 | cout << p[i] << " "; 19 | } 20 | cout << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /1454B/unique-bid-auction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | map memo, pos; 13 | for (int i = 0; i < n; i++) { 14 | int a; 15 | cin >> a; 16 | memo[a]++; 17 | pos[a] = i; 18 | } 19 | int ans = -1; 20 | for (auto [num, cnt]: memo) { 21 | if (cnt == 1) { 22 | ans = pos[num] + 1; 23 | break; 24 | } 25 | } 26 | cout << ans << endl; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1454C/sequence-transformation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | vector a(n); 13 | map memo; 14 | for (int i = 0; i < n; i++) { 15 | cin >> a[i]; 16 | memo[a[i]]++; 17 | } 18 | if (memo.size() == 1) { 19 | cout << 0 << endl; 20 | continue; 21 | } 22 | memo[a[0]]--; 23 | memo[a[n - 1]]--; 24 | for (int i = 0; i < n - 1; i++) { 25 | if (a[i] == a[i + 1]) { 26 | memo[a[i]]--; 27 | } 28 | } 29 | int ans = n; 30 | for (auto [num, cnt]: memo) { 31 | ans = min(ans, cnt); 32 | } 33 | cout << ans + 1 << endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /1469A/regular-bracket-sequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | string s; 11 | cin >> s; 12 | bool ok = true; 13 | int q = 0; 14 | for (int i = 0; i < s.size(); i++) { 15 | if (s[i] == '?') { 16 | q++; 17 | } 18 | } 19 | int score = 0, qc = 0; 20 | for (int i = 0; i < s.size(); i++) { 21 | if (score < 0) { 22 | ok = false; 23 | } 24 | if (s[i] == '?') { 25 | s[i] = qc < q / 2 ? '(' : ')'; 26 | qc++; 27 | } 28 | if (s[i] == '(') { 29 | score++; 30 | } else { 31 | score--; 32 | } 33 | } 34 | if (ok and score == 0) { 35 | cout << "YES" << endl; 36 | } else { 37 | cout << "NO" << endl; 38 | } 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /1469B/red-and-blue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | vector r(n); 13 | for (int i = 0; i < n; i++) { 14 | cin >> r[i]; 15 | } 16 | int m; 17 | cin >> m; 18 | vector b(m); 19 | for (int i = 0; i < m; i++) { 20 | cin >> b[i]; 21 | } 22 | vector rs(n), bs(m); 23 | partial_sum(r.begin(), r.end(), rs.begin()); 24 | partial_sum(b.begin(), b.end(), bs.begin()); 25 | int rmx = *max_element(rs.begin(), rs.end()); 26 | int bmx = *max_element(bs.begin(), bs.end()); 27 | cout << max({0, rmx, bmx, rmx + bmx}) << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1669A/division.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int r; 11 | cin >> r; 12 | if (r >= 1900) { 13 | cout << "Division 1" << "\n"; 14 | } else if (r >= 1600 and r < 1900) { 15 | cout << "Division 2" << "\n"; 16 | } else if (r >= 1400 and r < 1600) { 17 | cout << "Division 3" << "\n"; 18 | } else { 19 | cout << "Division 4" << "\n"; 20 | } 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /1669B/triple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | vector counter(n + 1, 0); 13 | for (int i = 0; i < n; i++) { 14 | int a; 15 | cin >> a; 16 | counter[a]++; 17 | } 18 | bool found = false; 19 | for (int i = 0; i < (int) counter.size(); i++) { 20 | if (counter[i] >= 3) { 21 | found = true; 22 | cout << i << "\n"; 23 | break; 24 | } 25 | } 26 | if (!found) { 27 | cout << -1 << "\n"; 28 | } 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1669C/odd-even-increments.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | vector a(n); 13 | for (int i = 0; i < n; i++) { 14 | cin >> a[i]; 15 | } 16 | bool ok = true; 17 | for (int i = 0; i < n; i += 2) { 18 | if (a[i] % 2 != a[0] % 2) { 19 | ok = false; 20 | break; 21 | } 22 | } 23 | for (int i = 1; i < n; i += 2) { 24 | if (a[i] % 2 != a[1] % 2) { 25 | ok = false; 26 | break; 27 | } 28 | } 29 | if (ok) { 30 | cout << "YES" << "\n"; 31 | } else { 32 | cout << "NO" << "\n"; 33 | } 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /1669D/colorful-stamp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool valid(int r, int b) { 5 | bool c1 = r > 0 and b == 0; 6 | bool c2 = r == 0 and b > 0; 7 | if (c1 or c2) { 8 | return false; 9 | } 10 | return true; 11 | } 12 | 13 | int main() { 14 | ios::sync_with_stdio(false); 15 | cin.tie(nullptr); 16 | int t; 17 | cin >> t; 18 | while (t--) { 19 | int n; 20 | cin >> n; 21 | string s; 22 | cin >> s; 23 | bool ok = true; 24 | int r = 0, b = 0; 25 | for (int i = 0; i < n; i++) { 26 | if (s[i] == 'R') { 27 | r++; 28 | } else if (s[i] == 'B') { 29 | b++; 30 | } else { 31 | ok = ok and valid(r, b); 32 | r = 0, b = 0; 33 | } 34 | } 35 | ok = ok and valid(r, b); 36 | if (ok) { 37 | cout << "YES" << "\n"; 38 | } else { 39 | cout << "NO" << "\n"; 40 | } 41 | } 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /1669E/2-letter-strings-v2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | map memo; 13 | long long ans = 0; 14 | for (int i = 0; i < n; i++) { 15 | string s; 16 | cin >> s; 17 | for (int j = 0; j < 2; j++) { 18 | for (char c = 'a'; c <= 'k'; c++) { 19 | if (s[j] == c) { 20 | continue; 21 | } 22 | string ns = s; 23 | ns[j] = c; 24 | ans += memo[ns]; 25 | } 26 | } 27 | memo[s]++; 28 | } 29 | cout << ans << "\n"; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1669F/eating-candies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | vector w(n); 13 | for (int i = 0; i < n; i++) { 14 | cin >> w[i]; 15 | } 16 | int ans = 0; 17 | long long a = 0, b = 0; 18 | int ac = 0, bc = 0; 19 | int i = 0, j = n - 1; 20 | while (i <= j) { 21 | if (a < b) { 22 | a += w[i]; 23 | ac++; 24 | i++; 25 | } else if (a > b) { 26 | b += w[j]; 27 | bc++; 28 | j--; 29 | } else if (w[i] < w[j]) { 30 | a += w[i]; 31 | ac++; 32 | i++; 33 | } else { 34 | b += w[j]; 35 | bc++; 36 | j--; 37 | } 38 | if (a == b) { 39 | ans = max(ans, ac + bc); 40 | } 41 | } 42 | cout << ans << "\n"; 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /1669H/maximal-and.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, k; 11 | cin >> n >> k; 12 | vector> a(n); 13 | for (int i = 0; i < n; i++) { 14 | int num; 15 | cin >> num; 16 | a[i] = bitset<31>(num); 17 | } 18 | bitset<31> ans(INT_MAX); 19 | for (int i = 0; i < n; i++) { 20 | ans &= a[i]; 21 | } 22 | for (int i = 30; i > -1; i--) { 23 | int cnt = 0; 24 | for (int j = 0; j < n; j++) { 25 | if (a[j].test(i) == 0) { 26 | cnt++; 27 | } 28 | } 29 | if (cnt <= k) { 30 | k -= cnt; 31 | ans.set(i); 32 | } 33 | } 34 | cout << ans.to_ulong() << "\n"; 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /1676A/lucky.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | string s; 11 | cin >> s; 12 | int a = s[0] - '0', b = s[1] - '0', c = s[2] - '0'; 13 | int x = s[3] - '0', y = s[4] - '0', z = s[5] - '0'; 14 | if (a + b + c == x + y + z) { 15 | cout << "YES" << "\n"; 16 | } else { 17 | cout << "NO" << "\n"; 18 | } 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /1676B/equal-candies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n; 11 | cin >> n; 12 | vector a(n); 13 | long long mn = INT_MAX; 14 | for (int i = 0; i < n; i++) { 15 | cin >> a[i]; 16 | mn = min(mn, a[i]); 17 | } 18 | long long ans = 0; 19 | for (int i = 0; i < n; i++) { 20 | ans += a[i] - mn; 21 | } 22 | cout << ans << "\n"; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1676C/most-similar-words.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long cost(string s, string t) { 5 | long long c = 0; 6 | for (int i = 0; i < s.size(); i++) { 7 | c += abs(s[i] - t[i]); 8 | } 9 | return c; 10 | } 11 | 12 | int main() { 13 | ios::sync_with_stdio(false); 14 | cin.tie(nullptr); 15 | int t; 16 | cin >> t; 17 | while (t--) { 18 | int n, m; 19 | cin >> n >> m; 20 | vector s(n); 21 | for (int i = 0; i < n; i++) { 22 | cin >> s[i]; 23 | } 24 | long long ans = INT_MAX; 25 | for (int i = 0; i < n - 1; i++) { 26 | for (int j = i + 1; j < n; j++) { 27 | ans = min(ans, cost(s[i], s[j])); 28 | } 29 | } 30 | cout << ans << "\n"; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /1676E/eating-queries.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int t; 8 | cin >> t; 9 | while (t--) { 10 | int n, q; 11 | cin >> n >> q; 12 | vector a(n); 13 | for (int i = 0; i < n; i++) { 14 | cin >> a[i]; 15 | } 16 | sort(a.rbegin(), a.rend()); 17 | vector ps(n); 18 | partial_sum(a.begin(), a.end(), ps.begin()); 19 | while (q--) { 20 | long long x; 21 | cin >> x; 22 | auto lower = lower_bound(ps.begin(), ps.end(), x); 23 | if (lower == ps.end()) { 24 | cout << -1 << "\n"; 25 | } else { 26 | cout << lower - ps.begin() + 1 << "\n"; 27 | } 28 | } 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1676H1/maximum-crossings-easy-version.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | using namespace __gnu_pbds; 5 | 6 | template 7 | using ordered_set = tree, rb_tree_tag, 8 | tree_order_statistics_node_update>; 9 | 10 | int main() { 11 | ios::sync_with_stdio(false); 12 | cin.tie(nullptr); 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | int n; 17 | cin >> n; 18 | vector a(n); 19 | for (int i = 0; i < n; i++) { 20 | cin >> a[i]; 21 | } 22 | ordered_set> s; 23 | long long ans = 0; 24 | for (int i = 0; i < n; i++) { 25 | int k = s.order_of_key({a[i], INT_MIN}); 26 | ans += s.size() - k; 27 | s.insert({a[i], i}); 28 | } 29 | cout << ans << "\n"; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1676H2/maximum-crossings-hard-version.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | using namespace __gnu_pbds; 5 | 6 | template 7 | using ordered_set = tree, rb_tree_tag, 8 | tree_order_statistics_node_update>; 9 | 10 | int main() { 11 | ios::sync_with_stdio(false); 12 | cin.tie(nullptr); 13 | int t; 14 | cin >> t; 15 | while (t--) { 16 | int n; 17 | cin >> n; 18 | vector a(n); 19 | for (int i = 0; i < n; i++) { 20 | cin >> a[i]; 21 | } 22 | ordered_set> s; 23 | long long ans = 0; 24 | for (int i = 0; i < n; i++) { 25 | int k = s.order_of_key({a[i], INT_MIN}); 26 | ans += s.size() - k; 27 | s.insert({a[i], i}); 28 | } 29 | cout << ans << "\n"; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1692E/generator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int rand(int a, int b) { 5 | return a + rand() % (b - a + 1); 6 | } 7 | 8 | int main(int argc, char** argv) { 9 | int seed = stoi(argv[1]); 10 | srand(seed); 11 | cout << 1 << "\n"; 12 | int n = rand(1, 10); 13 | int s = rand(1, n); 14 | cout << n << " " << s << "\n"; 15 | for (int i = 0; i < n; i++) { 16 | cout << rand(0, 1) << " "; 17 | } 18 | cout << "\n"; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /1692E/naive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(vector& a, int l, int r, int sa, int s) { 5 | if (sa == s) { 6 | return 0; 7 | } 8 | int o1 = 1 + solve(a, l + 1, r, sa - a[l], s); 9 | int o2 = 1 + solve(a, l, r - 1, sa - a[r], s); 10 | return min(o1, o2); 11 | } 12 | 13 | int main() { 14 | int t; 15 | cin >> t; 16 | while (t--) { 17 | int n, s; 18 | cin >> n >> s; 19 | vector a(n); 20 | for (int i = 0; i < n; i++) { 21 | cin >> a[i]; 22 | } 23 | int sa = accumulate(a.begin(), a.end(), 0); 24 | if (sa < s) { 25 | cout << -1 << "\n"; 26 | continue; 27 | } 28 | int ans = solve(a, 0, n - 1, sa, s); 29 | cout << ans << "\n"; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /1692E/stress.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++20 generator.cpp -o generator 2 | g++ -std=c++20 binary-deque.cpp -o original 3 | g++ -std=c++20 naive.cpp -o naive 4 | 5 | for ((i = 0; i < 10000; i++)); do 6 | echo "Test: " $i 7 | ./generator $i >sin 8 | ./original souto 9 | ./naive soutn 10 | diff -w souto soutn || break 11 | done 12 | -------------------------------------------------------------------------------- /764A/taymyr-is-calling-you.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m, z; 6 | cin >> n >> m >> z; 7 | set t; 8 | for (int i = n; i <= z; i += n) { 9 | t.insert(i); 10 | } 11 | int ans = 0; 12 | for (int i = m; i <= z; i += m) { 13 | ans += t.count(i); 14 | t.insert(i); 15 | } 16 | cout << ans << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /764B/timofey-and-cubes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector c(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> c[i]; 10 | } 11 | for (int i = 0; i < n / 2; i++) { 12 | if (i % 2 == 0) { 13 | swap(c[i], c[n - 1 - i]); 14 | } 15 | } 16 | for (int k = 0; k < n; k++) { 17 | cout << c[k] << " "; 18 | } 19 | cout << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /765A/neverending-competitions.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | home = input() 3 | 4 | flights = [] 5 | while n > 0: 6 | s, t = input().split("->") 7 | flights.append(s) 8 | flights.append(t) 9 | n -= 1 10 | 11 | count = 0 12 | for flight in flights: 13 | if flight == home: 14 | count += 1 15 | 16 | if count % 2 == 0: 17 | print("home") 18 | else: 19 | print("contest") 20 | -------------------------------------------------------------------------------- /765B/code-obfuscation.py: -------------------------------------------------------------------------------- 1 | obfuscation = list(input()) 2 | letters = list("abcdefghijklmnopqrstuvwxyz") 3 | 4 | if len(obfuscation) == 1: 5 | if obfuscation[0] == 'a': 6 | print("YES") 7 | else: 8 | print("NO") 9 | else: 10 | while len(obfuscation) > 0: 11 | if obfuscation[0] == letters[0]: 12 | while letters[0] in obfuscation: 13 | obfuscation.remove(letters[0]) 14 | letters.remove(letters[0]) 15 | else: 16 | print("NO") 17 | exit() 18 | 19 | print("YES") 20 | -------------------------------------------------------------------------------- /765C/table-tennis-game-2.py: -------------------------------------------------------------------------------- 1 | k, a, b = [int(x) for x in input().split()] 2 | 3 | score_a = 0 4 | if a >= k: 5 | score_a = a // k 6 | 7 | score_b = 0 8 | if b >= k: 9 | score_b = b // k 10 | 11 | if score_a == 0 and score_b == 0: 12 | print(-1) 13 | elif score_a == 0: 14 | if b % k == 0: 15 | print(score_b) 16 | else: 17 | print(-1) 18 | elif score_b == 0: 19 | if a % k == 0: 20 | print(score_a) 21 | else: 22 | print(-1) 23 | else: 24 | print(score_a + score_b) 25 | -------------------------------------------------------------------------------- /766A/mahmoud-and-longest-uncommon-subsequence.py: -------------------------------------------------------------------------------- 1 | """ Created by Henrikh Kantuni on 2/7/17 """ 2 | 3 | a = input() 4 | b = input() 5 | ans = -1 if a == b else max(len(a), len(b)) 6 | print(ans) 7 | -------------------------------------------------------------------------------- /766B/mahmoud-and-a-triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector lens(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> lens[i]; 10 | } 11 | sort(lens.begin(), lens.end()); 12 | bool triangle = false; 13 | for (int i = 0; i < lens.size() - 2; i++) { 14 | if (lens[i] + lens[i + 1] > lens[i + 2]) { 15 | triangle = true; 16 | break; 17 | } 18 | } 19 | if (triangle) { 20 | cout << "YES" << endl; 21 | } else { 22 | cout << "NO" << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /767A/snacktower.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector s(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> s[i]; 10 | } 11 | vector pos(n + 1); 12 | for (int i = 1; i < n; i++) { 13 | pos[s[i]] = i; 14 | } 15 | for (int i = 0; i < pos[n]; i++) { 16 | cout << endl; 17 | } 18 | cout << s[pos[n]]; 19 | int prv = pos[n]; 20 | for (int i = n - 1; i > 0; i--) { 21 | if (pos[i] < prv) { 22 | cout << " " << s[pos[i]]; 23 | } else { 24 | int bfr = pos[i] - prv; 25 | while (bfr--) { 26 | cout << endl; 27 | } 28 | cout << s[pos[i]]; 29 | prv = pos[i]; 30 | } 31 | } 32 | cout << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /768A/oath-of-the-nights-watch.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | strengths = [int(s) for s in input().split()] 3 | 4 | mx = max(strengths) 5 | mn = min(strengths) 6 | 7 | ans = 0 8 | for s in strengths: 9 | if mn < s < mx: 10 | ans += 1 11 | 12 | print(ans) 13 | -------------------------------------------------------------------------------- /768B/code-for-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | long long n, l, r; 8 | cin >> n >> l >> r; 9 | l--, r--; 10 | if (n == 0) { 11 | cout << 0 << endl; 12 | } else { 13 | set zeros; 14 | long long nc = n; 15 | for (int i = log2(nc); i > 0; i--) { 16 | long long sz = pow(2, i) - 1; 17 | if (nc % 2 == 0) { 18 | zeros.insert(sz); 19 | } 20 | nc /= 2; 21 | } 22 | long long tmp = 0; 23 | for (auto z: zeros) { 24 | long double d = 2 * (z + 1); 25 | long long lower = ceil((l - z) / d); 26 | long long upper = floor((r - z) / d); 27 | tmp += upper - lower + 1; 28 | } 29 | cout << r - l + 1 - tmp << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /769A/year-of-university-entrance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | vector a(n); 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | } 13 | sort(a.begin(), a.end()); 14 | cout << a[n / 2] << "\n"; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /769D/k-interesting-pairs-of-integers-v2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, k; 8 | cin >> n >> k; 9 | vector a(n); 10 | map freq; 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | freq[a[i]]++; 14 | } 15 | long long ans = 0; 16 | for (int i = 0; i < 10001; i++) { 17 | for (int j = i; j < 10001; j++) { 18 | int diff = __builtin_popcount(i ^ j); 19 | if (diff != k) { 20 | continue; 21 | } 22 | if (k == 0) { 23 | ans += freq[i] * (freq[i] - 1) / 2; 24 | } else { 25 | ans += freq[i] * freq[j]; 26 | } 27 | } 28 | } 29 | cout << ans << "\n"; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /770A/new-password.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, k; 8 | cin >> n >> k; 9 | string ans; 10 | for (int i = 0; ans.size() < n; i++) { 11 | ans += 'a' + i % k; 12 | } 13 | cout << ans << "\n"; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /771A/bear-and-friendship-condition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n, m; 5 | vector> g; 6 | vector color; 7 | 8 | int dfs(int u) { 9 | color[u] = 1; 10 | int cnt = 1; 11 | for (auto v: g[u]) { 12 | if (color[v] == 0) { 13 | cnt += dfs(v); 14 | } 15 | } 16 | return cnt; 17 | } 18 | 19 | int main() { 20 | ios::sync_with_stdio(false); 21 | cin.tie(nullptr); 22 | cin >> n >> m; 23 | g.resize(n); 24 | color.resize(n); 25 | for (int i = 0; i < m; i++) { 26 | int u, v; 27 | cin >> u >> v; 28 | u--, v--; 29 | g[u].push_back(v); 30 | g[v].push_back(u); 31 | } 32 | bool ok = true; 33 | long long ec = 0; 34 | for (int u = 0; u < n; u++) { 35 | long long uc = dfs(u); 36 | ec += uc * (uc - 1) / 2; 37 | } 38 | if (ok and ec == m) { 39 | cout << "YES" << endl; 40 | } else { 41 | cout << "NO" << endl; 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /771B/bear-and-different-names.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, k; 8 | cin >> n >> k; 9 | vector s(n - k + 1); 10 | for (int i = 0; i < s.size(); i++) { 11 | cin >> s[i]; 12 | } 13 | vector names(n); 14 | for (int i = 0; i < n; i++) { 15 | names[i] = 'A' + (i % 26); 16 | for (int j = 0; j < i / 26; j++) { 17 | names[i] += 'a'; 18 | } 19 | } 20 | for (int i = 0; i < s.size(); i++) { 21 | if (s[i] == "NO") { 22 | names[i + k - 1] = names[i]; 23 | } 24 | } 25 | for (auto name: names) { 26 | cout << name << " "; 27 | } 28 | cout << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /776A/a-serial-killer.py: -------------------------------------------------------------------------------- 1 | initial = input().split() 2 | n = int(input()) 3 | 4 | victims = [] 5 | m = n 6 | while m > 0: 7 | s, t = input().split() 8 | victims.append([s, t]) 9 | m -= 1 10 | 11 | answer = initial[0] + " " + initial[1] 12 | 13 | for i in range(len(victims)): 14 | if initial[0] == victims[i][0]: 15 | initial[0] = victims[i][1] 16 | answer += "\n" + initial[0] + " " + initial[1] 17 | elif initial[1] == victims[i][0]: 18 | initial[1] = victims[i][1] 19 | answer += "\n" + initial[0] + " " + initial[1] 20 | 21 | print(answer) 22 | -------------------------------------------------------------------------------- /776B/sherlock-and-his-girlfriend.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | n = int(input()) 4 | answer = "" 5 | if n < 3: 6 | print(1) 7 | else: 8 | print(2) 9 | 10 | m = n + 2 11 | numbers = [True] * m 12 | numbers[0] = False 13 | numbers[1] = False 14 | 15 | for i in range(2, int(math.sqrt(m)) + 1): 16 | j = 0 17 | while i ** 2 + j * i < m: 18 | numbers[i ** 2 + j * i] = False 19 | j += 1 20 | 21 | for i in range(2, m): 22 | if numbers[i]: 23 | answer += "1 " 24 | else: 25 | answer += "2 " 26 | 27 | answer = answer[:-1] 28 | print(answer) 29 | -------------------------------------------------------------------------------- /777A/shell-game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, x; 6 | cin >> n >> x; 7 | vector b(3); 8 | b[x] = 1; 9 | for (int i = n % 6; i > 0; i--) { 10 | if (i % 2 == 1) { 11 | swap(b[0], b[1]); 12 | } else { 13 | swap(b[1], b[2]); 14 | } 15 | } 16 | int ans = b[0] == 1 ? 0 : (b[1] == 1 ? 1 : 2); 17 | cout << ans << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /777B/game-of-credit-cards.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int mf(string s, string m) { 5 | int cnt = 0; 6 | for (auto num: s) { 7 | auto lower = lower_bound(m.begin(), m.end(), num); 8 | if (lower != m.end()) { 9 | m.erase(lower); 10 | cnt++; 11 | } 12 | } 13 | return s.size() - cnt; 14 | } 15 | 16 | int sf(string s, string m) { 17 | int cnt = 0; 18 | for (auto num: s) { 19 | auto upper = upper_bound(m.begin(), m.end(), num); 20 | if (upper != m.end()) { 21 | m.erase(upper); 22 | cnt++; 23 | } 24 | } 25 | return cnt; 26 | } 27 | 28 | int main() { 29 | int n; 30 | cin >> n; 31 | string s, m; 32 | cin >> s >> m; 33 | sort(s.begin(), s.end()); 34 | sort(m.begin(), m.end()); 35 | cout << mf(s, m) << endl; 36 | cout << sf(s, m) << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /779A/pupils-redistribution.py: -------------------------------------------------------------------------------- 1 | """ Created by Henrikh Kantuni on 2/26/17 """ 2 | 3 | n = int(input()) 4 | al = [int(x) for x in input().split()] 5 | bl = [int(x) for x in input().split()] 6 | a = { 7 | 1: al.count(1), 8 | 2: al.count(2), 9 | 3: al.count(3), 10 | 4: al.count(4), 11 | 5: al.count(5), 12 | } 13 | b = { 14 | 1: bl.count(1), 15 | 2: bl.count(2), 16 | 3: bl.count(3), 17 | 4: bl.count(4), 18 | 5: bl.count(5), 19 | } 20 | 21 | possible = True 22 | for key in a.keys(): 23 | if (a[key] + b[key]) % 2 != 0: 24 | possible = False 25 | break 26 | 27 | if possible: 28 | swaps = 0 29 | for key in a.keys(): 30 | if a[key] > b[key]: 31 | swaps += (a[key] - b[key]) // 2 32 | print(swaps) 33 | else: 34 | print(-1) 35 | -------------------------------------------------------------------------------- /779B/weird-rounding.py: -------------------------------------------------------------------------------- 1 | """ Created by Henrikh Kantuni on 2/26/17 """ 2 | 3 | n, k = [int(x) for x in input().split()] 4 | digits = list(str(n)) 5 | 6 | zero_count = digits.count('0') 7 | if zero_count < k: 8 | print(len(str(n)) - 1) 9 | exit() 10 | 11 | count = 0 12 | i = len(digits) - 1 13 | 14 | while int(''.join(digits)) % 10 ** k > 0: 15 | if digits[i] != '0': 16 | del digits[i] 17 | count += 1 18 | i -= 1 19 | 20 | print(count) 21 | -------------------------------------------------------------------------------- /779C/dishonest-sellers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | vector b(n); 12 | for (int i = 0; i < n; i++) { 13 | cin >> b[i]; 14 | } 15 | vector> diff(n); 16 | for (int i = 0; i < n; i++) { 17 | diff[i] = {a[i] - b[i], i}; 18 | } 19 | sort(diff.begin(), diff.end()); 20 | int ans = 0, pos = 0, min_index; 21 | while (k--) { 22 | min_index = diff[pos].second; 23 | ans += a[min_index]; 24 | a[min_index] = b[min_index] = 0; 25 | pos++; 26 | } 27 | while (pos < n and diff[pos].first < 0) { 28 | min_index = diff[pos].second; 29 | ans += a[min_index]; 30 | a[min_index] = b[min_index] = 0; 31 | pos++; 32 | } 33 | for (auto val: b) { 34 | ans += val; 35 | } 36 | cout << ans << endl; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /779C/dishonest-sellers.py: -------------------------------------------------------------------------------- 1 | """ Created by Henrikh Kantuni on 2/26/17 """ 2 | 3 | if __name__ == "__main__": 4 | n, k = [int(x) for x in input().split()] 5 | al = [int(x) for x in input().split()] 6 | bl = [int(x) for x in input().split()] 7 | 8 | diff = [] 9 | for i in range(n): 10 | diff.append((al[i] - bl[i], i)) 11 | 12 | diff.sort(key=lambda x: x[0]) 13 | 14 | total = 0 15 | while k > 0: 16 | min_index = diff[0][1] 17 | total += al[min_index] 18 | al[min_index] = 0 19 | bl[min_index] = 0 20 | del diff[0] 21 | k -= 1 22 | 23 | while diff and diff[0][0] < 0: 24 | min_index = diff[0][1] 25 | total += al[min_index] 26 | al[min_index] = 0 27 | bl[min_index] = 0 28 | del diff[0] 29 | 30 | for v in bl: 31 | total += v 32 | 33 | print(total) 34 | -------------------------------------------------------------------------------- /785A/anton-and-polyhedrons.py: -------------------------------------------------------------------------------- 1 | n = int(input()) 2 | 3 | total = 0 4 | faces = { 5 | "Tetrahedron": 4, 6 | "Cube": 6, 7 | "Octahedron": 8, 8 | "Dodecahedron": 12, 9 | "Icosahedron": 20, 10 | } 11 | 12 | while n > 0: 13 | total += faces[input()] 14 | n -= 1 15 | 16 | print(total) 17 | -------------------------------------------------------------------------------- /785B/anton-and-classes.py: -------------------------------------------------------------------------------- 1 | """ Created by Henrikh Kantuni on 3/15/17 """ 2 | 3 | if __name__ == "__main__": 4 | n = int(input()) 5 | min_r1 = 2 * 10 ** 9 6 | max_l1 = 0 7 | 8 | while n > 0: 9 | l1, r1 = [int(x) for x in input().split()] 10 | if r1 < min_r1: 11 | min_r1 = r1 12 | if l1 > max_l1: 13 | max_l1 = l1 14 | n -= 1 15 | 16 | m = int(input()) 17 | min_r2 = 2 * 10 ** 9 18 | max_l2 = 0 19 | 20 | while m > 0: 21 | l2, r2 = [int(x) for x in input().split()] 22 | if r2 < min_r2: 23 | min_r2 = r2 24 | if l2 > max_l2: 25 | max_l2 = l2 26 | m -= 1 27 | 28 | if max_l2 - min_r1 >= 0 and max_l1 - min_r2 >= 0: 29 | print(max(max_l2 - min_r1, max_l1 - min_r2)) 30 | elif max_l2 - min_r1 >= 0: 31 | print(max_l2 - min_r1) 32 | elif max_l1 - min_r2 >= 0: 33 | print(max_l1 - min_r2) 34 | else: 35 | print(0) 36 | -------------------------------------------------------------------------------- /785C/anton-and-fairy-tale.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, m; 6 | cin >> n >> m; 7 | if (m >= n) { 8 | cout << n << endl; 9 | } else { 10 | // double is not enough here 11 | long double d = 1 + 8 * (n - m); 12 | long double days = (-1 + sqrt(d)) / 2; 13 | // cast to long long before printing the answer 14 | long long ans = ceil(days) + m; 15 | cout << ans << endl; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /787A/the-monster.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a, b, c, d; 6 | cin >> a >> b >> c >> d; 7 | int ans = -1; 8 | for (int i = 0; i <= 100; i++) { 9 | int jc = b - d + i * a; 10 | if (jc >= 0 and jc % c == 0) { 11 | ans = b + i * a; 12 | break; 13 | } 14 | } 15 | cout << ans << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /787B/not-afraid.py: -------------------------------------------------------------------------------- 1 | """ Created by Henrikh Kantuni on 3/23/17 """ 2 | 3 | n, m = map(int, input().split()) 4 | destroy = False 5 | 6 | while m > 0: 7 | group = [int(g) for g in input().split()][1:] 8 | 9 | if len(group) == 1: 10 | destroy = True 11 | break 12 | 13 | group = [abs(g) for g in list(set(group))] 14 | old_size = len(group) 15 | group = list(set(group)) 16 | 17 | if len(group) == old_size: 18 | destroy = True 19 | break 20 | 21 | m -= 1 22 | 23 | if destroy: 24 | print("YES") 25 | else: 26 | print("NO") 27 | -------------------------------------------------------------------------------- /789A/anastasia-and-pebbles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | priority_queue pq; 8 | for (int i = 0; i < n; i++) { 9 | int w; 10 | cin >> w; 11 | pq.push(w); 12 | } 13 | long long ans = 0; 14 | while (!pq.empty()) { 15 | int w = pq.top(); 16 | pq.pop(); 17 | if (w > 2 * k) { 18 | ans += w / (2 * k); 19 | w = w % (2 * k); 20 | } else if (w > k and w <= 2 * k) { 21 | w = 0; 22 | ans++; 23 | } else { 24 | if (!pq.empty()) { 25 | w = pq.top(); 26 | pq.pop(); 27 | } 28 | w = max(w - k, 0); 29 | ans++; 30 | } 31 | if (w > 0) { 32 | pq.push(w); 33 | } 34 | } 35 | cout << ans << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /795C/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner scan = new Scanner(System.in); 6 | int n = scan.nextInt(); 7 | StringBuilder answer = new StringBuilder(); 8 | if (n % 2 == 1) { 9 | answer.append(7); 10 | n -= 3; 11 | } 12 | while (n > 0) { 13 | answer.append(1); 14 | n -= 2; 15 | } 16 | System.out.println(answer.toString()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /795D/lie-or-truth.py: -------------------------------------------------------------------------------- 1 | n, l, r = map(int, input().split()) 2 | a = [int(x) for x in input().split()] 3 | b = [int(x) for x in input().split()] 4 | 5 | before = True 6 | for i in range(0, l - 1): 7 | if a[i] != b[i]: 8 | before = False 9 | break 10 | 11 | after = True 12 | for i in range(r, len(a)): 13 | if a[i] != b[i]: 14 | after = False 15 | break 16 | 17 | if before and after: 18 | print("TRUTH") 19 | else: 20 | print("LIE") 21 | -------------------------------------------------------------------------------- /798A/mike-and-palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string s; 6 | cin >> s; 7 | if (s.size() == 1) { 8 | cout << "YES" << endl; 9 | return 0; 10 | } 11 | bool palindrome = true; 12 | int cnt = 1; 13 | for (int i = 0; i < s.size() / 2; i++) { 14 | if (s[i] != s[s.size() - 1 - i]) { 15 | if (cnt > 0) { 16 | cnt--; 17 | } else { 18 | palindrome = false; 19 | break; 20 | } 21 | } 22 | } 23 | if (s.size() % 2 == 1 and cnt == 1) { 24 | cnt--; 25 | } 26 | if (palindrome and cnt == 0) { 27 | cout << "YES" << endl; 28 | } else { 29 | cout << "NO" << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /798B/mike-and-strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int offset(string a, string b) { 6 | int cnt = 0; 7 | while (a != b) { 8 | rotate(b.begin(), b.begin() + 1, b.end()); 9 | cnt++; 10 | if (cnt > b.size()) { 11 | return -1; 12 | } 13 | } 14 | return cnt; 15 | } 16 | 17 | int main() { 18 | int n; 19 | cin >> n; 20 | vector s(n); 21 | for (int i = 0; i < n; i++) { 22 | cin >> s[i]; 23 | } 24 | int ans = INF; 25 | for (int i = 0; i < n; i++) { 26 | int cnt = 0; 27 | for (int j = 0; j < n; j++) { 28 | if (i == j) { 29 | continue; 30 | } 31 | if (offset(s[i], s[j]) == -1) { 32 | cout << -1 << endl; 33 | return 0; 34 | } 35 | cnt += offset(s[i], s[j]); 36 | } 37 | ans = min(ans, cnt); 38 | } 39 | cout << ans << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /805A/fake-np.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long l, r; 6 | cin >> l >> r; 7 | long long ans = (l == r) ? l : 2; 8 | cout << ans << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /805B/3-palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string ans; 8 | for (int i = 0; i < n; i++) { 9 | ans += 'a'; 10 | } 11 | for (int i = 2; i < n; i += 4) { 12 | ans[i] = 'b'; 13 | if (i + 1 < n) { 14 | ans[i + 1] = 'b'; 15 | } 16 | } 17 | cout << ans << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /805C/find-amir.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | cout << (n - 1) / 2 << endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /805D/minimum-number-of-steps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define P 1000000007 3 | using namespace std; 4 | 5 | long long fast_pow(long long x, long long y) { 6 | if (y == 0) { 7 | return 1; 8 | } 9 | if (y == 1) { 10 | return x; 11 | } 12 | if (y % 2 == 0) { 13 | return fast_pow(x * x % P, y / 2) % P; 14 | } 15 | return (x % P * fast_pow(x * x % P, (y - 1) / 2) % P) % P; 16 | } 17 | 18 | int main() { 19 | string s; 20 | cin >> s; 21 | long long ans = 0, cnt = 0; 22 | for (int i = 0; i < s.size(); i++) { 23 | if (s[i] == 'a') { 24 | cnt++; 25 | } else { 26 | ans += fast_pow(2, cnt) - 1; 27 | ans %= P; 28 | } 29 | } 30 | cout << ans << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /816A/karen-and-morning.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string T; 6 | cin >> T; 7 | vector t = { 8 | T[0] - '0', 9 | T[1] - '0', 10 | T[3] - '0', 11 | T[4] - '0' 12 | }; 13 | int ans = 0; 14 | while (true) { 15 | if (t[0] == t[3] and t[1] == t[2]) { 16 | break; 17 | } 18 | t[3] += 1; 19 | if (t[3] > 9) { 20 | t[2] += 1; 21 | t[3] = 0; 22 | if (t[2] > 5) { 23 | t[1] += 1; 24 | t[2] = 0; 25 | t[3] = 0; 26 | if (t[0] != 2 and t[1] > 9) { 27 | t[0] += 1; 28 | t[1] = 0; 29 | t[2] = 0; 30 | t[3] = 0; 31 | } 32 | if (t[0] == 2 and t[1] > 3) { 33 | t[0] = 0; 34 | t[1] = 0; 35 | t[2] = 0; 36 | t[3] = 0; 37 | } 38 | } 39 | } 40 | ans++; 41 | } 42 | cout << ans << endl; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /816B/karen-and-coffee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 200005 3 | using namespace std; 4 | 5 | int main() { 6 | int n, k, q; 7 | cin >> n >> k >> q; 8 | vector f(N); 9 | while (n--) { 10 | int l, r; 11 | cin >> l >> r; 12 | f[l]++; 13 | f[r + 1]--; 14 | } 15 | int cnt = 0; 16 | for (int i = 0; i < f.size(); i++) { 17 | cnt += f[i]; 18 | f[i] = (cnt >= k) ? 1 : 0; 19 | } 20 | vector p(N); 21 | for (int i = 1; i < p.size(); i++) { 22 | p[i] = f[i - 1] + p[i - 1]; 23 | } 24 | while (q--) { 25 | int l, r; 26 | cin >> l >> r; 27 | cout << p[r + 1] - p[l] << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /820A/mister-b-and-book-reading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int c, v0, v1, a, l; 8 | cin >> c >> v0 >> v1 >> a >> l; 9 | int ans = 0, read = 0; 10 | for (int i = 0; read < c; i++) { 11 | read = max(0, read - l); 12 | read += min(v1, v0 + i * a); 13 | ans++; 14 | } 15 | cout << ans << "\n"; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /820B/mister-b-and-angle-in-polygon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n, a; 8 | cin >> n >> a; 9 | vector> angles; 10 | for (int i = 0; i < n - 2; i++) { 11 | double alpha = (180.0 / n) * (i + 1); 12 | angles.push_back({alpha, 2, 1, i + 3}); 13 | } 14 | tuple ans; 15 | double best = 180; 16 | for (auto [alpha, x, y, z]: angles) { 17 | if (abs(alpha - a) < best) { 18 | best = abs(alpha - a); 19 | ans = {x, y, z}; 20 | } 21 | } 22 | auto [x, y, z] = ans; 23 | cout << x << " " << y << " " << z << "\n"; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /821A/okabe-and-future-gadget-laboratory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int L[n][n]; 8 | for (int i = 0; i < n; i++) { 9 | for (int j = 0; j < n; j++) { 10 | cin >> L[i][j]; 11 | } 12 | } 13 | for (int i = 0; i < n; i++) { 14 | for (int j = 0; j < n; j++) { 15 | if (L[i][j] != 1) { 16 | bool good = false; 17 | for (int k = 0; k < n; k++) { 18 | for (int l = 0; l < n; l++) { 19 | if (k != j and l != i and L[i][k] + L[l][j] == L[i][j]) { 20 | good = true; 21 | break; 22 | } 23 | } 24 | } 25 | if (!good) { 26 | cout << "No" << endl; 27 | return 0; 28 | } 29 | } 30 | } 31 | } 32 | cout << "Yes" << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /821B/okabe-and-banana-trees.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long sum(long long x, long long y) { 5 | long long first = x * (x + 1) / 2; 6 | long long last = first + y * (x + 1); 7 | return (first + last) * (y + 1) / 2; 8 | } 9 | 10 | int main() { 11 | long long m, b; 12 | cin >> m >> b; 13 | long long ans = 0; 14 | for (long long y = 0; y <= b; y++) { 15 | long long x = (b - y) * m; 16 | ans = max(ans, sum(x, y)); 17 | } 18 | cout << ans << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /821C/okabe-and-boxes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int n; 8 | cin >> n; 9 | stack s; 10 | set sorted; 11 | int nxt = 1; 12 | int ans = 0; 13 | for (int i = 0; i < 2 * n; i++) { 14 | string cmd; 15 | cin >> cmd; 16 | if (cmd == "add") { 17 | int x; 18 | cin >> x; 19 | s.push(x); 20 | } else { 21 | if (s.empty()) { 22 | sorted.erase(nxt); 23 | } else { 24 | if (s.top() == nxt) { 25 | s.pop(); 26 | } else { 27 | while (!s.empty()) { 28 | sorted.insert(s.top()); 29 | s.pop(); 30 | } 31 | sorted.erase(nxt); 32 | ans++; 33 | } 34 | } 35 | nxt++; 36 | } 37 | } 38 | cout << ans << "\n"; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /834B/the-festive-evening.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | string s; 8 | cin >> s; 9 | if (k == 26) { 10 | cout << "NO" << endl; 11 | return 0; 12 | } 13 | vector letters(26, 0); 14 | vector seen(26, 0); 15 | bool unguarded = false; 16 | for (int i = 0; i < s.size(); i++) { 17 | int index = s[i] - 65; 18 | letters[index]++; 19 | } 20 | for (int i = 0; i < s.size(); i++) { 21 | char index = s[i] - 65; 22 | if (seen[index] == 0) { 23 | k--; 24 | } 25 | seen[index]++; 26 | if (k < 0) { 27 | unguarded = true; 28 | break; 29 | } 30 | if (letters[index] == seen[index]) { 31 | k++; 32 | } 33 | } 34 | if (unguarded) { 35 | cout << "YES" << endl; 36 | } else { 37 | cout << "NO" << endl; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /834C/the-meaningless-game.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | for (int i = 0; i < n; i++) { 9 | long long a, b; 10 | cin >> a >> b; 11 | if (a == 1 and b == 1) { 12 | cout << "Yes" << endl; 13 | continue; 14 | } 15 | if (a == b * b or b == a * a) { 16 | cout << "Yes" << endl; 17 | continue; 18 | } 19 | long long m = a * b; 20 | long long base = round(cbrt(m)); 21 | bool perfect_cube = false; 22 | if (m % base == 0) { 23 | m /= base; 24 | if (m % base == 0) { 25 | m /= base; 26 | if (m % base == 0) { 27 | m /= base; 28 | if (m == 1) { 29 | perfect_cube = true; 30 | } 31 | } 32 | } 33 | } 34 | if (perfect_cube) { 35 | cout << "Yes" << endl; 36 | } else { 37 | cout << "No" << endl; 38 | } 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /835A/key-races.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int s, v1, v2, t1, t2; 6 | cin >> s >> v1 >> v2 >> t1 >> t2; 7 | int p1 = 2 * t1 + v1 * s; 8 | int p2 = 2 * t2 + v2 * s; 9 | if (p1 == p2) { 10 | cout << "Friendship" << endl; 11 | } else if (p1 < p2) { 12 | cout << "First" << endl; 13 | } else { 14 | cout << "Second" << endl; 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /835B/the-number-on-the-board.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | typedef unsigned long long ull; 6 | typedef vector vull; 7 | typedef vector vvull; 8 | 9 | int main() { 10 | ull k; 11 | string n; 12 | cin >> k >> n; 13 | if (k == 1) { 14 | cout << 0 << endl; 15 | return 0; 16 | } 17 | vvull digits(10); 18 | ull sum = 0; 19 | for (ull i = 0; i < n.size(); i++) { 20 | int digit = n[i] - '0'; 21 | sum += digit; 22 | digits[digit].push_back(i); 23 | } 24 | ull col = 0, row = 0; 25 | ull mnd = 0; 26 | while (sum < k) { 27 | if (row >= digits[col].size()) { 28 | col++; 29 | row = 0; 30 | } 31 | if (digits[col].size() == 0) { 32 | col++; 33 | continue; 34 | } 35 | int index = digits[col][row]; 36 | sum -= n[index] - '0'; 37 | n[index] = '9'; 38 | sum += 9; 39 | row++; 40 | mnd++; 41 | } 42 | cout << mnd << endl; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /839A/arya-and-bran.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | int arya = 0, bran = 0; 8 | int mnd = 0; 9 | for (int i = 0; i < n; i++) { 10 | int today; 11 | cin >> today; 12 | if (today + arya >= 8) { 13 | bran += 8; 14 | arya += today - 8; 15 | } else { 16 | bran += today + arya; 17 | arya = 0; 18 | } 19 | mnd++; 20 | if (bran >= k) { 21 | cout << mnd << endl; 22 | return 0; 23 | } 24 | } 25 | if (bran < k) { 26 | cout << -1 << endl; 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /839C/journey.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | vector> g; 6 | vector color; 7 | double ans = 0; 8 | 9 | void dfs(int u, int d, double p) { 10 | color[u] = 1; 11 | int children = 0; 12 | for (auto v: g[u]) { 13 | if (color[v] == 0) { 14 | children++; 15 | } 16 | } 17 | for (auto v: g[u]) { 18 | if (color[v] == 0) { 19 | dfs(v, d + 1, p / children); 20 | } 21 | } 22 | if (children == 0) { 23 | ans += p * d; 24 | } 25 | } 26 | 27 | int main() { 28 | ios::sync_with_stdio(false); 29 | cin.tie(nullptr); 30 | cin >> n; 31 | g.resize(n); 32 | color.resize(n); 33 | for (int i = 0; i < n - 1; i++) { 34 | int u, v; 35 | cin >> u >> v; 36 | u--, v--; 37 | g[u].push_back(v); 38 | g[v].push_back(u); 39 | } 40 | dfs(0, 0, 1.0); 41 | cout << fixed << setprecision(7); 42 | cout << ans << endl; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /842A/kirill-and-the-game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long l, r, x, y, k; 6 | cin >> l >> r >> x >> y >> k; 7 | bool found = false; 8 | for (long long b = x; b <= y; b++) { 9 | long long a = k * b; 10 | if (l <= a and a <= r) { 11 | found = true; 12 | break; 13 | } 14 | } 15 | if (found) { 16 | cout << "YES" << endl; 17 | } else { 18 | cout << "NO" << endl; 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /842B/gleb-and-pizza.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct Sausage { 5 | int x, y, r; 6 | }; 7 | 8 | int main() { 9 | int r, d; 10 | cin >> r >> d; 11 | int n; 12 | cin >> n; 13 | vector sausages; 14 | for (int i = 0; i < n; i++) { 15 | int xi, yi, ri; 16 | cin >> xi >> yi >> ri; 17 | Sausage s; 18 | s.x = xi; 19 | s.y = yi; 20 | s.r = ri; 21 | sausages.push_back(s); 22 | } 23 | int ans = 0; 24 | for (int i = 0; i < sausages.size(); i++) { 25 | Sausage s = sausages[i]; 26 | double R = hypot(s.x, s.y); 27 | bool outside = true; 28 | if (R <= r - s.r) { 29 | outside = false; 30 | } 31 | bool main = false; 32 | if (R < r - d + s.r) { 33 | main = true; 34 | } 35 | if (!outside and !main) { 36 | ans++; 37 | } 38 | } 39 | cout << ans << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /849A/odds-and-ends.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector s(n); 8 | for (int i = 0; i < s.size(); i++) { 9 | cin >> s[i]; 10 | } 11 | bool c1 = s.front() % 2 == 1; 12 | bool c2 = s.back() % 2 == 1; 13 | bool c3 = s.size() % 2 == 1; 14 | if (c1 and c2 and c3) { 15 | cout << "Yes" << endl; 16 | } else { 17 | cout << "No" << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /854A/fraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gcd(int a, int b) { 5 | return b == 0 ? a : gcd(b, a % b); 6 | } 7 | 8 | int main() { 9 | int n; 10 | cin >> n; 11 | double mx = 0.0; 12 | int num = 1; 13 | for (int i = 1; i <= n / 2; i++) { 14 | int a = i, b = n - i; 15 | if (gcd(a, b) == 1) { 16 | if (a > b * mx) { 17 | mx = a / b; 18 | num = i; 19 | } 20 | } 21 | } 22 | cout << num << " " << n - num << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /854B/maxim-buys-an-apartment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, k; 6 | cin >> n >> k; 7 | if (k == 0 or k == n) { 8 | cout << "0 0" << endl; 9 | return 0; 10 | } 11 | long long mx = (n > 3 * k) ? 2 * k : n - k; 12 | cout << 1 << " " << mx << endl; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /864A/fair-game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector cnt(101, 0); 8 | set s; 9 | for (int i = 0; i < n; i++) { 10 | int a; 11 | cin >> a; 12 | s.insert(a); 13 | cnt[a]++; 14 | } 15 | vector sv(s.begin(), s.end()); 16 | if (sv.size() == 2 and cnt[sv[0]] == cnt[sv[1]]) { 17 | cout << "YES" << endl; 18 | cout << sv[0] << " " << sv[1] << endl; 19 | } else { 20 | cout << "NO" << endl; 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /864B/polycarp-and-letters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s; 8 | cin >> s; 9 | set cs; 10 | int ans = 0; 11 | for (int i = 0; i < n; i++) { 12 | if (islower(s[i]) and cs.count(s[i]) == 0) { 13 | cs.insert(s[i]); 14 | ans = max(ans, (int) cs.size()); 15 | } 16 | if (isupper(s[i])) { 17 | cs.clear(); 18 | } 19 | } 20 | cout << ans << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /864C/bus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | ios::sync_with_stdio(false); 6 | cin.tie(nullptr); 7 | int a, b, f, k; 8 | cin >> a >> b >> f >> k; 9 | int ans = (b < f) ? -1 : 0; 10 | int t = b - f, need = 0; 11 | for (int i = 0; ans != -1 and i < k; i++) { 12 | if (i % 2 == 0) { 13 | need = (i + 1 < k) ? 2 * (a - f) : a - f; 14 | } else { 15 | need = (i + 1 < k) ? 2 * f : f; 16 | } 17 | if (need > b) { 18 | ans = -1; 19 | } else if (need > t) { 20 | t = b; 21 | ans++; 22 | } 23 | t -= need; 24 | } 25 | cout << ans << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /867A/between-the-offices.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s; 8 | cin >> s; 9 | int sf = 0, fs = 0; 10 | for (int i = 1; i < s.size(); i++) { 11 | if (s[i - 1] != s[i]) { 12 | if (s[i - 1] == 'S') { 13 | sf++; 14 | } else { 15 | fs++; 16 | } 17 | } 18 | } 19 | if (sf > fs) { 20 | cout << "YES" << endl; 21 | } else { 22 | cout << "NO" << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /869A/the-artful-expedient.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | set s; 8 | vector x(n); 9 | for (int i = 0; i < n; i++) { 10 | cin >> x[i]; 11 | s.insert(x[i]); 12 | } 13 | vector y(n); 14 | for (int i = 0; i < n; i++) { 15 | cin >> y[i]; 16 | s.insert(y[i]); 17 | } 18 | int cnt = 0; 19 | for (int i = 0; i < n; i++) { 20 | for (int j = 0; j < n; j++) { 21 | int xy = x[i] xor y[j]; 22 | if (s.count(xy) > 0) { 23 | cnt++; 24 | } 25 | } 26 | } 27 | if (cnt % 2 == 0) { 28 | cout << "Karen" << endl; 29 | } else { 30 | cout << "Koyomi" << endl; 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /869B/the-eternal-immortality.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long a, b; 6 | cin >> a >> b; 7 | long long ans = 1; 8 | for (long long i = a + 1; i <= b; i++) { 9 | ans = (ans % 10) * (i % 10); 10 | if (ans % 10 == 0) { 11 | break; 12 | } 13 | } 14 | cout << ans % 10 << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /879A/boryas-diagnosis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector s(n), d(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> s[i] >> d[i]; 10 | } 11 | int ans = s[0]; 12 | for (int i = 1; i < n; i++) { 13 | int x = 0; 14 | while (s[i] + x * d[i] <= ans) { 15 | x++; 16 | } 17 | ans = s[i] + x * d[i]; 18 | } 19 | cout << ans << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /879B/table-tennis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, k; 6 | cin >> n >> k; 7 | vector p(n); 8 | int ans = -1; 9 | for (int i = 0; i < n; i++) { 10 | cin >> p[i]; 11 | ans = max(ans, p[i]); 12 | } 13 | if (k <= n) { 14 | queue q; 15 | for (int i = 0 ; i < n; i++) { 16 | q.push(p[i]); 17 | } 18 | int wins = 0; 19 | ans = q.front(); q.pop(); 20 | while (wins < k) { 21 | int front = q.front(); 22 | if (ans > front) { 23 | q.push(front); q.pop(); 24 | wins++; 25 | } else { 26 | q.push(ans); 27 | ans = q.front(); q.pop(); 28 | wins = 1; 29 | } 30 | } 31 | } 32 | cout << ans << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /887A/div-64.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string s; 6 | cin >> s; 7 | if (s.size() < 7) { 8 | cout << "no" << endl; 9 | return 0; 10 | } 11 | int zeroes = 0, last; 12 | for (int i = s.size() - 1; i >= 0; i--) { 13 | if (s[i] == '0') { 14 | zeroes++; 15 | last = i; 16 | if (zeroes == 6) 17 | break; 18 | } 19 | } 20 | if (zeroes < 6) { 21 | cout << "no" << endl; 22 | return 0; 23 | } 24 | for (int i = last; i > -1; i--) { 25 | if (s[i] == '1') { 26 | cout << "yes" << endl; 27 | return 0; 28 | } 29 | } 30 | cout << "no" << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /893A/chess-for-three.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int x = 1, y = 2; 8 | bool ok = true; 9 | while (n--) { 10 | int w; 11 | cin >> w; 12 | if (!ok) { 13 | continue; 14 | } 15 | if (w == x or w == y) { 16 | y = 6 - x - y; 17 | x = w; 18 | } else { 19 | ok = false; 20 | } 21 | } 22 | if (ok) { 23 | cout << "YES" << endl; 24 | } else { 25 | cout << "NO" << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /893B/beautiful-divisors.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int ans = 1; 8 | int k = 1, m = 0; 9 | while (m <= n) { 10 | if (m != 0 and n % m == 0) { 11 | ans = m; 12 | } 13 | m = (pow(2, k) - 1) * pow(2, k - 1); 14 | k++; 15 | } 16 | cout << ans << endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /894A/qaq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | string s; 6 | cin >> s; 7 | int ans = 0; 8 | for (int i = 0; i < s.size(); i++) { 9 | for (int j = i + 1; j < s.size(); j++) { 10 | for (int k = j + 1; k < s.size(); k++) { 11 | if (s[i] == 'Q' and s[j] == 'A' and s[k] == 'Q') { 12 | ans++; 13 | } 14 | } 15 | } 16 | } 17 | cout << ans << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /894B/ralph-and-his-magic-field.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | #define P 1000000007 4 | using namespace std; 5 | 6 | long long f(long long n) { 7 | long long ans = 1; 8 | for (long long i = 2; i <= n; i++) { 9 | ans = (ans * i) % P; 10 | } 11 | return ans; 12 | } 13 | 14 | int main() { 15 | long long n, m, k, ans = 0; 16 | cin >> n >> m >> k; 17 | if (n == 1 or m == 1) { 18 | if (k == 1) { 19 | cout << "1" << endl; 20 | } else { 21 | cout << ((n * m) % 2 == 0 ? "0" : "1") << endl; 22 | } 23 | return 0; 24 | } 25 | if (n != m) { 26 | if (k == -1) { 27 | cout << "0" << endl; 28 | } else { 29 | ans += (n % 2 == 0) ? m : n; 30 | } 31 | return 0; 32 | } 33 | ans = f(n); 34 | if (n % 2 == 1) { 35 | ans = (ans + (n * n) % P) % P; 36 | ans = (ans + 1) % P; 37 | } 38 | cout << ans << endl; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /916A/jamie-and-alarm-snooze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int x; 6 | cin >> x; 7 | int h, m; 8 | cin >> h >> m; 9 | int ans = 0; 10 | while (h % 10 != 7 and m % 10 != 7) { 11 | if (m < x) { 12 | m = (m + 60 - x) % 60; 13 | h = (h == 0) ? 23 : h - 1; 14 | } else { 15 | m -= x; 16 | } 17 | ans++; 18 | } 19 | cout << ans << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /922A/cloning-toys.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long x, y; 6 | cin >> x >> y; 7 | if (y == 0) { 8 | cout << "No" << endl; 9 | return 0; 10 | } 11 | long long originals = 1, copies = 0; 12 | while (originals < y) { 13 | originals++; 14 | copies++; 15 | } 16 | // There needs to be at least one copy 17 | // to use a copy machine. 18 | while (copies > 0 and copies < x) { 19 | copies += 2; 20 | } 21 | if (y == originals and x == copies) { 22 | cout << "Yes" << endl; 23 | } else { 24 | cout << "No" << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /922B/magic-forest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int ans = 0; 8 | for (int i = 1; i < n - 1; i++) { 9 | for (int j = i + 1; j < n; j++) { 10 | for (int k = j + 1; k < i + j and k < n + 1; k++) { 11 | int sum = i xor j xor k; 12 | if (sum == 0) { 13 | ans++; 14 | } 15 | } 16 | } 17 | } 18 | cout << ans << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /922C/cave-painting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, k; 6 | cin >> n >> k; 7 | if (k == 1) { 8 | cout << "Yes" << endl; 9 | } else { 10 | set s; 11 | long long ans = 0; 12 | for (long long i = 1; i <= k; i++) { 13 | s.insert(n % i); 14 | ans++; 15 | if (ans != s.size()) { 16 | break; 17 | } 18 | } 19 | if (ans == s.size()) { 20 | cout << "Yes" << endl; 21 | } else { 22 | cout << "No" << endl; 23 | } 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /939A/love-triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector f(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> f[i]; 10 | } 11 | bool triangle = false; 12 | for (int i = 0; i < n; i++) { 13 | if (i == f[f[f[i] - 1] - 1] - 1) { 14 | triangle = true; 15 | break; 16 | } 17 | } 18 | if (triangle) { 19 | cout << "YES" << endl; 20 | } else { 21 | cout << "NO" << endl; 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /939B/hamster-farm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n; 6 | int k; 7 | cin >> n >> k; 8 | vector a(k); 9 | for (int i = 0; i < k; i++) { 10 | cin >> a[i]; 11 | } 12 | long long rem = n; 13 | long long t = -1; 14 | for (int i = 0; i < k; i++) { 15 | if (n / a[i] > 0 and n % a[i] < rem) { 16 | rem = n % a[i]; 17 | t = i; 18 | } 19 | } 20 | if (t != -1) { 21 | cout << t + 1 << " "; 22 | cout << n / a[t] << endl; 23 | } else { 24 | cout << "1 0" << endl; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /939C/convenient-for-everybody.cpp: -------------------------------------------------------------------------------- 1 | // TLE 2 | #include 3 | using namespace std; 4 | 5 | int n, s, f; 6 | vector a; 7 | 8 | int nofp(int h) { 9 | int cnt = 0; 10 | for (int i = 0; i < n; i++) { 11 | int local = (h + i) % n == 0 ? h + i : (h + i) % n; 12 | if (local >= s and local < f) { 13 | cnt += a[i]; 14 | } 15 | } 16 | return cnt; 17 | } 18 | 19 | int main() { 20 | cin >> n; 21 | a.resize(n); 22 | for (int i = 0; i < n; i++) { 23 | cin >> a[i]; 24 | } 25 | cin >> s >> f; 26 | int mx = 0, hour; 27 | for (int i = 1; i <= n / 2 + 1; i++) { 28 | int c1 = nofp(i); 29 | int c2 = nofp(n - i + 1); 30 | if (c1 > mx) { 31 | mx = c1; 32 | hour = i; 33 | } 34 | if (c2 > mx) { 35 | mx = c2; 36 | hour = n - i + 1; 37 | } 38 | } 39 | cout << hour << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /954A/diagonal-walking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s; 8 | cin >> s; 9 | for (int i = 0; i < n - 1; i++) { 10 | bool c1 = s[i] == 'U' and s[i + 1] == 'R'; 11 | bool c2 = s[i] == 'R' and s[i + 1] == 'U'; 12 | if (c1 or c2) { 13 | s[i] = 'D'; 14 | s = s.substr(0, i + 1) + s.substr(i + 2); 15 | } 16 | } 17 | cout << s.size() << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /954B/string-typing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool pands(string s) { 5 | for (int i = 0; i < s.size() / 2; i++) { 6 | if (s[i] != s[s.size() / 2 + i]) { 7 | return false; 8 | } 9 | } 10 | return true; 11 | } 12 | 13 | int main() { 14 | int n; 15 | cin >> n; 16 | string s; 17 | cin >> s; 18 | int lmx = -1; 19 | for (int i = 2; i <= n; i += 2) { 20 | string t = s.substr(0, i); 21 | if (pands(t)) { 22 | lmx = max(lmx, i / 2); 23 | } 24 | } 25 | if (lmx == -1) { 26 | cout << s.size() << endl; 27 | } else { 28 | cout << s.size() - lmx + 1 << endl; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /954C/matrix-walk.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | vector a(n); 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | } 12 | int x = 2, y = 1; 13 | int c2 = 2; 14 | bool ok = true, flat = true; 15 | for (int i = 0; i < n - 1; i++) { 16 | int diff = abs(a[i + 1] - a[i]); 17 | if (diff > 1) { 18 | flat = false; 19 | if (y != 1 and diff != y) { 20 | ok = false; 21 | break; 22 | } 23 | y = diff; 24 | x = max(x, c2++); 25 | } 26 | } 27 | if (ok) { 28 | cout << "YES" << endl; 29 | if (flat) { 30 | cout << "1 " << n << endl; 31 | } else { 32 | cout << x << " " << y << endl; 33 | } 34 | } else { 35 | cout << "NO" << endl; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /959A/mahmoud-and-ehab-and-the-even-odd-game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | if (n % 2 > 0) { 8 | cout << "Ehab" << endl; 9 | } else { 10 | cout << "Mahmoud" << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /959B/mahmoud-and-ehab-and-the-message.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | int n, k, m; 7 | cin >> n >> k >> m; 8 | map wid; 9 | for (int i = 0; i < n; i++) { 10 | string w; 11 | cin >> w; 12 | wid[w] = i; 13 | } 14 | vector c(n); 15 | for (int i = 0; i < n; i++) { 16 | cin >> c[i]; 17 | } 18 | while (k--) { 19 | int x; 20 | cin >> x; 21 | vector g(x); 22 | for (int i = 0; i < x; i++) { 23 | cin >> g[i]; 24 | g[i]--; 25 | } 26 | int mnc = INF; 27 | for (int i = 0; i < x; i++) { 28 | mnc = min(mnc, c[g[i]]); 29 | } 30 | for (int i = 0; i < x; i++) { 31 | c[g[i]] = mnc; 32 | } 33 | } 34 | long long ans = 0; 35 | vector w(m); 36 | for (int i = 0; i < m; i++) { 37 | cin >> w[i]; 38 | ans += c[wid[w[i]]]; 39 | } 40 | cout << ans << endl; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /959C/mahmoud-and-ehab-and-the-wrong-algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | if (n < 6) { 8 | cout << "-1" << endl; 9 | } else { 10 | for (int i = 2; i < n - 1; i++) { 11 | cout << "1 " << i << endl; 12 | } 13 | cout << "2 " << n - 1 << endl; 14 | cout << "2 " << n << endl; 15 | } 16 | for (int i = 2; i < n + 1; i++) { 17 | cout << "1 " << i << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /962A/equator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | long long sum = 0; 9 | for (int i = 0; i < n; i++) { 10 | cin >> a[i]; 11 | sum += a[i]; 12 | } 13 | long long cur = 0; 14 | for (int i = 0; i < n; i++) { 15 | cur += a[i]; 16 | if (cur >= ceil(sum / 2.0)) { 17 | cout << i + 1 << endl; 18 | break; 19 | } 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /962B/students-in-railway-carriage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, a, b; 6 | cin >> n >> a >> b; 7 | string s; 8 | cin >> s; 9 | char cur = (a > b) ? 'A' : 'B'; 10 | for (int i = 0; i < n; i++) { 11 | if (s[i] == '*') { 12 | cur = (a > b) ? 'A' : 'B'; 13 | } else if (s[i] == '.') { 14 | if (cur == 'A') { 15 | if (a == 0) { 16 | cur = 'B'; 17 | continue; 18 | } 19 | s[i] = cur; 20 | cur = 'B'; 21 | a--; 22 | } else if (cur == 'B') { 23 | if (b == 0) { 24 | cur = 'A'; 25 | continue; 26 | } 27 | s[i] = cur; 28 | cur = 'A'; 29 | b--; 30 | } 31 | } 32 | } 33 | int ans = 0; 34 | for (int i = 0; i < n; i++) { 35 | if (s[i] != '*' and s[i] != '.') { 36 | ans++; 37 | } 38 | } 39 | cout << ans << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /964A/splits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | cout << n / 2 + 1 << endl; 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /964B/messages.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | int n, A, B, C, T; 7 | cin >> n >> A >> B >> C >> T; 8 | vector t(n); 9 | int mnt = INF; 10 | for (int i = 0; i < n; i++) { 11 | cin >> t[i]; 12 | mnt = min(mnt, t[i]); 13 | } 14 | if (mnt > T) { 15 | cout << 0 << endl; 16 | return 0; 17 | } 18 | vector c(n, A); 19 | int ans1 = n * A; 20 | int ans2 = 0; 21 | for (int i = 0; i < n; i++) { 22 | int k = T - t[i]; 23 | ans2 += A - k * B; 24 | ans2 += C * k; 25 | } 26 | cout << max(ans1, ans2) << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /965A/paper-airplanes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int k, n, s, p; 6 | cin >> k >> n >> s >> p; 7 | int ans = -1; 8 | for (int i = 0; ans == -1; i++) { 9 | int y = i * p / k; 10 | if (s * y >= n) { 11 | ans = i; 12 | break; 13 | } 14 | } 15 | cout << ans << endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /965C/greedy-arkady.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | long long n, k, M, D; 7 | cin >> n >> k >> M >> D; 8 | if (n <= M) { 9 | cout << n << endl; 10 | return 0; 11 | } 12 | long long low, mid, high; 13 | low = n / (k * D); 14 | high = M; 15 | mid = (low + high) / 2; 16 | while (low != high) { 17 | mid = (low + high) / 2; 18 | if (mid > low) { 19 | low = mid; 20 | } else { 21 | high = mid; 22 | } 23 | } 24 | cout << mid * D << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /977A/wrong-subtraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | while (k--) { 8 | if (n % 10 > 0) { 9 | n--; 10 | } else { 11 | n /= 10; 12 | } 13 | } 14 | cout << n << endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /977B/two-gram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s; 8 | cin >> s; 9 | map memo; 10 | for (int i = 0; i < n - 1; i++) { 11 | string two = string(1, s[i]) + string(1, s[i + 1]); 12 | memo[two]++; 13 | } 14 | int mx = -1; 15 | string ans; 16 | for (auto [t, cnt]: memo) { 17 | if (cnt > mx) { 18 | ans = t; 19 | mx = cnt; 20 | } 21 | } 22 | cout << ans << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /977C/less-or-equal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | sort(a.begin(), a.end()); 12 | if (k == 0) { 13 | if (a[0] > 1) { 14 | cout << a[0] - 1 << endl; 15 | } else { 16 | cout << -1 << endl; 17 | } 18 | } else if (k == n) { 19 | cout << a[n - 1] << endl; 20 | } else { 21 | if (a[k - 1] == a[k]) { 22 | cout << -1 << endl; 23 | } else { 24 | cout << a[k - 1] << endl; 25 | } 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /978A/remove-duplicates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | vector ans; 12 | for (int i = 0; i < n; i++) { 13 | bool nodup = true; 14 | for (int j = i + 1; j < n; j++) { 15 | if (a[i] == a[j]) { 16 | nodup = false; 17 | break; 18 | } 19 | } 20 | if (nodup) { 21 | ans.push_back(a[i]); 22 | } 23 | } 24 | cout << ans.size() << endl; 25 | for (int i = 0; i < ans.size(); i++) { 26 | cout << ans[i] << " "; 27 | } 28 | cout << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /978B/file-name.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string s; 8 | cin >> s; 9 | int ans = 0; 10 | while (true) { 11 | int pos = s.find("xxx"); 12 | if (pos == string::npos) { 13 | break; 14 | } 15 | s.replace(pos, 3, "xx"); 16 | ans++; 17 | } 18 | cout << ans << endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /978C/letters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m; 6 | cin >> n >> m; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | vector b(m); 12 | for (int i = 0; i < m; i++) { 13 | cin >> b[i]; 14 | } 15 | vector asum(n); 16 | asum[0] = a[0]; 17 | for (int i = 1; i < n; i++) { 18 | asum[i] = asum[i - 1] + a[i]; 19 | } 20 | int cur = 0; 21 | for (int i = 0; i < m; i++) { 22 | while (b[i] > asum[cur]) { 23 | cur++; 24 | } 25 | cout << cur + 1 << " "; 26 | if (cur == 0) { 27 | cout << b[i] << endl; 28 | } else { 29 | cout << b[i] - asum[cur - 1] << endl; 30 | } 31 | } 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /978E/bus-video-system.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | ios::sync_with_stdio(false); 7 | cin.tie(0); 8 | int n, w; 9 | cin >> n >> w; 10 | vector a(n); 11 | for (int i = 0; i < n; i++) { 12 | cin >> a[i]; 13 | } 14 | vector ps(n + 1); 15 | for (int i = 1; i < n + 1; i++) { 16 | ps[i] = ps[i - 1] + a[i - 1]; 17 | } 18 | int l = 0, h = w; 19 | for (int i = 1; i < n + 1; i++) { 20 | if (ps[i] > 0) { 21 | h = min(h, w - ps[i]); 22 | } else { 23 | l = max(l, -ps[i]); 24 | } 25 | } 26 | int ans = h - l + 1; 27 | if (ans < 0 or ans > w + 1) { 28 | ans = 0; 29 | } 30 | cout << ans << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /979A/pizza-pizza-pizza.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n; 6 | cin >> n; 7 | if (n == 0) { 8 | cout << 0 << endl; 9 | } else if (n % 2 == 0) { 10 | cout << n + 1 << endl; 11 | } else { 12 | cout << (n + 1) / 2 << endl; 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /979C/kuro-and-walking-route.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | int n, x, y; 7 | cin >> n >> x >> y; 8 | vector> cnn(n); 9 | for (int i = 0; i < n - 1; i++) { 10 | int a, b; 11 | cin >> a >> b; 12 | cnn[a - 1].push_back(b - 1); 13 | cnn[b - 1].push_back(a - 1); 14 | } 15 | long long all = n * (n - 1); 16 | long long no = cnn[x - 1].size() * cnn[y - 1].size(); 17 | cout << all - no << endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /988A/diverse-team.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | set s(a.begin(), a.end()); 12 | if (s.size() < k) { 13 | cout << "NO" << endl; 14 | } else { 15 | cout << "YES" << endl; 16 | int cnt = 0; 17 | for (auto item: s) { 18 | if (cnt == k) { 19 | break; 20 | } 21 | for (int i = 0; i < n; i++) { 22 | if (a[i] == item) { 23 | cout << i + 1 << " "; 24 | cnt++; 25 | break; 26 | } 27 | } 28 | } 29 | cout << endl; 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /988B/substrings-sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int bysize(string a, string b) { 5 | return a.size() < b.size(); 6 | } 7 | 8 | bool solve(vector vs) { 9 | bool ok = true; 10 | for (int i = 1; i < vs.size(); i++) { 11 | for (int j = 0; j < i; j++) { 12 | if (vs[i].find(vs[j]) == string::npos) { 13 | ok = false; 14 | break; 15 | } 16 | } 17 | } 18 | return ok; 19 | } 20 | 21 | int main() { 22 | int n; 23 | cin >> n; 24 | vector vs(n); 25 | for (int i = 0; i < n; i++) { 26 | cin >> vs[i]; 27 | } 28 | sort(vs.begin(), vs.end(), bysize); 29 | bool ok = solve(vs); 30 | if (ok) { 31 | cout << "YES" << endl; 32 | for (auto s: vs) { 33 | cout << s << endl; 34 | } 35 | } else { 36 | cout << "NO" << endl; 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /988C/equal-sums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int k; 6 | cin >> k; 7 | vector> nums(k); 8 | vector> sums(k); 9 | map> memo; 10 | for (int i = 0; i < k; i++) { 11 | int n; 12 | cin >> n; 13 | nums[i].resize(n); 14 | int sum = 0; 15 | for (int j = 0; j < n; j++) { 16 | cin >> nums[i][j]; 17 | sum += nums[i][j]; 18 | } 19 | for (int j = 0; j < n; j++) { 20 | int num = sum - nums[i][j]; 21 | sums[i].insert(num); 22 | if (!memo.count(num)) { 23 | memo[num] = {i, j}; 24 | } else { 25 | if (memo[num].first != i) { 26 | cout << "YES" << endl; 27 | cout << memo[num].first + 1 << " "; 28 | cout << memo[num].second + 1 << endl; 29 | cout << i + 1 << " " << j + 1 << endl; 30 | return 0; 31 | } 32 | } 33 | } 34 | } 35 | cout << "NO" << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /990A/commentary-boxes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | long long n, m, a, b; 6 | cin >> n >> m >> a >> b; 7 | long long ac, bc; 8 | if (n % m == 0) { 9 | cout << 0 << endl; 10 | } else if (n > m) { 11 | ac = (n / m + 1) * m - n; 12 | bc = n % m; 13 | cout << min(ac * a, bc * b) << endl; 14 | } else { 15 | ac = m - n; 16 | bc = n; 17 | cout << min(ac * a, bc * b) << endl; 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /990B/micro-world.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | sort(a.begin(), a.end()); 12 | int ans = n; 13 | for (int i = 0; i < n; i++) { 14 | auto upper = upper_bound(a.begin(), a.end(), a[i]); 15 | int j = upper - a.begin(); 16 | if (j < n and a[j] - a[i] <= k) { 17 | ans--; 18 | } 19 | } 20 | cout << ans << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /991A/if-at-first-you-dont-succeed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a, b, c, n; 6 | cin >> a >> b >> c >> n; 7 | if (c > a or c > b or a + b - c + 1 > n) { 8 | cout << -1 << endl; 9 | } else { 10 | cout << n - (a + b - c) << endl; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /991B/getting-an-a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector g(n); 8 | int sum = 0; 9 | for (int i = 0; i < n; i++) { 10 | cin >> g[i]; 11 | sum += g[i]; 12 | } 13 | sort(g.begin(), g.end()); 14 | int ans = 0; 15 | int avg = 1.0 * sum / n + 0.5; 16 | for (int i = 0; i < n and avg < 5; i++) { 17 | sum -= g[i]; 18 | sum += 5; 19 | ans++; 20 | avg = 1.0 * sum / n + 0.5; 21 | } 22 | cout << ans << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /991C/candies.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | long long solve(long long n, long long k) { 6 | long long vasya = 0, petya = 0; 7 | while (true) { 8 | vasya += min(n, k); 9 | n -= min(n, k); 10 | if (n > 9) { 11 | petya += n / 10; 12 | n -= n / 10; 13 | } else { 14 | break; 15 | } 16 | } 17 | vasya += (n > 0) ? n : 0; 18 | return vasya; 19 | } 20 | 21 | int main() { 22 | long long n; 23 | cin >> n; 24 | long long low = 1, high = n / 2, mid; 25 | long long half = n - n / 2; 26 | while (high - low != 1) { 27 | mid = (low + high) / 2; 28 | long long sm = solve(n, mid); 29 | if (sm < half) { 30 | low = mid; 31 | } else { 32 | high = mid; 33 | } 34 | } 35 | cout << high << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /996A/hit-the-lottery.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector bills = {100, 20, 10, 5, 1}; 8 | int ans = 0; 9 | for (int i = 0; i < bills.size(); i++) { 10 | ans += n / bills[i]; 11 | n -= bills[i] * (n / bills[i]); 12 | } 13 | cout << ans << endl; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /996B/world-cup.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define INF (int) 1e9 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | vector a(n); 9 | int m = INF; 10 | for (int i = 0; i < n; i++) { 11 | cin >> a[i]; 12 | m = min(m, a[i]); 13 | } 14 | for (int i = 0; i < n; i++) { 15 | a[i] -= m; 16 | } 17 | int cur = 0; 18 | for (int i = m % n; i < n; i++) { 19 | if (a[i] <= cur) { 20 | cout << i + 1 << endl; 21 | break; 22 | } 23 | cur++; 24 | if (i == n - 1) { 25 | i = -1; 26 | } 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /996D/suit-and-tie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | vector a(2 * n); 8 | for (int i = 0; i < 2 * n; i++) { 9 | cin >> a[i]; 10 | } 11 | int ans = 0; 12 | for (int i = 0; i < n; i++) { 13 | if (a[i * 2] != a[i * 2 + 1]) { 14 | int index = -1; 15 | for (int j = i * 2 + 2; j < 2 * n; j++) { 16 | if (a[j] == a[i * 2]) { 17 | index = j; 18 | break; 19 | } 20 | } 21 | for (int j = index; j != i * 2 + 1; j--) { 22 | swap(a[j - 1], a[j]); 23 | ans++; 24 | } 25 | } 26 | } 27 | cout << ans << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /996E/leaving-the-bar.cpp: -------------------------------------------------------------------------------- 1 | // WA 2 | #include 3 | using namespace std; 4 | 5 | string stringify(int x, int y) { 6 | return to_string(x) + "," + to_string(y); 7 | } 8 | 9 | int custom(pair a, pair b) { 10 | if (a.first == b.first) { 11 | return a.second < b.second; 12 | } 13 | return a.first > b.first; 14 | } 15 | 16 | int main() { 17 | int n; 18 | cin >> n; 19 | vector> v(n); 20 | map memo; 21 | vector ans(n, 1); 22 | for (int i = 0; i < n; i++) { 23 | int x, y; 24 | cin >> x >> y; 25 | v[i] = {x, y}; 26 | memo[stringify(x, y)] = i; 27 | } 28 | sort(v.begin(), v.end(), custom); 29 | int c = 1; 30 | for (int i = 0; i < n; i++) { 31 | auto p = v[i]; 32 | ans[memo[stringify(p.first, p.second)]] = c; 33 | c = (c == 1) ? -1 : 1; 34 | } 35 | for (int i = 0; i < n; i++) { 36 | cout << ans[i] << " "; 37 | } 38 | cout << endl; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /999A/mishka-and-contest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | vector a(n); 8 | for (int i = 0; i < n; i++) { 9 | cin >> a[i]; 10 | } 11 | vector color(n); 12 | int ans = 0; 13 | for (int i = 0; i < n and a[i] <= k; i++) { 14 | if (color[i] == 0) { 15 | color[i] = 1; 16 | ans++; 17 | } 18 | } 19 | for (int i = n - 1; i >= 0 and a[i] <= k; i--) { 20 | if (color[i] == 0) { 21 | color[i] = 1; 22 | ans++; 23 | } 24 | } 25 | cout << ans << endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /999B/reversing-encryption.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | string t; 8 | cin >> t; 9 | vector divisors; 10 | for (int i = 1; i <= n; i++) { 11 | if (n % i == 0) { 12 | divisors.push_back(i); 13 | } 14 | } 15 | string s = t; 16 | for (int i = 0; i < divisors.size(); i++) { 17 | for (int j = 0; j < divisors[i] / 2; j++) { 18 | swap(s[j], s[divisors[i] - 1 - j]); 19 | } 20 | } 21 | cout << s << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /999C/alphabetic-removals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, k; 6 | cin >> n >> k; 7 | string s; 8 | cin >> s; 9 | string ss = s; 10 | sort(ss.begin(), ss.end()); 11 | vector alphabet(26); 12 | for (int i = 0; k > 0 and i < n; i++, k--) { 13 | alphabet[ss[i] - 'a']++; 14 | } 15 | for (int i = 0; i < n; i++) { 16 | if (alphabet[s[i] - 'a'] > 0) { 17 | alphabet[s[i] - 'a']--; 18 | } else { 19 | cout << s[i]; 20 | } 21 | } 22 | cout << endl; 23 | return 0; 24 | } 25 | --------------------------------------------------------------------------------