id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
CodeChef/rmath
Little Ron of Byteland was poor at mathematics. He always used to get zero marks in mathematics test. One evening when he showed his result to his father, his father scolded him badly. So, he thought if zeroes were not there in this world how good the world would have been. The next day he was given an assignment by hi...
[ { "input": { "stdin": "1\n205 402" }, "output": { "stdout": "YES" } } ]
{ "tags": [], "title": "rmath" }
{ "cpp": null, "java": null, "python": "t=input()\nwhile t:\n x,y=map(int,raw_input().split())\n res=x+y\n res=list(str(res))\n while '0' in res:\n res.remove('0')\n x=list(str(x))\n y=list(str(y))\n while '0' in x:\n x.remove('0')\n while '0' in y:\n y.remove('0')\n ...
CodeChef/vcs
A version control system(VCS) is a repository of files, often the files for the source code of computer programs, with monitored access. Every change made to the source is tracked, along with who made the change, why they made it, and references to problems fixed, or enhancements introduced, by the change. Version c...
[ { "input": { "stdin": "2\n7 4 6\n1 4 6 7\n1 2 3 4 6 7\n4 2 2\n1 4\n3 4" }, "output": { "stdout": "4 1\n1 1\n" } }, { "input": { "stdin": "2\n7 4 6\n2 4 6 7\n1 2 3 5 6 7\n7 2 2\n3 4\n3 4" }, "output": { "stdout": "3 0\n2 5\n" } }, { "input": { ...
{ "tags": [], "title": "vcs" }
{ "cpp": null, "java": null, "python": "for _ in xrange(input()):\n n, m, k = map(int, raw_input().split())\n a = set(map(int, raw_input().split()))\n b = set(map(int, raw_input().split()))\n n1 = []\n for i in range(1,n+1):\n n1.append(i)\n n1 = set(n1)\n print len(a & b), len(n1 - (a...
Codeforces/1019/E
# Raining season By the year 3018, Summer Informatics School has greatly grown. Hotel «Berendeetronik» has been chosen as a location of the school. The camp consists of n houses with n-1 pathways between them. It is possible to reach every house from each other using the pathways. Everything had been perfect until th...
[ { "input": { "stdin": "5 10\n1 2 0 100\n1 3 0 100\n1 4 10 80\n1 5 20 0\n" }, "output": { "stdout": "200 200 200 210 220 230 260 290 320 350 " } }, { "input": { "stdin": "5 10\n3 2 41 54\n1 5 87 62\n3 1 64 22\n4 2 77 77\n" }, "output": { "stdout": "215 484 753 ...
{ "tags": [ "data structures", "divide and conquer", "trees" ], "title": "Raining season" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(2)\n#pragma GCC optimize(3)\n#pragma GCC optimize(\"Ofast\")\nusing namespace std;\nnamespace ywy {\ninline int get() {\n int n = 0;\n char c;\n while ((c = getchar()) || 23333) {\n if (c >= '0' && c <= '9') break;\n if (c == '-') goto s;\n }\n n = c...
Codeforces/1042/D
# Petya and Array Petya has an array a consisting of n integers. He has learned partial sums recently, and now he can calculate the sum of elements on any segment of the array really fast. The segment is a non-empty sequence of elements standing one next to another in the array. Now he wonders what is the number of s...
[ { "input": { "stdin": "5 4\n5 -1 3 4 -1\n" }, "output": { "stdout": "5" } }, { "input": { "stdin": "3 0\n-1 2 -3\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "4 -1\n-2 1 -2 3\n" }, "output": { "stdout": "3" } ...
{ "tags": [ "data structures", "divide and conquer", "two pointers" ], "title": "Petya and Array" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long Data[200010], Sum[200010];\nlong long N = 0, T = 0, Index = 0;\nclass SplayNode {\n public:\n SplayNode() : Value(0LL), SSize(1LL), Lson(NULL), Rson(NULL), Fa(NULL) {}\n SplayNode(long long Val)\n : Value(Val), SSize(1LL), Lson(NULL), Rson(NUL...
Codeforces/1065/C
# Make It Equal There is a toy building consisting of n towers. Each tower consists of several cubes standing on each other. The i-th tower consists of h_i cubes, so it has height h_i. Let's define operation slice on some height H as following: for each tower i, if its height is greater than H, then remove some top c...
[ { "input": { "stdin": "4 5\n2 3 4 5\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5 5\n3 1 2 2 4\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5 10\n2 2 2 2 3\n" }, "output": { "stdout": "1\n" ...
{ "tags": [ "greedy" ], "title": "Make It Equal" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long a[200005];\nlong long b[200005];\nint main(void) {\n ios::sync_with_stdio(false);\n long long n, k;\n cin >> n >> k;\n long long mx = -0x3f3f3f3f, mn = 0x3f3f3f3f;\n for (int i = 1; i <= n; ++i) {\n cin >> a[i];\n b[a[i]]++;\n mx = max(...
Codeforces/1088/A
# Ehab and another construction problem Given an integer x, find 2 integers a and b such that: * 1 ≤ a,b ≤ x * b divides a (a is divisible by b). * a ⋅ b>x. * a/b<x. Input The only line contains the integer x (1 ≤ x ≤ 100). Output You should output two integers a and b, satisfying the given condition...
[ { "input": { "stdin": "1\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "10\n" }, "output": { "stdout": "4 4\n" } }, { "input": { "stdin": "82\n" }, "output": { "stdout": "10 10\n" } }, { "input": { ...
{ "tags": [ "brute force", "constructive algorithms" ], "title": "Ehab and another construction problem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long n;\n cin >> n;\n if (n == 1) {\n cout << \"-1\";\n } else {\n cout << to_string(n) + \" \" + to_string(n);\n }\n}\n", "java": "import java.io.*;\nimport java.util.*;\npublic class EhabAnotherConstruction {\n\n\tpublic stat...
Codeforces/1107/A
# Digits Sequence Dividing You are given a sequence s consisting of n digits from 1 to 9. You have to divide it into at least two segments (segment — is a consecutive sequence of elements) (in other words, you have to place separators between some digits of the sequence) in such a way that each element belongs to exa...
[ { "input": { "stdin": "4\n6\n654321\n4\n1337\n2\n33\n4\n2122\n" }, "output": { "stdout": "YES\n2\n6 54321\nYES\n2\n1 337\nNO\nYES\n2\n2 122\n" } }, { "input": { "stdin": "2\n2\n45\n2\n67\n" }, "output": { "stdout": "YES\n2\n4 5\nYES\n2\n6 7\n" } }, { ...
{ "tags": [ "greedy", "strings" ], "title": "Digits Sequence Dividing" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst unsigned int M = 1000000007;\nlong long int ceil_div(long long int a, long long int b) {\n return (a + b - 1) / b;\n}\nvoid fun() {\n int a, b, n, i, j, x, nb, q, k;\n cin >> n;\n string str;\n cin >> str;\n if (n == 2) {\n if (str[0] >= str[1])...
Codeforces/1136/A
# Nastya Is Reading a Book After lessons Nastya decided to read a book. The book contains n chapters, going one after another, so that one page of the book belongs to exactly one chapter and each chapter contains at least one page. Yesterday evening Nastya did not manage to finish reading the book, so she marked the ...
[ { "input": { "stdin": "3\n1 4\n5 9\n10 12\n9\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "1\n1 7\n4\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3\n1 3\n4 7\n8 11\n2\n" }, "output": { "stdout": ...
{ "tags": [ "implementation" ], "title": "Nastya Is Reading a Book" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long gcd(long long a, long long b) {\n while (b ^= a ^= b ^= a %= b)\n ;\n return a;\n}\nconst int maxn = 2e5 + 9;\nconst int mod = 998244353;\ntemplate <class T>\ninline void sc(T &ret) {\n char c;\n ret = 0;\n while ((c = getchar()) < '0' || c >...
Codeforces/1154/E
# Two Teams There are n students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team. The i-th student has integer programming skill a_i. All programming skills are distinct and between 1 and n, inclusive. Firstly, the first coach...
[ { "input": { "stdin": "5 1\n2 4 5 3 1\n" }, "output": { "stdout": "21112\n" } }, { "input": { "stdin": "7 1\n7 2 1 3 5 4 6\n" }, "output": { "stdout": "1121122\n" } }, { "input": { "stdin": "5 1\n2 1 3 5 4\n" }, "output": { "std...
{ "tags": [ "data structures", "implementation", "sortings" ], "title": "Two Teams" }
{ "cpp": "#include <bits/stdc++.h>\nint a[200005], left[200005], right[200005], pos[200005], team[200005];\nint main() {\n int n, k;\n scanf(\"%d %d\", &n, &k);\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", a + i);\n a[i]--;\n pos[a[i]] = i;\n team[a[i]] = -1;\n left[i] = i - 1;\n right[i] = i ...
Codeforces/1176/C
# Lose it! You are given an array a consisting of n integers. Each a_i is one of the six following numbers: 4, 8, 15, 16, 23, 42. Your task is to remove the minimum number of elements to make this array good. An array of length k is called good if k is divisible by 6 and it is possible to split it into k/6 subsequen...
[ { "input": { "stdin": "5\n4 8 15 16 23\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "15\n4 8 4 8 15 16 8 16 23 15 16 4 42 23 42\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "12\n4 8 4 15 16 8 23 15 16 42 23 42\...
{ "tags": [ "dp", "greedy", "implementation" ], "title": "Lose it!" }
{ "cpp": "#include <bits/stdc++.h>\ninline int in();\ninline void wr(int);\ninline int ch(int);\nconst int a[] = {4, 8, 15, 16, 23, 42};\nconst int N = (int)5e5 + 5;\nint ju[6][N], tot[6] = {1, 1, 1, 1, 1, 1};\nint main(int argc, char** argv) {\n register int n = in();\n for (register int i = 1; i <= n; ++i) {\n ...
Codeforces/1195/B
# Sport Mafia Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia. For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs n actions. The first action performed is to put a single candy into the box. For each ...
[ { "input": { "stdin": "1 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "3 2\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "9 11\n" }, "output": { "stdout": "4\n" } }, { "input": { ...
{ "tags": [ "binary search", "brute force", "math" ], "title": "Sport Mafia" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid fast() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n}\nvector<long long> a;\nlong long k, n;\nint main() {\n fast();\n long long flag = 0, x, y, t, ans = -1, apos;\n cin >> n >> k;\n int cur = 0, sum = 0, kush = 0...
Codeforces/1211/F
# kotlinkotlinkotlinkotlin... Polycarp really likes writing the word "kotlin". He wrote this word several times in a row without spaces. For example, he could write the string like "kotlinkotlinkotlinkotlin". Polycarp sliced (cut) the written string into n pieces and mixed them. As a result, he has n strings s_1, s_2...
[ { "input": { "stdin": "2\nlin\nkot\n" }, "output": { "stdout": "\n2 1 " } }, { "input": { "stdin": "4\nlinkotlinkotlinkotl\nkotlin\nin\nkot\n" }, "output": { "stdout": "\n2 4 1 3 " } }, { "input": { "stdin": "8\ni\nn\ntlin\no\nko\nt\nk\nl\n" ...
{ "tags": [ "*special", "graphs", "implementation", "strings" ], "title": "kotlinkotlinkotlinkotlin..." }
{ "cpp": null, "java": null, "python": null }
Codeforces/1234/D
# Distinct Characters Queries You are given a string s consisting of lowercase Latin letters and q queries for this string. Recall that the substring s[l; r] of the string s is the string s_l s_{l + 1} ... s_r. For example, the substrings of "codeforces" are "code", "force", "f", "for", but not "coder" and "top". Th...
[ { "input": { "stdin": "dfcbbcfeeedbaea\n15\n1 6 e\n1 4 b\n2 6 14\n1 7 b\n1 12 c\n2 6 8\n2 1 6\n1 7 c\n1 2 f\n1 10 a\n2 7 9\n1 10 a\n1 14 b\n1 1 f\n2 1 11\n" }, "output": { "stdout": "5\n2\n5\n2\n6\n" } }, { "input": { "stdin": "abacaba\n5\n2 1 4\n1 4 b\n1 5 b\n2 4 6\n2 1 7\...
{ "tags": [ "data structures" ], "title": "Distinct Characters Queries" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\nusing V = vector<T>;\ntemplate <class T>\nusing Vv = vector<vector<T>>;\ntemplate <class T>\nusing Pq = priority_queue<T>;\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n string inp;\n cin >> inp;\n int q;\n cin >> q;\n V<ch...
Codeforces/1253/C
# Sweets Eating Tsumugi brought n delicious sweets to the Light Music Club. They are numbered from 1 to n, where the i-th sweet has a sugar concentration described by an integer a_i. Yui loves sweets, but she can eat at most m sweets each day for health reasons. Days are 1-indexed (numbered 1, 2, 3, …). Eating the s...
[ { "input": { "stdin": "1 1\n7\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "9 2\n6 19 3 4 4 2 6 7 8\n" }, "output": { "stdout": "2 5 11 18 30 43 62 83 121\n" } }, { "input": { "stdin": "5 5\n98816 43061 167639 85627 15007\n" ...
{ "tags": [ "dp", "greedy", "math", "sortings" ], "title": "Sweets Eating" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint T, M, N, a;\nunsigned long long b;\nvector<int> input;\nvector<unsigned long long> prefs;\nvector<unsigned long long> output;\nunsigned long long prf(int idx1, int idx2) {\n if (idx2 < idx1) return 0;\n return (prefs[idx2] - (idx1 > 0 ? prefs[idx1 - 1] :...
Codeforces/1277/B
# Make Them Odd There are n positive integers a_1, a_2, ..., a_n. For the one move you can choose any even value c and divide by two all elements that equal c. For example, if a=[6,8,12,6,3,12] and you choose c=6, and a is transformed into a=[3,8,12,3,3,12] after the move. You need to find the minimal number of move...
[ { "input": { "stdin": "4\n6\n40 6 40 3 20 1\n1\n1024\n4\n2 4 8 16\n3\n3 1 7\n" }, "output": { "stdout": "4\n10\n4\n0\n" } }, { "input": { "stdin": "4\n6\n37 6 79 3 20 1\n1\n1024\n4\n2 6 10 16\n1\n3 1 7\n" }, "output": { "stdout": "3\n10\n6\n0\n" } }, {...
{ "tags": [ "greedy", "number theory" ], "title": "Make Them Odd" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n int T;\n cin >> T;\n set<unsigned long long> setDzielnikow;\n for (int i = 0; i < T; i++) {\n int n;\n cin >> n;\n for (int k = 0; k < n; k++) {\n unsigned long long tmp;\n cin >> tmp;\...
Codeforces/1297/B
# Cartoons Tanya likes cartoons. She knows that n new cartoons will be released in his favorite cinema: the i-th of them will be airing from the day a_i to the day b_i (1 ≤ a_i ≤ b_i ≤ 10^9). The cinema has a special offer: there is a huge discount every day when only one cartoon is airing. Tanya doesn't care which ...
[ { "input": { "stdin": "5\n1\n1 1\n3\n2 1000000000\n2 500000000\n500000002 1000000000\n3\n1 2\n3 4\n1 4\n2\n4 11\n4 9\n3\n1 5\n10 10\n1 5\n" }, "output": { "stdout": "\n1\n500000001\n-1\n10\n10\n" } } ]
{ "tags": [ "*special", "implementation", "sortings" ], "title": "Cartoons" }
{ "cpp": null, "java": null, "python": null }
Codeforces/1320/C
# World of Darkraft: Battle for Azathoth Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind. Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is wor...
[ { "input": { "stdin": "2 3 3\n2 3\n4 7\n2 4\n3 2\n5 11\n1 2 4\n2 1 6\n3 4 6\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "4 4 5\n5 10\n6 2\n10 3\n9 5\n7 7\n4 3\n9 2\n3 6\n7 1 6\n8 3 2\n2 6 9\n3 8 2\n1 2 9\n" }, "output": { "stdout": "23\n" ...
{ "tags": [ "brute force", "data structures", "sortings" ], "title": "World of Darkraft: Battle for Azathoth" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nconst int SZ = 1e6 + 20;\nint n, m, p;\nint atk[SZ], def[SZ];\nlong long tree[SZ << 2], lazy[SZ << 2];\nint maxatk, maxdef;\nstruct zt {\n int d, a, c;\n} node[SZ];\nbool cmp(zt a, zt b) {\n if (a.d != b.d)\n return a.d < b.d;...
Codeforces/133/A
# HQ9+ HQ9+ is a joke programming language which has only four one-character instructions: * "H" prints "Hello, World!", * "Q" prints the source code of the program itself, * "9" prints the lyrics of "99 Bottles of Beer" song, * "+" increments the value stored in the internal accumulator. Instructions "H"...
[ { "input": { "stdin": "Codeforces\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "Hi!\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "!3YPv@2JQ44@)R2O_4`GO\n" }, "output": { "stdout": "YES\n" }...
{ "tags": [ "implementation" ], "title": "HQ9+" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nset<char> vals = {'H', 'Q', '9'};\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n string s;\n cin >> s;\n for (char c : s) {\n if (vals.count(c) != 0) {\n cout << \"YES\";\n return 0;\n }\n }\n cout << \"NO\";\n return 0;\n}\n"...
Codeforces/1361/B
# Johnny and Grandmaster Johnny has just found the new, great tutorial: "How to become a grandmaster?". The tutorial tells many strange and unexpected for Johnny things, such as you have to be patient or that very important is solving many harder and harder problems. The boy has found an online judge with tasks divi...
[ { "input": { "stdin": "4\n5 2\n2 3 4 4 3\n3 1\n2 10 1000\n4 5\n0 1 1 100\n1 8\n89\n" }, "output": { "stdout": "4\n1\n146981438\n747093407\n" } }, { "input": { "stdin": "1\n20 22328\n2572 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n" }, "output": { "stdout": "100000...
{ "tags": [ "greedy", "implementation", "math", "sortings" ], "title": "Johnny and Grandmaster" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 100000 + 10, mod = 1e9 + 7;\nlong long qpow(long long n, long long k) {\n long long res = 1;\n while (k) {\n if (k & 1) res = res * n % mod;\n n = n * n % mod;\n k >>= 1;\n }\n return res;\n}\nint main() {\n ios::sync_with_stdio(0);...
Codeforces/1381/C
# Mastermind In the game of Mastermind, there are two players — Alice and Bob. Alice has a secret code, which Bob tries to guess. Here, a code is defined as a sequence of n colors. There are exactly n+1 colors in the entire universe, numbered from 1 to n+1 inclusive. When Bob guesses a code, Alice tells him some info...
[ { "input": { "stdin": "7\n5 2 4\n3 1 1 2 5\n5 3 4\n1 1 2 1 2\n4 0 4\n5 5 3 3\n4 1 4\n2 3 2 3\n6 1 2\n3 2 1 1 1 1\n6 2 4\n3 3 2 1 1 1\n6 2 6\n1 1 3 2 1 1\n" }, "output": { "stdout": "YES\n5 1 1 4 2\nYES\n3 1 1 1 2\nYES\n3 3 5 5\nNO\nYES\n1 4 4 4 4 1\nYES\n4 4 3 3 1 1\nYES\n2 3 1 1 1 1\n" ...
{ "tags": [ "constructive algorithms", "graph matchings", "greedy", "implementation", "sortings", "two pointers" ], "title": "Mastermind" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = 1e9;\nint t, x, y, n, b[100005], c[100005], a[100005];\nint printc[100005];\nint main() {\n cin >> t;\n while (t--) {\n cin >> n >> x >> y;\n set<pair<int, int> > s;\n vector<int> las[100004];\n int notin = -1;\n for (int i = 0...
Codeforces/1401/D
# Maximum Distributed Tree You are given a tree that consists of n nodes. You should label each of its n-1 edges with an integer in such way that satisfies the following conditions: * each integer must be greater than 0; * the product of all n-1 numbers should be equal to k; * the number of 1-s among all n-1...
[ { "input": { "stdin": "3\n4\n1 2\n2 3\n3 4\n2\n2 2\n4\n3 4\n1 3\n3 2\n2\n3 2\n7\n6 1\n2 3\n4 6\n7 3\n5 1\n3 6\n4\n7 5 13 3\n" }, "output": { "stdout": "17\n18\n286\n" } }, { "input": { "stdin": "4\n3\n3 1\n2 1\n4\n22861 20707 59627 9293\n3\n3 1\n1 2\n6\n15767 40973 20807 24...
{ "tags": [ "dfs and similar", "dp", "greedy", "implementation", "math", "number theory", "sortings", "trees" ], "title": "Maximum Distributed Tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long MOD = 1000 * 1000 * 1000 + 7;\nvoid dfs(int v, vector<vector<int>> &gr, vector<int> &c, vector<long long> &cnt,\n int p, int n) {\n c[v] = 1;\n for (auto it : gr[v]) {\n if (it != p) {\n dfs(it, gr, c, cnt, v, n);\n c[v] ...
Codeforces/1424/K
# Light switches Nikola owns a large warehouse which is illuminated by N light bulbs, numbered 1 to N. At the exit of the warehouse, there are S light switches, numbered 1 to S. Each switch swaps the on/off state for some light bulbs, so if a light bulb is off, flipping the switch turns it on, and if the light bulb is...
[ { "input": { "stdin": "4 3 4\n2 1 2\n2 2 3\n1 2\n1 1\n2 1 3\n3 1 2 3\n3 1 2 4\n" }, "output": { "stdout": "2\n2\n3\n-1\n" } }, { "input": { "stdin": "7 8 10\n2 1 3\n5 1 4 5 6 7\n2 1 4\n6 1 3 4 5 6 7\n3 5 6 7\n3 3 5 7\n3 1 5 6\n5 1 3 5 6 7\n4 1 3 4 5\n4 3 4 6 7\n1 3\n2 4 6\n...
{ "tags": [ "meet-in-the-middle" ], "title": "Light switches" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<bitset<1000> > ss;\nunordered_map<bitset<1000>, int> rec;\nvoid dfs1(int i, const bitset<1000>& b, int step) {\n if (i == int(ss.size()) * 2 / 3) {\n if (rec.count(b))\n rec[b] = min(rec[b], step);\n else\n rec[b] = step;\n return;\n...
Codeforces/1446/B
# Catching Cheaters You are given two strings A and B representing essays of two students who are suspected cheaters. For any two strings C, D we define their similarity score S(C,D) as 4⋅ LCS(C,D) - |C| - |D|, where LCS(C,D) denotes the length of the Longest Common Subsequence of strings C and D. You believe that o...
[ { "input": { "stdin": "7 7\nuiibwws\nqhtkxcn\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "4 5\nabba\nbabab\n" }, "output": { "stdout": "5" } }, { "input": { "stdin": "8 10\nbbbbabab\nbbbabaaaaa\n" }, "output": { "...
{ "tags": [ "dp", "strings" ], "title": "Catching Cheaters" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m;\nchar a[5010], b[5010];\nint f[5010][5010];\nint ans;\nint main() {\n ios::sync_with_stdio(false);\n cin >> n >> m;\n for (int i = 1; i <= n; ++i) cin >> a[i];\n for (int i = 1; i <= m; ++i) cin >> b[i];\n for (int i = 1; i <= n; ++i)\n for (...
Codeforces/1470/B
# Strange Definition Let us call two integers x and y adjacent if (lcm(x, y))/(gcd(x, y)) is a perfect square. For example, 3 and 12 are adjacent, but 6 and 9 are not. Here gcd(x, y) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x and y, and lcm(x, y) d...
[ { "input": { "stdin": "2\n4\n6 8 4 2\n1\n0\n6\n12 3 20 5 80 1\n1\n1\n" }, "output": { "stdout": "\n2\n3\n" } }, { "input": { "stdin": "1\n5\n1 1 3 3 1\n1\n4294967296\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "1\n40\n1 3 5 6...
{ "tags": [ "bitmasks", "graphs", "hashing", "math", "number theory" ], "title": "Strange Definition" }
{ "cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n// using mint = long double;\n// using mint = modint998244353;\n// using mint = modint1000000007;\n\ntypedef long long ll;\ntypedef pair<ll, ll> P;\ntypedef pair<P, ll> T;\ntypedef pair<ll, vector<ll>> Pd;\n\nconst ll INF = 1e18;\nconst ll fact_table = 320...
Codeforces/1497/A
# Meximization You are given an integer n and an array a_1, a_2, …, a_n. You should reorder the elements of the array a in such way that the sum of MEX on prefixes (i-th prefix is a_1, a_2, …, a_i) is maximized. Formally, you should find an array b_1, b_2, …, b_n, such that the sets of elements of arrays a and b are ...
[ { "input": { "stdin": "3\n7\n4 2 0 1 3 3 7\n5\n2 2 8 6 9\n1\n0\n" }, "output": { "stdout": "\n0 1 2 3 4 7 3 \n2 6 8 9 2 \n0 \n" } }, { "input": { "stdin": "1\n1\n2\n" }, "output": { "stdout": "2 \n" } }, { "input": { "stdin": "1\n100\n1 1 1 1...
{ "tags": [ "brute force", "data structures", "greedy", "sortings" ], "title": "Meximization" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint t;\ntypedef long long ll;\nint main()\n{\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n cin>>t;\n while(t--)\n {\n ll n,z=0;\n cin>>n;\n ll a[n],m[n];\n for(ll i=0;i<n;i++)\n cin>>a[i];\n sort(...
Codeforces/1519/F
# Chests and Keys Alice and Bob play a game. Alice has got n treasure chests (the i-th of which contains a_i coins) and m keys (the j-th of which she can sell Bob for b_j coins). Firstly, Alice puts some locks on the chests. There are m types of locks, the locks of the j-th type can only be opened with the j-th key. ...
[ { "input": { "stdin": "2 3\n3 3\n1 1 4\n10 20 100\n20 15 80\n" }, "output": { "stdout": "\n205\n" } }, { "input": { "stdin": "2 3\n3 4\n1 1 4\n10 20 100\n20 15 80\n" }, "output": { "stdout": "\n-1\n" } }, { "input": { "stdin": "2 3\n3 3\n2 1 ...
{ "tags": [ "bitmasks", "brute force", "dfs and similar", "dp", "flows" ], "title": "Chests and Keys" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\n\ntypedef long long LL;\n\nconst int N=6,M=15625,INF=(1<<30)-1;\n\nint n,m,a[N+9],sa,b[N+9],c[N+9][N+9];\n\nvoid into(){\n scanf(\"%d%d\",&n,&m);\n for (int i=0;i<n;++i){\n\tscanf(\"%d\",&a[i]);\n\tsa+=a[i];\n }\n for (int i=0;i<m;++i)\n\tscanf(\"%d\",&b[i]...
Codeforces/1546/F
# AquaMoon and Wrong Coordinate Cirno gives AquaMoon a problem. There are m people numbered from 0 to m - 1. They are standing on a coordinate axis in points with positive integer coordinates. They are facing right (i.e. in the direction of the coordinate increase). At this moment everyone will start running with the ...
[ { "input": { "stdin": "5 7\n6 9 9 6 9\n10 7 10 8 10\n11 11 11 10 8\n12 12 12 12 9\n14 13 12 10 13\n11 14 16 14 14\n12 15 18 15 15\n" }, "output": { "stdout": "4 13\n" } }, { "input": { "stdin": "5 7\n9 6 6 9 9\n1 2 1 1 1\n2 3 4 1 0\n0 2 3 1 4\n4 3 0 1 2\n1 3 4 0 2\n1 4 0 2 ...
{ "tags": [ "constructive algorithms", "interactive", "math" ], "title": "AquaMoon and Wrong Coordinate" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m, i, j, k, ans1, ans2;\ntypedef long long ll;\nll A[1100][1100];\nll C[1100], x, y, s, t, temp;\n\nint main() {\n\tscanf(\"%d%d\", &n, &m);\n\tfor (int i = 0; i < m; i++)\n\t\tfor (int j = 1; j <= n; j++) {\n\t\t\tscanf(\"%lld\", &A[i][j]);\n\t\t\tC[i]...
Codeforces/174/B
# File List Eudokimus, a system administrator is in trouble again. As a result of an error in some script, a list of names of very important files has been damaged. Since they were files in the BerFS file system, it is known that each file name has a form "name.ext", where: * name is a string consisting of lowerca...
[ { "input": { "stdin": "read.meexample.txtb.cpp\n" }, "output": { "stdout": "YES\nread.mee\nxample.txt\nb.cpp\n" } }, { "input": { "stdin": "version.infontldrcontestdata.zip\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "ph..ql...
{ "tags": [ "dp", "greedy", "implementation" ], "title": "File List" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar str[500000];\nstring s[500000];\nvoid solve() {\n int len = strlen(str);\n int ind = -1;\n string tmp = \"\";\n for (int i = 0; i < len; i++) {\n tmp += str[i];\n if (str[i] == '.') {\n ind = i;\n break;\n }\n }\n if (ind == -1 ||...
Codeforces/194/C
# Cutting Figure You've gotten an n × m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected. A set of painted squares is called connected, ...
[ { "input": { "stdin": "5 5\n#####\n#...#\n#####\n#...#\n#####\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5 4\n####\n#..#\n#..#\n#..#\n####\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3 3\n###\n#.#\n###\n" ...
{ "tags": [ "constructive algorithms", "graphs", "trees" ], "title": "Cutting Figure" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m, nodes = 0;\nchar a[51][51];\nbool v[51][51];\nint dx[4] = {0, 0, -1, 1};\nint dy[4] = {1, -1, 0, 0};\nvector<pair<int, int> > np;\nint dfs(int r, int c) {\n if (r >= n || c >= m || r < 0 || c < 0 || v[r][c] || a[r][c] == '.') return 0;\n int s = 1;...
Codeforces/218/C
# Ice Skating Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any se...
[ { "input": { "stdin": "2\n2 1\n1 2\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "2\n2 1\n4 1\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "7\n429 506\n346 307\n99 171\n853 916\n322 263\n115 157\n906 924\n" ...
{ "tags": [ "brute force", "dfs and similar", "dsu", "graphs" ], "title": "Ice Skating" }
{ "cpp": "#include <bits/stdc++.h>\nint pre[101];\nstruct node {\n int x, y;\n} a[101];\nvoid init(int n) {\n for (int i = 0; i <= n; ++i) pre[i] = i;\n}\nint find(int x) {\n if (x != pre[x]) pre[x] = find(pre[x]);\n return pre[x];\n}\nint main() {\n int n;\n while (~scanf(\"%d\", &n)) {\n init(n);\n int ...
Codeforces/241/G
# Challenging Balloons Martha — as a professional problemsetter — proposed a problem for a world-class contest. This is the problem statement: Tomorrow is Nadia's birthday, and Bardia (her brother) is assigned to make the balloons ready! There are n balloons (initially empty) that are tied to a straight line on cert...
[ { "input": { "stdin": "" }, "output": { "stdout": "302\n0 500000\n24495 300\n25094 299\n25691 298\n26286 297\n26879 296\n27470 295\n28059 294\n28646 293\n29231 292\n29814 291\n30395 290\n30974 289\n31551 288\n32126 287\n32699 286\n33270 285\n33839 284\n34406 283\n34971 282\n35534 281\n36095 ...
{ "tags": [ "constructive algorithms" ], "title": "Challenging Balloons" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int i, j, k, n = 305;\n printf(\"%d\\n\", n);\n printf(\"1 1000000\\n\");\n int x = 300000, tmp = 303;\n for (i = 2; i < n; i++) {\n printf(\"%d %d\\n\", x, tmp);\n x += tmp;\n tmp--;\n x += tmp;\n }\n printf(\"1000000 1000000...
Codeforces/267/A
# Subtractions You've got two numbers. As long as they are both larger than zero, they go through the same operation: subtract the lesser number from the larger one. If they equal substract one number from the another. For example, one operation transforms pair (4,17) to pair (4,13), it transforms (5,5) to (0,5). You...
[ { "input": { "stdin": "2\n4 17\n7 987654321\n" }, "output": { "stdout": "8\n141093479\n" } }, { "input": { "stdin": "20\n1000000000 999999999\n1000000000 999999999\n1000000000 999999999\n1000000000 999999999\n1000000000 999999999\n1000000000 999999999\n1000000000 999999999\...
{ "tags": [ "math", "number theory" ], "title": "Subtractions" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int testcases;\n cin >> testcases;\n int arr[testcases];\n for (int i = 0; i < testcases; ++i) {\n long long int a, b, d = 0, counter = 0;\n int flag = 0;\n cin >> a >> b;\n if (a == b) {\n arr[i] = 1;\n continue;\n ...
Codeforces/290/A
# Mysterious strings Input The input contains a single integer a (1 ≤ a ≤ 40). Output Output a single string. Examples Input 2 Output Adams Input 8 Output Van Buren Input 29 Output Harding
[ { "input": { "stdin": "8\n" }, "output": { "stdout": "Van Buren\n" } }, { "input": { "stdin": "2\n" }, "output": { "stdout": "Adams\n" } }, { "input": { "stdin": "29\n" }, "output": { "stdout": "Harding\n" } }, { "in...
{ "tags": [ "*special", "implementation" ], "title": "Mysterious strings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint duiin[] = {0, 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1,\n 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1,\n 51, 1, 2, 1, 14, 1, 2, 2, 14, 1, 6, 1, 4, 2, 2, 1,\n 52, 2, 5, 1, 5, 1, 15, 2, ...
Codeforces/316/A1
# Special Task Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. The content special agent has got an important...
[ { "input": { "stdin": "1?AA\n" }, "output": { "stdout": "100\n" } }, { "input": { "stdin": "AJ\n" }, "output": { "stdout": "81\n" } }, { "input": { "stdin": "?C\n" }, "output": { "stdout": "90\n" } }, { "input": { ...
{ "tags": [ "greedy" ], "title": "Special Task" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint f[12] = {0};\nint main() {\n string s;\n cin >> s;\n int q = 0;\n for (int i = 0; i < s.length(); i++)\n if (s[i] == '?')\n q++;\n else if (s[i] >= 'A' && s[i] <= 'J')\n f[s[i] - 'A']++;\n long long ans = 1;\n int start = 10;\n if (s...
Codeforces/338/C
# Divisor Tree A divisor tree is a rooted tree that meets the following conditions: * Each vertex of the tree contains a positive integer number. * The numbers written in the leaves of the tree are prime numbers. * For any inner vertex, the number within it is equal to the product of the numbers written in i...
[ { "input": { "stdin": "4\n6 72 8 4\n" }, "output": { "stdout": "12\n" } }, { "input": { "stdin": "2\n6 10\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "1\n7\n" }, "output": { "stdout": "1\n" } }, { "i...
{ "tags": [ "brute force", "number theory", "trees" ], "title": "Divisor Tree" }
{ "cpp": "#include <bits/stdc++.h>\nconst long long MAX_N = 1e5 + 10;\nconst long long MOD = 1e9 + 9;\nusing namespace std;\nlong long a[20];\nlong long p[20];\nlong long ans = 1e18;\nmultiset<long long> st;\nint n;\nvoid bt(int pt, long long now, int big) {\n if (pt == n + 1) {\n ans = min(now + (big > 1), ans);...
Codeforces/360/E
# Levko and Game Levko loves sports pathfinding competitions in his city very much. In order to boost his performance, Levko spends his spare time practicing. The practice is a game. The city consists of n intersections connected by m + k directed roads. Two or more roads can connect the same pair of intersections. B...
[ { "input": { "stdin": "4 1 3\n1 3 4\n3 2 2\n1 2 1 3\n2 4 1 3\n3 4 1 3\n" }, "output": { "stdout": "WIN\n1 1 3\n" } }, { "input": { "stdin": "4 1 3\n1 3 4\n3 2 2\n1 2 1 3\n2 4 1 3\n3 4 1 2\n" }, "output": { "stdout": "DRAW\n1 1 2\n" } }, { "input": ...
{ "tags": [ "graphs", "greedy", "shortest paths" ], "title": "Levko and Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace ::std;\nconst long long maxn = 4e5 + 500;\nconst long long mod = 1e9 + 7;\nconst long long inf = 1e17 + 500;\nvector<long long> ger[maxn];\nvector<long long> gerr[maxn];\nlong long t[maxn];\nlong long s[maxn];\nlong long l[maxn];\nlong long r[maxn];\nlong long w[max...
Codeforces/384/C
# Milking cows Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of th...
[ { "input": { "stdin": "5\n1 0 1 0 1\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "4\n0 0 1 0\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "2\n0 1\n" }, "output": { "stdout": "0" } }, { "in...
{ "tags": [ "data structures", "greedy" ], "title": "Milking cows" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long x, y, xx, j, yy, n, m, t, k, l, ans, ans1, o, i;\nstruct my {\n int x;\n int l;\n int r;\n};\nmy a[200000 + 10];\nint main() {\n cin >> n;\n for (i = 1; i <= n; i++) cin >> a[i].x;\n for (i = 1; i <= n; i++) {\n a[i].r = o;\n if (a[i].x =...
Codeforces/405/B
# Domino Effect Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a "domino show". Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes som...
[ { "input": { "stdin": "14\n.L.R...LR..L..\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "5\nR....\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "1\n.\n" }, "output": { "stdout": "1" } }, { "...
{ "tags": [], "title": "Domino Effect" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n int n, i, j = 0, count = 0, fl = 0;\n int pre;\n scanf(\"%d\", &n);\n getchar();\n char x, y;\n for (i = 0; i < n; i++) {\n scanf(\"%c\", &x);\n if (fl == 0) {\n if (x == 'R') {\n count += i - j;\n fl = 1;\n pre = i;\n } els...
Codeforces/432/C
# Prime Swaps You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): * choose two indexes, i and j (1 ≤ i < j ≤ n; (j - i + 1) is a prime number); * swap the ele...
[ { "input": { "stdin": "3\n3 2 1\n" }, "output": { "stdout": "1\n1 3\n\n" } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4\n4 2 3 1\n" }, "output": { "stdout": "3\n2 4\n1 2\n2 4\n...
{ "tags": [ "greedy", "sortings" ], "title": "Prime Swaps" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MOD = 1e9 + 7;\nint pos[100005], a[100005];\nint main() {\n int n, t, m, l, k, ans, i, j, res = 0, fl;\n t = 1;\n while (t--) {\n int isprime[100005];\n for (i = 2; i <= 100004; ++i) isprime[i] = 1;\n isprime[0] = isprime[1] = 0;\n for...
Codeforces/454/E
# Little Pony and Summer Sun Celebration Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on ...
[ { "input": { "stdin": "2 0\n0 0\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5 7\n1 2\n1 3\n1 4\n1 5\n3 4\n3 5\n4 5\n0 1 0 1 0\n" }, "output": { "stdout": "10\n2 1 3 4 5 4 5 4 3 1 " } }, { "input": { "stdin": "3 2\n1 2\n2 3\n...
{ "tags": [ "constructive algorithms", "dfs and similar", "graphs" ], "title": "Little Pony and Summer Sun Celebration" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long INF = 1e9, MOD = INF + 7;\nlong long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); }\nlong long power(long long a, long long b) {\n return (!b ? 1 : power(a, b / 2) * power(a, b / 2) * (b % 2 ? a : 1));\n}\nlong long mod(long...
Codeforces/477/C
# Dreamoon and Strings Dreamoon has a string s and a pattern string p. He first removes exactly x characters from s obtaining string s' as a result. Then he calculates <image> that is defined as the maximal number of non-overlapping substrings equal to p that can be found in s'. He wants to make this number as big as ...
[ { "input": { "stdin": "aaaaa\naa\n" }, "output": { "stdout": "2 2 1 1 0 0 \n" } }, { "input": { "stdin": "axbaxxb\nab\n" }, "output": { "stdout": "0 1 1 2 1 1 0 0 \n" } }, { "input": { "stdin": "abxxxaxbxaxxxba\naba\n" }, "output": { ...
{ "tags": [ "dp", "strings" ], "title": "Dreamoon and Strings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXS = 2000 + 5;\nconst int MAXP = 500 + 5;\nconst int INF = INT_MAX / 2;\nvector<int> ans;\nchar s[MAXS], p[MAXP];\nint nx[MAXS];\nint lens, lenp;\nbool usd[MAXS][MAXS];\nint rec[MAXS][MAXS];\nint f(int i, int k) {\n if (k == 0) return 0;\n if (i ...
Codeforces/4/D
# Mysterious Present Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1, a2, ..., an}, where the width and the height of the i-th envelope is strictly higher than the ...
[ { "input": { "stdin": "3 3 3\n5 4\n12 11\n9 8\n" }, "output": { "stdout": "3\n1 3 2\n" } }, { "input": { "stdin": "2 1 1\n2 2\n2 2\n" }, "output": { "stdout": "1\n1\n" } }, { "input": { "stdin": "5 13 13\n4 4\n10 10\n7 7\n1 1\n13 13\n" },...
{ "tags": [ "dp", "sortings" ], "title": "Mysterious Present" }
{ "cpp": null, "java": null, "python": "import sys\n\n\ndef log(*args): print(*args, file=sys.stderr)\ndef line(): return sys.stdin.readline().split()\n\n\ndef main():\n n, w, h = (int(x) for x in line())\n dims = [[int(x) for x in line()] for _ in range(n)]\n e = sorted(list(enumerate(dims)), key=lambda...
Codeforces/525/C
# Ilya and Sticks In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li. Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in s...
[ { "input": { "stdin": "4\n2 4 4 2\n" }, "output": { "stdout": "8\n" } }, { "input": { "stdin": "4\n2 2 3 5\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4\n100003 100004 100005 100006\n" }, "output": { "stdout": ...
{ "tags": [ "greedy", "math", "sortings" ], "title": "Ilya and Sticks" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long int n;\n cin >> n;\n long long int ar[n];\n for (long long int i = 0; i < n; i++) {\n cin >> ar[i];\n }\n sort(ar, ar + n);\n long long int count = 0;\n long long int a, b;\n long long int ans = 0;\n for (long long int i =...
Codeforces/550/D
# Regular Bridge An undirected graph is called k-regular, if the degrees of all its vertices are equal k. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components. Build a connected undirected k-regular graph containing at least one bridge, or else s...
[ { "input": { "stdin": "1\n" }, "output": { "stdout": "YES\n2 1\n1 2\n" } }, { "input": { "stdin": "30\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "68\n" }, "output": { "stdout": "NO\n" } }, { "input...
{ "tags": [ "constructive algorithms", "graphs", "implementation" ], "title": "Regular Bridge" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint k;\nint main() {\n cin >> k;\n if (k & 1) {\n cout << \"YES\\n\" << 4 * k - 2 << ' ' << k * (2 * k - 1) << '\\n';\n cout << \"1 \" << 2 * k << '\\n';\n for (int i = 2; i < k + 1; i++) cout << \"1 \" << i << '\\n';\n for (int i = 2; i < k + 1;...
Codeforces/577/E
# Points on Plane On a plane are n points (xi, yi) with integer coordinates between 0 and 106. The distance between the two points with numbers a and b is said to be the following value: <image> (the distance calculated by such formula is called Manhattan distance). We call a hamiltonian path to be some permutation p...
[ { "input": { "stdin": "5\n0 7\n8 10\n3 4\n5 0\n9 12\n" }, "output": { "stdout": "4 3 1 2 5\n" } }, { "input": { "stdin": "2\n0 0\n1000000 1000000\n" }, "output": { "stdout": "1 2 " } }, { "input": { "stdin": "4\n0 0\n0 1000000\n1000000 0\n100...
{ "tags": [ "constructive algorithms", "divide and conquer", "geometry", "greedy", "sortings" ], "title": "Points on Plane" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma comment(linker, \"/STACK:256000000\")\nusing namespace std;\nconst double eps = 1e-15;\nconst double pi = acos(-1.0);\nconst int MAXN = (int)1e6 + 7;\nconst int INF = (long long)1e9 + 7;\nconst long long LINF = (long long)4e18;\nconst int MOD = (long long)1e9 + 7;\nconst in...
Codeforces/59/C
# Title Vasya has recently finished writing a book. Now he faces the problem of giving it the title. Vasya wants the title to be vague and mysterious for his book to be noticeable among others. That's why the title should be represented by a single word containing at least once each of the first k Latin letters and no...
[ { "input": { "stdin": "3\na?c\n" }, "output": { "stdout": "IMPOSSIBLE\n" } }, { "input": { "stdin": "2\na??a\n" }, "output": { "stdout": "abba\n" } }, { "input": { "stdin": "2\n?b?a\n" }, "output": { "stdout": "abba\n" } }...
{ "tags": [ "expression parsing" ], "title": "Title" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ndouble gcd(double a, double b) { return a < 0.01 ? b : gcd(fmod(b, a), a); }\nlong long bpow(long long a, long long b, long long m) {\n a %= m;\n long long res = 1;\n while (b > 0) {\n if (b & 1) res = res * a % m;\n a = a * a % m;\n b >>= 1;\n }\...
Codeforces/621/A
# Wet Shark and Odd and Even Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark. Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0. ...
[ { "input": { "stdin": "5\n999999999 999999999 999999999 999999999 999999999\n" }, "output": { "stdout": "3999999996" } }, { "input": { "stdin": "3\n1 2 3\n" }, "output": { "stdout": "6" } }, { "input": { "stdin": "4\n2 3 7 7\n" }, "ou...
{ "tags": [ "implementation" ], "title": "Wet Shark and Odd and Even" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n int cnt = 0;\n long long min = 1000000000;\n long long s = 0;\n for (int i = 0; i < n; i++) {\n long long x;\n cin >> x;\n s += x;\n if (x % 2 == 1) {\n cnt++;\n if (x < min) min = x;\n }\n }\...
Codeforces/641/F
# Little Artem and 2-SAT Little Artem is a very smart programmer. He knows many different difficult algorithms. Recently he has mastered in 2-SAT one. In computer science, 2-satisfiability (abbreviated as 2-SAT) is the special case of the problem of determining whether a conjunction (logical AND) of disjunctions (log...
[ { "input": { "stdin": "2 1 1\n1 2\n1 2\n" }, "output": { "stdout": "SIMILAR\n" } }, { "input": { "stdin": "2 1 1\n1 2\n1 -2\n" }, "output": { "stdout": "0 0\n" } }, { "input": { "stdin": "2 1 1\n2 2\n1 2\n" }, "output": { "stdou...
{ "tags": [], "title": "Little Artem and 2-SAT" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a, b, c, d, n, m, k, m1, m2;\nstruct IG {\n std::bitset<1004> good[2004], bad[2004];\n bool bv[2004];\n IG() {\n for (int _n((2004) - 1), i(0); i <= _n; i++) {\n good[i].reset();\n bad[i].reset();\n }\n memset((bv), 0, sizeof(bv));\n ...
Codeforces/66/D
# Petya and His Friends Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to n. Let us remind you the definition of the greatest common divisor: GCD(a1, ..., ak) = d, where d represents such a maximal positive number that...
[ { "input": { "stdin": "4\n" }, "output": { "stdout": "105\n70\n42\n30\n" } }, { "input": { "stdin": "3\n" }, "output": { "stdout": "15\n10\n6\n" } }, { "input": { "stdin": "8\n" }, "output": { "stdout": "4849845\n3233230\n193993...
{ "tags": [ "constructive algorithms", "math", "number theory" ], "title": "Petya and His Friends" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> prime;\nbool is_prime[300];\nint num[200], ret[200], sub[200];\nvoid Multi(int K) {\n for (int i = 0; i < 200; i++) ret[i] = 0;\n for (int i = 0; K > 0; i++) {\n int MM = K % 10;\n K /= 10;\n for (int j = 0; j < 200; j++) sub[j] = 0;\n ...
Codeforces/691/A
# Fashion in Berland According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open. You are given a jacket with n buttons. Det...
[ { "input": { "stdin": "3\n1 0 0\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "3\n1 0 1\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "10\n0 0 0 0 0 0 0 0 0 0\n" }, "output": { "stdout": "NO\n" ...
{ "tags": [ "implementation" ], "title": "Fashion in Berland" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n int n, x, sum_ = 0;\n scanf(\"%d\", &n);\n bool ans = false;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &x);\n if (x == 0) sum_++;\n }\n if (n == 1) {\n if (sum_ == 0) ans = true;\n } else if (sum_ == 1)\n ans = true;\n if (ans)\n printf(\...
Codeforces/716/A
# Crazy Computer ZS the Coder is coding on a crazy computer. If you don't type in a word for a c consecutive seconds, everything you typed disappear! More formally, if you typed a word at second a and then the next word at second b, then if b - a ≤ c, just the new word is appended to other words on the screen. If b ...
[ { "input": { "stdin": "6 1\n1 3 5 7 9 10\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "6 5\n1 3 8 14 19 20\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "2 1000000000\n1 1000000000\n" }, "output": { ...
{ "tags": [ "implementation" ], "title": "Crazy Computer" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, c;\n cin >> n >> c;\n vector<int> T;\n for (int i = 0; i < n; ++i) {\n int t;\n cin >> t;\n T.push_back(t);\n }\n int sum = 1;\n for (int i = 1; i < n; ++i) {\n if ((T[i] - T[i - 1]) <= c)\n sum++;\n else\n ...
Codeforces/736/C
# Ostap and Tree Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph. Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex ...
[ { "input": { "stdin": "2 1\n1 2\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "7 2\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7\n" }, "output": { "stdout": "91" } }, { "input": { "stdin": "2 0\n1 2\n" }, "output": { "stdout": "1" ...
{ "tags": [ "dp", "trees" ], "title": "Ostap and Tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long dp[105][85];\nint n, k;\nvector<int> e[105];\nlong long mod = 1e9 + 7;\nvoid dfs(int u, int fa) {\n int len = e[u].size();\n if (len == 1 && fa == e[u][0]) {\n dp[u][k + 1] = 1;\n dp[u][0] = 1;\n } else {\n for (int i = 0; i < len; i++) {...
Codeforces/760/C
# Pavel and barbecue Pavel cooks barbecue. There are n skewers, they lay on a brazier in a row, each on one of n positions. Pavel wants each skewer to be cooked some time in every of n positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation p a...
[ { "input": { "stdin": "4\n4 3 2 1\n0 1 1 1\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3\n2 3 1\n0 0 0\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3\n1 3 2\n0 0 1\n" }, "output": { "stdout": "...
{ "tags": [ "constructive algorithms", "dfs and similar" ], "title": "Pavel and barbecue" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long N;\nlong long P[1000000];\nbool visited[1000000];\nlong long out = 0;\nvoid search(long long at) {\n if (visited[at]) return;\n visited[at] = true;\n search(P[at]);\n}\nint main() {\n cin >> N;\n for (long long i = 0; i < N; i++) {\n cin >> P...
Codeforces/780/G
# Andryusha and Nervous Barriers Andryusha has found a perplexing arcade machine. The machine is a vertically adjusted board divided into square cells. The board has w columns numbered from 1 to w from left to right, and h rows numbered from 1 to h from the bottom to the top. Further, there are barriers in some of bo...
[ { "input": { "stdin": "10 5 1\n3 2 3 10\n" }, "output": { "stdout": "7\n" } }, { "input": { "stdin": "10 5 2\n3 1 3 10\n5 3 5 10\n" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "10 5 2\n3 1 3 7\n5 3 5 10\n" }, "output": ...
{ "tags": [ "data structures", "dp" ], "title": "Andryusha and Nervous Barriers" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 3e5 + 100;\nconst int mod = 1e9 + 7;\nconst int inf = 1e9;\nint n, H, W, al[maxn], ar[maxn];\nvector<pair<pair<int, int>, pair<int, int> > > a;\nvector<pair<int, int> > vec;\npair<int, pair<int, int> > seg[maxn << 2];\npair<int, pair<int, int>...
Codeforces/804/D
# Expected diameter of a tree Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem. We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v...
[ { "input": { "stdin": "5 2 3\n2 4\n4 3\n4 2\n4 1\n2 5\n" }, "output": { "stdout": "-1\n2.6666666667\n2.6666666667\n" } }, { "input": { "stdin": "3 1 2\n1 3\n3 1\n2 3\n" }, "output": { "stdout": "-1\n2.0000000000\n" } }, { "input": { "stdin": ...
{ "tags": [ "binary search", "brute force", "dfs and similar", "dp", "sortings", "trees" ], "title": "Expected diameter of a tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint getint() {\n int x = 0, f = 1;\n char ch = getchar();\n while (ch < '0' || '9' < ch) {\n if (ch == '-') f = -1;\n ch = getchar();\n }\n while ('0' <= ch && ch <= '9') {\n x = x * 10 + ch - '0';\n ch = getchar();\n }\n return f * x;\n}\ni...
Codeforces/82/C
# General Mobilization The Berland Kingdom is a set of n cities connected with each other with n - 1 railways. Each road connects exactly two different cities. The capital is located in city 1. For each city there is a way to get from there to the capital by rail. In the i-th city there is a soldier division number i...
[ { "input": { "stdin": "5\n5 4 3 2 1\n1 2 1\n2 3 1\n2 4 1\n4 5 1\n" }, "output": { "stdout": "0 1 4 2 3\n" } }, { "input": { "stdin": "4\n40 10 30 20\n1 2 1\n2 3 1\n4 2 1\n" }, "output": { "stdout": "0 1 3 2\n" } }, { "input": { "stdin": "7\n4...
{ "tags": [ "data structures", "dfs and similar", "sortings" ], "title": "General Mobilization" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 5001;\nint n, prior[MAXN], t[MAXN], p[MAXN], cap[MAXN];\nint a, b, c, cnt;\npair<int, int> d[MAXN];\nvector<pair<int, int> > E[MAXN];\nvoid dfs(int v) {\n d[v].second = v;\n for (int i = 0; i < E[v].size(); i++) {\n int to = E[v][i].first...
Codeforces/850/E
# Random Elections The presidential election is coming in Bearland next year! Everybody is so excited about this! So far, there are three candidates, Alice, Bob, and Charlie. There are n citizens in Bearland. The election result will determine the life of all citizens of Bearland for many years. Because of this gre...
[ { "input": { "stdin": "3\n01010101\n" }, "output": { "stdout": "216\n" } }, { "input": { "stdin": "3\n01101001\n" }, "output": { "stdout": "168\n" } }, { "input": { "stdin": "6\n1100110100100111101010001011010011010010111010100001101101001100...
{ "tags": [ "bitmasks", "brute force", "divide and conquer", "fft", "math" ], "title": "Random Elections" }
{ "cpp": "#include <bits/stdc++.h>\ninline int in() {\n int k = 0;\n char ch = getchar();\n bool p = 1;\n while (ch < '-') ch = getchar();\n if (ch == '-') ch = getchar(), p = 0;\n while (ch > '-') k = k * 10 + ch - '0', ch = getchar();\n return p ? k : -k;\n}\nconst int YL = 1e9 + 7, N = 1 << 20;\ninline int ...
Codeforces/875/A
# Classroom Watch Eighth-grader Vova is on duty today in the class. After classes, he went into the office to wash the board, and found on it the number n. He asked what is this number and the teacher of mathematics Inna Petrovna answered Vova that n is the answer to the arithmetic task for first-graders. In the textb...
[ { "input": { "stdin": "21\n" }, "output": { "stdout": "1\n15 " } }, { "input": { "stdin": "20\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "75\n" }, "output": { "stdout": "0\n" } }, { "input": { ...
{ "tags": [ "brute force", "math" ], "title": "Classroom Watch" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class c>\nstruct rge {\n c b, e;\n};\ntemplate <class c>\nrge<c> range(c i, c j) {\n return rge<c>{i, j};\n}\ntemplate <class c>\nauto dud(c* x) -> decltype(cerr << *x, 0);\ntemplate <class c>\nchar dud(...);\nstruct debug {\n template <class c>\n...
Codeforces/899/F
# Letters Removing Petya has a string of length n consisting of small and large English letters and digits. He performs m operations. Each operation is described with two integers l and r and a character c: Petya removes from the string all characters c on positions between l and r, inclusive. It's obvious that the l...
[ { "input": { "stdin": "3 2\nA0z\n1 3 0\n1 1 z\n" }, "output": { "stdout": "Az\n" } }, { "input": { "stdin": "10 4\nagtFrgF4aF\n2 5 g\n4 9 F\n1 5 4\n1 7 a\n" }, "output": { "stdout": "tFrg4\n" } }, { "input": { "stdin": "4 2\nabac\n1 3 a\n2 2 ...
{ "tags": [ "data structures", "strings" ], "title": "Letters Removing" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 222222;\nconst int R = 66;\nint n, q, ans[N], w[N], h[N];\nchar ch[N], cc;\nset<int> S[R];\nset<int>::iterator it, i1, i2;\nint T[N << 2];\nvoid build(int k = 1, int l = 1, int r = n) {\n T[k] = r - l + 1;\n if (l == r) return;\n int x = l + r...
Codeforces/920/B
# Tea Queue Recently n students from city S moved to city P to attend a programming camp. They moved there by train. In the evening, all students in the train decided that they want to drink some tea. Of course, no two people can use the same teapot simultaneously, so the students had to form a queue to get their tea...
[ { "input": { "stdin": "2\n2\n1 3\n1 4\n3\n1 5\n1 1\n2 3\n" }, "output": { "stdout": "1 2\n1 0 2\n" } }, { "input": { "stdin": "1\n5\n1 1\n1 2\n1 5\n1 1\n1 1\n" }, "output": { "stdout": "1 2 3 0 0\n" } }, { "input": { "stdin": "1\n11\n1 1\n1 1...
{ "tags": [ "implementation" ], "title": "Tea Queue" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int t;\n cin >> t;\n while (t--) {\n int n;\n cin >> n;\n vector<pair<int, int> > l;\n int mp[n];\n for (int i = 0; i < n; i++) {\n int x, y;\n cin >> x >> y;\n l.push_back({x, i});\n mp[i] = y;\n }\n ...
Codeforces/948/E
# Picking Strings Alice has a string consisting of characters 'A', 'B' and 'C'. Bob can use the following transitions on any substring of our string in any order any number of times: * A <image> BC * B <image> AC * C <image> AB * AAA <image> empty string Note that a substring is one or more consecutive c...
[ { "input": { "stdin": "AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3\n" }, "output": { "stdout": "10011\n" } }, { "input": { "stdin": "AAAAACAAAAAB\nAAAAABAAAAAC\n20\n1 6 10 12\n10 12 7 12\n9 12 8 12\n2 6 8 12\n7 12 2 6\n9 12 7 12\n1 6 10 12\n4 6 3 6\n7 12...
{ "tags": [ "constructive algorithms", "implementation", "strings" ], "title": "Picking Strings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 100009;\nchar s1[MAXN], s2[MAXN], ans[MAXN];\nint q, B1[MAXN], A1[MAXN], B2[MAXN], A2[MAXN];\nint main() {\n memset(A1, 0, sizeof(A1));\n memset(B1, 0, sizeof(B1));\n memset(A2, 0, sizeof(A2));\n memset(B2, 0, sizeof(B2));\n scanf(\"%s %s...
Codeforces/976/E
# Well played! Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them: Max owns n creatures, i-th of them can be described with two numbers — its health hpi and its damage dmgi. Max als...
[ { "input": { "stdin": "2 1 1\n10 15\n6 1\n" }, "output": { "stdout": "27\n" } }, { "input": { "stdin": "3 0 3\n10 8\n7 11\n5 2\n" }, "output": { "stdout": "26\n" } }, { "input": { "stdin": "1 0 0\n2 1\n" }, "output": { "stdout":...
{ "tags": [ "greedy", "sortings" ], "title": "Well played!" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MOD = 1e9 + 7;\nconst long double PI = 3.141592653589793238462643383279502884197;\nlong long fac[1] = {1}, inv[1] = {1};\nlong long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }\nlong long mp(long long a, long long b) {\n long long...
Codeforces/996/C
# Tesla Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He knows that this will give him a big status boost. As Allen is planning out all of the different types of cars he will own and how he will arrange them, he realizes that he has a problem. Allen's future parking lot can...
[ { "input": { "stdin": "1 2\n1\n1\n2\n2\n" }, "output": { "stdout": "2\n1 1 1\n2 4 1\n" } }, { "input": { "stdin": "4 5\n1 2 0 4\n1 2 0 4\n5 0 0 3\n0 5 0 3\n" }, "output": { "stdout": "12\n1 1 1\n2 1 2\n4 1 4\n3 4 4\n5 2 1\n5 2 2\n5 2 3\n5 2 4\n5 3 4\n5 3 3\n5 ...
{ "tags": [ "constructive algorithms", "implementation" ], "title": "Tesla" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint col, k, res = 0, full = false;\nint cnt = 0;\nvector<tuple<int, int, int>> seq;\nvector<vector<int>> grid;\nvoid reset() {\n for (auto &row : grid) {\n for (auto &c : row) {\n if (c == -1) c = 0;\n }\n }\n}\nvoid rotate() {\n for (int i = 1; ...
HackerEarth/bandu-the-saviour
The Bandu of the Hackers' community had a very urgent job to do. He needs to diffuse all the bombs placed by Poker. Poker is extremely clever and cruel. He selected a region of 1000 X 1000 points, chose N center points of the form (X,Y) and placed bombs on every integral point that was on or inside the circular region ...
[ { "input": { "stdin": "1\n172 686 234" }, "output": { "stdout": "0" } } ]
{ "tags": [], "title": "bandu-the-saviour" }
{ "cpp": null, "java": null, "python": "t = int(input())\nmat = [ [0 for i in range(0, 1001)] for j in range(0, 1001)]\n \nwhile(t):\n\tval = str(raw_input())\n\tval = val.split(\" \")\n\tx,y,r = int(val[0]), int(val[1]), int(val[2])\n\t#print(\"X Y R\")\n\t#print x,y,r\n \n\trSquare = r ** 2\n\t#print(\"rSquare ...
HackerEarth/city-and-fireman-vincent
Fatland is a town with N cities numbered 1, 2, ..., N, connected with 2-way roads. Vincent is a villian who wants to set the entire town ablaze. For each city, there is a certain risk factor E[i] for setting that city ablaze. But once a city c is set ablaze, all cities that can be reached from c will be set ablaze imme...
[ { "input": { "stdin": "4\n3 3 3 3\n2\n1 2\n4 1\n\nSAMPLE" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "523\n99 579 761 271 923 129 332 471 667 607 959 414 553 147 885 77 776 670 194 603 22 688 288 317 223 844 193 657 919 827 627 918 502 278 957 646 874 501 760...
{ "tags": [], "title": "city-and-fireman-vincent" }
{ "cpp": null, "java": null, "python": "N = int(raw_input())\n\narr = []\nsize = []\n\n\ndef initialize(arr, n):\n for i in range(n):\n arr.append(i)\n size.append(1)\n \n\n\n\ninitialize(arr, N)\n\ne = map(int, raw_input().strip().split(\" \"))\n\n\ndef root(arr, i):\n while arr[i] != ...
HackerEarth/et-comes-to-india
An extraterrestrial visit! Remember your childhood friend JAADU from outer space?? Well, he is back again to our mighty Planet Earth. But do you expect us geeks to introduce a character like Rohit Mehra in this story? A Hell No!! Instead, he encounters the creepy Scientists of Planet Earth all the way from S.H.I.E....
[ { "input": { "stdin": "4\n1 1\n2 1\n2 3\n2 5\n\nSAMPLE" }, "output": { "stdout": "G\nG\nC\n-1\n" } }, { "input": { "stdin": "4\n1 1\n2 1\n2 3\n2 5" }, "output": { "stdout": "G\nG\nC\n-1\n" } }, { "input": { "stdin": "659\n8 972726106237281\n2...
{ "tags": [], "title": "et-comes-to-india" }
{ "cpp": null, "java": null, "python": "def pf(k):\n h = k & -k\n k &= (h << 1)\n return (k==0)\nfor _ in range(int(raw_input())):\n\tn, k = map(int, raw_input().split())\n\tif 2 ** n - 1 < k:\n\t\tprint -1\n\telse:\n\t\tprint \"G\" if pf(k) else \"C\"" }
HackerEarth/help-out-the-indian-army
Protection of the Indian border and safe transport of items from one point to another along the border are the paramount jobs for the Indian army. However they need some information about the protection status along the length of the border. The border can be viewed as the real x-axis. Along the axis, Indian army has N...
[ { "input": { "stdin": "5 229 8419\n1795 95\n4873 720\n3149 81\n6101 2325\n3674 629\n\nSAMPLE" }, "output": { "stdout": "2626\n" } }, { "input": { "stdin": "5 1875 31573\n572 10\n788 110\n472 155\n7295 3261\n109 2508\n\nMLTPAD" }, "output": { "stdout": "22434\n...
{ "tags": [], "title": "help-out-the-indian-army" }
{ "cpp": null, "java": null, "python": "__author__ = 'apritam'\n\n\ndef is_in_range(value , start, end):\n if value >=start and value <=end:\n return True\n return False\n\ntest_case_input=raw_input()\nn_s_e_list=test_case_input.split(' ')\nnumber_of_check = long(n_s_e_list[0])\nindex = 0;\nl=[];\n#l...
HackerEarth/lucky-time
Alice is a geeky girl. She has a lot of codes to execute but she always choose a lucky time to execute a code. Time is shown in 24 hour format as hh:mm:ss Time is said to be lucky if all the 6 characters (except ':') are different. Given the time when she completed the code find a lucky time to execute it so that Ali...
[ { "input": { "stdin": "3\n00:00:00\n12:59:59\n23:59:59\n\nSAMPLE" }, "output": { "stdout": "01:23:45\n13:02:45\n01:23:45\n" } }, { "input": { "stdin": "3\n11:00:10\n05:39:59\n23:59:58\n\nRDQBKM" }, "output": { "stdout": "12:03:45\n05:41:23\n01:23:45\n" } ...
{ "tags": [], "title": "lucky-time" }
{ "cpp": null, "java": null, "python": "import sys\nclass test:\n def __init__(self):\n self.s = raw_input()\n self.ti = self.s.split(':')\n for i in range(0, 3):\n self.ti[i] = int(self.ti[i])\n if self.ti[0] > 23 or self.ti[1] > 59 or self.ti[2] > 59:\n raise...
HackerEarth/navi-and-maths
Navi is a famous mathematician. He is working on Division, Multiplication and Addition. He is in need of some hidden patterns to discover some new concepts. He is giving you a task to find that sub-sequence of an array which has the maximum P % mod value, where value of the mod is given below. This sub-sequence shou...
[ { "input": { "stdin": "2\n2\n2 2\n2\n6 3\n\nSAMPLE" }, "output": { "stdout": "Case #1: 1\nCase #2: 2\n" } }, { "input": { "stdin": "2\n2\n4 2\n2\n8 3\n\nSAMPLE" }, "output": { "stdout": "Case #1: 333333337\nCase #2: 636363643\n" } }, { "input": { ...
{ "tags": [], "title": "navi-and-maths" }
{ "cpp": null, "java": null, "python": "def main():\n from itertools import combinations\n from sys import stdin\n from operator import mul\n M = 1000000007\n \n def egcd(a, b):\n if a == 0:\n return (b, 0, 1)\n else:\n g, y, x = egcd(b % a, a)\n re...
HackerEarth/prof-k-trivedis-love-for-strings
Aditya is a professor in Engilsh loves to play with words. Today he asks his assistant Abishek to perform an experiment where he wants him to calculate the total number of word that can be formed from a given word such that the characters are arranged lexicographically(in alphabetical order). The words that are to be f...
[ { "input": { "stdin": "3\nankit\naman\ncvrcebhubaneswar\n\nSAMPLE" }, "output": { "stdout": "5\n3\n11\n" } }, { "input": { "stdin": "100\nyqypgenznlzztptieaqkgldlzzvfptbgkaxbfygoqyaagbvulzulemtnybvszhmpbrqmyodsudywprbyfbdlwdusnrgxytlrcaybobwugeosucuqreyfzgktbatrcponfibpsbfw...
{ "tags": [], "title": "prof-k-trivedis-love-for-strings" }
{ "cpp": null, "java": null, "python": "for _ in range(int(raw_input())):\n\tst = str(raw_input())\n\tln = len(st)\n\tlst = []\n\tcount = 0\n\tfor i in range(0, ln):\n\t\tif(st[i] not in lst):\n\t\t lst.append(st[i])\n\t\t count += 1\n\tprint(count)" }
HackerEarth/sherlock-and-ciphers-5
Sherlock has intercepted some encrypted messages, which he suspects are from Professor Moriarty. He found that the number of characters in each message (including spaces) is always a square number. From this he deduces that it must be a Caesar Box Cipher. In a Caesar Box Cipher, suppose the number of characters is L ...
[ { "input": { "stdin": "101\ng hJtLZNqYMMjePTIaxEBMyVWqwohAxuXQLPzJH JsJCjQeNmJbKYjU FyEFnTZSwNKEmkZktwcQCPGtgzYjeZbg OXIiEBMzomm\nBDSBHdwXUpfbYonKssmVOlcsQGKAohfsHcdQvUeywFRhOLUDuzRFquPqwHJrEslf\neYLKUFamzcZ CZMHwiTLZBxhXgiFpThKSYhg\nwiUcahEtiTIUZxirNMRPryjZobZHcgknTVJTNLHZcxKOLZxJfJbwbkmlzGNfE LblFMxrxhm...
{ "tags": [], "title": "sherlock-and-ciphers-5" }
{ "cpp": null, "java": null, "python": "n = input()\nfor _ in xrange(n):\n s = raw_input()\n m = int(len(s) ** .5)\n l = []\n for i in xrange(m):\n v = []\n for j in xrange(m):\n v += [s[i*m+j]]\n l += [v]\n print ''.join(map(lambda t: ''.join(list(t)), zip(*l)))" }
HackerEarth/the-cipher
Vinay loves stories on serial killers. He is hypnotized to the stories of The Zodiac Killer. Zodiac was a serial killer in USA in late 1960s and early 1970s. He never revealed his original identity but named himself as Zodiac on the letters he posted to police and news papers. He used to post them about the murders he ...
[ { "input": { "stdin": "1\n6\n133 144 155 150 158 156\n\nSAMPLE" }, "output": { "stdout": "zodiac\n" } }, { "input": { "stdin": "10\n21\n182 223 158 146 223 165 144 155 150 158 156 223 172 143 154 158 148 150 145 152 209\n82\n182 223 158 146 223 139 151 154 223 144 145 154 2...
{ "tags": [], "title": "the-cipher" }
{ "cpp": null, "java": null, "python": "'''\n# Read input from stdin and provide input before running code\n\nname = raw_input('What is your name?\\n')\nprint 'Hi, %s.' % name\n'''\ndef find_message(input_numbers):\n\toutput = ''\n\tfor number in input_numbers:\n\t\tcharacter = 255 - number\n\t\toutput = output +...
HackerEarth/xenny-and-k-matching
Xenny had N boxes with an integer printed on each of the boxes. He wanted to find out how many distinct pairs of boxes that were at distance k from each other, had an absolute value of difference or sum that was a multiple of k. Help him in this task. (Note: Consider two indices i1 and i2. Pairs (i1, i2) and (i2, i1...
[ { "input": { "stdin": "10 1\n1 2 3 4 5 6 7 8 9 10\n\nSAMPLE" }, "output": { "stdout": "9\n" } }, { "input": { "stdin": "4 1\n1 2 0 4 3 6 13 8 5 7\n\nSAMPLF" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "4 1\n1 2 0 4 5 6 13 8 6 7\...
{ "tags": [], "title": "xenny-and-k-matching" }
{ "cpp": null, "java": null, "python": "s=map(int,raw_input().split())\nn,k=s\nN=map(int,raw_input().split())\nd={}\nfor i in xrange(0,n):\n if (i+k)<n:\n if (N[i]+N[i+k])%k==0 or abs(N[i]-N[i+k])%k==0:\n if (i,i+k) not in d and (i+k,i) not in d:\n d[(i,i+k)]=1\n else:\n ...
p02649 Tokio Marine & Nichido Fire Insurance Programming Contest 2020 - O(rand)
Given are N pairwise distinct non-negative integers A_1,A_2,\ldots,A_N. Find the number of ways to choose a set of between 1 and K numbers (inclusive) from the given numbers so that the following two conditions are satisfied: * The bitwise AND of the chosen numbers is S. * The bitwise OR of the chosen numbers is T. C...
[ { "input": { "stdin": "3 3 0 3\n1 2 3" }, "output": { "stdout": "2" } }, { "input": { "stdin": "5 3 1 7\n3 4 9 1 5" }, "output": { "stdout": "2" } }, { "input": { "stdin": "5 4 0 15\n3 4 9 1 5" }, "output": { "stdout": "3" }...
{ "tags": [], "title": "p02649 Tokio Marine & Nichido Fire Insurance Programming Contest 2020 - O(rand)" }
{ "cpp": "#include <algorithm>\n#include <array>\n#include <cassert>\n#include <iostream>\n#include <vector>\nusing namespace std;\n\ntemplate<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = \", \"; return os << '}'; }\ntemplate<t...
p02778 AtCoder Beginner Contest 154 - I miss you...
Given is a string S. Replace every character in S with `x` and print the result. Constraints * S is a string consisting of lowercase English letters. * The length of S is between 1 and 100 (inclusive). Input Input is given from Standard Input in the following format: S Output Replace every character in S with ...
[ { "input": { "stdin": "sardine" }, "output": { "stdout": "xxxxxxx" } }, { "input": { "stdin": "xxxx" }, "output": { "stdout": "xxxx" } }, { "input": { "stdin": "gone" }, "output": { "stdout": "xxxx" } }, { "input": {...
{ "tags": [], "title": "p02778 AtCoder Beginner Contest 154 - I miss you..." }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() \n{\n string s; cin >> s;\n cout << string(s.length(), 'x');\n \n return 0;\n}", "java": "import java.util.Scanner;\n\npublic class Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\t\tString str = s...
p02913 AtCoder Beginner Contest 141 - Who Says a Pun?
Given is a string S of length N. Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following: ...
[ { "input": { "stdin": "13\nstrangeorange" }, "output": { "stdout": "5" } }, { "input": { "stdin": "2\nxy" }, "output": { "stdout": "0" } }, { "input": { "stdin": "5\nababa" }, "output": { "stdout": "2" } }, { "input"...
{ "tags": [], "title": "p02913 AtCoder Beginner Contest 141 - Who Says a Pun?" }
{ "cpp": "#include<iostream>\n#include<string>\n\nusing namespace std;\n\nint main(){\n int N;\n cin >> N;\n string S;\n cin >> S;\n\n int ans = 0;\n\n\n for(int i = 1; i<N; i++ ){\n int now = 0;\n for(int j = 0; j < N-i; j++){\n if(S[j] == S[i + j]){\n now ++...
p03049 diverta 2019 Programming Contest - AB Substrings
Snuke has N strings. The i-th string is s_i. Let us concatenate these strings into one string after arranging them in some order. Find the maximum possible number of occurrences of `AB` in the resulting string. Constraints * 1 \leq N \leq 10^{4} * 2 \leq |s_i| \leq 10 * s_i consists of uppercase English letters. In...
[ { "input": { "stdin": "9\nBEWPVCRWH\nZZNQYIJX\nBAVREA\nPA\nHJMYITEOX\nBCJHMRMNK\nBP\nQVFABZ\nPRGKSPUNA" }, "output": { "stdout": "4" } }, { "input": { "stdin": "7\nRABYBBE\nJOZ\nBMHQUVA\nBPA\nISU\nMCMABAOBHZ\nSZMEHMA" }, "output": { "stdout": "4" } }, ...
{ "tags": [], "title": "p03049 diverta 2019 Programming Contest - AB Substrings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for (int i = 0; i < (n); ++i)\n\nint main() {\n\tint N,l,ba=0,b=0,a=0,r=0;\n\tstring S;\n\tcin >> N;\n\n\trep(i,N) {\n\t\tcin >> S;\n\t\tl = S.size();\n\n\t\trep(j,l-1)\n\t\t\tif(S.substr(j,2) == \"AB\")\n\t\t\t\t++r;\n\n\t\tif(S.at(0)=='B' &&...
p03190 AtCoder Grand Contest 030 - Less than 3
You are given strings s and t, both of length N. s and t consist of `0` and `1`. Additionally, in these strings, the same character never occurs three or more times in a row. You can modify s by repeatedly performing the following operation: * Choose an index i (1 \leq i \leq N) freely and invert the i-th character i...
[ { "input": { "stdin": "1\n0\n0" }, "output": { "stdout": "0" } }, { "input": { "stdin": "8\n00110011\n10101010" }, "output": { "stdout": "10" } }, { "input": { "stdin": "4\n0011\n0101" }, "output": { "stdout": "4" } }, {...
{ "tags": [], "title": "p03190 AtCoder Grand Contest 030 - Less than 3" }
{ "cpp": "#include<cstdio>\n#include<iostream>\n#include<algorithm>\n#include<cstring>\n#include<queue>\n#include<cmath>\n#include<vector>\n#include<stack>\n#include<map>\n#include<set>\n#define Run(i,l,r) for(int i=l;i<=r;i++)\n#define Don(i,l,r) for(int i=l;i>=r;i--)\n#define ll long long\n#define ld long double\n#...
p03337 AtCoder Beginner Contest 098 - Add Sub Mul
You are given two integers A and B. Find the largest value among A+B, A-B and A \times B. Constraints * -1000 \leq A,B \leq 1000 * All values in input are integers. Input Input is given from Standard Input in the following format: A B Output Print the largest value among A+B, A-B and A \times B. Examples Inp...
[ { "input": { "stdin": "0 0" }, "output": { "stdout": "0" } }, { "input": { "stdin": "3 1" }, "output": { "stdout": "4" } }, { "input": { "stdin": "4 -2" }, "output": { "stdout": "6" } }, { "input": { "stdin": "...
{ "tags": [], "title": "p03337 AtCoder Beginner Contest 098 - Add Sub Mul" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main(){\n int A,B;cin>>A>>B;\n cout<<max({A+B,A-B,A*B});\n}", "java": "import java.util.Scanner;\n\npublic class Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\t\tint a = sc.nextInt();\n\t\tint b = sc.nex...
p03499 AtCoder Regular Contest 086 - Smuggling Marbles
Snuke has a rooted tree with N+1 vertices. The vertices are numbered 0 through N, and Vertex 0 is the root of the tree. The parent of Vertex i (1 \leq i \leq N) is Vertex p_i. Besides this tree, Snuke also has an box which is initially empty and many marbles, and playing with them. The play begins with placing one mar...
[ { "input": { "stdin": "2\n0 0" }, "output": { "stdout": "8" } }, { "input": { "stdin": "31\n0 1 0 2 4 0 4 1 6 4 3 9 7 3 7 2 15 6 12 10 12 16 5 3 20 1 25 20 23 24 23" }, "output": { "stdout": "730395550" } }, { "input": { "stdin": "5\n0 1 1 0 ...
{ "tags": [], "title": "p03499 AtCoder Regular Contest 086 - Smuggling Marbles" }
{ "cpp": "#include \"bits/stdc++.h\"\nusing namespace std;\nconst int N = 2e5 + 5;\nconst int mod = 1e9 + 7;\nint n;\nint parent[N];\nvector < int > v[N];\ndeque < pair < int , pair < int , int > > > val[N];\nint mx;\nvoid merge(int node , int child){\n\tfor(int i = 0 ; i < val[child].size() ; ++i){\n\t\tint azero = ...
p03658 AtCoder Beginner Contest 067 - Snake Toy
Snuke has N sticks. The length of the i-th stick is l_i. Snuke is making a snake toy by joining K of the sticks together. The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy. Constraints * 1 \leq K \leq N \leq 50 * 1 \leq l_i \leq 50 ...
[ { "input": { "stdin": "15 14\n50 26 27 21 41 7 42 35 7 5 5 36 39 1 45" }, "output": { "stdout": "386" } }, { "input": { "stdin": "5 3\n1 2 3 4 5" }, "output": { "stdout": "12" } }, { "input": { "stdin": "15 14\n7 26 27 21 12 18 42 35 7 5 5 31...
{ "tags": [], "title": "p03658 AtCoder Beginner Contest 067 - Snake Toy" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nint main(){\n\tint N,K;\n\tcin>>N>>K;\n\tint l[N];\n\tint sum=0;\n\tfor(int i=0;i<N;i++){\n\t\tcin>>l[i];\n\t}\n\tsort(l,l+N);\n\treverse(l,l+N);\n\tfor(int k=0;k<K;k++){\n\t\tsum+=l[k];\n\t}\n\tcout<<sum<<endl;\n}\n", "java": "import java.util.Arrays;\nimpor...
p03814 AtCoder Beginner Contest 053 - A to Z String
Snuke has decided to construct a string that starts with `A` and ends with `Z`, by taking out a substring of a string s (that is, a consecutive part of s). Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with `A` and ends w...
[ { "input": { "stdin": "HASFJGHOGAKZZFEGA" }, "output": { "stdout": "12" } }, { "input": { "stdin": "ZABCZ" }, "output": { "stdout": "4" } }, { "input": { "stdin": "QWERTYASDFZXCV" }, "output": { "stdout": "5" } }, { ...
{ "tags": [], "title": "p03814 AtCoder Beginner Contest 053 - A to Z String" }
{ "cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nint main() {\n string s; cin >> s;\n int min_a = s.find('A');\n int max_z = s.rfind('Z');\n cout << max_z - min_a + 1;\n}", "java": "import java.util.Scanner;\n\npublic class Main {\n\n\tpublic static void main(String[] args) {\n\t\...
p03983 Kyoto University Programming Contest 2016 - Handing out leaflets
Eli- 1 started a part-time job handing out leaflets for N seconds. Eli- 1 wants to hand out as many leaflets as possible with her special ability, Cloning. Eli- gen can perform two kinds of actions below. * Clone herself and generate Eli- (gen + 1) . (one Eli- gen (cloning) and one Eli- (gen + 1) (cloned) exist as a r...
[ { "input": { "stdin": "1\n20 3" }, "output": { "stdout": "67" } }, { "input": { "stdin": "1\n200 1" }, "output": { "stdout": "148322100" } }, { "input": { "stdin": "2\n20 8\n20 12" }, "output": { "stdout": "24\n20" } } ]
{ "tags": [], "title": "p03983 Kyoto University Programming Contest 2016 - Handing out leaflets" }
{ "cpp": "#include <iostream>\nusing namespace std;\n#pragma warning (disable: 4996)\n\nint mod = 1000000007;\nint prev_[100009], dp[100009], dpr[100009];\nint mulr[100009];\n\nlong long Q;\nlong long N[1 << 18], C[1 << 18], Base[1 << 18];\nlong long Answer[1 << 18];\n\nvoid init(int pos) {\n\tint lims = (pos - 2) * ...
AIZU/p00071
# Bombs Chain There is a plane like Figure 1 with 8 vertical and 8 horizontal squares. There are several bombs on that plane. Figure 2 shows an example (● = bomb). | □ | □ | □ | □ | □ | □ | □ | □ --- | --- | --- | --- | --- | --- | --- | --- □ | □ | □ | □ | □ | □ | □ | □ □ | □ | □ | □ | □ | □ | □ | □ □ | □ | □ | □ | ...
[ { "input": { "stdin": "2\n\n00010010\n00000100\n10001001\n00100010\n01000000\n00001000\n10100010\n01010010\n2\n5\n\n00010010\n00000100\n10001001\n00100010\n01000000\n00001000\n10100010\n01010010\n2\n5" }, "output": { "stdout": "Data 1:\n00000000\n00000100\n10001001\n00100000\n00000000\n00001...
{ "tags": [], "title": "Bombs Chain" }
{ "cpp": "#include<iostream>\nusing namespace std;\nstring s[8];\nvoid f(int x,int y)\n{\n s[x][y]='0';\n for(int i=-3;i<=3;i++)\n {\n if(x+i>=0&&x+i<8&&s[x+i][y]=='1')f(x+i,y);\n if(y+i>=0&&y+i<8&&s[x][y+i]=='1')f(x,y+i);\n }\n}\nint main()\n{\n int t;cin>>t;\n for(int count=1;count<=...
AIZU/p00203
# A New Plan of Aizu Ski Resort Yukiya, the owner of Aizuyama Ski Resort, has prepared a course for advanced skiers with obstacles and jumping hills. There are various ways to slide on the course, and users who can slide in all patterns during the season will be given a gift. Let's create a program for the oil tree s...
[ { "input": { "stdin": "5 5\n0 0 0 0 1\n2 1 0 2 0\n1 0 0 1 1\n0 2 1 2 0\n0 1 0 0 0\n5 5\n0 0 1 0 0\n2 1 0 2 0\n1 0 0 1 1\n0 2 1 2 0\n0 1 0 0 0\n15 15\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0...
{ "tags": [], "title": "A New Plan of Aizu Ski Resort" }
{ "cpp": "#include<iostream>\n#include<cstring>\nusing namespace std;\n\nint dp[17][17];\nint map[17][17];\nint y,x;\n\nlong long int check(int i,int j)\n{\n if(j>=x||j<0)\n return 0;\n if(i>=y)\n return 1;\n\n if(map[i][j]==1)\n return 0;\n\n if(dp[i][j]!=-1)\n return dp[i][j];\n\n long long int sum...
AIZU/p00359
# Dungeon Bob is playing a popular game called "Dungeon". The game is played on a rectangular board consisting of W × H squares. Each square is identified with its column and row number, thus the square located in the x-th column and the y-th row is represented as (x, y). The left-most square in the top row is (0, 0) ...
[ { "input": { "stdin": "5 4 4\n0 3\n1 1\n2 2\n2 3" }, "output": { "stdout": "2" } }, { "input": { "stdin": "6 6 5\n2 1\n5 2\n3 3\n1 4\n1 5" }, "output": { "stdout": "4" } }, { "input": { "stdin": "8 8 4\n6 0\n7 0\n0 6\n0 7" }, "output"...
{ "tags": [], "title": "Dungeon" }
{ "cpp": "#include <bits/stdc++.h>\ntypedef long long ll;\nusing namespace std;\nconst int MOD = 1e9 + 7;\nint main(){\n\tint w,h,n;\n\tcin >> w >> h >> n;\n\tstd::vector<int> py(n);\n\tstd::vector<int> px(n);\n\tstd::vector<pair<int,int> > x(n);\n\tstd::vector<pair<int,int> > y(n);\n\n\tfor(int i = 0; i < n; ++i){\n...
AIZU/p00570
# Stove JOI has a stove in your room. JOI himself is resistant to the cold, so he doesn't need to put on the stove when he is alone in the room, but he needs to put on the stove when there are visitors. On this day, there are N guests under JOI. The ith (1 \ leq i \ leq N) visitor arrives at time T_i and leaves at ti...
[ { "input": { "stdin": "3 2\n1\n3\n6" }, "output": { "stdout": "4" } }, { "input": { "stdin": "3 3\n0\n3\n10" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "3 2\n0\n5\n20" }, "output": { "stdout": "7\n" } }, {...
{ "tags": [], "title": "Stove" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing P = pair<int,int>;\n#define enld '\\n'\n#define rep(i,n) for(int i=0; i<(n); i++)\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native\")\n#pragma GCC optimize(\"Ofast\")\nconstexpr ll INF = 1e18;\nconstexpr...
AIZU/p00717
# Polygonal Line Search Multiple polygonal lines are given on the xy-plane. Given a list of polygonal lines and a template, you must find out polygonal lines which have the same shape as the template. A polygonal line consists of several line segments parallel to x-axis or y-axis. It is defined by a list of xy-coordi...
[ { "input": { "stdin": "5\n5\n0 0\n2 0\n2 1\n4 1\n4 0\n5\n0 0\n0 2\n-1 2\n-1 4\n0 4\n5\n0 0\n0 1\n-2 1\n-2 2\n0 2\n5\n0 0\n0 -1\n2 -1\n2 0\n4 0\n5\n0 0\n2 0\n2 -1\n4 -1\n4 0\n5\n0 0\n2 0\n2 1\n4 1\n4 0\n4\n4\n-60 -75\n-60 -78\n-42 -78\n-42 -6\n4\n10 3\n10 7\n-4 7\n-4 40\n4\n-74 66\n-74 63\n-92 63\n-92 135\...
{ "tags": [], "title": "Polygonal Line Search" }
{ "cpp": "#include<iostream>\n#include<algorithm>\n#include<vector>\nusing namespace std;\ntypedef vector<int> vi;\n\nvoid rotate(vi& x, vi& y){\n for(int i = 0; i < x.size(); i++){\n swap(x[i], y[i]);\n y[i] *= -1;\n }\n}\n\nbool same(vi ax, vi ay, vi bx, vi by){\n for(int i = 0; i < ax.size()...
AIZU/p00857
# Lowest Pyramid You are constructing a triangular pyramid with a sheet of craft paper with grid lines. Its base and sides are all of triangular shape. You draw the base triangle and the three sides connected to the base on the paper, cut along the outer six edges, fold the edges of the base, and assemble them up as a...
[ { "input": { "stdin": "0 0 1 0 0 1\n0 0 5 0 2 5\n-100 -100 100 -100 0 100\n-72 -72 72 -72 0 72\n0 0 0 0 0 0" }, "output": { "stdout": "2\n1.49666\n-1\n8.52936" } }, { "input": { "stdin": "-2 0 3 0 0 1\n0 0 5 1 2 1\n-100 -100 100 -100 -1 100\n-40 -3 85 -72 0 72\n0 0 0 0 0 0"...
{ "tags": [], "title": "Lowest Pyramid" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, j) for(int i=0; i < (int)(j); i++)\n#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)\ntemplate<class T> bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; }\n\nconst double INF = 1e18;\nconstexpr double EPS = 1e-8;\...
AIZU/p00988
# Fun Region Fun Region Dr. Ciel lives in a planar island with a polygonal coastline. She loves strolling on the island along spiral paths. Here, a path is called spiral if both of the following are satisfied. * The path is a simple planar polyline with no self-intersections. * At all of its vertices, the line segme...
[ { "input": { "stdin": "4\n10 0\n20 10\n10 30\n0 10" }, "output": { "stdout": "300.00" } } ]
{ "tags": [], "title": "Fun Region" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nmt19937 mt(48);\n\n#define rep(i, a, b) for(int i = a; i < (b); ++i)\n#define trav(a, x) for(auto& a : x)\n#define all(x) x.begin(), x.end()\n#define sz(x) (int)(x).size()\ntypedef long long ll;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\n\nusing ...
AIZU/p01120
# Tally Counters <!-- Problem D --> Tally Counters A number of tally counters are placed in a row. Pushing the button on a counter will increment the displayed value by one, or, when the value is already the maximum, it goes down to one. All the counters are of the same model with the same maximum value. <image> ...
[ { "input": { "stdin": "4 5\n2 3 1 4\n2 5 5 2\n3 100\n1 10 100\n1 10 100\n5 10000\n4971 7482 1238 8523 1823\n3287 9013 9812 297 1809\n0 0" }, "output": { "stdout": "4\n0\n14731" } }, { "input": { "stdin": "4 5\n2 3 2 4\n2 5 5 2\n3 100\n1 2 100\n1 14 100\n5 10000\n4971 7482 1...
{ "tags": [], "title": "Tally Counters" }
{ "cpp": "#include <iostream>\nusing namespace std;\nint n,m,s[1000],a[1000],b[1000],ans,dp[1000][1001];\nint main(void){\n while(1){\n cin>>n>>m;\n if(n==0&&m==0)return 0;\n ans=1e9;\n for(int i=0;i<n;i++)for(int j=0;j<=n;j++)dp[i][j]=1e9;\n for(int i=0;i<n;i++)cin>>a[i];\n for(int i=0;i<n;i++)c...
AIZU/p01258
# Memory Match Memory match is a single-player game which employs a set of 2M cards. Each card is labeled with a number between 1 and M on its face. For each number i (1 ≤ i ≤ M), there are exactly two cards which have the number i. At the start of the game, all cards are shuffled and laid face down on a table. In eac...
[ { "input": { "stdin": "2\n4\n6\n8\n10\n52\n0" }, "output": { "stdout": "0.0000000000\n0.6666666667\n1.3333333333\n1.9238095238\n2.5523809524\n15.4435236099" } }, { "input": { "stdin": "2\n6\n0\n2\n0\n44\n-2" }, "output": { "stdout": "0.00000000\n1.33333333\n" ...
{ "tags": [], "title": "Memory Match" }
{ "cpp": "#include <stdio.h>\n#include <cmath>\n#include <algorithm>\n#include <cfloat>\n#include <stack>\n#include <queue>\n#include <vector>\n#include <string>\n#include <iostream>\n#include <set>\n#include <map>\n#include <time.h>\ntypedef long long int ll;\ntypedef unsigned long long int ull;\n#define BIG_NUM 200...
AIZU/p01419
# On or Off Saving electricity is very important! You are in the office represented as R \times C grid that consists of walls and rooms. It is guaranteed that, for any pair of rooms in the office, there exists exactly one route between the two rooms. It takes 1 unit of time for you to move to the next room (that is, ...
[ { "input": { "stdin": "3 3 5\n...\n.##\n..#\n1 1 1\n1 0 0\n1 1 0\n3 3 3\n3 0 0\n3 3 0\n5 4 5\n4 0 0\n5 4 0\n1 0\n2 1\n0 2\n2 0\n0 0" }, "output": { "stdout": "77" } }, { "input": { "stdin": "1 3 2\n...\n1 1 1\n1 2 1\n1 1 1\n0 0\n0 2" }, "output": { "stdout": "...
{ "tags": [], "title": "On or Off" }
{ "cpp": "/* template.cpp {{{ */\n#include <bits/stdc++.h>\nusing namespace std;\n#define get_macro(a, b, c, d, name, ...) name\n#define rep(...) get_macro(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)\n#define rrep(...) get_macro(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)\n#define rep1(n) rep2(i_, n)\...
AIZU/p01573
# Complex Integer Solutions Let f(x) = a0 + a1x + a2x2 + ... + adxd be the function where each ai (0 ≤ i ≤ d) is a constant integer (and ad is non-zero) and x is a variable. Your task is to write a program that finds all complex integer solutions of the equation f(x) = 0 for a given f(x). Here, by complex integers, we...
[ { "input": { "stdin": "4\n-2 0 0 0 2" }, "output": { "stdout": "4\n-1 -i i 1" } }, { "input": { "stdin": "8\n0 0 25 15 17 -10 1 -1 1" }, "output": { "stdout": "5\n-1-2i -1+2i 0 2-i 2+i" } } ]
{ "tags": [], "title": "Complex Integer Solutions" }
{ "cpp": "//Bairstow\n\n#include <cstdio>\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <utility>\n#include <complex>\n#include <cmath>\nusing namespace std;\n \n#define EPS_S 1e-9\n#define EPS 1e-4\n \ntypedef complex<double> P;\ntypedef pair<int,int> pii;\n \nvoid bairstow(double &p, doubl...
AIZU/p01735
# Optimal alpha beta pruning Fox Ciel is developing an artificial intelligence (AI) for a game. This game is described as a game tree T with n vertices. Each node in the game has an evaluation value which shows how good a situation is. This value is the same as maximum value of child nodes’ values multiplied by -1. Va...
[ { "input": { "stdin": "3\n0 1 1\n2 2 3\n0\n0" }, "output": { "stdout": "2 2" } }, { "input": { "stdin": "3\n0 1 4\n2 2 3\n0\n0" }, "output": { "stdout": "2 2\n" } }, { "input": { "stdin": "3\n-4 1 -1\n2 3 3\n0\n1" }, "output": { ...
{ "tags": [], "title": "Optimal alpha beta pruning" }
{ "cpp": "#include<deque>\n#include<queue>\n#include<vector>\n#include<algorithm>\n#include<iostream>\n#include<set>\n#include<cmath>\n#include<tuple>\n#include<string>\n#include<chrono>\n#include<functional>\n#include<iterator>\n#include<random>\n#include<unordered_set>\n#include<array>\n#include<map>\n#include<ioma...
AIZU/p01875
# Complex Oracle D: Complex Oracle --Complex Oracle- problem * This problem is a reactive problem. In other words, it is necessary to create a program that derives the correct answer by interactively responding to the program prepared on the server side. Also, since the program on the server side also shares compute...
[ { "input": { "stdin": "" }, "output": { "stdout": "" } } ]
{ "tags": [], "title": "Complex Oracle" }
{ "cpp": "#include<iostream>\n#include<vector>\n#include<string>\n#include<algorithm>\t\n#include<map>\n#include<set>\n#include<utility>\n#include<cmath>\n#include<cstring>\n#include<queue>\n#include<cstdio>\n#include<sstream>\n#include<iomanip>\n#define loop(i,a,b) for(int i=a;i<b;i++) \n#define rep(i,a) loop(i,0,a)...
AIZU/p02011
# Enlarge Circles You are given $N$ distinct points on the 2-D plane. For each point, you are going to make a single circle whose center is located at the point. Your task is to maximize the sum of perimeters of these $N$ circles so that circles do not overlap each other. Here, "overlap" means that two circles have a ...
[ { "input": { "stdin": "9\n91 -18\n13 93\n73 -34\n15 2\n-46 0\n69 -42\n-23 -13\n-87 41\n38 68" }, "output": { "stdout": "1049.191683488" } }, { "input": { "stdin": "3\n0 0\n5 0\n0 5" }, "output": { "stdout": "53.630341225" } }, { "input": { "s...
{ "tags": [], "title": "Enlarge Circles" }
{ "cpp": "#include <cstdio>\n#include <cstdint>\n#include <cmath>\n#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <utility>\n#include <tuple>\n#include <limits>\n\ntemplate <typename Tp>\nclass linear_program {\n // optimize given linear program (in a standard form) with simplex algorithm\n\np...
AIZU/p02154
# Equilateral Triangle Problem Given a convex polygon consisting of $ N $ vertices. When considering an equilateral triangle that includes all the vertices of the convex polygon, find the minimum value of the length of one side of the equilateral triangle. Constraints The input satisfies the following conditions. ...
[ { "input": { "stdin": "4\n2 3\n1 2\n2 1\n3 2" }, "output": { "stdout": "3.04720672" } }, { "input": { "stdin": "7\n-96469 25422\n-55204 -45592\n-29140 -72981\n98837 -86795\n92303 63297\n19059 96012\n-67980 70342" }, "output": { "stdout": "310622.35426197" ...
{ "tags": [], "title": "Equilateral Triangle" }
{ "cpp": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<random>\n#include<map>\n#include<set>\n#include<complex>\n#include<bitset>\n#include<stack>\n#include<unordere...
AIZU/p02295
# Cross Point For given two segments s1 and s2, print the coordinate of the cross point of them. s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3. Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xpi, ypi ≤ 10000 * p0 ≠ p1 and p2 ≠ p3. * The given segments have a cross point and are not in parall...
[ { "input": { "stdin": "3\n0 0 2 0 1 1 1 -1\n0 0 1 1 0 1 1 0\n0 0 1 1 1 0 0 1" }, "output": { "stdout": "1.0000000000 0.0000000000\n0.5000000000 0.5000000000\n0.5000000000 0.5000000000" } }, { "input": { "stdin": "3\n-1 -1 2 0 2 2 1 -1\n0 0 2 1 0 1 0 0\n0 0 1 1 1 -1 0 2" ...
{ "tags": [], "title": "Cross Point" }
{ "cpp": "#include <iostream>\n#include <cstdio>\n#include <cmath>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\ntemplate<class T>\nstruct Vec2\n{\n\tVec2(){}\n\tVec2(T _x, T _y) : x(_x), y(_y) {}\n\n\tVec2 operator+(const Vec2& rhs) const { return Vec2(x + rhs.x, y + rhs.y); }\n\tVec2 operator-...
AIZU/p02442
# Lexicographical Comparison Compare given two sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and $B = \\{b_0, b_1, ..., b_{m-1}$ lexicographically. Constraints * $1 \leq n, m \leq 1,000$ * $0 \leq a_i, b_i \leq 1,000$ Input The input is given in the following format. $n$ $a_0 \; a_1, ..., \; a_{n-1}$ $m$ $b_0 \; b...
[ { "input": { "stdin": "3\n1 1 2\n4\n1 1 2 2" }, "output": { "stdout": "1" } }, { "input": { "stdin": "3\n1 2 3\n2\n2 4" }, "output": { "stdout": "1" } }, { "input": { "stdin": "4\n5 4 7 0\n5\n1 2 3 4 5" }, "output": { "stdout": ...
{ "tags": [], "title": "Lexicographical Comparison" }
{ "cpp": "#pragma GCC optimize (\"O3\")\n#include <iostream>\n#include <iomanip>\n#include <istream>\n#include <ostream>\n#include <sstream>\n#include <iterator>\n#include <vector>\n#include <algorithm>\n#include <queue>\n#include <deque>\n#include <list>\n#include <stack>\n#include <map>\n#include <unordered_map>\n#...