id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
Codeforces/99/B
# Help Chef Gerasim In a far away kingdom young pages help to set the table for the King. As they are terribly mischievous, one needs to keep an eye on the control whether they have set everything correctly. This time the royal chef Gerasim had the impression that the pages have played a prank again: they had poured t...
[ { "input": { "stdin": "5\n250\n250\n250\n250\n250\n" }, "output": { "stdout": "Exemplary pages.\n" } }, { "input": { "stdin": "5\n270\n250\n250\n230\n250\n" }, "output": { "stdout": "20 ml. from cup #4 to cup #1.\n" } }, { "input": { "stdin":...
{ "tags": [ "implementation", "sortings" ], "title": "Help Chef Gerasim" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[1024], f[10010];\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n int n, l = INT_MAX, r = INT_MIN, cnt = 0, src, dst;\n cin >> n;\n for (int i = 0; i < n; ++i) {\n cin >> a[i];\n ++f[a[i]];\n }\n for (int i = 0; i < 10001; +...
HackerEarth/benny-and-the-broken-odometer
One fine day, Benny decided to calculate the number of kilometers that she traveled by her bicycle. Therefore, she bought an odometer and installed it onto her bicycle. But the odometer was broken. It was not able to display the digit 3. This would precisely mean, that the odometer won't be able to display the numbers ...
[ { "input": { "stdin": "5\n5\n14\n76\n67\n40\n\nSAMPLE" }, "output": { "stdout": "4\n12\n59\n51\n27\n" } }, { "input": { "stdin": "100\n402\n409\n278\n29\n268\n874\n527\n617\n727\n485\n616\n724\n660\n190\n286\n562\n270\n21\n75\n625\n1\n122\n579\n89\n764\n679\n145\n847\n405\n...
{ "tags": [], "title": "benny-and-the-broken-odometer" }
{ "cpp": null, "java": null, "python": "def p(x,y):\n\tif y==0: return 1\n\treturn x*p(x,y-1)\n\nfor _ in range(int(input())):\n\tn = (raw_input().split())[0]\n\tans = 0\n\tj = 0\n\t#print n\n\tfor i in range(len(n)-1,-1,-1):\n\t\t#print i\n\t\t#print n[i]\n\t\tif int(n[i])<3: ans+=p(9,j)*int(n[i])\n\t\telse: ans...
HackerEarth/composition
We all know that every positive integer can be represented as the sum of one or more odd integers. For eg: 4 = 1 + 1 + 1 + 1. Now, your task in this question is to find out the value of G(n), which equals to the number of all possible representations, as described above, of the given integer, n. Since the answer coul...
[ { "input": { "stdin": "2\n1\n4\n\nSAMPLE" }, "output": { "stdout": "1\n3" } }, { "input": { "stdin": "1000\n25681017298294515\n312600462688402121\n833646696039112627\n223491184116692039\n162199167831855427\n534855812752103589\n300213762583795578\n991699364918096631\n4036923...
{ "tags": [], "title": "composition" }
{ "cpp": null, "java": null, "python": "fib_matrix = [[1,1],\n [1,0]]\n\ndef matrix_square(A, mod):\n return mat_mult(A,A,mod)\n\n\ndef mat_mult(A,B, mod):\n if mod is not None:\n return [[(A[0][0]*B[0][0] + A[0][1]*B[1][0])%mod, (A[0][0]*B[0][1] + A[0][1]*B[1][1])%mod],\n [(A[1][0]...
HackerEarth/fill-the-box
Vipul has N empty boxes, numbered from 1 to N, with infinite capacity. He performs M operations. Each operation is described by 3 integers, a, b, and k. Here, a and b are indices of the boxes, and k is the number of marbles to be added inside each box whose index lies between a and b (both inclusive). Can you tell the ...
[ { "input": { "stdin": "5 3\n1 2 100\n2 5 100\n3 4 100\n\nSAMPLE" }, "output": { "stdout": "160\n" } }, { "input": { "stdin": "4 3\n2 3 603\n1 1 286\n4 4 882" }, "output": { "stdout": "593\n" } }, { "input": { "stdin": "5 3\n-1 1 111\n0 2 010\...
{ "tags": [], "title": "fill-the-box" }
{ "cpp": null, "java": null, "python": "n,m = map(int,raw_input().split())\nL = [0]*n\nwhile(m>0):\n\tm-=1\n\tf,s,a = map(int,raw_input().split())\n\tfor i in range(f-1,s):\n\t\tL[i] = L[i] + a\nprint sum(L)/n" }
HackerEarth/ikshu-and-his-new-year-matrix
Ikshu and his prime matrix Ikshu is in love with prime numbers. He has a matrix of size N X N and wants atleast 5 prime numbers in that matrix arranged like a cross as shown in figure. Let us call this matrix "new year matrix" X  X   X X  X If matrix is not a "new year matrix" he can alter it with the operation a...
[ { "input": { "stdin": "3 1\n2 2 3\n4 5 6\n7 8 9\n\nSAMPLE" }, "output": { "stdout": "yes\n2\n2 2\n" } }, { "input": { "stdin": "142 3\n1639 47 1213 1989 256 4557 1411 765 4454 4006 2184 4267 399 3040 244 457 4028 3120 2447 2609 949 1324 1954 2453 4273 4587 1632 2181 1378 22...
{ "tags": [], "title": "ikshu-and-his-new-year-matrix" }
{ "cpp": null, "java": null, "python": "import math\n\ndef getPrime(Max):\n used = [0] * Max\n \n used[1] = 1\n for i in xrange( 4, Max, 2 ):\n used[i] = 1\n \n for i in xrange( 3, int(math.sqrt(Max) + 1), 2 ):\n if used[i] == 1:\n continue\n for j in xrange( i * ...
HackerEarth/marut-and-girls
Marut is now a well settled person. Impressed by the coding skills of Marut, N girls wish to marry him. Marut will consider marriage proposals of only those girls who have some special qualities. Qualities are represented by positive non-zero integers. Marut has a list of M qualities which he wants in a girl. He can a...
[ { "input": { "stdin": "5\n1 2 3 4 5\n3\n1 2 3 4 5 6\n1 2 3 4 5\n1 2 3 4SAMPLE" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "10\n1 2 3 4 5 6 7 8 9 10\n100\n259 243 208 421 218 444 265 342 468 192 124 143 48 290 56 79 147 66 467 439 149 431 90 133 241 383 116 20...
{ "tags": [], "title": "marut-and-girls" }
{ "cpp": null, "java": null, "python": "lmq = int(input())\nmarutqual = raw_input().split()\nnumgals = int(input())\nnumlg = 0\nfor i in xrange(numgals):\n qualgals = {i:1 for i in raw_input().split()}\n sumreq = 0 \n count = 0\n while(sumreq<lmq and count<lmq):\n try:\n if(qualga...
HackerEarth/number-theory
Quan_Lank is a great team with some uncommon interests in programming. Sometimes the team loves to solve strings puzzles, sometimes game puzzles and sometimes metrix type puzzles . Yesterday they have added a new interest to their list that is 'number theory' as they have solved some amazing puzzles related to number t...
[ { "input": { "stdin": "2\n1\n10\n\nSAMPLE" }, "output": { "stdout": "1\n2" } }, { "input": { "stdin": "2\n2\n10\n\nASESLM" }, "output": { "stdout": "1\n2\n" } }, { "input": { "stdin": "2\n8\n105\n\nMRL@SD" }, "output": { "stdout...
{ "tags": [], "title": "number-theory" }
{ "cpp": null, "java": null, "python": "t = input()\nwhile t > 0:\n\tn = input()\n\tif(n == 1):\n\t\tprint 1\n\t\tt = t-1\n\t\tcontinue\n\tans = 0\n\ti = 1\n\twhile i*i <= n:\n\t\tif n%i == 0:\n\t\t\tx = i\n\t\t\ty = n/i\n#\t\t\tprint str(x) + \" \" + str(y)\n\t\t\ttemp = n\n\t\t\tflag = 0\n\t\t\twhile x > 0:\n\t...
HackerEarth/raghu-vs-sayan
Raghu and Sayan both like to eat (a lot) but since they are also looking after their health, they can only eat a limited amount of calories per day. So when Kuldeep invites them to a party, both Raghu and Sayan decide to play a game. The game is simple, both Raghu and Sayan will eat the dishes served at the party till ...
[ { "input": { "stdin": "3\n15 20 3\n10 5 4\n3 10 2\n4 7\n10 8 3\n4 5 5\n\nSAMPLE" }, "output": { "stdout": "Sayan Won\nSayan Won\nRaghu Won\n" } }, { "input": { "stdin": "3\n15 20 3\n10 5 4\n3 10 2\n7 5\n10 8 3\n2 5 5\n\nSAMPLE" }, "output": { "stdout": "Sayan ...
{ "tags": [], "title": "raghu-vs-sayan" }
{ "cpp": null, "java": null, "python": "test=input()\nwhile(test>0):\n\ttest-=1\n\ta,b,n=[int(s) for s in raw_input().split()]\n\tcal=[int(s) for s in raw_input().split()]\n\tcal.sort()\n\tl=len(cal)\n\tr=0\n\ts=0\n\trc=0\n\tsc=0\n\twhile(r<l and rc+cal[r]<=a):\n\t\trc+=cal[r]\n\t\tr+=1\n\twhile(s<l and sc+cal[s]...
HackerEarth/shils-romantic-message
Shil is now finally in a relationship with Uttu. Both of them like to exchange love letters. However, to avoid exposing their relationship, they use "encryption" to send their messages. They use the famous Caesar cipher to encrypt their messages, which mathematically explained is as follows: Encryption of a letter x b...
[ { "input": { "stdin": "1\nphqghumeay\n\nSAMPLE" }, "output": { "stdout": "asbrsfxplj" } }, { "input": { "stdin": "100\nphqghumeaylnlfdxfircvscxggbwkfnqduxwfnfozvsrtkjprepggxrpnrvystmwcysyycqpevikeffmznimkkasvwsrenzkycxf\nxtlsgypsfadpooefxzbcoejuvpvaboygpoeylfpbnpljvrvipyamy...
{ "tags": [], "title": "shils-romantic-message" }
{ "cpp": null, "java": null, "python": "def find(s):\n\tn=26-(ord(s[0])-ord('a'))\n\tz=\"z\"\n\tout=\"\"\n\tfor i in s:\n\t\tif ord(i)+n <= ord(z):\n\t\t\tout+=chr(ord(i)+n)\n\t\telse:\n\t\t\tx=ord(z)-ord(i)\n\t\t\ty=n-x-1\n\t\t\tout+=chr(ord('a')+y)\n\tprint out\n\nif __name__ == '__main__':\n\tt=int(raw_input()...
HackerEarth/the-reversed-numbers
HackerMan has a message that he has coded in form of digits, which means that the message contains only numbers and nothing else. He is fearful that the enemy may get their hands on the secret message and may decode it. HackerMan already knows the message by heart and he can simply destroy it. But he wants to keep it ...
[ { "input": { "stdin": "3\n353 575\n238746 39857\n890 231\n\nSAMPLE" }, "output": { "stdout": "829\n527327\n32\n" } }, { "input": { "stdin": "994\n4789 1537\n2363 1131\n3948 2406\n4077 621\n4449 387\n406 3977\n2759 878\n1058 3651\n2356 1943\n3039 2573\n3138 2424\n1371 518\n4...
{ "tags": [], "title": "the-reversed-numbers" }
{ "cpp": null, "java": null, "python": "t = input(\"\")\nfor i in range(t):\n\ts = raw_input(\"\")\n\ta, b = s.split()\n\ta = a[::-1]\n\tb = b [::-1]\n\tw = int(a) + int(b)\n\tq = str(w)\n\tw = int(q[::-1])\n\tprint (w)" }
p02539 ACL Beginner Contest - Heights and Pairs
There are 2N people numbered 1 through 2N. The height of Person i is h_i. How many ways are there to make N pairs of people such that the following conditions are satisfied? Compute the answer modulo 998,244,353. * Each person is contained in exactly one pair. * For each pair, the heights of the two people in the pai...
[ { "input": { "stdin": "2\n1\n1\n2\n3" }, "output": { "stdout": "2" } }, { "input": { "stdin": "5\n30\n10\n20\n40\n20\n10\n10\n30\n50\n60" }, "output": { "stdout": "516" } }, { "input": { "stdin": "5\n126\n5\n16\n238\n37\n0\n0\n1\n34\n010" ...
{ "tags": [], "title": "p02539 ACL Beginner Contest - Heights and Pairs" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing uint = unsigned int;\n#define rep(i,n) for(int i=0;i<int(n);i++)\n#define rep1(i,n) for(int i=1;i<=int(n);i++)\n#define per(i,n) for(int i=int(n)-1;i>=0;i--)\n#define per1(i,n) for(int i=int(n);i>0;i--)\n#define all(c) c.begin(),c....
p02670 AtCoder Grand Contest 044 - Joker
Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to rig...
[ { "input": { "stdin": "4\n6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8" }, "output": { "stdout": "3" } }, { "input": { "stdin": "6\n11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30" }, "output": { "stdout": "11" ...
{ "tags": [], "title": "p02670 AtCoder Grand Contest 044 - Joker" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nint f[510][510],n,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1},res;\nbool g[510][510];\nvoid qwq(int x,int y){\n\tfor(int i=0;i<4;i++){\n\t\tint nx=x+dx[i],ny=y+dy[i];\n\t\tif(nx>n||nx<1||ny>n||ny<1)continue;\n\t\tif(f[nx][ny]<=f[x][y]+g[x][y])continue;\n\t\tf[nx][ny]=f[x...
p02799 Keyence Programming Contest 2020 - Bichromization
We have a connected undirected graph with N vertices and M edges. Edge i in this graph (1 \leq i \leq M) connects Vertex U_i and Vertex V_i bidirectionally. We are additionally given N integers D_1, D_2, ..., D_N. Determine whether the conditions below can be satisfied by assigning a color - white or black - to each v...
[ { "input": { "stdin": "5 5\n3 4 3 5 7\n1 2\n1 3\n3 2\n4 2\n4 5" }, "output": { "stdout": "BWWBB\n4\n3\n1\n5\n2" } }, { "input": { "stdin": "4 6\n1 1 1 1\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4" }, "output": { "stdout": "BBBW\n1\n1\n1\n2\n1\n1" } }, { "input"...
{ "tags": [], "title": "p02799 Keyence Programming Contest 2020 - Bichromization" }
{ "cpp": "#include <algorithm>\n#include <cstdio>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nconstexpr int kMaxC = 1000000000;\n\nstruct Edge\n{\n int u, v, c;\n};\n\nstruct Node\n{\n int i, d;\n char color;\n vector<Edge*> edges;\n};\n\nbool operator<(const Node& u, const Node& v)\n...
p02935 AtCoder Beginner Contest 138 - Alchemist
You have a pot and N ingredients. Each ingredient has a real number parameter called value, and the value of the i-th ingredient (1 \leq i \leq N) is v_i. When you put two ingredients in the pot, they will vanish and result in the formation of a new ingredient. The value of the new ingredient will be (x + y) / 2 where...
[ { "input": { "stdin": "2\n3 4" }, "output": { "stdout": "3.5" } }, { "input": { "stdin": "3\n500 300 200" }, "output": { "stdout": "375" } }, { "input": { "stdin": "5\n138 138 138 138 138" }, "output": { "stdout": "138" } ...
{ "tags": [], "title": "p02935 AtCoder Beginner Contest 138 - Alchemist" }
{ "cpp": "#include <bits/stdc++.h>\n#define ld long double\nusing namespace std;\nint n;\nld a[60];\nld ans;\nint main(){\n\tcin>>n;\n\tfor(int i=1;i<=n;i++)cin>>a[i];\n\tsort(a+1,a+n+1);\n\tans=a[1];\n\tfor(int i=2;i<=n;i++)ans=(a[i]+ans)/2.0;\n\tcout<<ans<<endl;\n\treturn 0;\n}\n", "java": "import java.util.Array...
p03072 AtCoder Beginner Contest 124 - Great Ocean View
There are N mountains ranging from east to west, and an ocean to the west. At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns. The height of the i-th mountain from the west is H_i. You can certainly see the ocean from the inn at the top of the westmost mountain. F...
[ { "input": { "stdin": "4\n6 5 6 8" }, "output": { "stdout": "3" } }, { "input": { "stdin": "5\n9 5 6 8 4" }, "output": { "stdout": "1" } }, { "input": { "stdin": "5\n4 5 3 5 4" }, "output": { "stdout": "3" } }, { "in...
{ "tags": [], "title": "p03072 AtCoder Beginner Contest 124 - Great Ocean View" }
{ "cpp": "#include <iostream>\nshort n, h, a, m;\nsigned main()\n{\n std::cin >> n;\n while (n--)\n {\n std::cin >> h;\n if (h >= m) a++, m = h;\n }\n std::cout << a;\n}\n", "java": "import java.util.Scanner;\n\npublic class Main {\n public static void main (String[] args) {\n Scann...
p03214 Dwango Programming Contest V - Thumbnail
Niwango-kun is an employee of Dwango Co., Ltd. One day, he is asked to generate a thumbnail from a video a user submitted. To generate a thumbnail, he needs to select a frame of the video according to the following procedure: * Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of t...
[ { "input": { "stdin": "3\n1 2 3" }, "output": { "stdout": "1" } }, { "input": { "stdin": "4\n2 5 2 5" }, "output": { "stdout": "0" } }, { "input": { "stdin": "4\n-4 0 3 8" }, "output": { "stdout": "2\n" } }, { "input...
{ "tags": [], "title": "p03214 Dwango Programming Contest V - Thumbnail" }
{ "cpp": "#include<cstdio>\n#include<functional>\n#include<algorithm>\nusing namespace std;\nint main(void)\n{\n\tint n,a[100],sum,b[100],mn,mi,x,i;\n\tscanf(\"%d\",&n);\n\tsum=0;\n\tfor(i=0;i<n;i++)\t{\n\t\tscanf(\"%d\",&a[i]);\n\t\tsum+=a[i];\n\t\tb[i]=a[i]*n;\n\t}\n\tmn=sum;\n\tfor(i=0;i<n;i++)\t{\n\t\tx=sum-b[i];...
p03363 AtCoder Grand Contest 023 - Zero-Sum Ranges
We have an integer sequence A, whose length is N. Find the number of the non-empty contiguous subsequences of A whose sums are 0. Note that we are counting the ways to take out subsequences. That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from differ...
[ { "input": { "stdin": "7\n1 -1 1 -1 1 -1 1" }, "output": { "stdout": "12" } }, { "input": { "stdin": "5\n1 -2 3 -4 5" }, "output": { "stdout": "0" } }, { "input": { "stdin": "6\n1 3 -4 2 2 -2" }, "output": { "stdout": "3" } ...
{ "tags": [], "title": "p03363 AtCoder Grand Contest 023 - Zero-Sum Ranges" }
{ "cpp": "#include \"bits/stdc++.h\"\nusing namespace std;\ntypedef long long LL;\nint A[200000];\nint main() {\n\tint N;\n\tcin >> N;\n\tfor (int i = 0; i < N; i++) cin >> A[i];\n\tmap<LL, int> cnt;\n\tLL sum = 0;\n\tLL ans = 0;\n\tfor (int i = 0; i < N; i++) {\n\t\tsum += A[i];\n\t\tif (sum == 0) ans++;\n\t\tans +=...
p03521 CODE FESTIVAL 2017 Exhibition (Parallel) - Awkward
ButCoder Inc. is a startup company whose main business is the development and operation of the programming competition site "ButCoder". There are N members of the company including the president, and each member except the president has exactly one direct boss. Each member has a unique ID number from 1 to N, and the m...
[ { "input": { "stdin": "4\n1\n2\n3" }, "output": { "stdout": "2" } }, { "input": { "stdin": "15\n1\n2\n3\n1\n4\n2\n7\n1\n8\n2\n8\n1\n8\n2" }, "output": { "stdout": "97193524" } }, { "input": { "stdin": "3\n1\n2" }, "output": { "s...
{ "tags": [], "title": "p03521 CODE FESTIVAL 2017 Exhibition (Parallel) - Awkward" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define pb push_back\n#define mp make_pair\n#define x first\n#define y second\n\ntypedef long long ll;\ntypedef pair<int,int> pii;\ntypedef vector<int> vi;\ntypedef vector<pii> vpii;\n\nconst int N=2010;\nconst int mod=1000000007;\nconst int inv2=(mod+1)/2;\n\...
p03686 AtCoder Regular Contest 076 - Exhausted?
There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i ≤ M) is i. N people of the Takahashi clan played too much games, and they are all suffering from backaches. They need to sit in chairs and rest, but they are particular about which chairs they sit in. Specifically, the i-th person wishes to...
[ { "input": { "stdin": "6 6\n1 6\n1 6\n1 5\n1 5\n2 6\n2 6" }, "output": { "stdout": "2" } }, { "input": { "stdin": "4 4\n0 3\n2 3\n1 3\n3 4" }, "output": { "stdout": "0" } }, { "input": { "stdin": "7 6\n0 7\n1 5\n3 6\n2 7\n1 6\n2 6\n3 7" }...
{ "tags": [], "title": "p03686 AtCoder Regular Contest 076 - Exhausted?" }
{ "cpp": "#include<cstdio>\n#include<algorithm>\n#include<queue>\nusing namespace std;\nconst int N=200005;\nstruct node { int l,r; } v[N];\nint s[N],i,j,k,n,m,z,an,R;\nchar c;\n\ninline char getc()\n{\n\t#define VV 10000000\n\tstatic char s[VV],*l=s,*r=s;\n\tif (l==r)\n\t\tl=s,r=s+fread(s,1,VV,stdin);\n\treturn l==r...
p03839 AtCoder Grand Contest 008 - Contiguous Repainting
There are N squares aligned in a row. The i-th square from the left contains an integer a_i. Initially, all the squares are white. Snuke will perform the following operation some number of times: * Select K consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares...
[ { "input": { "stdin": "10 5\n5 -4 -5 -8 -4 7 2 -4 0 7" }, "output": { "stdout": "17" } }, { "input": { "stdin": "1 1\n-10" }, "output": { "stdout": "0" } }, { "input": { "stdin": "5 3\n-10 10 -10 10 -10" }, "output": { "stdout":...
{ "tags": [], "title": "p03839 AtCoder Grand Contest 008 - Contiguous Repainting" }
{ "cpp": "#include<iostream>\n#include<cstdio>\n#include<cstring>\n#include<cstdlib>\n#include<algorithm>\n#include<cmath>\n#define ll long long\nusing namespace std;\nint n,k;\nll ans=0,kk;\nll a[100010],sa[100010],sb[100010];\nint main(){\n\tscanf(\"%d%d\",&n,&k);\n\tint i,j;\n\tsa[0]=0;sb[0]=0;\n\tfor(i=1;i<=n;++i...
p04006 AtCoder Grand Contest 004 - Colorful Slimes
Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together. Snuke can perform the following two actions: * Select a color i ...
[ { "input": { "stdin": "4 10\n1 2 3 4" }, "output": { "stdout": "10" } }, { "input": { "stdin": "2 10\n1 100" }, "output": { "stdout": "12" } }, { "input": { "stdin": "3 10\n100 1 100" }, "output": { "stdout": "23" } }, {...
{ "tags": [], "title": "p04006 AtCoder Grand Contest 004 - Colorful Slimes" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[2010],mn[2010];\nint main(){\n\tint i,j,n,x;\n\tlong long s,ans=1e18;\n\tscanf(\"%d%d\",&n,&x);\n\tfor(i=0;i<n;++i)scanf(\"%d\",a+i),mn[i]=a[i];\n\tfor(i=0;i<n;++i){\n\t\tfor(j=s=0;j<n;++j)s+=mn[j];\n\t\tans=min(ans,s+1ll*x*i);\n\t\tfor(j=0;j<n;++j)mn[j]...
AIZU/p00092
# Square Searching There are a total of n x n squares, n rows vertically and n columns horizontally. Some squares are marked. Create a program that reads the marked state of each square and displays the length of the side of the largest square consisting of only the unmarked squares as an output. For example, each da...
[ { "input": { "stdin": "10\n...*....**\n..........\n**....**..\n........*.\n..*.......\n..........\n.*........\n..........\n....*..***\n.*....*...\n10\n****.*****\n*..*.*....\n****.*....\n*....*....\n*....*****\n..........\n****.*****\n*..*...*..\n****...*..\n*..*...*..\n0" }, "output": { "st...
{ "tags": [], "title": "Square Searching" }
{ "cpp": "#include<iostream>\n#include<string>\n\n#define MAX 1002\nusing namespace std;\n\nint main(void){\n\twhile( true ){\n\t\tint n;\n\t\tint maxSize = 0;\n\t\tint line[MAX];\n\t\tint sqsz[2][MAX] = {{0,},};\n\n\t\tcin >> n;\n\t\t\n\t\tif( n == 0 ) break;\n\n\t\tfor(int i = 0; i < n; i++){\n\t\t\tstring s;\n\t\t...
AIZU/p00224
# Bicycle Diet Mr. A loves sweets, but recently his wife has told him to go on a diet. One day, when Mr. A went out from his home to the city hall, his wife recommended that he go by bicycle. There, Mr. A reluctantly went out on a bicycle, but Mr. A, who likes sweets, came up with the idea of ​​stopping by a cake shop...
[ { "input": { "stdin": "1 1 2 5\n35\nH L1 5\nC1 D 6\nC1 H 12\nL1 D 10\nC1 L1 20\n2 1 4 6\n100 70\nH L1 5\nC1 L1 12\nC1 D 11\nC2 L1 7\nC2 D 15\nL1 D 8\n0 0 0 0" }, "output": { "stdout": "1\n-2" } }, { "input": { "stdin": "1 1 2 5\n29\nH L1 5\nC1 D 6\nC1 H 12\nL1 D 10\nC1 L1 2...
{ "tags": [], "title": "Bicycle Diet" }
{ "cpp": "#include<iostream>\n#include<queue>\n#include<sstream>\n\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < int(n); ++i)\n\nstruct Edge{int to, d;};\nstruct Status{\n int cal, pos, bit;\n bool operator<(const Status &s) const {return cal > s.cal;}\n};\n\ntemplate<class A, class B> B convert(A ...
AIZU/p00386
# Gathering You are a teacher at Iazu High School is the Zuia Kingdom. There are $N$ cities and $N-1$ roads connecting them that allow you to move from one city to another by way of more than one road. Each of the roads allows bidirectional traffic and has a known length. As a part of class activities, you are planni...
[ { "input": { "stdin": "15 15\n1 2 45\n2 3 81\n1 4 29\n1 5 2\n5 6 25\n4 7 84\n7 8 56\n4 9 2\n4 10 37\n7 11 39\n1 12 11\n11 13 6\n3 14 68\n2 15 16\n10 13 14\n13 14 15\n2 14 15\n7 12 15\n10 14 15\n9 10 15\n9 14 15\n8 13 15\n5 6 13\n11 13 15\n12 13 14\n2 3 10\n5 13 15\n10 11 14\n6 8 11" }, "output": {...
{ "tags": [], "title": "Gathering" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nstruct edge{ int to,cost; };\nint N,Q;\n\nvector<edge> G[100005];\n\nint parent[100005];\nint depth[100005];\nint dist[100005];\n\nint p[30][100005];\nvoid dfs(int pos,int prev,int Depth,int Dist){\n parent[pos]=prev;\n depth[pos]=Depth;\n dist[pos]=Dist...
AIZU/p00602
# Fibonacci Sets Fibonacci number f(i) appear in a variety of puzzles in nature and math, including packing problems, family trees or Pythagorean triangles. They obey the rule f(i) = f(i - 1) + f(i - 2), where we set f(0) = 1 = f(-1). Let V and d be two certain positive integers and be N ≡ 1001 a constant. Consider a...
[ { "input": { "stdin": "5 5\n50 1\n13 13" }, "output": { "stdout": "2\n50\n8" } }, { "input": { "stdin": "11 7\n66 1\n22 20" }, "output": { "stdout": "7\n64\n13\n" } }, { "input": { "stdin": "5 7\n118 2\n22 23" }, "output": { "st...
{ "tags": [], "title": "Fibonacci Sets" }
{ "cpp": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <cmath>\n#include <cassert>\nusing namespace std;\n\nconst int N = 1001;\nint V, d;\n\nunsigned F[1002];\n\nint main() {\n\n while( cin >> V >> d ) {\n \n F[0] = 2;\n F[1] = 3;\n for(int i=2; i<1001; i++) {\n F[i] = (F[...
AIZU/p00738
# Roll-A-Big-Ball ACM University holds its sports day in every July. The "Roll-A-Big-Ball" is the highlight of the day. In the game, players roll a ball on a straight course drawn on the ground. There are rectangular parallelepiped blocks on the ground as obstacles, which are fixed on the ground. During the game, the ...
[ { "input": { "stdin": "2\n-40 -40 100 30\n-100 -100 -50 -30 1\n30 -70 90 -30 10\n2\n-4 -4 10 3\n-10 -10 -5 -3 1\n3 -7 9 -3 1\n2\n-40 -40 100 30\n-100 -100 -50 -30 3\n30 -70 90 -30 10\n2\n-400 -400 1000 300\n-800 -800 -500 -300 7\n300 -700 900 -300 20\n3\n20 70 150 70\n0 0 50 50 4\n40 100 60 120 8\n130 80 ...
{ "tags": [], "title": "Roll-A-Big-Ball" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\ntypedef pair<int,P> P1;\ntypedef pair<P,P> P2;\n#define pu push\n#define pb push_back\n#define mp make_pair\n#define eps 1e-7\n#define INF 1000000000\n#define mod 1000000007\n#define fi first\n#define sc second\...
AIZU/p00878
# Origami Through-Hole Origami is the traditional Japanese art of paper folding. One day, Professor Egami found the message board decorated with some pieces of origami works pinned on it, and became interested in the pinholes on the origami paper. Your mission is to simulate paper folding and pin punching on the folde...
[ { "input": { "stdin": "2\n90 90 80 20\n80 20 75 50\n50 35\n2\n90 90 80 20\n75 50 80 20\n55 20\n3\n5 90 15 70\n95 90 85 75\n20 67 20 73\n20 75\n3\n5 90 15 70\n5 10 15 55\n20 67 20 73\n75 80\n8\n1 48 1 50\n10 73 10 75\n31 87 31 89\n91 94 91 96\n63 97 62 96\n63 80 61 82\n39 97 41 95\n62 89 62 90\n41 93\n5\n2...
{ "tags": [], "title": "Origami Through-Hole" }
{ "cpp": "#include <stdio.h>\n#include <assert.h>\n#include <iostream>\n#include <complex>\n#include <vector>\n#include <queue>\n#include <map>\n#include <algorithm>\nusing namespace std;\n#define rep(i, n) for(int i=0; i<(int)(n); i++)\n#define mp make_pair\ntypedef complex<double> P;\ntypedef vector<P> convex;\n#de...
AIZU/p01009
# Room of Time and Spirit Problem In 20XX, a scientist developed a powerful android with biotechnology. This android is extremely powerful because it is made by a computer by combining the cells of combat masters. At this rate, the earth would be dominated by androids, so the N warriors decided to fight the androids...
[ { "input": { "stdin": "4 3\nIN 1 4 10\nIN 2 3 20\nCOMPARE 1 2" }, "output": { "stdout": "WARNING" } }, { "input": { "stdin": "10 4\nIN 10 8 2328\nIN 8 4 3765\nIN 3 8 574\nCOMPARE 4 8" }, "output": { "stdout": "-3191" } }, { "input": { "stdin"...
{ "tags": [], "title": "Room of Time and Spirit" }
{ "cpp": "#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <string>\n#include <sstream>\n#include <cstring>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <queue>\n#include <stack>\n#include <map>\n#include <set>\n#include <numeric>\n#include <cctype>\n#include <tuple>\n#incl...
AIZU/p01141
# Lifeguard in the Pool Lifeguard in the Pool Pool guard English text is not available in this practice contest. Horton Moore works as a pool watchman. As he walked around the edge of the pool to look around, he noticed a girl drowning in the pool. Of course, he must go to rescue immediately. Moreover, it is diffic...
[ { "input": { "stdin": "4\n0 0 10 0 10 10 0 10\n10\n12\n0 5\n9 5\n4\n0 0 10 0 10 10 0 10\n10\n12\n0 0\n9 1\n4\n0 0 10 0 10 10 0 10\n10\n12\n0 1\n9 1\n8\n2 0 4 0 6 2 6 4 4 6 2 6 0 4 0 2\n10\n12\n3 0\n3 5\n0" }, "output": { "stdout": "108.0\n96.63324958071081\n103.2664991614216\n60.0" } }...
{ "tags": [], "title": "Lifeguard in the Pool" }
{ "cpp": "#include<stdio.h>\n#include<algorithm>\n#include<math.h>\n#include<vector>\n#include<queue>\nusing namespace std;\nconst double EPS = 1e-10;\nconst double INF = 1e+10;\nconst double PI = acos(-1);\nint sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }\ninline double ABS(double a){return max(a,...
AIZU/p01280
# Galaxy Wide Web Service The volume of access to a web service varies from time to time in a day. Also, the hours with the highest volume of access varies from service to service. For example, a service popular in the United States may receive more access in the daytime in the United States, while another service pop...
[ { "input": { "stdin": "2\n4 0 1 2 3 4\n2 0 2 1\n0" }, "output": { "stdout": "5" } }, { "input": { "stdin": "2\n4 0 -2 -1 0 8\n2 1 3 3\n0" }, "output": { "stdout": "11\n" } }, { "input": { "stdin": "2\n4 0 1 2 3 4\n2 -1 2 1\n0" }, "out...
{ "tags": [], "title": "Galaxy Wide Web Service" }
{ "cpp": "#include <stdio.h>\n#include <string.h>\n#include <algorithm>\n#include <iostream>\n#include <math.h>\n#include <assert.h>\n#include <vector>\n\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned int uint;\ntypedef unsigned long long ull;\nstatic const double EPS = 1e-9;\nstatic const double PI =...
AIZU/p01450
# My friends are small I have a lot of friends. Every friend is very small. I often go out with my friends. Put some friends in your backpack and go out together. Every morning I decide which friends to go out with that day. Put friends one by one in an empty backpack. I'm not very strong. Therefore, there is a limit ...
[ { "input": { "stdin": "6 37\n5\n9\n13\n18\n26\n33" }, "output": { "stdout": "6" } }, { "input": { "stdin": "4 25\n20\n15\n20\n15" }, "output": { "stdout": "4" } }, { "input": { "stdin": "4 8\n1\n2\n7\n9" }, "output": { "stdout":...
{ "tags": [], "title": "My friends are small" }
{ "cpp": "#include <iostream>\n#include <cstdio>\n#include <cstdlib>\n#include <cmath>\n#include <algorithm>\n#include <functional>\n#include <string>\n#include <vector>\n#include <list>\n#include <set>\n#include <map>\n\nusing namespace std;\n\n#define FOR(i,a,b) for(int i=(a);i<(b);++i)\n#define REP(i,n) FOR(i,0,n...
AIZU/p01600
# Tree Construction Problem J: Tree Construction Consider a two-dimensional space with a set of points (xi, yi) that satisfy xi < xj and yi > yj for all i < j. We want to have them all connected by a directed tree whose edges go toward either right (x positive) or upward (y positive). The figure below shows an exampl...
[ { "input": { "stdin": "5\n1 5\n2 4\n3 3\n4 2\n5 1" }, "output": { "stdout": "12" } }, { "input": { "stdin": "1\n10000 0" }, "output": { "stdout": "0" } }, { "input": { "stdin": "1\n11110 2" }, "output": { "stdout": "0\n" } ...
{ "tags": [], "title": "Tree Construction" }
{ "cpp": "#include <cstdio>\n#include <cmath>\n#include <cstring>\n#include <cstdlib>\n#include <climits>\n#include <ctime>\n#include <queue>\n#include <stack>\n#include <algorithm>\n#include <list>\n#include <vector>\n#include <set>\n#include <map>\n#include <iostream>\n#include <deque>\n#include <complex>\n#include...
AIZU/p01756
# Longest Match Given the string S and m queries. The i-th query is given by the two strings xi and yi. For each query, answer the longest substring of the string S, starting with xi and ending with yi. For the string S, | S | represents the length of S. Also, the fact that the character string T is a substring of t...
[ { "input": { "stdin": "howistheprogress\n4\nist prog\ns ss\nhow is\nthe progress" }, "output": { "stdout": "9\n12\n5\n11" } }, { "input": { "stdin": "abracadabra\n5\nab a\na a\nb c\nac ca\nz z" }, "output": { "stdout": "11\n11\n4\n3\n0" } }, { "inp...
{ "tags": [], "title": "Longest Match" }
{ "cpp": "#include<iostream>\nusing namespace std;\n//construct SA by SA-IS O(N)\n#include<string>\n#include<vector>\nstruct SA{\n\tstring s;\n\tvector<int>sa;\n\tSA(const string&s_):s(s_)\n\t{\n\t\tsa=build(vector<int>(s.begin(),s.end()),256);\n\t}\n\tvector<int>induced_sort(const vector<int>&S,const vector<int>&id,...
AIZU/p01896
# Folding Paper Problem statement There is a rectangular piece of paper divided in a grid with a height of $ H $ squares and a width of $ W $ squares. The integer $ i \ times W + j $ is written in the cell of the $ i $ line from the top and the $ j $ column from the left counting from $ 0 $. An example of $ H = 2 and...
[ { "input": { "stdin": "1 4\n0 1 2 3" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "0 2\n-1 1 2 10" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "0 6\n-1 -1 4 0" }, "output": { "stdout": "YES\n" } ...
{ "tags": [], "title": "Folding Paper" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\ntypedef pair<int,int> P;\n\nint H,W;\nint t[250000];\nint u[250001];\nbool rh[250001];\nbool rw[250001];\n\nint dy[]={-1,0,1,0};\nint dx[]={0,1,0,-1};\n\nbool solve(){\n int sy=t[0]/W;\n int sx=t[0]%W;\n for(int i=1;i<H*W;i++){\n int y=t[i]/W;\n int x=...
AIZU/p02033
# Arrow D: Arrow / Arrow problem rodea is in a one-dimensional coordinate system and stands at x = 0. From this position, throw an arrow of positive integer length that always moves at speed 1 towards the target at x = N. However, rodea is powerless, so we have decided to put a total of M blowers in the section 0 \ ...
[ { "input": { "stdin": "5 1\n2\n1\n3" }, "output": { "stdout": "2" } }, { "input": { "stdin": "2 1\n0\n1\n1" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5 1\n3\n1\n0" }, "output": { "stdout": "-1\n" } }, { ...
{ "tags": [], "title": "Arrow" }
{ "cpp": "#include <bits/stdc++.h>\n#define trace1(x) cout<<#x<<\": \"<<x<<endl\n#define trace2(x, y) cout<<#x<<\": \"<<x<<\" | \"<<#y<<\": \"<<y<<endl\n#define trace3(x, y, z) cout<<#x<<\":\" <<x<<\" | \"<<#y<<\": \"<<y<<\" | \"<<#z<<\": \"<<z<<endl\n#define trace4(a, b, c, d) ...
AIZU/p02176
# Shortest Crypt problem Cryptography is all the rage at xryuseix's school. Xryuseix, who lives in a grid of cities, has come up with a new cryptography to decide where to meet. The ciphertext consists of the $ N $ character string $ S $, and the $ S_i $ character determines the direction of movement from the curren...
[ { "input": { "stdin": "5\nANazA" }, "output": { "stdout": "1\nA" } }, { "input": { "stdin": "5\nABbNx" }, "output": { "stdout": "1\nA\n" } }, { "input": { "stdin": "5\nbACOx" }, "output": { "stdout": "1\nA\n" } }, { ...
{ "tags": [], "title": "Shortest Crypt" }
{ "cpp": "#include <bits/stdc++.h>\n#define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME\n#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr8,pr7,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl\n#define pr1(a) (#a)<<\"=\"<<(a)<<\" \"\n#define pr2(a,b) pr1(a)<<pr1(b)\n#define pr3(a,b,c) pr1(a)<<pr2(b,c)\n#define pr4(a,b...
AIZU/p02319
# 0-1 Knapsack Problem II You have N items that you want to put them into a knapsack. Item i has value vi and weight wi. You want to find a subset of items to put such that: * The total value of the items is as large as possible. * The items have combined weight at most W, that is capacity of the knapsack. Find t...
[ { "input": { "stdin": "4 5\n4 2\n5 2\n2 1\n8 3" }, "output": { "stdout": "13" } }, { "input": { "stdin": "2 20\n5 9\n4 10" }, "output": { "stdout": "9" } }, { "input": { "stdin": "4 5\n0 1\n5 2\n1 1\n8 3" }, "output": { "stdout"...
{ "tags": [], "title": "0-1 Knapsack Problem II" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define int long long\n#define pb push_back\n#define fi first\n#define se second\n#define rep(i,s,n) for(int i = s;i<n;i++)\n#define rrep(i,s,n) for(int i = (n)-1;i>=(s);i--)\n#define all(v) (v).begin(),(v).end()\n#define chmin(a,b) a=min((a),(b))\n#define chm...
AIZU/p02464
# Set Intersection Find the intersection of two sets $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and $B = \\{b_0, b_1, ..., b_{m-1}\\}$. Constraints * $1 \leq n, m \leq 200,000$ * $0 \leq a_0 < a_1 < ... < a_{n-1} \leq 10^9$ * $0 \leq b_0 < b_1 < ... < b_{m-1} \leq 10^9$ Input The input is given in the following format. ...
[ { "input": { "stdin": "4\n1 2 5 8\n5\n2 3 5 9 11" }, "output": { "stdout": "2\n5" } }, { "input": { "stdin": "4\n1 0 5 10\n9\n2 2 5 9 11" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "4\n2 3 1 1\n7\n2 3 5 9 11" }, "output...
{ "tags": [], "title": "Set Intersection" }
{ "cpp": "#define _CRT_SECURE_NO_WARNINGS\n#include<bits/stdc++.h>\n#define INF 1e9\n#define EPS 1e-9\n#define REP(i,n) for(lint i=0,i##_len=(n);i<i##_len;++i)\n#define REP1(i,n) for(lint i=1,i##_len=(n);i<=i##_len;++i)\n#define REPR(i,n) for(lint i=(n)-1;i>=0;--i)\n#define REPR1(i,n) for(lint...
CodeChef/a3
Alice and Johnny are playing a simple guessing game. Johnny picks an arbitrary positive integer n (1 ≤ n ≤ 10^9) and gives Alice exactly k hints about the value of n. It is Alice's task to guess n, based on the received hints. Alice often has a serious problem guessing the value of n, and she's beginning to suspect t...
[ { "input": { "stdin": "3\n2\n< 100 No\n> 100 No\n3\n< 2 Yes\n> 4 Yes\n= 3 No\n6\n< 2 Yes\n> 1 Yes\n= 1 Yes\n= 1 Yes\n> 1 Yes\n= 1 Yes" }, "output": { "stdout": "0\n1\n2" } }, { "input": { "stdin": "3\n2\n< 100 No\n> 101 No\n3\n< 2 Yes\n> 4 Yes\n= 3 oN\n6\n< 2 Yes\n> 1 Yes\n...
{ "tags": [], "title": "a3" }
{ "cpp": null, "java": null, "python": "import sys\nfrom collections import Counter\n \nt = sys.stdin.readline()\nm = 1\nM = 1000000000\nfor _ in range(int(t)):\n\tk = sys.stdin.readline()\n\tk = int(k)\n\tassert(1 <= k <= 100000)\n\tc = Counter()\n\tfor _ in range(k):\n\t\tline = sys.stdin.readline()\n\t\to, l, ...
CodeChef/chefbm
Spring is interesting season of year. Chef is thinking about different things, but last time he thinks about interesting game - "Strange Matrix". Chef has a matrix that consists of n rows, each contains m elements. Initially, the element aij of matrix equals j. (1 ≤ i ≤ n, 1 ≤ j ≤ m). Then p times some element aij is...
[ { "input": { "stdin": "4 4 6\n2 2\n3 2 \n3 2 \n4 3\n4 4\n4 3" }, "output": { "stdout": "3\n3\n-1\n4" } }, { "input": { "stdin": "1 6 1\n0 2\n-2 7 \n3 2 \n4 3\n6 3\n7 2" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "4 5 6\n2 2\n3 ...
{ "tags": [], "title": "chefbm" }
{ "cpp": null, "java": null, "python": "n, m, p = map(int, raw_input().split())\narr = [dict() for _ in xrange(n)]\nfor _ in xrange(p):\n i,j = map(int,raw_input().split())\n i -= 1\n j -= 1\n if j not in arr[i]:\n arr[i][j] = j+1\n else:\n arr[i][j] += 1\n \ndef chefbm(arr,i):\n f...
CodeChef/digjump
Chef loves games! But he likes to invent his own. Now he plays game "Digit Jump". Chef has sequence of digits S1, S2,..., SN,. He is staying in the first digit (S1) and want to reach the last digit (SN) in the minimal number of jumps. While staying in some digit x with index i (digit Si) Chef can jump into digits with...
[ { "input": { "stdin": "01234567890" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "012134444444443" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "142160349875" }, "output": { "stdout": "6\n" } }, ...
{ "tags": [], "title": "digjump" }
{ "cpp": null, "java": null, "python": "arr=raw_input().strip()\nl=arr.__len__()\nnum=[]\nh={}\nfor i in range(0,l):\n\tnum.append(int(arr[i]))\nfor i in range(10):\n\th[i]=[]\nfor i in xrange(l):\n\th[num[i]].append(i)\n\n#print h\nvisited=[]\na={0:0}\nq=[0]\n\ndef bfs():\n\t\n\ti=q.pop(0)\n\tif i==l-1:\n\t\tret...
CodeChef/insoma4
In the hidden country of Lapatrecta, an age old custom was followed to ensure that no outsider ever entered their country undetected. The security measure, though simple, was an effective one. Every person born in Lapatrecta had the initials of all his forefathers included in front of his name. Every once in a while, c...
[ { "input": { "stdin": "3\nAPJQ\nAPJRS\nPJQ" }, "output": { "stdout": "3" } } ]
{ "tags": [], "title": "insoma4" }
{ "cpp": null, "java": null, "python": "N = int (raw_input())\n\nwords = []\nmaxDep = 0\n\ndef dfs(dic, depth):\n global maxDep\n if dic['__a'] > 1:\n if depth > maxDep:\n maxDep = depth\n for key in dic.keys():\n if key!='__a':\n if dic[key]['__a'] > 1:\n ...
CodeChef/nf02
NITMAS Fest is live. The members of the community are very busy. People from different colleges have come to compete. But, Naveen is very possessive about his girlfriend, Archana. He always remains close to his girlfriend. Couples (boys in a line and girls in another) have to stand in different rows in a line for a gam...
[ { "input": { "stdin": "4 2 4 6 8\n5 1 3 5 7 9" }, "output": { "stdout": "1" } } ]
{ "tags": [], "title": "nf02" }
{ "cpp": null, "java": null, "python": "a=list(map(int,raw_input().split()))\nb=list(map(int,raw_input().split()))\narr=[]\nfor i in range(1,len(a)):\n for j in range(1,len(b)):\n arr.append(abs(a[i]-b[j]))\narr=sorted(arr)\nprint arr[0]" }
CodeChef/salg04
p { font-size:14px; text-align:justify; } Two positive integers n amd m, n greater than or equal to m, will be given as input. Make a code that generates all possible combinations of 1,2,3,...,n taking m integers at a time in "increasing order". Comparison between two combinations: a1,a2,a3,...,am b1,b2,b3,...,bm ...
[ { "input": { "stdin": "5\n3" }, "output": { "stdout": "1 2 3 \n1 2 4\n1 2 5\n1 3 4\n1 3 5\n1 4 5\n2 3 4\n2 3 5\n2 4 5\n3 4 5" } } ]
{ "tags": [], "title": "salg04" }
{ "cpp": null, "java": null, "python": "import itertools\n\nn = int(raw_input())\nm = int(raw_input())\nls = range(1, n + 1)\nop = list(itertools.combinations(ls, m))\nfor t in op:\n\tfor x in t:\n\t\tprint x,\n\tprint" }
Codeforces/1000/A
# Codehorses T-shirts Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners. The valid sizes of T-shirts are either "M" or from 0 to 3 "X" followed by "S" or "L". For example, sizes "M", "XXS", "L", "XXXL" are valid and "XM", "Z",...
[ { "input": { "stdin": "2\nM\nXS\nXS\nM\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "3\nXS\nXS\nM\nXL\nS\nXS\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "2\nXXXL\nXXL\nXXL\nXXXS\n" }, "output": { ...
{ "tags": [ "greedy", "implementation" ], "title": "Codehorses T-shirts" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long n, i, j, k, l, sum = 0, flag = 0, t, a[1000000], ans = 0;\nmap<long long, long long> m;\nmap<string, long long> mm;\nstring s;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n cin >> n;\n for (int i = 0; i < n; i+...
Codeforces/1025/B
# Weakened Common Divisor During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mathematician, introduced a brand new concept of the weakened common divisor (WCD) of a list of pairs of integers. For a given list of pairs of integers (a_1, b_1), (a_2, b_2), ..., (a...
[ { "input": { "stdin": "2\n10 16\n7 17\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "3\n17 18\n15 24\n12 15\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5\n90 108\n45 105\n75 40\n165 175\n33 30\n" }, "...
{ "tags": [ "brute force", "greedy", "number theory" ], "title": "Weakened Common Divisor" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 150005;\nstruct node {\n long long x;\n long long y;\n} a[N];\nlong long prime[N], p1[N], p2[N];\nlong long n, m1, m2;\nvoid divide1(int n) {\n m1 = 0;\n for (int i = 2; i <= sqrt(n); i++) {\n if (n % i == 0) {\n p1[++m1] = i;\n ...
Codeforces/1045/D
# Interstellar battle In the intergalactic empire Bubbledom there are N planets, of which some pairs are directly connected by two-way wormholes. There are N-1 wormholes. The wormholes are of extreme religious importance in Bubbledom, a set of planets in Bubbledom consider themselves one intergalactic kingdom if and o...
[ { "input": { "stdin": "5\n0.50 0.29 0.49 0.95 0.83\n2 3\n0 3\n3 4\n2 1\n3\n4 0.66\n1 0.69\n0 0.36\n" }, "output": { "stdout": "1.68040\n1.48440\n1.61740\n" } }, { "input": { "stdin": "1\n0.28\n30\n0 0.72\n0 0.55\n0 0.80\n0 0.26\n0 0.51\n0 0.28\n0 0.24\n0 0.59\n0 0.72\n0 0.3...
{ "tags": [ "math", "probabilities", "trees" ], "title": "Interstellar battle" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 205000;\nstruct edge {\n int x, y;\n} e[maxn * 2];\ndouble a[maxn], f[maxn];\nint n, q;\nint fa[maxn];\ndouble ans;\nvector<int> g[maxn];\nvoid dfs(int u) {\n for (auto v : g[u]) {\n if (v == fa[u]) continue;\n fa[v] = u;\n f[u] += ...
Codeforces/1068/D
# Array Without Local Maximums Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers from 1 to 200. Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally: a_{1} ≤ a_{2}, a_{n} ≤ a_...
[ { "input": { "stdin": "3\n1 -1 2\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "2\n-1 -1\n" }, "output": { "stdout": "200\n" } }, { "input": { "stdin": "8\n-1 -1 -1 59 -1 -1 -1 -1\n" }, "output": { "stdout": "6584...
{ "tags": [ "dp" ], "title": "Array Without Local Maximums " }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long int;\nconst ll INF = 1e15 + 373;\ntemplate <typename T>\nusing vector2 = vector<vector<T>>;\ntemplate <typename T>\nvector2<T> init_vector2(size_t n0, size_t n1, T e = T()) {\n return vector2<T>(n0, vector<T>(n1, e));\n}\ntemplate <typena...
Codeforces/1090/C
# New Year Presents Santa has prepared boxes with presents for n kids, one box for each kid. There are m kinds of presents: balloons, sweets, chocolate bars, toy cars... A child would be disappointed to receive two presents of the same kind, so all kinds of presents in one box are distinct. Having packed all the pres...
[ { "input": { "stdin": "3 5\n5 1 2 3 4 5\n2 1 2\n2 3 4\n" }, "output": { "stdout": "2\n1 2 3\n1 3 1\n" } }, { "input": { "stdin": "1 1\n0\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "1 1\n1 1\n" }, "output": { "s...
{ "tags": [ "constructive algorithms", "data structures" ], "title": "New Year Presents" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int UB = 500000;\nvector<tuple<int, int, int>> res;\nvoid func(int n, int m, vector<set<int>> &kss, int lb, int ub) {\n vector<int> cnt(n);\n for (int i = 0; i < n; i++) cnt[i] = kss[i].size();\n vector<vector<int>> pats(m);\n for (int i = 0; i < n; ...
Codeforces/1109/F
# Sasha and Algorithm of Silence's Sounds One fine day Sasha went to the park for a walk. In the park, he saw that his favorite bench is occupied, and he had to sit down on the neighboring one. He sat down and began to listen to the silence. Suddenly, he got a question: what if in different parts of the park, the sile...
[ { "input": { "stdin": "4 4\n4 3 2 16\n1 13 14 15\n5 7 8 12\n6 11 9 10\n" }, "output": { "stdout": "50\n" } }, { "input": { "stdin": "2 3\n1 2 3\n4 5 6\n" }, "output": { "stdout": "15\n" } }, { "input": { "stdin": "1 5\n1 2 3 4 5\n" }, ...
{ "tags": [ "data structures", "trees" ], "title": "Sasha and Algorithm of Silence's Sounds" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 2 * 1e5 + 10;\nconst int dx[4] = {1, -1, 0, 0};\nconst int dy[4] = {0, 0, -1, 1};\nint n;\nint m;\nstruct mar {\n int f[N * 2];\n inline int* operator[](const int& x) { return f + x * m; }\n} val;\nbool book[N];\nstruct data {\n int x;\n int ...
Codeforces/1139/C
# Edgy Trees You are given a tree (a connected undirected graph without cycles) of n vertices. Each of the n - 1 edges of the tree is colored in either black or red. You are also given an integer k. Consider sequences of k vertices. Let's call a sequence [a_1, a_2, …, a_k] good if it satisfies the following criterion...
[ { "input": { "stdin": "4 4\n1 2 1\n2 3 1\n3 4 1\n" }, "output": { "stdout": " 252\n" } }, { "input": { "stdin": "4 6\n1 2 0\n1 3 0\n1 4 0\n" }, "output": { "stdout": " ...
{ "tags": [ "dfs and similar", "dsu", "graphs", "math", "trees" ], "title": "Edgy Trees" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx,avx2,fma\")\n#pragma GCC optimization(\"unroll-loops\")\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\nvoid __print(int x) { cout << x; }\nvoid __print(long x) { cout << x; }\nvo...
Codeforces/1157/C2
# Increasing Subsequence (hard version) The only difference between problems C1 and C2 is that all values in input of problem C1 are distinct (this condition may be false for problem C2). You are given a sequence a consisting of n integers. You are making a sequence of moves. During each move you must take either th...
[ { "input": { "stdin": "7\n1 3 5 6 5 4 2\n" }, "output": { "stdout": "6\nLRLRRR" } }, { "input": { "stdin": "5\n1 2 4 3 2\n" }, "output": { "stdout": "4\nLRRR" } }, { "input": { "stdin": "4\n1 2 4 3\n" }, "output": { "stdout": "4...
{ "tags": [ "greedy" ], "title": "Increasing Subsequence (hard version)" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 3e5 + 10;\nint n, a[maxn], pt1, pt2, q, t, g, numl, numr;\nchar s[maxn];\nvector<char> x;\nint main() {\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n }\n int l = 1, r = n, last = 0;\n for (int i = 1; i...
Codeforces/1179/E
# Alesya and Discrete Math We call a function good if its domain of definition is some set of integers and if in case it's defined in x and x-1, f(x) = f(x-1) + 1 or f(x) = f(x-1). Tanya has found n good functions f_{1}, …, f_{n}, which are defined on all integers from 0 to 10^{18} and f_i(0) = 0 and f_i(10^{18}) = L...
[ { "input": { "stdin": "5 5\n? 1 0\n? 1 1\n? 2 1\n? 2 2\n? 3 2\n? 3 3\n? 4 3\n? 4 4\n? 5 4\n? 5 5\n!\n0 1\n1 2\n2 3\n3 4\n4 5\n" }, "output": { "stdout": "\n0\n1\n1\n2\n2\n3\n3\n4\n4\n4\n5\n" } }, { "input": { "stdin": "1 1\n1\n1 144596170576560149 -144596170576560149 1\n" ...
{ "tags": [ "divide and conquer", "interactive" ], "title": "Alesya and Discrete Math" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1005;\ninline long long query(int id, long long x) {\n if (!x) return 0;\n printf(\"? %d %lld\\n\", id, x);\n fflush(stdout);\n long long ret;\n scanf(\"%lld\", &ret);\n return ret;\n}\ninline long long findx(int id, long long L, long long ...
Codeforces/1198/A
# MP3 One common way of digitalizing sound is to record sound intensity at particular time moments. For each time moment intensity is recorded as a non-negative integer. Thus we can represent a sound file as an array of n non-negative integers. If there are exactly K distinct values in the array, then we need k = ⌈ l...
[ { "input": { "stdin": "6 1\n2 1 2 3 4 3\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "6 1\n1 1 2 2 3 3\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "6 2\n2 1 2 3 4 3\n" }, "output": { "stdout": "0...
{ "tags": [ "sortings", "two pointers" ], "title": "MP3" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long n, I, k, len, j = 1, ans, cnt;\nlong long a[400001], num[400001], sum[400001];\nint main() {\n cin >> n >> I;\n k = I * 8 / n;\n len = 1 << k;\n for (int i = 1; i <= n; ++i) cin >> a[i];\n sort(a + 1, a + n + 1);\n for (int i = 1; i <= n; ++i) ...
Codeforces/1214/H
# Tiles Placement The new pedestrian zone in Moscow city center consists of n squares connected with each other by n - 1 footpaths. We define a simple path as a sequence of squares such that no square appears in this sequence twice and any two adjacent squares in this sequence are directly connected with a footpath. T...
[ { "input": { "stdin": "7 3\n1 3\n2 3\n3 4\n4 5\n5 6\n5 7\n" }, "output": { "stdout": "No\n" } }, { "input": { "stdin": "7 4\n1 3\n2 3\n3 4\n4 5\n5 6\n5 7\n" }, "output": { "stdout": "Yes\n1 1 2 3 4 1 1 \n" } }, { "input": { "stdin": "5 2\n3 1...
{ "tags": [ "constructive algorithms", "dfs and similar", "trees" ], "title": "Tiles Placement" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<int> adj[200005];\nint d[200005], mx[200005];\nint c1[200005], c2[200005];\nint dfs(int v, int p) {\n d[v] = mx[v] = d[p] + 1;\n int u = v;\n for (int& x : adj[v]) {\n if (x != p) {\n int t = dfs(x, v);\n mx[v] = max(mx[v], mx[x]);\n ...
Codeforces/1238/B
# Kill 'Em All Ivan plays an old action game called Heretic. He's stuck on one of the final levels of this game, so he needs some help with killing the monsters. The main part of the level is a large corridor (so large and narrow that it can be represented as an infinite coordinate line). The corridor is divided into...
[ { "input": { "stdin": "2\n3 2\n1 3 5\n4 1\n5 2 3 5\n" }, "output": { "stdout": "2\n2\n" } }, { "input": { "stdin": "1\n3 1383\n1 2 3\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "1\n3 3\n1 2 3\n" }, "output": { "...
{ "tags": [ "greedy", "sortings" ], "title": "Kill 'Em All" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool cmp(int a, int b) { return a > b; }\nint a[100010];\nint vis[100010];\nint main() {\n int q;\n scanf(\"%d\", &q);\n while (q--) {\n int n, r;\n scanf(\"%d%d\", &n, &r);\n int b = 0;\n for (int i = 0; i < n; i++) {\n int x;\n scanf...
Codeforces/1256/C
# Platforms Jumping There is a river of width n. The left bank of the river is cell 0 and the right bank is cell n + 1 (more formally, the river can be represented as a sequence of n + 2 cells numbered from 0 to n + 1). There are also m wooden platforms on a river, the i-th platform has length c_i (so the i-th platfor...
[ { "input": { "stdin": "7 3 2\n1 2 1\n" }, "output": { "stdout": "YES\n0 1 0 2 2 0 3\n" } }, { "input": { "stdin": "10 1 5\n2\n" }, "output": { "stdout": "YES\n0 0 0 0 1 1 0 0 0 0\n" } }, { "input": { "stdin": "10 1 11\n1\n" }, "output...
{ "tags": [ "greedy" ], "title": "Platforms Jumping" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[1000 + 5];\nint main() {\n int n, m, d, sum = 0;\n scanf(\"%d%d%d\", &n, &m, &d);\n for (int i = 1; i <= m; i++) scanf(\"%d\", a + i), sum += a[i];\n if (sum + (d - 1) * (m + 1) < n) return puts(\"NO\"), 0;\n puts(\"YES\");\n int l = 0, L = 1;\n f...
Codeforces/127/C
# Hot Bath Bob is about to take a hot bath. There are two taps to fill the bath: a hot water tap and a cold water tap. The cold water's temperature is t1, and the hot water's temperature is t2. The cold water tap can transmit any integer number of water units per second from 0 to x1, inclusive. Similarly, the hot wa...
[ { "input": { "stdin": "300 500 1000 1000 300\n" }, "output": { "stdout": "1000 0\n" } }, { "input": { "stdin": "10 70 100 100 25\n" }, "output": { "stdout": "99 33\n" } }, { "input": { "stdin": "143 456 110 117 273\n" }, "output": { ...
{ "tags": [ "binary search", "brute force", "math" ], "title": "Hot Bath" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long t1, t2, x1, x2, t;\ndouble mx = 1e8;\nlong long x, y;\nvoid Readinput() { cin >> t1 >> t2 >> x1 >> x2 >> t; }\nvoid binsearch(long long l, long long r, long long k) {\n if (l > r) return;\n int mid = (l + r) >> 1;\n double tt = (t1 * k + t2 * mid)...
Codeforces/12/B
# Correct Solution? One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and giving each other interesting problems to solve. When it was Alice's turn, she told the number n to Bob and said: —Shuffle the digits in this number in order to obtain the smallest possible number wi...
[ { "input": { "stdin": "3310\n1033\n" }, "output": { "stdout": "OK\n" } }, { "input": { "stdin": "4\n5\n" }, "output": { "stdout": "WRONG_ANSWER\n" } }, { "input": { "stdin": "17109\n01179\n" }, "output": { "stdout": "WRONG_ANSWE...
{ "tags": [ "implementation", "sortings" ], "title": "Correct Solution?" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma\")\n#pragma GCC optimize(\"unroll-loops\")\nusing namespace std;\nlong long int MOD = 1000000007;\ndouble eps = 1e-12;\nvoid solve();\nlong long int gcd(long long int a, long ...
Codeforces/1323/D
# Present Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one — xor of all pairwise sums of elements in the array,...
[ { "input": { "stdin": "3\n1 2 3\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "51\n50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 ...
{ "tags": [ "binary search", "bitmasks", "constructive algorithms", "data structures", "math", "sortings" ], "title": "Present" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int t = 1;\n while (t--) {\n int n;\n cin >> n;\n int a[n];\n for (int i = 0; i < n; i++) cin >> a[i];\n long long vl = 0, ans = 0;\n for (int i = 0; i < 26; i++) {\n ...
Codeforces/1342/C
# Yet Another Counting Problem You are given two integers a and b, and q queries. The i-th query consists of two numbers l_i and r_i, and the answer to it is the number of integers x such that l_i ≤ x ≤ r_i, and ((x mod a) mod b) ≠ ((x mod b) mod a). Calculate the answer for each query. Recall that y mod z is the rem...
[ { "input": { "stdin": "2\n4 6 5\n1 1\n1 3\n1 5\n1 7\n1 9\n7 10 2\n7 8\n100 200\n" }, "output": { "stdout": "0\n0\n0\n2\n4\n0\n91\n" } }, { "input": { "stdin": "1\n200 200 1\n1 1000000000000000000\n" }, "output": { "stdout": "0\n" } }, { "input": { ...
{ "tags": [ "math", "number theory" ], "title": "Yet Another Counting Problem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n int t;\n cin >> t;\n while (t--) {\n int a, b, q;\n cin >> a >> b >> q;\n int rem = a * b;\n vector<int> pref(rem + 1);\n pref[0] = 0;\n for (int x = ...
Codeforces/1364/C
# Ehab and Prefix MEXs Given an array a of length n, find another array, b, of length n such that: * for each i (1 ≤ i ≤ n) MEX(\\{b_1, b_2, …, b_i\})=a_i. The MEX of a set of integers is the smallest non-negative integer that doesn't belong to this set. If such array doesn't exist, determine this. Input The...
[ { "input": { "stdin": "3\n1 2 3\n" }, "output": { "stdout": "0 1 2\n" } }, { "input": { "stdin": "3\n1 1 3\n" }, "output": { "stdout": "0 2 1\n" } }, { "input": { "stdin": "4\n0 0 0 2\n" }, "output": { "stdout": "1 3 4 0\n" ...
{ "tags": [ "brute force", "constructive algorithms", "greedy" ], "title": "Ehab and Prefix MEXs" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename T>\nT ii() {\n T a;\n cin >> a;\n return a;\n}\nstruct custom_hash {\n static uint64_t splitmix64(uint64_t x) {\n x += 0x9e3779b97f4a7c15;\n x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;\n x = (x ^ (x >> 27)) * 0x94d049bb133111eb;\n...
Codeforces/1384/D
# GameGame Koa the Koala and her best friend want to play a game. The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts. Let's describe a move in the game: * During his move, a player chooses an...
[ { "input": { "stdin": "4\n5\n4 1 5 1 3\n4\n1 0 1 6\n1\n0\n2\n5 4\n" }, "output": { "stdout": "WIN\nWIN\nDRAW\nWIN\n" } }, { "input": { "stdin": "3\n3\n1 2 2\n3\n2 2 3\n5\n0 0 0 2 2\n" }, "output": { "stdout": "WIN\nLOSE\nDRAW\n" } }, { "input": { ...
{ "tags": [ "bitmasks", "constructive algorithms", "dp", "games", "greedy", "math" ], "title": "GameGame" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 100005;\nint a[N];\nint main() {\n int t;\n scanf(\"%d\", &t);\n while (t--) {\n int n;\n scanf(\"%d\", &n);\n int res = 0;\n for (int i = 1; i <= n; ++i) scanf(\"%d\", &a[i]), res ^= a[i];\n if (res == 0) {\n puts(\"DRAW\"...
Codeforces/1406/A
# Subset Mex Given a set of integers (it can contain equal elements). You have to split it into two subsets A and B (both of them can contain equal elements or be empty). You have to maximize the value of mex(A)+mex(B). Here mex of a set denotes the smallest non-negative integer that doesn't exist in the set. For ex...
[ { "input": { "stdin": "4\n6\n0 2 1 5 0 1\n3\n0 1 2\n4\n0 2 0 1\n6\n1 2 3 4 5 6\n" }, "output": { "stdout": "5\n3\n4\n0\n" } }, { "input": { "stdin": "1\n100\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 ...
{ "tags": [ "greedy", "implementation", "math" ], "title": "Subset Mex" }
{ "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<int> v(n);\n for (int i = 0; i < n; i++) {\n cin >> v[i];\n }\n sort(v.begin(), v.end());\n int l = 0, r = 0, i;\n int flag = 0;\n for (i = 0; i ...
Codeforces/1427/B
# Chess Cheater You like playing chess tournaments online. In your last tournament you played n games. For the sake of this problem, each chess game is either won or lost (no draws). When you lose a game you get 0 points. When you win you get 1 or 2 points: if you have won also the previous game you get 2 points, oth...
[ { "input": { "stdin": "8\n5 2\nWLWLL\n6 5\nLLLWWL\n7 1\nLWLWLWL\n15 5\nWWWLLLWWWLLLWWW\n40 7\nLLWLWLWWWLWLLWLWWWLWLLWLLWLLLLWLLWWWLWWL\n1 0\nL\n1 1\nL\n6 1\nWLLWLW\n" }, "output": { "stdout": "7\n11\n6\n26\n46\n0\n1\n6\n" } }, { "input": { "stdin": "8\n5 2\nLLWLW\n6 5\nLLLW...
{ "tags": [ "greedy", "implementation", "sortings" ], "title": "Chess Cheater" }
{ "cpp": "#include <bits/stdc++.h>\n#pragma GCC optimize(\"Ofast\")\n#pragma GCC target(\"avx,avx2,fma\")\n#pragma comment(linker, \"/stack:200000000\")\n#pragma GCC optimize(\"unroll-loops\")\nusing namespace std;\ntemplate <typename A>\nostream &operator<<(ostream &cout, vector<A> const &v);\ntemplate <typename A, ...
Codeforces/1450/B
# Balls of Steel You have n distinct points (x_1, y_1),…,(x_n,y_n) on the plane and a non-negative integer parameter k. Each point is a microscopic steel ball and k is the attract power of a ball when it's charged. The attract power is the same for all balls. In one operation, you can select a ball i to charge it. On...
[ { "input": { "stdin": "3\n3 2\n0 0\n3 3\n1 1\n3 3\n6 7\n8 8\n6 9\n4 1\n0 0\n0 1\n0 2\n0 3\n" }, "output": { "stdout": "\n-1\n1\n-1\n" } }, { "input": { "stdin": "3\n3 2\n0 -1\n3 3\n1 0\n3 3\n6 7\n4 0\n6 9\n4 1\n0 -1\n1 2\n0 2\n0 3\n" }, "output": { "stdout": "...
{ "tags": [ "brute force", "geometry", "greedy" ], "title": "Balls of Steel" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long\n#define rep(n) for(int i=0;i<n;i++)\n#define rrep(n) for(int i=n-1;i>=0;i--)\n#define mod 1000000007\ntypedef vector<int> vi; \ntypedef pair<int, int> pi; \n#define F first \n#define S second \n#define PB push_back\nvoid solve()\n{\n\tll n...
Codeforces/1473/G
# Tiles Consider a road consisting of several rows. Each row is divided into several rectangular tiles, and all tiles in the same row are equal. The first row contains exactly one rectangular tile. Look at the picture below which shows how the tiles are arranged. The road is constructed as follows: * the first ro...
[ { "input": { "stdin": "2\n4 2\n2 3\n" }, "output": { "stdout": "\n850\n" } }, { "input": { "stdin": "8\n328 323\n867 868\n715 718\n721 722\n439 435\n868 870\n834 834\n797 796\n" }, "output": { "stdout": "\n759099319\n" } }, { "input": { "stdi...
{ "tags": [ "combinatorics", "dp", "fft", "math" ], "title": "Tiles" }
{ "cpp": "//EDIR\n#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define forn(i, n) for (int i = 0; i < int(n); ++i)\n#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)\n#define sz(a) int((a).size())\n\ntemplate<const int &MOD>\nstruct _m_int {\n int val;\n \n _m_int(int64_t v = 0) {\n if (v < 0) v...
Codeforces/149/B
# Martian Clock Having stayed home alone, Petya decided to watch forbidden films on the Net in secret. "What ungentlemanly behavior!" — you can say that, of course, but don't be too harsh on the kid. In his country films about the Martians and other extraterrestrial civilizations are forbidden. It was very unfair to P...
[ { "input": { "stdin": "2A:13\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "11:20\n" }, "output": { "stdout": "3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 " } }, { "input": { "stdin": "000B:00001\n" }, "output": ...
{ "tags": [ "implementation" ], "title": "Martian Clock" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint num(char s) {\n if (s >= '0' && s <= '9')\n return s - '0';\n else\n return s - 'A' + 10;\n}\nint main() {\n string input, h, m;\n vector<int> result;\n int i, hrs, mts, j;\n cin >> input;\n int max = -1;\n for (i = 0; i < input.size(); i++)\...
Codeforces/1523/B
# Lord of the Values <image> While trading on his favorite exchange trader William realized that he found a vulnerability. Using this vulnerability he could change the values of certain internal variables to his advantage. To play around he decided to change the values of all internal variables from a_1, a_2, …, a_n ...
[ { "input": { "stdin": "2\n4\n1 1 1 1\n4\n4 3 1 2\n" }, "output": { "stdout": "\n8\n2 1 2\n2 1 2\n2 1 3\n2 1 3\n2 1 4\n2 1 4\n1 1 2\n1 1 2\n8\n2 1 4\n1 2 4\n1 2 4\n1 2 4\n1 3 4\n1 1 2\n1 1 2\n1 1 4\n" } }, { "input": { "stdin": "2\n4\n1 1 1 1\n4\n4 3 1 2\n" }, "outpu...
{ "tags": [ "constructive algorithms" ], "title": "Lord of the Values" }
{ "cpp": "#include<bits/stdc++.h>\n#define rep(i,a,b) for(int i=a;i<b;i++)\n#define ret(x) return cout<<x,0;\n#define rety return cout<<\"YES\",0;\n#define retn return cout<<\"NO\",0;\ntypedef long long ll;\ntypedef double db;\ntypedef long double ld;\n#define stt string\n#define ve vector<ll>\n#define se set<ll>\...
Codeforces/155/A
# I_love_%username% Vasya adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him. One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress o...
[ { "input": { "stdin": "10\n4664 6496 5814 7010 5762 5736 6944 4850 3698 7242\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "5\n100 50 200 150 200\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5\n100 36 53 7 81\n...
{ "tags": [ "brute force" ], "title": "I_love_%username%" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int i, num, max = 0, min = 0, count = 0;\n cin >> num;\n int a[num];\n for (i = 0; i < num; i++) cin >> a[i];\n max = min = a[0];\n for (i = 0; i < num; i++) {\n if (a[i] > max) {\n ++count;\n max = a[i];\n } else if (a[i] ...
Codeforces/177/C1
# Party To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected b...
[ { "input": { "stdin": "9\n8\n1 2\n1 3\n2 3\n4 5\n6 7\n7 8\n8 9\n9 6\n2\n1 6\n7 9\n" }, "output": { "stdout": "3" } }, { "input": { "stdin": "14\n0\n0\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "14\n6\n1 2\n2 3\n3 4\n4 5\n8 9\n...
{ "tags": [ "dfs and similar", "dsu", "graphs" ], "title": "Party" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long parent[10000];\nlong long find(long long x) {\n if (parent[parent[x]] == parent[x]) return parent[x];\n return parent[x] = find(parent[x]);\n}\nvoid unite(long long x, long long y) {\n long long px = find(x);\n long long py = find(y);\n if (px !...
Codeforces/198/B
# Jumping on Walls Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon. The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from...
[ { "input": { "stdin": "7 3\n---X--X\n-X--XX-\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "6 2\n--X-X-\nX--XX-\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "2 1\n-X\nX-\n" }, "output": { "stdou...
{ "tags": [ "shortest paths" ], "title": "Jumping on Walls" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf = (int)1e9;\nconst int mod = (int)1e9 + 7;\nconst double pi = acos(-1.0);\nconst double eps = 1e-9;\nint n, k;\nstring second[2];\nint d[2][1000005];\nbool used[2][1000005];\nqueue<pair<int, int> > q;\nint main() {\n scanf(\"%d%d\\n\", &n, &k);\...
Codeforces/221/C
# Little Elephant and Problem The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array hi...
[ { "input": { "stdin": "3\n3 2 1\n" }, "output": { "stdout": "YES" } }, { "input": { "stdin": "4\n4 3 2 1\n" }, "output": { "stdout": "NO" } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "YES" } }, { "i...
{ "tags": [ "implementation", "sortings" ], "title": "Little Elephant and Problem" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong a[100005], b[100005];\nint main() {\n long n, i;\n cin >> n;\n for (i = 0; i < n; i++) {\n scanf(\"%ld\", &a[i]);\n b[i] = a[i];\n }\n sort(a, a + n);\n long ans = 0;\n for (i = 0; i < n; i++)\n if (a[i] != b[i]) ++ans;\n if (ans <= 2)\n ...
Codeforces/245/D
# Restoring Table Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequen...
[ { "input": { "stdin": "3\n-1 18 0\n18 -1 0\n0 0 -1\n" }, "output": { "stdout": "18 18 0 " } }, { "input": { "stdin": "4\n-1 128 128 128\n128 -1 148 160\n128 148 -1 128\n128 160 128 -1\n" }, "output": { "stdout": "128 180 148 160 " } }, { "input": {...
{ "tags": [ "constructive algorithms", "greedy" ], "title": "Restoring Table" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long a[103];\nlong long bb[103][103];\nint main() {\n int test;\n while (scanf(\"%d\", &test) != EOF) {\n memset(a, 0, sizeof(a));\n for (int i = 1; i <= test; i++) {\n for (int j = 1; j <= test; j++) cin >> bb[i][j];\n }\n for (int i =...
Codeforces/270/B
# Multithreading Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest mess...
[ { "input": { "stdin": "5\n5 2 1 3 4\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "4\n4 3 2 1\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "3\n1 2 3\n" }, "output": { "stdout": "0\n" } }, {...
{ "tags": [ "data structures", "greedy", "implementation" ], "title": "Multithreading" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, a[100001], res = 1;\nint main() {\n cin >> n;\n for (int i = 1; i <= n; i++) cin >> a[i];\n int i = n;\n while (a[i - 1] < a[i] && i > 1) res++, i--;\n cout << n - res;\n return 0;\n}\n", "java": "import java.io.*;\nimport java.util.*;\n\npublic...
Codeforces/293/D
# Ksusha and Square Ksusha is a vigorous mathematician. She is keen on absolutely incredible mathematical riddles. Today Ksusha came across a convex polygon of non-zero area. She is now wondering: if she chooses a pair of distinct points uniformly among all integer points (points with integer coordinates) inside or ...
[ { "input": { "stdin": "3\n17 136\n859 937\n16 641\n" }, "output": { "stdout": "66811.370415516887746\n" } }, { "input": { "stdin": "4\n-1 3\n4 5\n6 2\n3 -5\n" }, "output": { "stdout": "8.1583333333333333329\n" } }, { "input": { "stdin": "3\n0...
{ "tags": [ "geometry", "math", "probabilities", "two pointers" ], "title": "Ksusha and Square" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename T1, typename T2>\ninline T1 max(T1 a, T2 b) {\n return a < b ? b : a;\n}\ntemplate <typename T1, typename T2>\ninline T1 min(T1 a, T2 b) {\n return a < b ? a : b;\n}\ntemplate <typename T1, typename T2>\ninline T1 gmax(T1 &a, T2 b) {\n re...
Codeforces/317/B
# Ants It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1,...
[ { "input": { "stdin": "6 5\n0 -2\n0 -1\n0 0\n0 1\n0 2\n" }, "output": { "stdout": "0\n1\n2\n1\n0\n" } }, { "input": { "stdin": "1 3\n0 1\n0 0\n0 -1\n" }, "output": { "stdout": " 0\n ...
{ "tags": [ "brute force", "implementation" ], "title": "Ants" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, M[129][129];\n memset(M, 0, sizeof M);\n scanf(\"%d\", &n);\n M[64][64] = n;\n vector<pair<int, int> > s1, s2;\n bool used[129][129];\n if (n >= 4) s1.push_back(make_pair(64, 64));\n int it = 0;\n while (!s1.empty()) {\n memse...
Codeforces/341/B
# Bubble Sort Graph Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort exec...
[ { "input": { "stdin": "3\n3 1 2\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "100\n36 48 92 87 28 85 42 10 44 41 39 3 79 9 14 56 1 16 46 35 93 8 82 26 100 59 60 2 96 52 13 98 70 81 71 94 54 91 17 88 33 30 19 50 18 73 65 29 78 21 61 7 99 97 45 89 57 27 76 11 ...
{ "tags": [ "binary search", "data structures", "dp" ], "title": "Bubble Sort Graph" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int max_n = 2e5 + 10;\nint n, temp;\nvector<int> ans;\nint main() {\n ios_base::sync_with_stdio(false), cin.tie(0);\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> temp;\n int pos = lower_bound(ans.begin(), ans.end(), temp) - ans.begin();\n...
Codeforces/364/D
# Ghd John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positive integer g, such that all number from the set are evenly divisible by g and there isn't such g' (g' > g), that all numbers of the set are evenly divisible by g'. Unfortunately Jane couldn't cope with the task and John ...
[ { "input": { "stdin": "6\n6 2 3 4 5 6\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "5\n5 5 6 10 15\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "1\n1\n" }, "output": { "stdout": "1\n" } }, ...
{ "tags": [ "brute force", "math", "probabilities" ], "title": "Ghd" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long a[1000005];\nlong long delit[100005], quant[100005];\nlong long nod(long long a, long long b) {\n if (a > b) swap(a, b);\n if (a == 0) return b;\n return nod(b % a, a);\n}\nint main() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n srand(time...
Codeforces/388/C
# Fox and Card Game Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles of cards on the table. And there is a positive integer on each card. The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a c...
[ { "input": { "stdin": "3\n3 1 3 2\n3 5 4 6\n2 8 7\n" }, "output": { "stdout": "18 18\n" } }, { "input": { "stdin": "2\n1 100\n2 1 10\n" }, "output": { "stdout": "101 10\n" } }, { "input": { "stdin": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000...
{ "tags": [ "games", "greedy", "sortings" ], "title": "Fox and Card Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, m, F = 0, sum = 0, x, lim, s, y;\n scanf(\"%d\", &n);\n vector<int> V;\n for (int i = 0; i < n; i++) {\n scanf(\"%d\", &m);\n lim = m / 2;\n s = y = 0;\n for (int j = 0; j < m; j++) {\n scanf(\"%d\", &x);\n sum +...
Codeforces/409/A
# The Great Game Two teams meet in The Game World Championship. Some scientists consider this game to be the most intellectually challenging game in the world. You are given two strings describing the teams' actions in the final battle. Figure out who became the champion. Input The input contains two strings of equa...
[ { "input": { "stdin": "[]()[]8&lt;\n8&lt;[]()8&lt;\n" }, "output": { "stdout": "TEAM 2 WINS" } }, { "input": { "stdin": "8&lt;8&lt;()\n[]8&lt;[]\n" }, "output": { "stdout": "TEAM 1 WINS" } }, { "input": { "stdin": "()[][]()()[][]()8<8<\n8<[](...
{ "tags": [ "*special" ], "title": "The Great Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string s1, s2;\n cin >> s1 >> s2;\n int n = s1.length();\n int res = 0;\n for (int i = 0; i < n; i += 2) {\n string c1 = \"\", c2 = \"\";\n c1 += s1[i];\n c1 += s1[i + 1];\n c2 += s2[i];\n c2 += s2[i + 1];\n if (c1 == \"8<...
Codeforces/436/B
# Om Nom and Spiders Om Nom really likes candies and doesn't like spiders as they frequently steal candies. One day Om Nom fancied a walk in a park. Unfortunately, the park has some spiders and Om Nom doesn't want to see them at all. <image> The park can be represented as a rectangular n × m field. The park has k sp...
[ { "input": { "stdin": "2 2 2\n..\nRL\n" }, "output": { "stdout": "1 1 " } }, { "input": { "stdin": "3 3 4\n...\nR.L\nR.U\n" }, "output": { "stdout": "0 2 2 " } }, { "input": { "stdin": "2 2 2\n..\nUU\n" }, "output": { "stdout": ...
{ "tags": [ "implementation", "math" ], "title": "Om Nom and Spiders" }
{ "cpp": "#include <bits/stdc++.h>\nchar s[2010];\nint a[2010][2010];\nint main() {\n int n, m, k, i, j;\n scanf(\"%d %d %d\", &n, &m, &k);\n for (i = 0; i < n; i++) {\n scanf(\"%s\", s);\n for (j = 0; j < m; j++) {\n if (s[j] == 'U')\n a[i][j] = 1;\n else if (s[j] == 'R')\n a[i][j] =...
Codeforces/459/C
# Pashmak and Buses Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. ...
[ { "input": { "stdin": "3 2 2\n" }, "output": { "stdout": "1 2 1\n1 1 2\n" } }, { "input": { "stdin": "3 2 1\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "2 1 1000\n" }, "output": { "stdout": "-1\n" } }, ...
{ "tags": [ "combinatorics", "constructive algorithms", "math" ], "title": "Pashmak and Buses" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <typename T>\nT nextInt() {\n T x = 0, p = 1;\n char ch;\n do {\n ch = getchar();\n } while (ch <= ' ');\n if (ch == '-') {\n p = -1;\n ch = getchar();\n }\n while (ch >= '0' && ch <= '9') {\n x = x * 10 + (ch - '0');\n ch = getc...
Codeforces/480/B
# Long Jumps Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he ca...
[ { "input": { "stdin": "3 250 185 230\n0 185 250\n" }, "output": { "stdout": "1\n230\n" } }, { "input": { "stdin": "2 300 185 230\n0 300\n" }, "output": { "stdout": "2\n185 230\n" } }, { "input": { "stdin": "4 250 185 230\n0 20 185 250\n" ...
{ "tags": [ "binary search", "greedy", "implementation" ], "title": "Long Jumps" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 200005;\nint n, l, x, y;\nint a[maxn];\nset<int> pos;\nbool in(int x) { return x >= 0 && x <= l; }\nint main() {\n scanf(\"%d%d%d%d\", &n, &l, &x, &y);\n for (int i = 1, t; i <= n; i++) {\n scanf(\"%d\", &a[i]);\n pos.insert(a[i]);\n ...
Codeforces/505/B
# Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q queries. In the i-th quer...
[ { "input": { "stdin": "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4\n" }, "output": { "stdout": "1\n1\n1\n1\n2\n" } }, { "input": { "stdin": "4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4\n" }, "output": { "stdout...
{ "tags": [ "dfs and similar", "dp", "dsu", "graphs" ], "title": "Mr. Kitayuta's Colorful Graph" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long int N = 105;\nvector<pair<long long int, long long int>> adj[N];\nvector<bool> vis(N);\nbool dfs(long long int u, long long int v, long long int col) {\n vis[u] = true;\n if (u == v) {\n return true;\n }\n for (auto i : adj[u]) {\n lo...
Codeforces/529/A
# And Yet Another Bracket Sequence Polycarpus has a finite sequence of opening and closing brackets. In order not to fall asleep in a lecture, Polycarpus is having fun with his sequence. He is able to perform two operations: * adding any bracket in any position (in the beginning, the end, or between any two existin...
[ { "input": { "stdin": "()(\n" }, "output": { "stdout": "(())\n" } }, { "input": { "stdin": "()(())\n" }, "output": { "stdout": "(())()\n" } }, { "input": { "stdin": ")(()()))((\n" }, "output": { "stdout": "(()(()()))\n" } ...
{ "tags": [ "data structures", "greedy", "hashing", "string suffix structures", "strings" ], "title": "And Yet Another Bracket Sequence" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 4000010;\ninline int read() {\n int x = 0;\n int f = 0, c = getchar();\n for (; c > '9' || c < '0'; f = c == '-', c = getchar())\n ;\n for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 3) + (x << 1) + c - '0';\n return f ? -x : x;\n}\...
Codeforces/554/C
# Kyoya and Colored Balls Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color i before drawi...
[ { "input": { "stdin": "3\n2\n2\n1\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "4\n1\n2\n3\n4\n" }, "output": { "stdout": "1680\n" } }, { "input": { "stdin": "25\n35\n43\n38\n33\n47\n44\n40\n36\n41\n42\n33\n30\n49\n42\n62\n39\...
{ "tags": [ "combinatorics", "dp", "math" ], "title": "Kyoya and Colored Balls" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<vector<long long>> memo(1001, vector<long long>(1001, -1));\nlong long cnk(long long n, long long k) {\n if (memo[n][k] == -1) {\n if (n < k)\n memo[n][k] = 0;\n else if (n == k)\n memo[n][k] = 1;\n else if (k == 0)\n memo[n][k]...
Codeforces/580/B
# Kefa and Company Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn...
[ { "input": { "stdin": "5 100\n0 7\n11 32\n99 10\n46 8\n87 54\n" }, "output": { "stdout": "111" } }, { "input": { "stdin": "4 5\n75 5\n0 100\n150 20\n75 1\n" }, "output": { "stdout": "100" } }, { "input": { "stdin": "5 9\n0 98\n2 1000000000\n8...
{ "tags": [ "binary search", "sortings", "two pointers" ], "title": "Kefa and Company" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\npair<long long int, long long int> ar[100005];\nlong long int arr[100005];\nlong long int brr[100005];\nlong long int cm[100005];\nint main() {\n long long int i, n, t, a, b, pos, c, ans, k;\n scanf(\"%lld%lld\", &n, &t);\n for (i = 0; i < n; i++) {\n sc...
Codeforces/602/A
# Two Bases After seeing the "ALL YOUR BASE ARE BELONG TO US" meme for the first time, numbers X and Y realised that they have different bases, which complicated their relations. You're given a number X represented in base bx and a number Y represented in base by. Compare those two numbers. Input The first line of ...
[ { "input": { "stdin": "7 16\n15 15 4 0 0 7 10\n7 9\n4 8 0 3 1 5 0\n" }, "output": { "stdout": ">" } }, { "input": { "stdin": "3 3\n1 0 2\n2 5\n2 4\n" }, "output": { "stdout": "<" } }, { "input": { "stdin": "6 2\n1 0 1 1 1 1\n2 10\n4 7\n" ...
{ "tags": [ "brute force", "implementation" ], "title": "Two Bases" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long Deci(int arr[], int base, int n) {\n long long power = 1;\n long long num = 0;\n for (int i = n - 1; i >= 0; i--) {\n num += arr[i] * power;\n power = power * base;\n }\n return num;\n}\nint main() {\n int n, m, x, y;\n cin >> n >> x;\n ...
Codeforces/624/D
# Array GCD You are given array ai of length n. You may consecutively apply two operations to this array: * remove some subsegment (continuous subsequence) of length m < n and pay for it m·a coins; * change some elements of the array by at most 1, and pay b coins for each change. Please note that each of ope...
[ { "input": { "stdin": "3 1 4\n4 2 3\n" }, "output": { "stdout": " 1\n" } }, { "input": { "stdin": "8 3 4\n3 7 5 4 3 12 9 4\n" }, "output": { "stdout": " ...
{ "tags": [ "dp", "greedy", "number theory" ], "title": "Array GCD" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long oo = 1e15 + 1;\nconst int mod = 1e9 + 7;\nconst int maxn = 1e6 + 5;\nlong long n, f[maxn][3], a, b, s[maxn], res(+oo);\nvoid Check(int x, int w) {\n f[1][0] = f[1][1] = f[1][2] = w;\n for (int i = 2; i <= n; ++i) {\n f[i][0] = f[i - 1][0];...
Codeforces/64/A
# Factorial Print the factorial of the given integer number n. The factorial of n is equal to 1·2·...·n. Input The only line contains n (1 ≤ n ≤ 10). Output Print the factorial of n. Examples Input 3 Output 6 Input 5 Output 120
[ { "input": { "stdin": "3\n" }, "output": { "stdout": "6\n" } }, { "input": { "stdin": "5\n" }, "output": { "stdout": "120\n" } } ]
{ "tags": [ "*special", "implementation" ], "title": "Factorial" }
{ "cpp": null, "java": null, "python": null }
Codeforces/673/A
# Bear and Game Bear Limak likes watching sports on TV. He is going to watch a game today. The game lasts 90 minutes and there are no breaks. Each minute can be either interesting or boring. If 15 consecutive minutes are boring then Limak immediately turns TV off. You know that there will be n interesting minutes t1...
[ { "input": { "stdin": "3\n7 20 88\n" }, "output": { "stdout": "35\n" } }, { "input": { "stdin": "9\n15 20 30 40 50 60 70 80 90\n" }, "output": { "stdout": "90\n" } }, { "input": { "stdin": "9\n16 20 30 40 50 60 70 80 90\n" }, "output"...
{ "tags": [ "implementation" ], "title": "Bear and Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n vector<int> v, a, b;\n map<int, int> mp;\n int n, m;\n int cnt = 15;\n cin >> n;\n while (n--) {\n cin >> m;\n if (m <= cnt) {\n cnt = m + 15;\n }\n }\n if (cnt > 90) cnt = 90;\n cout << cnt << endl;\n}\n", "java": "impo...
Codeforces/698/B
# Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are many ways to represent such a tree. One way is to create an array with n integers p1, p2, ..., pn, where pi denotes a parent of vertex i (here, for convenience ...
[ { "input": { "stdin": "5\n3 2 2 5 3\n" }, "output": { "stdout": "0\n3 2 2 5 3 " } }, { "input": { "stdin": "8\n2 3 5 4 1 6 6 7\n" }, "output": { "stdout": "2 \n2 3 5 4 4 4 6 7 \n" } }, { "input": { "stdin": "4\n2 3 3 4\n" }, "output":...
{ "tags": [ "constructive algorithms", "dfs and similar", "dsu", "graphs", "trees" ], "title": "Fix a Tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nsigned main() {\n long long n;\n cin >> n;\n vector<long long> a(n);\n long long b = -1;\n long long ans = -1;\n for (long long i = (0); i < (n); i++) cin >> a[i], a[i]--;\n for (long long i = (0); i < (n); i++)\n if (i == a[i]) a[i] = b, b = i, ans+...
Codeforces/719/B
# Anatoly and Cockroaches Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room. Anatoly just made all his cockroaches to form a single l...
[ { "input": { "stdin": "5\nrbbrr\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "5\nbbbbb\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3\nrbr\n" }, "output": { "stdout": "0\n" } }, { "in...
{ "tags": [ "greedy" ], "title": "Anatoly and Cockroaches" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long power(long long a, long long b) {\n long long result = 1;\n while (b > 0) {\n if (b % 2 == 1) {\n result *= a;\n }\n a *= a;\n b /= 2;\n }\n return result;\n}\nlong long gcd(long long x, long long y) {\n long long r;\n while (y...
Codeforces/73/F
# Plane of Tanks Vasya plays the Plane of Tanks. The tanks in this game keep trying to finish each other off. But your "Pedalny" is not like that... He just needs to drive in a straight line from point A to point B on the plane. Unfortunately, on the same plane are n enemy tanks. We shall regard all the tanks as point...
[ { "input": { "stdin": "0 0 10 0\n1\n5 -5 4.71238 1\n0\n" }, "output": { "stdout": "4.244116\n" } }, { "input": { "stdin": "0 0 10 0\n1\n5 -5 4.71238 1\n1\n" }, "output": { "stdout": "0.0000\n" } }, { "input": { "stdin": "0 0 10 10\n1\n10 0 0 ...
{ "tags": [ "brute force", "geometry" ], "title": "Plane of Tanks" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\ninline void read(T& x) {\n bool fu = 0;\n char c;\n for (c = getchar(); c <= 32; c = getchar())\n ;\n if (c == '-') fu = 1, c = getchar();\n for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';\n if (fu) x = -x;\n};\ntemplate <...
Codeforces/763/D
# Timofey and a flat tree Little Timofey has a big tree — an undirected connected graph with n vertices and no simple cycles. He likes to walk along it. His tree is flat so when he walks along it he sees it entirely. Quite naturally, when he stands on a vertex, he sees the tree as a rooted tree with the root in this v...
[ { "input": { "stdin": "10\n1 7\n1 8\n9 4\n5 1\n9 2\n3 5\n10 6\n10 9\n5 10\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3\n1 2\n2 3\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "7\n1 2\n4 2\n2 3\n5 6\n6 7\n3 7\...
{ "tags": [ "data structures", "graphs", "hashing", "shortest paths", "trees" ], "title": "Timofey and a flat tree" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid pr(int x);\nvoid pr(const char* x);\nvoid ps();\ntemplate <class T, class... Ts>\nvoid ps(const T& t, const Ts&... ts);\nint n, a, b;\nvector<pair<int, int> > edges;\nvector<int> adj[210000];\nunsigned long long t[210000];\nint size[210000];\nint order[21...
Codeforces/787/A
# The Monster A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ... and Morty screams at times d, d + c, d + 2c, d + 3c, .... <image> The Monster will catch them if at any point they scream ...
[ { "input": { "stdin": "20 2\n9 19\n" }, "output": { "stdout": "82\n" } }, { "input": { "stdin": "2 1\n16 12\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "84 82\n38 6\n" }, "output": { "stdout": "82\n" } },...
{ "tags": [ "brute force", "math", "number theory" ], "title": "The Monster" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nconst int MAXN = 1e5 + 5;\nint main() {\n int a, b, c, d, ans;\n cin >> a >> b >> c >> d;\n if ((b > d && a > c) || (b < d && a < c)) ans = -1;\n for (int i = max(b, d); i < 1e5; i++) {\n if ((i - b) % a == 0 && (i - d) % c ...
Codeforces/808/E
# Selling Souvenirs After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market....
[ { "input": { "stdin": "4 3\n3 10\n2 7\n2 8\n1 1\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "2 2\n1 3\n2 2\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "1 1\n2 1\n" }, "output": { "stdout": "0\n...
{ "tags": [ "binary search", "dp", "greedy", "ternary search" ], "title": "Selling Souvenirs" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int Maxn = 300003;\nstruct Node {\n int p1, p2;\n long long val;\n} d[Maxn];\nint n, m;\nvector<int> save_val[5];\nint main() {\n scanf(\"%d%d\", &n, &m);\n for (int i = 1; i <= n; ++i) {\n int w, v;\n scanf(\"%d%d\", &w, &v);\n save_val[w]....
Codeforces/833/A
# The Meaningless Game <image> Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting. The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. Af...
[ { "input": { "stdin": "6\n2 4\n75 45\n8 8\n16 16\n247 994\n1000000000 1000000\n" }, "output": { "stdout": "Yes\nYes\nYes\nNo\nNo\nYes\n" } }, { "input": { "stdin": "1\n1062961 1031\n" }, "output": { "stdout": "Yes\n" } }, { "input": { "stdin"...
{ "tags": [ "math", "number theory" ], "title": "The Meaningless Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<long long int> cube;\nint main() {\n int t, u, v;\n long long int x;\n for (long long int i = 1; i <= 1000000; i++) cube.push_back(i * i * i);\n scanf(\"%d\", &t);\n while (t--) {\n scanf(\"%d %d\", &u, &v);\n x = (long long int)u * v;\n i...
Codeforces/853/D
# Michael and Charging Stations Michael has just bought a new electric car for moving across city. Michael does not like to overwork, so each day he drives to only one of two his jobs. Michael's day starts from charging his electric car for getting to the work and back. He spends 1000 burles on charge if he goes to t...
[ { "input": { "stdin": "6\n2000 2000 2000 2000 2000 1000\n" }, "output": { "stdout": "10000" } }, { "input": { "stdin": "3\n1000 2000 1000\n" }, "output": { "stdout": "3700" } }, { "input": { "stdin": "22\n1000 1000 1000 1000 1000 1000 1000 10...
{ "tags": [ "binary search", "dp", "greedy" ], "title": "Michael and Charging Stations" }
{ "cpp": "#include <bits/stdc++.h>\nconst int N = 3e5 + 10;\nusing namespace std;\nint n, A[N];\nint dp[2][50];\nint main() {\n memset(dp, 0x3f, sizeof(dp));\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; ++i) {\n scanf(\"%d\", &A[i]);\n A[i] /= 100;\n }\n int s = 0, t = 1;\n dp[0][0] = 0;\n for (int i =...