id
stringlengths
11
112
content
stringlengths
42
13k
test
listlengths
0
565
labels
dict
canonical_solution
dict
AIZU/p00819
# Unreliable Message The King of a little Kingdom on a little island in the Pacific Ocean frequently has childish ideas. One day he said, “You shall make use of a message relaying game when you inform me of something.” In response to the King’s statement, six servants were selected as messengers whose names were Mr. J...
[ { "input": { "stdin": "5\nAJMP\naB23d\nE\n86AE\nAM\n6\nJPEM\nWaEaETC302Q\nCP\nrTurnAGundam1isdefferentf" }, "output": { "stdout": "32Bad\nAE86\n7\nEC302QTWaEa\nTurnAGundam0isdefferentfr" } }, { "input": { "stdin": "5\nPMJA\naB4d3\nE\nE@67\nMA\n0\nMPJE\nW`EaETC302Q\nPC\nrTur...
{ "tags": [], "title": "Unreliable Message" }
{ "cpp": "//1240\n#include<iostream>\n#include<string>\n#include<algorithm>\nusing namespace std;\nchar table[10]={'0','1','2','3','4','5','6','7','8','9'};\nstring now;\nvoid A(){\nreverse(now.begin(),now.end());\n}\nvoid J(){\nchar tmp=now[0];\nfor(int i=0;i<now.size()-1;i++)now[i]=now[i+1];\nnow[now.size()-1]=tmp;...
AIZU/p00950
# Infallibly Crack Perplexing Cryptarithm Example Input ACM Output 0
[ { "input": { "stdin": "ACM" }, "output": { "stdout": "0" } }, { "input": { "stdin": "FBC" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "NC@" }, "output": { "stdout": "0\n" } }, { "input": { "stdin"...
{ "tags": [], "title": "Infallibly Crack Perplexing Cryptarithm" }
{ "cpp": "#include <bits/stdc++.h>\n#define r(i,n) for(int i=0;i<n;i++)\n#define int long long\nusing namespace std;\ntypedef pair<int,int>P;\n\nbool OK(string s){\n int cnt=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='-'){\n if(i+1==s.size())return 0;\n if(s[i+1]=='+')return 0;\n if(s[i+1]=='*')...
AIZU/p01083
# RedBlue Story At UZIA High School in the sky city AIZU, the club activities of competitive programming are very active. N Red Coders and n Blue Coders belong to this club. One day, during club activities, Red Coder and Blue Coder formed a pair, and from this club activity, n groups participated in a contest called...
[ { "input": { "stdin": "2\n3 3 2\n8 3 2\n3 0\n3 7\n8 0\n8 7" }, "output": { "stdout": "10.0000000000" } }, { "input": { "stdin": "2\n3 3 2\n8 3 2\n0 3\n3 7\n8 0\n8 7" }, "output": { "stdout": "13.8190642862" } }, { "input": { "stdin": "1\n10 1...
{ "tags": [], "title": "RedBlue" }
{ "cpp": "#include<iostream>\n#include<string>\n#include<cstdio>\n#include<vector>\n#include<cmath>\n#include<algorithm>\n#include<functional>\n#include<iomanip>\n#include<queue>\n#include<ciso646>\n#include<utility>\n#include<map>\n#include<complex>\nusing namespace std;\ntypedef long long ll;\nconst ll mod = 100000...
AIZU/p01219
# Private Teacher You are working as a private teacher. Since you are giving lessons to many pupils, you are very busy, especially during examination seasons. This season is no exception in that regard. You know days of the week convenient for each pupil, and also know how many lessons you have to give to him or her....
[ { "input": { "stdin": "2 2\n6 3\nMonday Tuesday Wednesday\n8 4\nThursday Friday Saturday Sunday\n2 2\n7 3\nMonday Tuesday Wednesday\n9 4\nThursday Friday Saturday Sunday\n0 0" }, "output": { "stdout": "Yes\nNo" } }, { "input": { "stdin": "2 2\n12 3\nMonday Tuesday Wednesday...
{ "tags": [], "title": "Private Teacher" }
{ "cpp": "#include <iostream>\n#include <vector>\n#include <map>\n#include <cstring>\n#include <climits>\nusing namespace std;\n \ntypedef long long ll;\n\n#define MAX_V 1000\n#define INF 100000000000LL\n\nll V;\nll cap[MAX_V][MAX_V];\nbool used[MAX_V];\n\nll dfs(ll v, ll g, ll f){\n if(v == g) return f;\n used[v] ...
AIZU/p01353
# Rabbit Plays Games! A rabbit is playing a role-playing game. Just before entering the castle, he was ambushed by an enemy! It was a battle between one hero operated by a rabbit and n enemies. Each character has four stats, health hi, attack power ai, defense power di, and agility si. I = 0 is the information of the...
[ { "input": { "stdin": "1\n1 1 1 1\n10000 10000 10000 10000" }, "output": { "stdout": "-1" } }, { "input": { "stdin": "2\n10 3 1 2\n2 4 1 3\n2 2 1 1" }, "output": { "stdout": "4" } }, { "input": { "stdin": "1\n1 -1 1 1\n00001 00111 01001 11101...
{ "tags": [], "title": "Rabbit Plays Games!" }
{ "cpp": "#include <cstdio>\n#include <iostream>\n#include <algorithm>\n#include <map>\n#define F first\n#define S second\nusing namespace std;\nint n;\n//long long int ph,pa,pd,ps;\nlong long int h[44444];\nlong long int a[44444];\nlong long int b[44444];\nlong long int s[44444];\npair<long double,int> p[44444];\nlo...
AIZU/p01535
# Markup language has Declined E: Markup language has declined It's been centuries since we humans have been declining slowly. The earth may already belong to "Progurama". Programa-sans with an average height of 170 cm, 7 heads, high intelligence, and loves Kodingu. I have returned to my hometown of Nibunki, becoming...
[ { "input": { "stdin": "1\nindex.dml\n<dml><title>Markup language has Declined</title><br>Programmers world</dml>\n1\n15 3 0 index" }, "output": { "stdout": "Markup language\n has Declined..\nProgrammers wor" } }, { "input": { "stdin": "1\nindex.dml\n<dml><title>Markup aangu...
{ "tags": [], "title": "Markup language has Declined" }
{ "cpp": "#include <cstdio>\n#include <cstdint>\n#include <cctype>\n#include <cassert>\n#include <algorithm>\n#include <vector>\n#include <string>\n#include <map>\n#include <regex>\n\n#define fprintf(...) void(0)\n\nstd::string join(const std::vector<std::string> &ss, char ch) {\n if (ss.empty()) return \"\";\n std...
AIZU/p01691
# Disappear Drive E --Disappear Drive Story The person in D likes basketball, but he likes shooting, so other techniques are crazy. I'm not particularly good at dribbling, and when I enter the opponent's defensive range, I always get the ball stolen. So I decided to come up with a deadly dribble that would surely pu...
[ { "input": { "stdin": "1 0\n25 47 10" }, "output": { "stdout": "96.2027355887" } }, { "input": { "stdin": "2 1\n-1 111 0" }, "output": { "stdout": "94.0000000000\n" } }, { "input": { "stdin": "9 -1\n0 126 8" }, "output": { "stdo...
{ "tags": [], "title": "Disappear Drive" }
{ "cpp": "#include<cstdio>\n#include<iostream>\n#include<vector>\n#include<complex>\n#include<string>\n#include<algorithm>\n#include<set>\n#include<cassert>\n\nusing namespace std;\n\n#define reps(i,f,n) for(int i=f;i<int(n);i++)\n#define rep(i,n) reps(i,0,n)\n\n\nconst double EPS = 1e-8;\nconst double INF = 1e12;\n\...
AIZU/p01835
# Donut Decoration Example Input 3 2 3 1 2 1 2 3 2 3 3 1 Output 1
[ { "input": { "stdin": "3 2\n3\n1 2 1\n2 3 2\n3 3 1" }, "output": { "stdout": "1" } }, { "input": { "stdin": "3 2\n1\n1 2 1\n2 3 0\n3 3 1" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "2 2\n3\n1 3 1\n2 3 2\n3 3 1" }, "outp...
{ "tags": [], "title": "Donut Decoration" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nconstexpr int invalid = -2;\n\nclass square_root_decomposition {\npublic:\n square_root_decomposition(int N)\n : B(sqrt(N))\n {\n N = (N + B - 1) / B * B;\n before.assign(N / B, -1);\n after.assign(N / B, -1);\n donut...
AIZU/p01970
# The Diversity of Prime Factorization D: The Diversity of Prime Factorization Problem Ebi-chan has the FACTORIZATION MACHINE, which can factorize natural numbers M (greater than 1) in O ($ \ log $ M) time! But unfortunately, the machine could display only digits and white spaces. In general, we consider the factor...
[ { "input": { "stdin": "3\n2 3 3" }, "output": { "stdout": "2" } }, { "input": { "stdin": "3\n2 4 0" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "3\n2 2 1" }, "output": { "stdout": "0\n" } }, { "input": ...
{ "tags": [], "title": "The Diversity of Prime Factorization" }
{ "cpp": "#include<iostream>\n#include<utility>\nusing namespace std;\n\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n\nconstexpr int Q_MAX = 1000006;\nbool is_prime[Q_MAX];\nvoid init() {\n rep(i, Q_MAX) is_prime[i] = true;\n is_prime[0] = is_prime[1] = false;\n rep(i, Q_MAX) if (i >= 2) {\n ...
AIZU/p02117
# Picnic Problem Tomorrow is finally the day of the excursion to Maizu Elementary School. Gatcho, who attends Maizu Elementary School, noticed that he had forgotten to buy tomorrow's sweets because he was so excited. Gaccho wants to stick to sweets so that he can enjoy the excursion to the fullest. Gaccho gets a $ ...
[ { "input": { "stdin": "3 10 10\n1\n1 1 1\n1\n3 3 3\n1\n5 5 5\n0 1 0\n1 0 0\n0 1 0" }, "output": { "stdout": "10" } }, { "input": { "stdin": "4 59 40\n1\n7 6 3\n1\n10 3 9\n2\n9 8 5\n7 6 10\n4\n8 2 9\n1 7 1\n7 7 9\n1 2 3\n0 28 7 26\n14 0 10 24\n9 6 0 21\n9 24 14 0" }, ...
{ "tags": [], "title": "Picnic" }
{ "cpp": "#include <bits/stdc++.h>\n#define MOD 1000000007LL\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\n\nstruct data{\n\tint bit;\n\tint cost;\n\tint v;\n\tdata(){}\n\tdata(int vv,int bb,int cc){\n\t\tv=vv;\n\t\tcost=cc;\n\t\tbit=bb;\n\t}\n\tbool operator<(const data &dat)const{\n\t\tret...
AIZU/p02257
# Prime Numbers A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a program which reads a list of N integers and prints the number of prime numbers in the list. Constraints 1 ≤ N ≤ 10000 2 ≤ a...
[ { "input": { "stdin": "5\n2\n3\n4\n5\n6" }, "output": { "stdout": "3" } }, { "input": { "stdin": "11\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17" }, "output": { "stdout": "4" } }, { "input": { "stdin": "11\n14\n8\n9\n10\n30\n12\n23\n4\n9\n13\n17...
{ "tags": [], "title": "Prime Numbers" }
{ "cpp": "#include <iostream>\n#include <math.h>\nusing namespace std;\n\nint prime(int x){\n int i=3;\n if(x==2)return 1;\n else if(x<2 || x%2==0)return 0;\n for(i; i<=sqrt(x); i+=2)if(x%i==0)return 0;\n\n return 1;\n}\n\nint main(){\n int i,n,x,count=0;\n cin>>n;\n for(i=0; i<n; i++){\n cin>>x;\n count+=p...
AIZU/p02405
# Print a Chessboard Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm. .#.#.#.#. .#.#.#.#.# .#.#.#.#. .#.#.#.#.# .#.#.#.#. .#.#.#.#.# Note that the top left corner should be drawn by '#'. Constrai...
[ { "input": { "stdin": "3 4\n5 6\n3 3\n2 2\n1 1\n0 0" }, "output": { "stdout": "#.#.\n.#.#\n#.#.\n\n#.#.#.\n.#.#.#\n#.#.#.\n.#.#.#\n#.#.#.\n\n#.#\n.#.\n#.#\n\n#.\n.#\n\n#" } }, { "input": { "stdin": "1 3\n5 9\n3 0\n4 3\n-1 1\n0 0" }, "output": { "stdout": "#.#\...
{ "tags": [], "title": "Print a Chessboard" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\n\n\nint main()\n{\n while(true)\n {\n int h,w;\n cin>>h>>w;\n if(h==0 && w==0) break;\n for (int i = 0; i < h; ++i)\n {\n for (int j = 0; j < w; ++j)\n {\n cout<<((i+j)%2==0? \"#\":\".\");\n }\n cout<<endl;\n }\n ...
CodeChef/anuwta
There are N+1 lights. Lights are placed at (0, 0), (1, 0), (2, 0) ... (N, 0). Initially all the lights are on. You want to turn off all of them one after one. You want to follow a special pattern in turning off the lights. You will start at (0, 0). First, you walk to the right most light that is on, turn it off. Th...
[ { "input": { "stdin": "2\n1\n2" }, "output": { "stdout": "2\n5\n" } }, { "input": { "stdin": "2\n-1\n35" }, "output": { "stdout": "-1\n665\n" } }, { "input": { "stdin": "2\n7\n1" }, "output": { "stdout": "35\n2\n" } }, {...
{ "tags": [], "title": "anuwta" }
{ "cpp": null, "java": null, "python": "t=int(raw_input())\nfor z in range (t):\n\tn=int(raw_input())\n\tprint (n*(n+3))/2" }
CodeChef/chsparr
Chef has a an array A consisting of N elements. He wants to add some elements into the array as per the below mentioned process. After each minute, Chef iterates over the array in order from left to right, and takes every two neighbouring pair of elements, say x and y, he adds a new element x + y in the middle of eleme...
[ { "input": { "stdin": "2\n3 1 1 5\n1 6 9\n3 2 6 7\n1 6 9" }, "output": { "stdout": "38\n36" } } ]
{ "tags": [], "title": "chsparr" }
{ "cpp": null, "java": null, "python": "p=10**9+7;\n\ndef modpow(x,n):\n\tif(n==0):\n\t\treturn 1;\n\telif(n%2==0):\n\t\tm=modpow(x,n/2);\n\t\treturn (m*m)%p;\n\telse:\n\t\tm=modpow(x,n/2);\n\t\treturn (m*m*x)%p;\t\t\n\ndef modinv(x):\n\treturn modpow(x,p-2);\n\n\n\ninv2=modinv(2);\nmods=[1]*31;\n\nmod2s=[1]*31;\...
CodeChef/etmx05
Middle Strings Miss Roma has got a new job in a computer agency. To test her skills the manager has appointed her with a small project. She will get a string of any length but of ODD length and what she has to do is finding a center string of length 3 in the original string. For Example: She gets a string 'CANDY' the...
[ { "input": { "stdin": "CANDY" }, "output": { "stdout": "AND" } }, { "input": { "stdin": "SOLVING" }, "output": { "stdout": "LVI" } } ]
{ "tags": [], "title": "etmx05" }
{ "cpp": null, "java": null, "python": "x=raw_input()\nl=len(x)\nif l>=3:\n l=l/2\n print x[l-1:l+2]\nelse:\n print \"0\"" }
CodeChef/laddu
You might have heard about our new goodie distribution program aka the "Laddu Accrual System". This problem is designed to give you a glimpse of its rules. You can read the page once before attempting the problem if you wish, nonetheless we will be providing all the information needed here itself. Laddu Accrual Syste...
[ { "input": { "stdin": "2\n4 INDIAN\nCONTEST_WON 1\nTOP_CONTRIBUTOR\nBUG_FOUND 100\nCONTEST_HOSTED\n4 NON_INDIAN\nCONTEST_WON 1\nTOP_CONTRIBUTOR\nBUG_FOUND 100\nCONTEST_HOSTED" }, "output": { "stdout": "3\n1\n" } }, { "input": { "stdin": "2\n4 INDIAN\nCONTEST_WON 1\nTOP_CONT...
{ "tags": [], "title": "laddu" }
{ "cpp": null, "java": null, "python": "def codechef_8():\n testcase=int(raw_input())\n while(testcase):\n user_info=raw_input().strip().split(\" \")\n user=int(user_info[0])\n laddus=0\n if(user_info[1]=='INDIAN'):\n redeem=200\n else:\n redeem=400\n...
CodeChef/pcsc1
George is getting tired of the decimal number system. He intends to switch and use the septenary (base 7) number system for his future needs. Write a program to help George start his transformation into the septenary number system by taking in a list of decimal numbers and print out the corresponding septenary number...
[ { "input": { "stdin": "1 2 88 42 99 -1" }, "output": { "stdout": "1 2 154 60 201" } }, { "input": { "stdin": "1 2 101 36 50 -1" }, "output": { "stdout": "1 2 203 51 101 \n" } }, { "input": { "stdin": "1 0 001 39 33 -1" }, "output": { ...
{ "tags": [], "title": "pcsc1" }
{ "cpp": null, "java": null, "python": "import sys\ndef numberToBase(n, b):\n temp=''\n while n:\n temp+=str(n % b)\n n /= b\n return temp\na=map(int,sys.stdin.readline().split())\ni=0\nwhile a[i]!=-1:\n s=numberToBase(a[i],7)\n s=s[::-1]\n sys.stdout.write(s+\" \")\n i+=1" }
CodeChef/stadium
The bustling town of Siruseri has just one sports stadium. There are a number of schools, colleges, sports associations, etc. that use this stadium as the venue for their sports events. Anyone interested in using the stadium has to apply to the Manager of the stadium indicating both the starting date (a positive inte...
[ { "input": { "stdin": "4\n2 5\n9 7\n15 6\n9 3" }, "output": { "stdout": "3" } } ]
{ "tags": [], "title": "stadium" }
{ "cpp": null, "java": null, "python": "#Manage maximum events given start date and duration for a case\ndef main():\n\t#f = open('C:\\\\Users\\\\GLCR3257\\\\Desktop\\\\pyLogics\\\\test.txt','r')\n\tN=int(raw_input())\n\t#print N\n\tE=[]\n\tfor i in range(N):\n\t\tstart,duration=map(int,raw_input().split())\n\t\t...
Codeforces/1007/E
# Mini Metro In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are n stations on the line, a_i people are waiting for the train at the i-th station at the beginning of the game. The game starts at the beginning of the 0-th hour. At the end...
[ { "input": { "stdin": "3 3 10\n2 4 10\n3 3 9\n4 2 8\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "4 10 5\n1 1 1\n1 0 1\n0 5 8\n2 7 100\n" }, "output": { "stdout": "12\n" } }, { "input": { "stdin": "2 5 1\n0 5 6\n0 2 7\n" }...
{ "tags": [ "dp" ], "title": "Mini Metro" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long int MAXN = 251, inf = 1e16;\nlong long int n, m, t, kk, rr, rx;\nlong long int x[MAXN], y[MAXN], v[MAXN], px[MAXN], py[MAXN];\nlong long int d[MAXN][MAXN][2], g[MAXN][MAXN][2];\ninline long long int read() {\n register long long int num = 0, n...
Codeforces/1030/E
# Vasya and Good Sequences Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2),...
[ { "input": { "stdin": "4\n1 2 1 16\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "3\n6 7 14\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "5\n1000000000000000000 352839520853234088 175235832528365792 753467583475...
{ "tags": [ "bitmasks", "dp" ], "title": "Vasya and Good Sequences" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int mod = 1e9 + 7;\nconst int MAXN = 3e5 + 100;\nint n, b[MAXN], cnt[2][MAXN];\nlong long a[MAXN];\nint main() {\n int tt = 1;\n while (tt--) {\n long long res = 0;\n int suff = 0;\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> a...
Codeforces/1053/C
# Putting Boxes Together There is an infinite line consisting of cells. There are n boxes in some cells of this line. The i-th box stands in the cell a_i and has weight w_i. All a_i are distinct, moreover, a_{i - 1} < a_i holds for all valid i. You would like to put together some boxes. Putting together boxes with in...
[ { "input": { "stdin": "5 8\n1 2 6 7 10\n1 1 1 1 2\n1 1\n1 5\n1 3\n3 5\n-3 5\n-1 10\n1 4\n2 5\n" }, "output": { "stdout": "0\n10\n3\n4\n18\n7\n" } }, { "input": { "stdin": "4 10\n1 333333333 666666666 1000000000\n1000000000 1000000000 1000000000 1000000000\n1 1\n1 2\n1 3\n1 ...
{ "tags": [ "data structures" ], "title": "Putting Boxes Together" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int M = 1e9 + 7;\nclass FenwickTree {\n public:\n FenwickTree(int _n) : n(_n), data(n, 0) {}\n void add(int x, long long v) {\n for (; x < n; x |= x + 1) {\n data[x] += v;\n }\n }\n long long get(int x) {\n long long res = 0;\n for (...
Codeforces/1075/D
# Intersecting Subtrees You are playing a strange game with Li Chen. You have a tree with n nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices from 1 to n. Neither of you know the other's labelling of the tree. You and Li Chen each chose a subt...
[ { "input": { "stdin": "1\n3\n1 2\n2 3\n1\n1\n1\n2\n2\n1\n" }, "output": { "stdout": "B 2\nA 1\nC -1\n" } }, { "input": { "stdin": "2\n6\n1 2\n1 3\n1 4\n4 5\n4 6\n4\n1 3 4 5\n3\n3 5 2\n3\n6\n1 2\n1 3\n1 4\n4 5\n4 6\n3\n1 2 3\n3\n4 1 6\n5\n" }, "output": { "stdo...
{ "tags": [ "dfs and similar", "interactive", "trees" ], "title": "Intersecting Subtrees" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing vi = vector<int>;\nusing vvi = vector<vi>;\nstruct Testcase {\n set<int> me, him;\n vvi g;\n vi near;\n void dfs(int from, int nearest, int p = -1) {\n if (me.count(from) != 0) {\n near[from] = from;\n } else {\n near[from] = nearest;...
Codeforces/1096/E
# The Top Scorer Hasan loves playing games and has recently discovered a game called TopScore. In this soccer-like game there are p players doing penalty shoot-outs. Winner is the one who scores the most. In case of ties, one of the top-scorers will be declared as the winner randomly with equal probability. They have...
[ { "input": { "stdin": "10 30 10\n" }, "output": { "stdout": "85932500\n" } }, { "input": { "stdin": "2 6 3\n" }, "output": { "stdout": "124780545\n" } }, { "input": { "stdin": "5 20 11\n" }, "output": { "stdout": "1\n" } }...
{ "tags": [ "combinatorics", "dp", "math", "probabilities" ], "title": "The Top Scorer" }
{ "cpp": "#include <bits/stdc++.h>\ninline long long read() {\n long long x = 0;\n char c = getchar(), f = 1;\n for (; c < '0' || '9' < c; c = getchar())\n if (c == '-') f = -1;\n for (; '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';\n return x * f;\n}\ninline void write(long long x) {\n static c...
Codeforces/1117/F
# Crisp String You are given a string of length n. Each character is one of the first p lowercase Latin letters. You are also given a matrix A with binary values of size p × p. This matrix is symmetric (A_{ij} = A_{ji}). A_{ij} = 1 means that the string can have the i-th and j-th letters of Latin alphabet adjacent. ...
[ { "input": { "stdin": "7 3\nabacaba\n1 1 1\n1 0 0\n1 0 0\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "7 3\nabacaba\n0 1 1\n1 0 0\n1 0 0\n" }, "output": { "stdout": "7" } }, { "input": { "stdin": "7 4\nbacadab\n0 1 1 1\n1 0 0 0\...
{ "tags": [ "bitmasks", "dp" ], "title": "Crisp String" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 0x3f3f3f3f, N = 1e6 + 10;\nint n, p, P, ans = INF, a[N], tmp[1 << 17], f[1 << 17], ln[1 << 17], g[17][17];\nchar s[N];\nvoid dfs0(int x) {\n if (tmp[x]) return;\n tmp[x] = 1;\n for (int y = x; y; y ^= y & -y) dfs0(x ^ y & -y);\n}\nvoid dfs1(...
Codeforces/1144/B
# Parity Alternated Deletions Polycarp has an array a consisting of n integers. He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains n-1 elements). For each of the next moves he chooses any element...
[ { "input": { "stdin": "2\n1000000 1000000\n" }, "output": { "stdout": "1000000\n" } }, { "input": { "stdin": "6\n5 1 2 4 6 3\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "5\n1 5 7 8 2\n" }, "output": { "stdout": ...
{ "tags": [ "greedy", "implementation", "sortings" ], "title": "Parity Alternated Deletions" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long int n;\n cin >> n;\n long int var;\n vector<long int> even, odd, even1, odd1;\n while (n--) {\n cin >> var;\n if (var % 2 == 0)\n even.push_back(var);\n else\n odd.push_back(var);\n }\n sort(even.begin(), even.end(...
Codeforces/1165/A
# Remainder You are given a huge decimal number consisting of n digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1. You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you...
[ { "input": { "stdin": "11 5 2\n11010100101\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "11 5 1\n11010100101\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "6 4 2\n100010\n" }, "output": { "stdout":...
{ "tags": [ "implementation", "math" ], "title": "Remainder" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n int n, x, y;\n cin >> n >> x >> y;\n string s;\n cin >> s;\n int ans = 0;\n for (int i = n - 1; i >= n - x; --i) {\n if (i == n - y - 1 || i == n - x - 1) {\n if (s[i] != '1') ++a...
Codeforces/1184/B3
# The Doctor Meets Vader (Hard) The rebels have saved enough gold to launch a full-scale attack. Now the situation is flipped, the rebels will send out the spaceships to attack the Empire bases! The galaxy can be represented as an undirected graph with n planets (nodes) and m wormholes (edges), each connecting two pl...
[ { "input": { "stdin": "6 7\n1 2\n2 3\n3 4\n4 6\n6 5\n4 4\n3 6\n4 2 2\n1 10 2 5\n3 8 2 7\n5 1 0 2\n6 5 4 1\n3 7 6\n5 2 3\n4 2\n3 2\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "1 0\n1 1 0\n1 446844829 77109657 780837560\n1 754808995 539371459\n" }, "o...
{ "tags": [ "flows", "shortest paths" ], "title": "The Doctor Meets Vader (Hard)" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct Edge {\n int u, v;\n long long cap, flow;\n Edge() {}\n Edge(int u, int v, long long cap) : u(u), v(v), cap(cap), flow(0) {}\n};\nstruct Dinic {\n int N;\n vector<Edge> E;\n vector<vector<int>> g;\n vector<int> d, pt;\n Dinic(int N) : N(N), E(0...
Codeforces/1202/C
# You Are Given a WASD-string... You have a string s — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: * 'W' — move one cell up; * 'S' — move one cell down; * 'A' — move one cell left; * 'D' — move one cell right. Let Grid...
[ { "input": { "stdin": "3\nDSAWWAW\nD\nWA\n" }, "output": { "stdout": "8\n2\n4\n" } }, { "input": { "stdin": "3\nAADWWSW\nD\nAW\n" }, "output": { "stdout": "6\n2\n4\n" } }, { "input": { "stdin": "3\nDAAWWSW\nD\nWA\n" }, "output": { ...
{ "tags": [ "brute force", "data structures", "dp", "greedy", "implementation", "math", "strings" ], "title": "You Are Given a WASD-string..." }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long int;\nusing int64 = long long int;\ntemplate <typename T>\nvoid chmax(T &a, T b) {\n a = max(a, b);\n}\ntemplate <typename T>\nvoid chmin(T &a, T b) {\n a = min(a, b);\n}\ntemplate <typename T>\nvoid chadd(T &a, T b) {\n a = a + b;\n}\n...
Codeforces/1219/H
# Function Composition We are definitely not going to bother you with another generic story when Alice finds about an array or when Alice and Bob play some stupid game. This time you'll get a simple, plain text. First, let us define several things. We define function F on the array A such that F(i, 1) = A[i] and F(i,...
[ { "input": { "stdin": "10\n2 3 1 5 6 4 2 10 7 7\n5\n10 1\n5 7\n10 6\n1 1\n10 8\n" }, "output": { "stdout": "3\n0\n1\n1\n0\n" } }, { "input": { "stdin": "10\n2 3 1 5 5 2 2 10 7 7\n5\n10 1\n5 7\n10 6\n0 2\n10 8\n" }, "output": { "stdout": "3\n0\n0\n1\n0\n" }...
{ "tags": [ "dfs and similar" ], "title": "Function Composition" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int Maxn = 2e5;\nstruct DSU {\n int fa[Maxn + 5];\n void init(int n) {\n for (int i = 1; i <= n; i++) fa[i] = i;\n }\n int find(int u) { return u == fa[u] ? u : fa[u] = find(fa[u]); }\n bool unite(int u, int v) {\n u = find(u), v = find(v);\n ...
Codeforces/1244/C
# The Football Season The football season has just ended in Berland. According to the rules of Berland football, each match is played between two teams. The result of each match is either a draw, or a victory of one of the playing teams. If a team wins the match, it gets w points, and the opposing team gets 0 points. ...
[ { "input": { "stdin": "30 60 3 1\n" }, "output": { "stdout": "20 0 10\n" } }, { "input": { "stdin": "20 0 15 5\n" }, "output": { "stdout": "0 0 20\n" } }, { "input": { "stdin": "10 51 5 4\n" }, "output": { "stdout": "-1\n" }...
{ "tags": [ "brute force", "math", "number theory" ], "title": "The Football Season" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class c>\nstruct rge {\n c b, e;\n};\ntemplate <class c>\nrge<c> range(c i, c j) {\n return rge<c>{i, j};\n}\ntemplate <class c>\nauto dud(c* x) -> decltype(cerr << *x, 0);\ntemplate <class c>\nchar dud(...);\nstruct debug {\n template <class c>\n...
Codeforces/1264/A
# Beautiful Regional Contest So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved p_i problems. Since the participants are primarily sorted by the number of solved problems, then p_1 ≥ p_2 ≥ ......
[ { "input": { "stdin": "5\n12\n5 4 4 3 2 2 1 1 1 1 1 1\n4\n4 3 2 1\n1\n1000000\n20\n20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\n32\n64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11\n" }, "output": { "stdout": "1 2 3\n0 0 0\n0 0 0\n1 2 7\n...
{ "tags": [ "greedy", "implementation" ], "title": "Beautiful Regional Contest" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long MOD = 1e9 + 7;\nconst double pi = acos(-1.0);\nint32_t main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n long long tt;\n cin >> tt;\n while (tt--) {\n long long n;\n cin >> n;\n vector<long long> v;\n map<long...
Codeforces/1285/C
# Fadi and LCM Today, Osama gave Fadi an integer X, and Fadi was wondering about the minimum possible value of max(a, b) such that LCM(a, b) equals X. Both a and b should be positive integers. LCM(a, b) is the smallest positive integer that is divisible by both a and b. For example, LCM(6, 8) = 24, LCM(4, 12) = 12, L...
[ { "input": { "stdin": "1\n" }, "output": { "stdout": "1 1\n" } }, { "input": { "stdin": "4\n" }, "output": { "stdout": "1 4\n" } }, { "input": { "stdin": "6\n" }, "output": { "stdout": "2 3\n" } }, { "input": { ...
{ "tags": [ "brute force", "math", "number theory" ], "title": "Fadi and LCM" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 1e9 + 10;\nconst long long int LINF = 1LL * INF * INF;\nconst int MAXN = 2e5 + 10;\nconst int MAXM = 5e3 + 10;\npriority_queue<int> pq;\nvector<vector<int> > graph;\nqueue<int> que;\nlong long int mygcd(long long int a, long long int b) {\n re...
Codeforces/1304/E
# 1-Trees and Queries Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree? Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too man...
[ { "input": { "stdin": "5\n1 2\n2 3\n3 4\n4 5\n5\n1 3 1 2 2\n1 4 1 3 2\n1 4 1 3 3\n4 2 3 3 9\n5 2 3 3 9\n" }, "output": { "stdout": "YES\nYES\nNO\nYES\nNO\n" } }, { "input": { "stdin": "9\n3 9\n3 4\n7 2\n6 9\n5 3\n6 2\n8 3\n1 9\n10\n8 4 8 2 5\n9 2 7 4 4\n8 5 7 3 3\n1 2 3 8 4...
{ "tags": [ "data structures", "dfs and similar", "shortest paths", "trees" ], "title": "1-Trees and Queries" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 200050;\nint n, q;\nstruct edge {\n int v, nxt;\n} e[N << 1];\nint head[N], ecnt;\ninline void ad(int u, int v) {\n e[++ecnt].v = v;\n e[ecnt].nxt = head[u];\n head[u] = ecnt;\n}\nint dep[N], f[N][23];\nvoid dfs(int u, int father) {\n dep[u]...
Codeforces/1328/F
# Make k Equal You are given the array a consisting of n elements and the integer k ≤ n. You want to obtain at least k equal elements in the array a. In one move, you can make one of the following two operations: * Take one of the minimum elements of the array and increase its value by one (more formally, if the m...
[ { "input": { "stdin": "6 5\n1 2 2 4 2 3\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "7 5\n3 3 2 1 1 1 3\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "21 6\n12 15 14 4 4 7 2 4 11 1 15 4 12 11 12 8 11 12 3 4 4\n...
{ "tags": [ "greedy" ], "title": "Make k Equal" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, k;\nint nums[55];\nint divs(int num, int target) {\n if (num < target) return 0x3f3f3f3f;\n if (num == target) return 0;\n return 1 + divs(num / 2, target);\n}\nint main() {\n cin >> n >> k;\n int max_num = 0;\n for (int i = 0; i < n; i++) {\n ...
Codeforces/1348/F
# Phoenix and Memory Phoenix is trying to take a photo of his n friends with labels 1, 2, ..., n who are lined up in a row in a special order. But before he can take the photo, his friends get distracted by a duck and mess up their order. Now, Phoenix must restore the order but he doesn't remember completely! He only...
[ { "input": { "stdin": "4\n1 3\n2 4\n3 4\n2 3\n" }, "output": { "stdout": "NO\n1 3 4 2\n1 2 4 3\n" } }, { "input": { "stdin": "4\n4 4\n1 3\n2 4\n3 4\n" }, "output": { "stdout": "YES\n4 1 2 3 " } }, { "input": { "stdin": "4\n3 4\n1 2\n3 4\n1 2\...
{ "tags": [ "data structures", "dfs and similar", "graphs", "greedy" ], "title": "Phoenix and Memory" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T1, class T2>\nostream &operator<<(ostream &os, pair<T1, T2> &p);\ntemplate <class T>\nostream &operator<<(ostream &os, vector<T> &v);\ntemplate <class T>\nostream &operator<<(ostream &os, set<T> &v);\ntemplate <class T1, class T2>\nostream &op...
Codeforces/1369/D
# TediousLee Lee tried so hard to make a good div.2 D problem to balance his recent contest, but it still doesn't feel good at all. Lee invented it so tediously slow that he managed to develop a phobia about div.2 D problem setting instead. And now he is hiding behind the bushes... Let's define a Rooted Dead Bush (RD...
[ { "input": { "stdin": "7\n1\n2\n3\n4\n5\n100\n2000000\n" }, "output": { "stdout": "0\n0\n4\n4\n12\n990998587\n804665184\n" } }, { "input": { "stdin": "3\n1234567\n1268501\n1268499\n" }, "output": { "stdout": "788765312\n999997375\n999999350\n" } }, { ...
{ "tags": [ "dp", "graphs", "greedy", "math", "trees" ], "title": "TediousLee" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long dp[2000006];\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n dp[1] = 0;\n dp[2] = 0;\n for (long long int i = 3; i < 2000006; i++) {\n long long p = (dp[i - 1] % 1000000007);\n p += ((2 * dp[i - 2]) % 1000...
Codeforces/1391/C
# Cyclic Permutations A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array). Consider a ...
[ { "input": { "stdin": "4\n" }, "output": { "stdout": "16\n" } }, { "input": { "stdin": "583291\n" }, "output": { "stdout": "135712853\n" } }, { "input": { "stdin": "66\n" }, "output": { "stdout": "257415584\n" } }, { ...
{ "tags": [ "combinatorics", "dp", "graphs", "math" ], "title": "Cyclic Permutations " }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long pow2(long long a, long long b, long long m) {\n long long ans = 1;\n while (b > 0) {\n if (b & 1) {\n ans = (ans * a) % m;\n }\n a = (a * a) % m;\n b >>= 1;\n }\n return ans;\n}\nlong long inv(long long a, long long b) {\n retur...
Codeforces/1413/F
# Roads and Ramen In the Land of Fire there are n villages and n-1 bidirectional road, and there is a path between any pair of villages by roads. There are only two types of roads: stone ones and sand ones. Since the Land of Fire is constantly renovating, every morning workers choose a single road and flip its type (s...
[ { "input": { "stdin": "5\n1 2 0\n1 3 0\n3 5 0\n3 4 0\n5\n3\n4\n1\n3\n4\n" }, "output": { "stdout": "3\n2\n3\n3\n2\n" } }, { "input": { "stdin": "10\n5 7 0\n2 10 1\n1 5 0\n6 8 0\n4 9 1\n2 5 1\n10 8 0\n2 3 1\n4 2 1\n10\n9\n9\n9\n5\n2\n5\n7\n2\n3\n2\n" }, "output": { ...
{ "tags": [ "data structures", "trees" ], "title": "Roads and Ramen" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ii = pair<int, int>;\nusing vi = vector<int>;\nstruct Node {\n Node *l = 0, *r = 0;\n int lo, hi;\n int mx[2];\n bool lz = 0;\n Node(int _lo, int _hi) : lo(_lo), hi(_hi) { mx[0] = mx[1] = 0; }\n Node(ii v[], int _lo, int _hi)...
Codeforces/1431/I
# Cyclic Shifts You are given a matrix consisting of n rows and m columns. The matrix contains lowercase letters of the Latin alphabet. You can perform the following operation any number of times you want to: choose two integers i (1 ≤ i ≤ m) and k (0 < k < n), and shift every column j such that i ≤ j ≤ m cyclically ...
[ { "input": { "stdin": "3 5 4\nabacc\nccbba\nccabc\nabacc\nacbbc\nababa\nacbbc\n" }, "output": { "stdout": "\n0\n2\n1\n2\n" } }, { "input": { "stdin": "6 4 4\ndaac\nbcba\nacad\ncbdc\naaaa\nbcbb\ndcdd\nacba\nbbbb\ndbcd\n" }, "output": { "stdout": "\n3\n1\n2\n-1\...
{ "tags": [ "*special", "strings" ], "title": "Cyclic Shifts" }
{ "cpp": null, "java": null, "python": null }
Codeforces/1455/F
# String and Operations You are given a string s consisting of n characters. These characters are among the first k lowercase letters of the Latin alphabet. You have to perform n operations with the string. During the i-th operation, you take the character that initially occupied the i-th position, and perform one of...
[ { "input": { "stdin": "6\n4 2\nbbab\n7 5\ncceddda\n6 5\necdaed\n7 4\ndcdbdaa\n8 3\nccabbaca\n5 7\neabba\n" }, "output": { "stdout": "\naaaa\nbaccacd\naabdac\naabacad\naaaaaaaa\nabadb\n" } }, { "input": { "stdin": "6\n4 2\nbbab\n7 5\nccdedda\n6 5\necdaed\n7 4\ndcdbdaa\n8 4\n...
{ "tags": [ "dp", "greedy" ], "title": "String and Operations" }
{ "cpp": "# include<bits/stdc++.h>\nusing namespace std;\n# define l long long \n# define db double \n# define rep(i,a,b) for(l i=a;i<b;i++)\n# define vi vector<l>\n# define vvi vector<vi>\n# define vsi vector<set<l> >\n# define pb push_back\n# define mp make_pair\n# define ss second\n# define ff first\n# define pii ...
Codeforces/147/B
# Smile House A smile house is created to raise the mood. It has n rooms. Some of the rooms are connected by doors. For each two rooms (number i and j), which are connected by a door, Petya knows their value cij — the value which is being added to his mood when he moves from room i to room j. Petya wondered whether h...
[ { "input": { "stdin": "4 4\n1 2 -10 3\n1 3 1 -10\n2 4 -10 -1\n3 4 0 -3\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "5 4\n1 2 1 -1\n2 3 1 -1\n3 1 1 -1\n4 5 2 -1\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3 3...
{ "tags": [ "binary search", "graphs", "matrices" ], "title": "Smile House" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int INF = 0x3f3f3f3f;\nconst int MOD7 = (int)1e9 + 7;\ntemplate <class T1, class T2>\nint upmin(T1 &x, T2 v) {\n if (x > v) {\n x = v;\n return 1;\n }\n return 0;\n}\ntemplate <class T1, class T2>\nint upmax(T1 &x, T2 v) {\n if (x < v) {\n x...
Codeforces/1506/F
# Triangular Paths Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The k-th layer of the triangle contains k points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers (r, c) (...
[ { "input": { "stdin": "4\n3\n1 4 2\n1 3 1\n2\n2 4\n2 3\n2\n1 1000000000\n1 1000000000\n4\n3 10 5 8\n2 5 2 4\n" }, "output": { "stdout": "\n0\n1\n999999999\n2\n" } }, { "input": { "stdin": "4\n3\n1 4 2\n1 3 1\n2\n2 5\n2 2\n2\n1 1100000000\n1 1000000000\n4\n3 10 5 8\n2 5 3 4\...
{ "tags": [ "constructive algorithms", "graphs", "math", "shortest paths", "sortings" ], "title": "Triangular Paths" }
{ "cpp": "#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nint main() {\n\tint line;\n\tcin >> line;\n\twhile (line > 0)\n\t{\n int n = 0;\n cin >> n;\n int result = 0;\n vector<pair<int, int>> nodes(n);\n\t\tfor (int i = 0; i < n; ++i) {\n ...
Codeforces/152/E
# Garden Vasya has a very beautiful country garden that can be represented as an n × m rectangular field divided into n·m squares. One beautiful day Vasya remembered that he needs to pave roads between k important squares that contain buildings. To pave a road, he can cover some squares of his garden with concrete. F...
[ { "input": { "stdin": "3 3 2\n1 2 3\n1 2 3\n1 2 3\n1 2\n3 3\n" }, "output": { "stdout": "9\n.X.\n.X.\n.XX\n" } }, { "input": { "stdin": "4 5 4\n1 4 5 1 2\n2 2 2 2 7\n2 4 1 4 5\n3 2 1 7 1\n1 1\n1 5\n4 1\n4 4\n" }, "output": { "stdout": "26\nX..XX\nXXXX.\nX.X..\...
{ "tags": [ "bitmasks", "dp", "graphs", "trees" ], "title": "Garden" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n;\nint k;\nint esp[10];\nint dist[210][210];\nint l, c;\nint M[210][210];\nint pai[210][210];\nint pd[210][210][2];\nint Vx[] = {-1, 1, 0, 0};\nint Vy[] = {0, 0, -1, 1};\nint C[210];\nbool resp[210];\nbool submask(int a, int b) {\n for (int i = 0; i < k;...
Codeforces/161/B
# Discounts One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two...
[ { "input": { "stdin": "3 2\n2 1\n3 2\n3 1\n" }, "output": { "stdout": "5.5\n1 3 \n2 1 2 \n" } }, { "input": { "stdin": "4 3\n4 1\n1 2\n2 2\n3 2\n" }, "output": { "stdout": "8.0\n1 1 \n1 2 \n2 3 4 \n" } }, { "input": { "stdin": "11 11\n6 2\n6 ...
{ "tags": [ "constructive algorithms", "greedy", "sortings" ], "title": "Discounts" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#pragma comment(linker, \"/STACK:200000000\")\nconst double EPS = 1E-9;\nconst int INF = 1000000000;\nconst long long INF64 = (long long)1E18;\nconst double PI = 3.1415926535897932384626433832795;\nint c[110000], tp[110000];\nbool cmp(int a, int b) { return c[...
Codeforces/180/D
# Name Everything got unclear to us in a far away constellation Tau Ceti. Specifically, the Taucetians choose names to their children in a very peculiar manner. Two young parents abac and bbad think what name to give to their first-born child. They decided that the name will be the permutation of letters of string s....
[ { "input": { "stdin": "abc\ndefg\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "czaaab\nabcdef\n" }, "output": { "stdout": "abczaa\n" } }, { "input": { "stdin": "aad\naac\n" }, "output": { "stdout": "aad\n" }...
{ "tags": [ "greedy", "strings" ], "title": "Name" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nmap<long long int, string> ma;\nint main() {\n ios_base::sync_with_stdio(0);\n cin.tie(NULL);\n string st, temp, fin = \"abcdefghijklmnopqrstuvwxyz\";\n cin >> st >> temp;\n long long int arr[30], i, j = 0, t = -1;\n for (i = 0; i < 26; i++) ma[i] = fin[...
Codeforces/203/E
# Transportation Valera came to Japan and bought many robots for his research. He's already at the airport, the plane will fly very soon and Valera urgently needs to bring all robots to the luggage compartment. The robots are self-propelled (they can potentially move on their own), some of them even have compartments...
[ { "input": { "stdin": "2 7 10\n3 12 10\n5 16 8\n" }, "output": { "stdout": "0 0\n" } }, { "input": { "stdin": "4 8 10\n0 12 3\n1 1 0\n0 3 11\n1 6 9\n" }, "output": { "stdout": "4 9\n" } }, { "input": { "stdin": "3 10 10\n0 12 10\n1 6 10\n0 1 ...
{ "tags": [ "greedy", "sortings", "two pointers" ], "title": "Transportation" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MOD = 1e9 + 7;\nint main() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n int n, s, d;\n cin >> n >> d >> s;\n vector<long long> vBase, vZero;\n pair<long long, long long> ans = {0, 0};\n long long addCap = 0, addCt = 0, subCt = 0, baseCap ...
Codeforces/228/D
# Zigzag The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function. The Zigag's sequenc...
[ { "input": { "stdin": "5\n2 3 1 5 5\n4\n2 2 3 2\n2 1 5 3\n1 3 5\n2 1 5 3\n" }, "output": { "stdout": "5\n26\n38\n" } }, { "input": { "stdin": "5\n42665793 142698407 856080769 176604645 248258165\n10\n1 5 141156007\n2 5 5 3\n2 4 4 2\n2 2 5 3\n1 2 942795810\n2 5 5 3\n1 3 1951...
{ "tags": [ "data structures" ], "title": "Zigzag" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m;\nlong long zz[7][100035];\nlong long a[100005], b[7][12][100005];\ninline int add(int ii, int jj, int kk, long long val) {\n while (kk <= n) {\n b[ii][jj][kk] += val;\n kk += (kk & (-kk));\n }\n return 0;\n}\ninline long long ask(int ii, int...
Codeforces/252/B
# Unsorting Array Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of n elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they...
[ { "input": { "stdin": "3\n1 1 1\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "1\n1\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "2\n1 2\n" }, "output": { "stdout": "-1\n" } }, { "inp...
{ "tags": [ "brute force", "sortings" ], "title": "Unsorting Array" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, a[100005];\ninline bool isX() {\n for (int i = 2; i <= n; i++)\n if (a[i - 1] > a[i]) return false;\n return true;\n}\ninline bool isN() {\n for (int i = 2; i <= n; i++)\n if (a[i - 1] < a[i]) return false;\n return true;\n}\ninline bool check...
Codeforces/277/C
# Game Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of th...
[ { "input": { "stdin": "2 1 0\n" }, "output": { "stdout": "FIRST\n1 0 1 1\n" } }, { "input": { "stdin": "2 2 4\n0 1 2 1\n0 1 2 1\n1 2 1 0\n1 1 1 2\n" }, "output": { "stdout": "SECOND\n" } }, { "input": { "stdin": "2 2 1\n0 1 1 1\n" }, ...
{ "tags": [ "games", "implementation" ], "title": "Game" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\nT sqr(T x) {\n return x * x;\n}\nconst int INF = 1000 * 1000 * 1000;\nconst long long INF64 = sqr(static_cast<long long>(INF));\nconst double PI = acos(-1.0);\nconst int K = 200500;\nlong long n, m;\nlong long mex;\nint k, a, b, c;\npair<l...
Codeforces/29/E
# Quarrel Friends Alex and Bob live in Bertown. In this town there are n crossroads, some of them are connected by bidirectional roads of equal length. Bob lives in a house at the crossroads number 1, Alex — in a house at the crossroads number n. One day Alex and Bob had a big quarrel, and they refused to see each ot...
[ { "input": { "stdin": "7 5\n1 2\n2 7\n7 6\n2 3\n3 4\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "2 1\n1 2\n" }, "output": { "stdout": "1\n1 2 \n2 1 \n" } }, { "input": { "stdin": "7 6\n1 2\n2 7\n7 6\n2 3\n3 4\n1 5\n" }, ...
{ "tags": [ "graphs", "shortest paths" ], "title": "Quarrel" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 505;\nvector<int> adj[N];\nint n, m, dist[N][N][2], p, ans;\nqueue<pair<pair<int, int>, bool> > Q;\npair<pair<int, int>, bool> par[N][N][2];\nint main() {\n cin >> n >> m;\n while (m--) {\n int u, v;\n cin >> u >> v;\n adj[u].push_back...
Codeforces/323/C
# Two permutations You are given two permutations p and q, consisting of n elements, and m queries of the form: l1, r1, l2, r2 (l1 ≤ r1; l2 ≤ r2). The response for the query is the number of such integers from 1 to n, that their position in the first permutation is in segment [l1, r1] (borders included), and position ...
[ { "input": { "stdin": "4\n4 3 2 1\n2 3 4 1\n3\n1 2 3 4\n1 3 2 1\n1 4 2 3\n" }, "output": { "stdout": "1\n1\n2\n" } }, { "input": { "stdin": "3\n3 1 2\n3 2 1\n1\n1 2 3 3\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "1\n1\n1\n1\...
{ "tags": [ "data structures" ], "title": "Two permutations" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 1e6 + 5;\nint ch[MAXN], arr[MAXN], x, a, b, c, d, l1, l2, r1, r2, n, m;\nvector<int> tree[4 * MAXN];\nvoid build(int v, int b, int e) {\n if (b == e)\n tree[v].push_back(arr[b]);\n else {\n int m = (b + e) / 2;\n build(v + v, b, m);...
Codeforces/348/A
# Mafia One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n - 1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimu...
[ { "input": { "stdin": "3\n3 2 2\n" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "4\n2 2 2 2\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "3\n1000000000 1000000000 10000000\n" }, "output": { "stdout":...
{ "tags": [ "binary search", "math", "sortings" ], "title": "Mafia" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1e5 + 3;\nint n, a[N];\nint main() {\n cin >> n;\n for (int i = 0; i < n; i++) cin >> a[i];\n int Max = -1;\n for (int i = 0; i < n; i++) Max = max(Max, a[i]);\n long long int cans = 0;\n for (int i = 0; i < n; i++) cans += Max - a[i];\n c...
Codeforces/371/B
# Fox Dividing Cheese Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces ...
[ { "input": { "stdin": "15 20\n" }, "output": { "stdout": "3\n" } }, { "input": { "stdin": "14 8\n" }, "output": { "stdout": "-1\n" } }, { "input": { "stdin": "6 6\n" }, "output": { "stdout": "0\n" } }, { "input": { ...
{ "tags": [ "math", "number theory" ], "title": "Fox Dividing Cheese" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a, b, x, ans, a2, a3, a5, b2, b3, b5;\nint main(int argc, char **argv) {\n scanf(\"%d%d\", &a, &b);\n x = a;\n while (x % 2 == 0) x /= 2, ++a2;\n x = a;\n while (x % 3 == 0) x /= 3, ++a3;\n x = a;\n while (x % 5 == 0) x /= 5, ++a5;\n x = b;\n whil...
Codeforces/392/D
# Three Arrays There are three arrays a, b and c. Each of them consists of n integers. SmallY wants to find three integers u, v, w (0 ≤ u, v, w ≤ n) such that the following condition holds: each number that appears in the union of a, b and c, appears either in the first u elements of a, or in the first v elements of b...
[ { "input": { "stdin": "3\n1 1 101\n1 2 1\n3 2 1\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "5\n1 1 2 2 3\n2 2 4 3 3\n3 3 1 1 1\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "1\n2\n3\n2\n" }, "output": ...
{ "tags": [ "data structures" ], "title": "Three Arrays" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 100000 + 10, INF = N * 3 + 1;\nint a[N], b[N], c[N], id[N * 3], L = 0;\nint find(int x) {\n int l = 0, r = L;\n while (l + 1 < r) {\n int mid = (l + r) >> 1;\n if (id[mid] >= x)\n r = mid;\n else\n l = mid;\n }\n return r;\...
Codeforces/415/E
# Mashmokh and Reverse Operation Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashm...
[ { "input": { "stdin": "2\n2 1 4 3\n4\n1 2 0 2\n" }, "output": { "stdout": " 0\n 6\n 6\n ...
{ "tags": [ "combinatorics", "divide and conquer" ], "title": "Mashmokh and Reverse Operation" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long int h, n, m, inv[25][2] = {0}, q;\nvector<int> a;\nvector<int> b;\nvector<int> c;\nvector<int> tp;\nvoid srt1(int l, int r, int ht) {\n int i = l, j = ((l + r) / 2) + 1, mid = (l + r) / 2, ct = 0,\n sz = (r - l + 1) / 2;\n while (i <= mid && j...
Codeforces/442/C
# Artem and Array Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the n...
[ { "input": { "stdin": "5\n1 2 3 4 5\n" }, "output": { "stdout": "6" } }, { "input": { "stdin": "5\n1 100 101 100 1\n" }, "output": { "stdout": "102" } }, { "input": { "stdin": "5\n3 1 5 2 6\n" }, "output": { "stdout": "11" }...
{ "tags": [ "data structures", "greedy" ], "title": "Artem and Array " }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool PIIfs(const pair<int, int>& a, const pair<int, int>& b) {\n return a.first < b.first || (a.first == b.first && a.second < b.second);\n}\nbool PIIsf(const pair<int, int>& a, const pair<int, int>& b) {\n return a.second < b.second || (a.second == b.second...
Codeforces/464/D
# World of Darkraft - 2 Roma found a new character in the game "World of Darkraft - 2". In this game the character fights monsters, finds the more and more advanced stuff that lets him fight stronger monsters. The character can equip himself with k distinct types of items. Power of each item depends on its level (pos...
[ { "input": { "stdin": "2 1\n" }, "output": { "stdout": "2.333333333" } }, { "input": { "stdin": "1 3\n" }, "output": { "stdout": "1.000000000" } }, { "input": { "stdin": "10 2\n" }, "output": { "stdout": "15.938076892" } }...
{ "tags": [ "dp", "probabilities" ], "title": "World of Darkraft - 2" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ndouble dp[2][605];\nint Max = 600;\nint main() {\n int n, k;\n while (cin >> n >> k) {\n memset(dp, sizeof(dp), 0);\n dp[0][1] = 1;\n int cur = 0;\n int next = 1;\n double ans = 0;\n for (int i = 1; i <= n; ++i) {\n for (int j = 1; j <...
Codeforces/488/C
# Fight the Monster A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF). During the battle, every second the monster's HP decrease by max(0, ATKY - DEFM), while Yang's H...
[ { "input": { "stdin": "1 2 1\n1 100 1\n1 100 100\n" }, "output": { "stdout": "99" } }, { "input": { "stdin": "100 100 100\n1 1 1\n1 1 1\n" }, "output": { "stdout": "0" } }, { "input": { "stdin": "51 89 97\n18 25 63\n22 91 74\n" }, "ou...
{ "tags": [ "binary search", "brute force", "implementation" ], "title": "Fight the Monster" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct PL {\n int hp, atk, def;\n};\nPL yan, mon;\nint h, a, d, ans;\nint upatk, updef, loatk;\nint main() {\n scanf(\"%d %d %d\", &yan.hp, &yan.atk, &yan.def);\n scanf(\"%d %d %d\", &mon.hp, &mon.atk, &mon.def);\n scanf(\"%d %d %d\", &h, &a, &d);\n ans =...
Codeforces/512/B
# Fox And Jumping Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i...
[ { "input": { "stdin": "5\n10 20 30 40 50\n1 1 1 1 1\n" }, "output": { "stdout": "-1" } }, { "input": { "stdin": "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n" }, "output": { "stdout": "7237\n" } }, { "input"...
{ "tags": [ "bitmasks", "brute force", "dp", "math" ], "title": "Fox And Jumping" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, a[1111], b[1111];\nconst int inf = 1e9;\nmap<int, int> mp, mp1;\nint nsd(int x, int y) {\n while (x > 0 && y > 0)\n if (x > y)\n x %= y;\n else\n y %= x;\n return x + y;\n}\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(...
Codeforces/536/C
# Tavas and Pashmaks Tavas is a cheerleader in the new sports competition named "Pashmaks". <image> This competition consists of two part: swimming and then running. People will immediately start running R meters after they finished swimming exactly S meters. A winner is a such person that nobody else finishes runni...
[ { "input": { "stdin": "3\n1 2\n1 1\n2 1\n" }, "output": { "stdout": "1 3\n" } }, { "input": { "stdin": "3\n1 3\n2 2\n3 1\n" }, "output": { "stdout": "1 2 3\n" } }, { "input": { "stdin": "5\n10 50\n10 50\n10 50\n10 50\n10 50\n" }, "out...
{ "tags": [ "geometry", "math" ], "title": "Tavas and Pashmaks" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct Node {\n int x, y, idx;\n} p[200005];\nbool che(Node A, Node B, Node C) {\n return 1ll * (C.x - B.x) * (A.y - B.y) * C.y * A.x <\n 1ll * (A.x - B.x) * (C.y - B.y) * A.y * C.x;\n}\nNode v[200005];\nbool cmp(Node A, Node B) { return A.x < B.x ||...
Codeforces/560/D
# Equivalent Strings Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: 1. They are equal. 2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of t...
[ { "input": { "stdin": "aaba\nabaa\n" }, "output": { "stdout": "YES\n" } }, { "input": { "stdin": "aabb\nabab\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "aabbaaaa\naaaaabab\n" }, "output": { "stdout": "NO\n" ...
{ "tags": [ "divide and conquer", "hashing", "sortings", "strings" ], "title": "Equivalent Strings" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar a[201000], b[201000];\nint hash1[201000], hash2[201000], seed = 131, base[201000];\nbool solve(int be1, int en1, int be2, int en2) {\n if ((en1 - be1 + 1) & 1 || (en2 - be2 + 1) & 1) return 0;\n int a1, a2, b1, b2;\n int mid1 = (be1 + en1) / 2;\n int ...
Codeforces/586/F
# Lizard Era: Beginning In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has n mandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the companions to the hero is an integer. Initiall...
[ { "input": { "stdin": "7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7\n" }, "output": { "stdout": "LM\nMW\nLM\nLW\nMW\nLM\nLW\n" } }, { "input": { "stdin": "2\n1 0 0\n1 1 0\n" }, "output": { "stdout": "Impossible\n" } }, { "input": { ...
{ "tags": [ "meet-in-the-middle" ], "title": "Lizard Era: Beginning" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int inf_int = 1e9 + 100;\nconst long long inf_ll = 2e18;\nconst double pi = 3.1415926535898;\nbool debug = 0;\nconst int MAXN = 2e5 + 100;\nconst int LOG = 25;\nconst int mod = 998244353;\nconst int MX = 1e6 + 100;\nconst long long MOD = 1000000000949747...
Codeforces/609/B
# The Best Gift Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the ...
[ { "input": { "stdin": "7 4\n4 2 3 1 2 4 3\n" }, "output": { "stdout": "18\n" } }, { "input": { "stdin": "4 3\n2 1 3 1\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "100 10\n7 4 5 5 10 10 5 8 5 7 4 5 4 6 8 8 2 6 3 3 10 7 10 8 6 ...
{ "tags": [ "constructive algorithms", "implementation" ], "title": "The Best Gift" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n, m, s = 0;\n cin >> n >> m;\n int ar[n];\n for (int i = 0; i < n; i++) {\n cin >> ar[i];\n }\n sort(ar, ar + n);\n for (int i = 0; i < n; i++) {\n int ch;\n ch = ar[i];\n int z, l = 0, r = n;\n while (l < r) {\n ...
Codeforces/630/A
# Again Twenty Five! The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. "Do I give such a hard task?" — the HR manager thought. "Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot...
[ { "input": { "stdin": "2\n" }, "output": { "stdout": "25\n" } }, { "input": { "stdin": "7\n" }, "output": { "stdout": "25\n" } }, { "input": { "stdin": "2000000000000000000\n" }, "output": { "stdout": "25\n" } }, { "...
{ "tags": [ "number theory" ], "title": "Again Twenty Five!" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int n;\n cin >> n;\n printf(\"25\\n\");\n return 0;\n}\n", "java": "import java.util.Scanner;\n\npublic class AgainTwentyFive {\n\n public static void main(String[] args) {\n \n\n Scanner s = new Scanner(System.in);\n\n ...
Codeforces/656/E
# Out of Controls You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. Input The first line of the input contains a single integer ...
[ { "input": { "stdin": "3\n0 1 1\n1 0 4\n1 4 0\n" }, "output": { "stdout": "2" } }, { "input": { "stdin": "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n" }, "output": { "stdout": "5" } }, { "input": { "stdin": "6\n0 41 48 86 94 14\n41 0 1 30 59 39\n...
{ "tags": [ "*special" ], "title": "Out of Controls" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint arr[105][105];\nint n;\nint read(int x, int y) {\n x == n ? x = n : (y == n ? y = n : scanf(\"%d\", &arr[x][y]));\n x == n ? x = n : (y == n ? read(x + 1, 0) : read(x, y + 1));\n return 0;\n}\nint floyd(int k, int i, int j) {\n k == n ? k = n\n : i ...
Codeforces/67/E
# Save the City! In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population...
[ { "input": { "stdin": "5\n4 8\n8 8\n9 4\n4 0\n0 4\n" }, "output": { "stdout": "5\n" } }, { "input": { "stdin": "5\n4 8\n5 8\n5 4\n7 4\n2 2\n" }, "output": { "stdout": "0\n" } }, { "input": { "stdin": "4\n889308 0\n110692 0\n0 461939\n146447 8...
{ "tags": [ "geometry" ], "title": "Save the City!" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct point {\n double x, y;\n point() {}\n point(double x, double y) : x(x), y(y) {}\n double operator*(const point &b) const {\n double tmp(x * b.y - y * b.x);\n if (tmp == 0)\n return 0;\n else\n return tmp > 0 ? 1 : -1;\n }\n poin...
Codeforces/702/E
# Analysis of Pathes in Functional Graph You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to n - 1. Graph is given as the array f0, f1, ..., fn - 1, where fi — the number of vertex to which goes the only arc from the vertex i. ...
[ { "input": { "stdin": "5 3\n1 2 3 4 0\n4 1 2 14 3\n" }, "output": { "stdout": " 7 1\n 17 ...
{ "tags": [ "data structures", "graphs" ], "title": "Analysis of Pathes in Functional Graph" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long up[100005][40][3];\nint main() {\n long long(n), (k);\n scanf(\"%lld%lld\", &(n), &(k));\n ;\n for (long long i = 0; i < (100005); i++)\n for (long long j = 0; j < (40); j++)\n for (long long k = 0; k < (3); k++) up[i][j][k] = 0;\n vecto...
Codeforces/724/F
# Uniformly Branched Trees A tree is a connected graph without cycles. Two trees, consisting of n vertices each, are called isomorphic if there exists a permutation p: {1, ..., n} → {1, ..., n} such that the edge (u, v) is present in the first tree if and only if the edge (pu, pv) is present in the second tree. Vert...
[ { "input": { "stdin": "10 3 409693891\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "65 4 177545087\n" }, "output": { "stdout": "910726\n" } }, { "input": { "stdin": "5 2 433416647\n" }, "output": { "stdout": "1\n...
{ "tags": [ "combinatorics", "dp", "trees" ], "title": "Uniformly Branched Trees" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar c;\ninline void read(long long& a) {\n a = 0;\n do c = getchar();\n while (c < '0' || c > '9');\n while (c <= '9' && c >= '0') a = (a << 3) + (a << 1) + c - '0', c = getchar();\n}\nlong long n, d, Mod, F[1001], G[11][1001], Rev[10];\ninline long long ...
Codeforces/746/F
# Music in Car Sasha reaches the work by car. It takes exactly k minutes. On his way he listens to music. All songs in his playlist go one by one, after listening to the i-th song Sasha gets a pleasure which equals ai. The i-th song lasts for ti minutes. Before the beginning of his way Sasha turns on some song x and...
[ { "input": { "stdin": "8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8\n" }, "output": { "stdout": "19\n" } }, { "input": { "stdin": "7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9\n" }, "output": { "stdout": "12\n" } }, { "input": { "stdin": "1 1 3\n4\n7...
{ "tags": [ "data structures", "greedy", "two pointers" ], "title": "Music in Car" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 2e5 + 7;\nint a[maxn], t[maxn], n, w, k, ple = 0, tme = 0, res = 0, cur = 1;\nset<pair<int, int> > half, full;\nbitset<maxn> is_half;\nvoid find_nxt() {\n while (cur <= n) {\n if ((int)half.size() < w) {\n if (tme + t[cur] / 2 + t[cur...
Codeforces/76/B
# Mice Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point ...
[ { "input": { "stdin": "3 2 0 2\n0 1 3\n2 5\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "20 18 1 2\n-9999944 -9999861 -9999850 -9999763 -9999656 -9999517 -9999375 -9999275 -9999203 -9999080 -9998988 -9998887 -9998714 -9998534 -9998475 -9998352 -9998164 -9998...
{ "tags": [ "greedy", "two pointers" ], "title": "Mice" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 1e5 + 10, INF = 0x3F3F3F3F;\nint a[MAXN], b[MAXN], minn[MAXN];\nint main() {\n int n, m, i, Y0, Y1, res = 0;\n scanf(\"%d %d %d %d\", &n, &m, &Y0, &Y1);\n for (i = 1; i <= n; i++) scanf(\"%d\", &a[i]);\n for (i = 1; i <= m; i++) scanf(\"%d...
Codeforces/793/F
# Julia the snail After hard work Igor decided to have some rest. He decided to have a snail. He bought an aquarium with a slippery tree trunk in the center, and put a snail named Julia into the aquarium. Igor noticed that sometimes Julia wants to climb onto the trunk, but can't do it because the trunk is too slippe...
[ { "input": { "stdin": "10\n10\n3 7\n1 4\n1 6\n5 5\n1 1\n3 9\n7 8\n1 2\n3 3\n7 10\n10\n2 4\n1 7\n3 4\n3 5\n2 8\n2 5\n5 5\n3 5\n7 7\n3 10\n" }, "output": { "stdout": "2\n7\n3\n3\n2\n2\n5\n3\n7\n10\n" } }, { "input": { "stdin": "8\n4\n1 2\n3 4\n2 5\n6 7\n5\n1 2\n1 4\n1 6\n2 7\...
{ "tags": [ "data structures", "divide and conquer", "dp" ], "title": "Julia the snail" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1e5 + 100;\nconst int SQRT = 1500;\nvector<int> up[N], down[N];\nvector<pair<int, pair<int, int>>> q[N];\nset<int> pos;\nint ans[N];\nint main() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n int n;\n cin >> n;\n int m;\n cin >> m;\n fo...
Codeforces/814/C
# An impassioned circulation of affection Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it! Still unsatisfied with the garland, Nadeko decided to polish it again. The garlan...
[ { "input": { "stdin": "15\nyamatonadeshiko\n10\n1 a\n2 a\n3 a\n4 a\n5 a\n1 b\n2 b\n3 b\n4 b\n5 b\n" }, "output": { "stdout": "3\n4\n5\n7\n8\n1\n2\n3\n4\n5\n" } }, { "input": { "stdin": "6\nkoyomi\n3\n1 o\n4 o\n4 m\n" }, "output": { "stdout": "3\n6\n5\n" } ...
{ "tags": [ "brute force", "dp", "strings", "two pointers" ], "title": "An impassioned circulation of affection" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\ninline int readInt() {\n int n = 0, ch = getchar();\n while (!isdigit(ch)) ch = getchar();\n while (isdigit(ch)) n = n * 10 + ch - '0', ch = getchar();\n return n;\n}\ninline int readChar() {\n int ch = getchar();\n while (!isalpha(ch)) ch = getchar();\n...
Codeforces/83/C
# Track You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest. The track's map is represented by a rectangle n × m in ...
[ { "input": { "stdin": "5 3 2\nSba\nccc\naac\nccc\nabT\n" }, "output": { "stdout": "bcccc\n" } }, { "input": { "stdin": "3 4 1\nSxyy\nyxxx\nyyyT\n" }, "output": { "stdout": "xxxx\n" } }, { "input": { "stdin": "1 3 3\nTyS\n" }, "output"...
{ "tags": [ "graphs", "greedy", "shortest paths" ], "title": "Track" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct point {\n char h;\n string res;\n bool poss;\n};\npoint a[50][50];\nint n, m, k;\nstring res;\nvector<int> qx, qy;\nint step[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\nvoid bfs() {\n for (int i = 0; i < qx.size(); i++) {\n int x, y;\n for (i...
Codeforces/85/D
# Sum of Medians In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algori...
[ { "input": { "stdin": "14\nadd 1\nadd 7\nadd 2\nadd 5\nsum\nadd 6\nadd 8\nadd 9\nadd 3\nadd 4\nadd 10\nsum\ndel 1\nsum\n" }, "output": { "stdout": "5 \n11 \n13 \n" } }, { "input": { "stdin": "6\nadd 4\nadd 5\nadd 1\nadd 2\nadd 3\nsum\n" }, "output": { "stdout"...
{ "tags": [ "binary search", "brute force", "data structures", "implementation" ], "title": "Sum of Medians" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstruct tree {\n int x, y, si;\n long long sum[5];\n tree *l, *r;\n};\ntree mem[100100];\nint mpos = 1;\ntree *nil = mem;\nvoid Calc(tree *t) {\n if (t == nil) return;\n t->si = 1 + t->l->si + t->r->si;\n for (int i = 0; i < 5; i++) t->sum[i] = t->l->sum[...
Codeforces/886/D
# Restoration of string A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent subst...
[ { "input": { "stdin": "3\nkek\npreceq\ncheburek\n" }, "output": { "stdout": "NO\n" } }, { "input": { "stdin": "4\nmail\nai\nlru\ncf\n" }, "output": { "stdout": "cfmailru\n" } }, { "input": { "stdin": "2\nab\nac\n" }, "output": { ...
{ "tags": [ "constructive algorithms", "graphs", "implementation" ], "title": "Restoration of string" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing db = long double;\nusing pll = pair<long long, long long>;\nstd::set<long long> g[27];\nint32_t main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n long long t, i, x, j, y, z, k, n;\n set<long long> let;\n long long tt;\n cin >> tt;...
Codeforces/909/D
# Colorful Points You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operatio...
[ { "input": { "stdin": "aabb\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "aabcaa\n" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "bbbbbbbbaaaaaaaaaaaccccccaaaaaaaaaaaaaaccccccccaaaaaaaaabbbbbbccbbbaaaaaabccccccaaa...
{ "tags": [ "data structures", "greedy", "implementation" ], "title": "Colorful Points" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n string s;\n cin >> s;\n int n = s.size();\n if (n == 1) {\n cout << 0 << \"\\n\";\n return 0;\n }\n set<pair<int, int> > intervals;\n int begin = 0;\n for (int i = 1; i < n;...
Codeforces/931/A
# Friends Meeting Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes...
[ { "input": { "stdin": "3\n4\n" }, "output": { "stdout": "1" } }, { "input": { "stdin": "5\n10\n" }, "output": { "stdout": "9" } }, { "input": { "stdin": "101\n99\n" }, "output": { "stdout": "2" } }, { "input": { ...
{ "tags": [ "brute force", "greedy", "implementation", "math" ], "title": "Friends Meeting" }
{ "cpp": "#include <bits/stdc++.h>\nint main() {\n int len, a, b, n, cntA, cntB, sum, x, y;\n scanf(\"%d%d\", &a, &b);\n len = abs(a - b);\n cntA = len / 2;\n cntB = len - len / 2;\n x = (cntA * (cntA + 1)) / 2;\n y = (cntB * (cntB + 1)) / 2;\n sum = x + y;\n printf(\"%d\", sum);\n}\n", "java": "import jav...
Codeforces/958/E2
# Guard Duty (medium) Princess Heidi decided to give orders to all her K Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at N specific moments t1, t2, ..., tN. The meetings with commanders must therefore start and stop at those times. Namely, each ...
[ { "input": { "stdin": "2 5\n1 4 6 7 12\n" }, "output": { "stdout": "4" } }, { "input": { "stdin": "4 12\n15 7 4 19 3 30 14 1 5 23 17 25\n" }, "output": { "stdout": "6" } }, { "input": { "stdin": "3 6\n6 3 4 2 5 1\n" }, "output": { ...
{ "tags": [ "binary search", "dp", "greedy", "sortings" ], "title": "Guard Duty (medium)" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAX = 5e5 + 5;\nint k, n, a[MAX];\nmultiset<pair<long long, int> > s_val;\nmultiset<pair<int, long long> > s_pos;\nint main() {\n scanf(\"%d%d\", &k, &n);\n for (int i = 1; i <= n; i++) scanf(\"%d\", &a[i]);\n sort(a + 1, a + n + 1);\n for (int i...
Codeforces/985/A
# Chess Placing You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW". Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to <image>. In one...
[ { "input": { "stdin": "10\n1 2 3 4 5\n" }, "output": { "stdout": "10\n" } }, { "input": { "stdin": "6\n1 2 6\n" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "10\n9 8 7 6 5\n" }, "output": { "stdout": "7\n" } }...
{ "tags": [ "implementation" ], "title": "Chess Placing" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int MAXN = 105;\nint p[MAXN];\nint main() {\n int n;\n scanf(\"%d\", &n);\n for (int i = 1; 2 * i <= n; i++) scanf(\"%d\", &p[i]);\n sort(p + 1, p + 1 + n / 2);\n int ans = 1e9, now = 0;\n int pos = n;\n for (int i = n / 2; i >= 1; i--) {\n now...
HackerEarth/abc-garfield
Garfield the cat likes candies A LOT. He always keeps a huge stock of it at his home. Today John, his owner, brought home three types of candies. He brought A pieces of Red candy, B pieces of Green candy and C pieces of Blue candy. Garfield is really happy. But the problem is that John won’t allow him to eat all of it ...
[ { "input": { "stdin": "3\n2 1 2 3\n1 1 1 1\n2 1 0 1\n\nSAMPLE" }, "output": { "stdout": "9\n4\n4" } } ]
{ "tags": [], "title": "abc-garfield" }
{ "cpp": null, "java": null, "python": "t = int(raw_input())\nfor _ in range(t):\n\tn, a, b, c = map(int, raw_input().split())\n\tways = 0\n\tfor i in range(a + 1):\n\t\tfor j in range(min(n - i, b) + 1):\n\t\t\tif(n - i - j < 0):\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tways += min(n - i - j, c) + 1\n\tprint(ways)" ...
HackerEarth/bobs-journey-5
Bob is travelling from one city to another. In his way, he sees many other cities pass by. What he does instead of learning the full names of the cities, he learns just the first character of the cities. For example, if he passes by "bhopal", he will just remember the 'b'. Given the list of N cities that come in h...
[ { "input": { "stdin": "2\n2\nbhopal\ndelhi\n3\nbhopal\ndelhi\ndehradun\n\nSAMPLE" }, "output": { "stdout": "YES\nNO\n" } }, { "input": { "stdin": "100\n4\nlrbbmqb\ncd\nr\nowkk\n7\nid\nqscdxrjmow\nrxsjybldbe\nsarcbyne\ndyggxxp\nlorel\nnmpa\n6\nfwkho\nkmcoqhnw\nkuewhsqmgb\nuq...
{ "tags": [], "title": "bobs-journey-5" }
{ "cpp": null, "java": null, "python": "for _ in range(input()):\n\talpha = \"abcdefghijklmnopqrstuvwxyz\"\n\tfreq = {}\n\tfor a in alpha:\n\t\tfreq[a] = 0\n\tflag = True\n\tfor _ in range(input()):\n\t\tn = raw_input()\n\t\tfreq[n[0]]+=1\n\tfor a in freq:\n\t\tif freq[a]>1:\n\t\t\tflag = False\n\t\t\tbreak\n\tif...
HackerEarth/cube-change-qualifier2
Chandan gave his son a cube with side N. The N X N X N cube is made up of small 1 X 1 X 1 cubes. Chandan's son is extremely notorious just like him. So he dropped the cube inside a tank filled with Coke. The cube got totally immersed in that tank. His son was somehow able to take out the cube from the tank. But soone...
[ { "input": { "stdin": "2\n1\n3\n\nSAMPLE" }, "output": { "stdout": "1\n26\n" } }, { "input": { "stdin": "61\n17357774\n248290406\n171408490\n20054376\n118774144\n110228788\n8827234\n85304880\n158466588\n168421585\n174586544\n51487597\n19100400\n4459509\n85309162\n445813368\...
{ "tags": [], "title": "cube-change-qualifier2" }
{ "cpp": null, "java": null, "python": "t = input()\nwhile t>0:\n\tt-=1\n\tn = input()\n\tif n == 1:\n\t\tprint \"1\"\n\telif n == 2:\n\t\tprint \"4\"\n\telse:\n\t\tprint pow(n, 3) - pow((n-2), 3)" }
HackerEarth/friendless-dr-sheldon-cooper-14
Leonard has decided to quit living with Dr. Sheldon Cooper and has started to live with Penny. Yes, you read it right. (And you read it here for the first time!) He is fed up of Sheldon, after all. Since, Sheldon no more has Leonard to drive him all around the city for various things, he's feeling a lot uneasy so he de...
[ { "input": { "stdin": "1\n3 3\n1 2\n2 3\n1 3\n\nSAMPLE" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "1\n3 3\n1 2\n2 3\n1 3" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "1\n3 3\n2 2\n2 3\n1 3" }, "output": {...
{ "tags": [], "title": "friendless-dr-sheldon-cooper-14" }
{ "cpp": null, "java": null, "python": "t = int(raw_input())\nfor i in range(t):\n\ta, b = map(int, raw_input().split())\n\tfor j in range(b):\n\t\tm, n = map(int, raw_input().split())\n\tprint(a-1)" }
HackerEarth/jumping-frog
There is a frog known as "CHAMELEON" because he has a special feature to change its body's color similar to stone's color on which he sits. There are N colorful stones lying in a row, but of only 1 to 100 different colors. Frog can hopp on another stone if the stone has same color as its body or (i-1)th stone if it is ...
[ { "input": { "stdin": "6\n2 3 1 3 2 4\n2\n1 0 4\n2 3 5\n\nSAMPLE" }, "output": { "stdout": "2\n-1" } }, { "input": { "stdin": "541\n54 98 68 63 83 94 55 35 12 63 30 17 97 62 96 26 63 76 91 19 52 42 55 95 8 97 6 18 96 3 46 21 55 88 14 27 65 8 94 93 52 39 40 52 12 94 89 39 38...
{ "tags": [], "title": "jumping-frog" }
{ "cpp": null, "java": null, "python": "from collections import deque\nclass Frog():\n\t\n\tdef __init__(self,n,stones):\n\t\tself.colors=[[] for _ in xrange(101)]\n\t\tself.N = n\n\t\tself.stones = stones\n\t\tfor i,c in enumerate(stones):\n\t\t\tself.colors[c].append(i)\n\t\tself.l=[0]*self.N\n\t\t\n\t\t\n\t\t\...
HackerEarth/mind-palaces-3
Sherlock Holmes loves mind palaces! We all know that. A mind palace, according to Mr. Holmes is something that lets him retrieve a given memory in the least time posible. For this, he structures his mind palace in a very special way. Let a NxM Matrix denote the mind palace of Mr. Holmes. For fast retrieval he keeps ea...
[ { "input": { "stdin": "5 5\n-993655555 -758584352 -725954642 -696391700 -649643547\n-591473088 -568010221 -432112275 -421496588 -351507172\n-323741602 -232192004 -30134637 -369573 100246476\n156824549 174266331 392354039 601294716 763826005\n768378344 802829330 818988557 992012759 999272829\n10\n156824549...
{ "tags": [], "title": "mind-palaces-3" }
{ "cpp": null, "java": null, "python": "def main():\n\tN, M = map(int, raw_input().split())\n\tmem = []\n\tfor i in range(0, N):\n\t\tmem.append(list(map(int, raw_input().split())))\n\tQ = int(raw_input())\n\tfor i in range(0, Q):\n\t\tX = int(raw_input())\n\t\t\n\t\txx = 0\n\t\tyy = M-1\n\t\tflag = 0\n\t\twhile ...
HackerEarth/palin-pairs
Panda has a thing for palindromes. Hence he was a given a problem by his master. The master will give Panda an array of strings S having N strings. Now Panda has to select the Palin Pairs from the given strings . A Palin Pair is defined as : (i,j) is a Palin Pair if Si = reverse(Sj) and i < j Panda wants to k...
[ { "input": { "stdin": "3\nbba\nabb\nabb\n\nSAMPLE" }, "output": { "stdout": "2\n" } }, { "input": { "stdin": "3\nbba\nabb\nabc\n\nSAPMLF" }, "output": { "stdout": "1\n" } }, { "input": { "stdin": "3\n`ba\ndba\n`bc\n\nAMNFLS" }, "outpu...
{ "tags": [], "title": "palin-pairs" }
{ "cpp": null, "java": null, "python": "from collections import defaultdict\nd=defaultdict(lambda:[0,0])\nn=int(raw_input())\nfor e in xrange(n):\n\ts=raw_input()\n\tif s in d or s[::-1] in d:\n\t\tif s in d:d[s][0]+=1\n\t\telif s[::-1] in d:d[s[::-1]][1]+=1\n\telse:d[s][0]=1\nsumm=0\nfor e in d:\n\tif d[e][0] an...
HackerEarth/reversemerge-shuffle-reverse
Given a string, S, we define some operations on the string as follows: a. reverse(S) denotes the string obtained by reversing string S. E.g.: reverse("abc") = "cba" b. shuffle(S) denotes any string that's a permutation of string S. E.g.: shuffle("god") ∈ ['god', 'gdo', 'ogd', 'odg', 'dgo', 'dog'] c. merge(S1,S2) den...
[ { "input": { "stdin": "eggegg\n\nSAMPLE" }, "output": { "stdout": "egg" } }, { "input": { "stdin": "bbcbccbabcbabcbaaccccaaabcbcaacacbabbbbcabcbbbbacbcaccccbcccbccaaabcabacccbaccccbbababccbccacbaccacbcccacbaaacacbbcbaaabbabbaaccbbbabccbccacacacabaababbcbcccbbcacabcabbbccaba...
{ "tags": [], "title": "reversemerge-shuffle-reverse" }
{ "cpp": null, "java": null, "python": "from collections import defaultdict\n\ndef solve(S):\n # Reverse S\n S = S[::-1]\n\n # Count each character in S.\n count = defaultdict(int)\n for c in S:\n count[c] += 1\n\n need = {}\n for c in count:\n need[c] = count[c] / 2\n\n solu...
HackerEarth/special-matrix-1
You are given a square matrix of size n (it will be an odd integer). Rows are indexed 0 to n-1 from top to bottom and columns are indexed 0 to n-1 form left to right. Matrix consists of only '*' and '.'. '*' appears only once in the matrix while all other positions are occupied by '.' Your task is to convert this matr...
[ { "input": { "stdin": "1\n7\n.......\n.*.....\n.......\n.......\n.......\n.......\n.......\n\nSAMPLE" }, "output": { "stdout": "4\n" } }, { "input": { "stdin": "1\n7\n/......\n.....*.\n.......\n.......\n.......\n.......\n.-../-.\n\nEMQMAS" }, "output": { "stdo...
{ "tags": [], "title": "special-matrix-1" }
{ "cpp": null, "java": null, "python": "test_case = input()\nt, t1, t2 = 0, 0, 0\nposi, posj = 0, 0\ndef Get_Matrix(n):\n\tglobal posi, posj, t, t1, t2\n a = []\n\tm = int(n/2) \n\t#a = [[0 for x in xrange(n+1)] for x in xrange(n+1)]\n for i in range(n):\n #j = 0\n #a[i...
HackerEarth/trailing-zero-problem-1
Given integer n, find length of n! (which is factorial of n) excluding trailing zeros. Input The first line of the standard input contains one integer t (t<10001) which is the number of test cases. In each of the next t lines there is number n (0 ≤ n ≤ 5*10^9). Output For each test, print the length of n! (which i...
[ { "input": { "stdin": "3\n5\n7\n10\n\nSAMPLE" }, "output": { "stdout": "2\n3\n5\n" } }, { "input": { "stdin": "4\n4568\n4545992\n9265642\n4592886" }, "output": { "stdout": "13598\n27154740\n58212156\n27455323\n" } }, { "input": { "stdin": "4\...
{ "tags": [], "title": "trailing-zero-problem-1" }
{ "cpp": null, "java": null, "python": "from math import *\ndef Trailing_Zeroes(x):\n fives = 0\n i = 1\n while 5**i <= x:\n fives+=x/(5**i)\n i+=1\n return fives\nt = int(raw_input())\nwhile t:\n t-=1\n n = int(raw_input())\n a = 0\n #for i in xrange(1,n+1):\n # a+=log...
p02574 AtCoder Beginner Contest 177 - Coprime
We have N integers. The i-th number is A_i. \\{A_i\\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N. \\{A_i\\} is said to be setwise coprime when \\{A_i\\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1. Determine if \\{A_i\\} is pairwise coprime, setw...
[ { "input": { "stdin": "3\n6 10 16" }, "output": { "stdout": "not coprime" } }, { "input": { "stdin": "3\n6 10 15" }, "output": { "stdout": "setwise coprime" } }, { "input": { "stdin": "3\n3 4 5" }, "output": { "stdout": "pairwis...
{ "tags": [], "title": "p02574 AtCoder Beginner Contest 177 - Coprime" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i, n) for (int i = 0; i < (int)(n); i++)\n#define rep3(i, s, n) for (int i = (s); i <= (int)(n); i++)\nint b[1000010];\nint d[1000010];\n\nint main() {\n\tint n,a;\n\tint p,ans;\n\n\tcin >> n ;\n\n\tans=0;\n\trep(i,1000010) b[i]=0;\n\tfor(int i = 1...
p02705 AtCoder Beginner Contest 163 - Circle Pond
Print the circumference of a circle of radius R. Constraints * 1 \leq R \leq 100 * All values in input are integers. Input Input is given from Standard Input in the following format: R Output Print the circumference of the circle. Your output is considered correct if and only if its absolute or relative error ...
[ { "input": { "stdin": "73" }, "output": { "stdout": "458.67252742410977361942" } }, { "input": { "stdin": "1" }, "output": { "stdout": "6.28318530717958623200" } }, { "input": { "stdin": "105" }, "output": { "stdout": "659.73445...
{ "tags": [], "title": "p02705 AtCoder Beginner Contest 163 - Circle Pond" }
{ "cpp": "#include <iostream> \nusing namespace std;\nint main(){\n\tint r;\n\tcin>>r;\n\tcout<<r*2*3.14;\n\treturn 0;\n}", "java": "import java.util.Scanner;\n\npublic class Main{\n public static void main(String[] args){\n\n Scanner sc = new Scanner(System.in);\n\n int R = sc.nextInt();\n double D = R *...
p02834 AtCoder Beginner Contest 148 - Playing Tag on Tree
We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally. Takahashi is standing at Vertex u, and Aoki is standing at Vertex v. Now, they will play a game of tag as follows: * 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vert...
[ { "input": { "stdin": "2 1 2\n1 2" }, "output": { "stdout": "0" } }, { "input": { "stdin": "5 4 5\n1 2\n1 3\n1 4\n1 5" }, "output": { "stdout": "1" } }, { "input": { "stdin": "5 4 1\n1 2\n2 3\n3 4\n3 5" }, "output": { "stdout": ...
{ "tags": [], "title": "p02834 AtCoder Beginner Contest 148 - Playing Tag on Tree" }
{ "cpp": "#include <bits/stdc++.h>\ntypedef long long int ll;\n#define vaibhavv06 \\\n\tios::sync_with_stdio(false); \\\n\tcin.tie(NULL); \\\n\tcout.tie(0);\n#define int long long\n#define MM(a,b) memset(a,b,sizeof(a))\nconst ll MOD = 1000000007;\nusing namespace std;\n\nconst int nax = 1e...
p02971 AtCoder Beginner Contest 134 - Exception Handling
You are given a sequence of length N: A_1, A_2, ..., A_N. For each integer i between 1 and N (inclusive), answer the following question: * Find the maximum value among the N-1 elements other than A_i in the sequence. Constraints * 2 \leq N \leq 200000 * 1 \leq A_i \leq 200000 * All values in input are integers. Inp...
[ { "input": { "stdin": "2\n5\n5" }, "output": { "stdout": "5\n5" } }, { "input": { "stdin": "3\n1\n4\n3" }, "output": { "stdout": "4\n3\n4" } }, { "input": { "stdin": "2\n1\n2" }, "output": { "stdout": "2\n1\n" } }, { ...
{ "tags": [], "title": "p02971 AtCoder Beginner Contest 134 - Exception Handling" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n int n; cin>>n; \n vector<int>a(n);\n vector<int>b(n);\n for(int i=0; i<n;i++){\n int A; cin>>A;\n a[i]=A;\n b[i]=A;\n }\n sort(a.begin(),a.end());\n for(int i=0;i<n;i++){\n if(b[i]==a[n-1]) cout<<a[n-2]<<endl;\n else cout...
p03107 AtCoder Beginner Contest 120 - Unification
There are N cubes stacked vertically on a desk. You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is `0`, and blue if that character is `1`. You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent...
[ { "input": { "stdin": "0" }, "output": { "stdout": "0" } }, { "input": { "stdin": "0011" }, "output": { "stdout": "4" } }, { "input": { "stdin": "11011010001011" }, "output": { "stdout": "12" } }, { "input": { ...
{ "tags": [], "title": "p03107 AtCoder Beginner Contest 120 - Unification" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int z=0, o=0;\n char j;\n while(scanf(\"%c\", &j) != EOF){\n if(j == '0') z++;\n else if(j == '1') o++;\n }\n cout << 2*min(z, o) << endl;\n return 0;\n}", "java": "import java.util.*;\nclass Main{\n\tpublic static void main(String...
p03254 AtCoder Grand Contest 027 - Candy Distribution Again
There are N children, numbered 1, 2, ..., N. Snuke has decided to distribute x sweets among them. He needs to give out all the x sweets, but some of the children may get zero sweets. For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets. Snuke is trying to maximize the number of happy ...
[ { "input": { "stdin": "4 1111\n1 10 100 1000" }, "output": { "stdout": "4" } }, { "input": { "stdin": "3 70\n20 30 10" }, "output": { "stdout": "2" } }, { "input": { "stdin": "3 10\n20 30 10" }, "output": { "stdout": "1" } ...
{ "tags": [], "title": "p03254 AtCoder Grand Contest 027 - Candy Distribution Again" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nint main() {\n\tint n; cin >> n;\n\tlong long x; cin >> x;\n\tlong long A[n];\n\tfor (int i = 0; i < n; i++) cin >> A[i];\n\tsort(A, A + n);\n\tint ans = 0;\n\tfor (int i = 0; i < n; i++) {\n\t\tx -= A[i];\n\t\tif (x >= 0) ans++;\n\t\tif (i == n - 1 && x > 0) ...
p03407 AtCoder Beginner Contest 091 - Two Coins
An elementary school student Takahashi has come to a variety store. He has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it? Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan. Constraints * All input values a...
[ { "input": { "stdin": "500 100 1000" }, "output": { "stdout": "No" } }, { "input": { "stdin": "19 123 143" }, "output": { "stdout": "No" } }, { "input": { "stdin": "19 123 142" }, "output": { "stdout": "Yes" } }, { "...
{ "tags": [], "title": "p03407 AtCoder Beginner Contest 091 - Two Coins" }
{ "cpp": "#include<stdio.h>\nint main()\n{\n\tint a,b,c;\n\tscanf(\"%d%d%d\",&a,&b,&c);\n\tif(a+b>c || (a+b)==c) puts(\"Yes\");\n\tif(a+b<c) puts(\"No\");\n}", "java": "import java.util.Scanner;\n\npublic class Main {\n\tpublic static void main(String[] args) {\n\t\tScanner sc = new Scanner(System.in);\n\t\tint A =...
p03570 CODE FESTIVAL 2017 qual C - Yet Another Palindrome Partitioning
We have a string s consisting of lowercase English letters. Snuke is partitioning s into some number of non-empty substrings. Let the subtrings obtained be s_1, s_2, ..., s_N from left to right. (Here, s = s_1 + s_2 + ... + s_N holds.) Snuke wants to satisfy the following condition: * For each i (1 \leq i \leq N), it ...
[ { "input": { "stdin": "byebye" }, "output": { "stdout": "1" } }, { "input": { "stdin": "abcdefghijklmnopqrstuvwxyz" }, "output": { "stdout": "26" } }, { "input": { "stdin": "aabxyyzz" }, "output": { "stdout": "2" } }, { ...
{ "tags": [], "title": "p03570 CODE FESTIVAL 2017 qual C - Yet Another Palindrome Partitioning" }
{ "cpp": "#include<cstdio>\n#include<iostream>\n#include<cstring>\n#include<algorithm>\nusing namespace std;\n\nconst int N=2e5+1e3+7;\n\nchar s[N];\n\nint n,f[(1<<26)],g[N];\n\nint main()\n{\n\tscanf(\"%s\",s+1);\n\tn=strlen(s+1);\n\tfor(int i=1;i<=n;i++)\n\t\tg[i]=g[i-1]^(1<<(s[i]-'a')); \n\tmemset(f,0x3f,sizeof(f)...
p03725 AtCoder Grand Contest 014 - Closed Rooms
Takahashi is locked within a building. This building consists of H×W rooms, arranged in H rows and W columns. We will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= `#`, the room is locked and cannot be entered; if A_{i,j}= `.`, the r...
[ { "input": { "stdin": "3 3 3\n#.#\n#S.\n###" }, "output": { "stdout": "1" } }, { "input": { "stdin": "3 3 3\n.#\nS." }, "output": { "stdout": "1" } }, { "input": { "stdin": "3 3 3\n\nS#" }, "output": { "stdout": "2" } }, ...
{ "tags": [], "title": "p03725 AtCoder Grand Contest 014 - Closed Rooms" }
{ "cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int H, W, K;\n cin >> H >> W >> K;\n string S[800];\n for(int i=0; i<H; i++) cin >> S[i];\n int dist[800][800];\n queue<pair<int, int>> que;\n for(int i=0; i<H; i++) for(int j=0; j<W; j++){\n dist[i][j] = 1e9;\n ...
p03889 CODE FESTIVAL 2016 Relay (Parallel) - Mirror String
You are given a string S consisting of letters `b`, `d`, `p` and `q`. Determine whether S is a mirror string. Here, a mirror string is a string S such that the following sequence of operations on S results in the same string S: 1. Reverse the order of the characters in S. 2. Replace each occurrence of `b` by `d`, `d...
[ { "input": { "stdin": "ppqb" }, "output": { "stdout": "No" } }, { "input": { "stdin": "pdbq" }, "output": { "stdout": "Yes" } }, { "input": { "stdin": "dpqb" }, "output": { "stdout": "Yes\n" } }, { "input": { "...
{ "tags": [], "title": "p03889 CODE FESTIVAL 2016 Relay (Parallel) - Mirror String" }
{ "cpp": "#include<bits/stdc++.h>\nusing namespace std;\nstring a,b;\nint main()\n{\n cin>>a;\n b=a;\n for(int i=0;i<a.size()/2;++i)swap(a[a.size()-1-i],a[i]);\n for(int i=0;i<a.size();++i)\n {\n if(a[i]=='b')a[i]='d';\n else if(a[i]=='d')a[i]='b';\n else if(a[i]=='p')a[i]='q';\n ...
p04048 AtCoder Grand Contest 001 - Mysterious Light
Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of Mysterious Light. Three mirrors of length N are set so that they form an equilateral triangle. Let the vertices of the triangle be a, b and c. Inside the triangle, the rifle is placed at the point p on segment ab such that ap ...
[ { "input": { "stdin": "5 2" }, "output": { "stdout": "12" } }, { "input": { "stdin": "32 2" }, "output": { "stdout": "90\n" } }, { "input": { "stdin": "28 3" }, "output": { "stdout": "81\n" } }, { "input": { "s...
{ "tags": [], "title": "p04048 AtCoder Grand Contest 001 - Mysterious Light" }
{ "cpp": "#include <iostream>\nusing namespace std;\n\nlong ref(long i, long j){\n if(i%j==0) return j*(2*i/j-1);\n else return 2*j*(i/j)+ref(j, i%j);\n}\n\nint main() {\n\tlong N, X; cin >> N >> X;\n\tcout << N+ref(X, N-X) << endl;\n\treturn 0;\n}\n", "java": "import java.io.BufferedReader;\nimport java.io.I...