id stringlengths 11 112 | content stringlengths 42 13k | test listlengths 0 565 | labels dict | canonical_solution dict |
|---|---|---|---|---|
p03625 AtCoder Beginner Contest 071 - Make a Rectangle | We have N sticks with negligible thickness. The length of the i-th stick is A_i.
Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides. Find the maximum possible area of the rectangle.
Constraints
* 4 \leq N \leq 10^5
* 1 \leq A_i \leq ... | [
{
"input": {
"stdin": "10\n3 3 3 3 4 4 4 5 5 5"
},
"output": {
"stdout": "20"
}
},
{
"input": {
"stdin": "6\n3 1 2 4 2 1"
},
"output": {
"stdout": "2"
}
},
{
"input": {
"stdin": "4\n1 2 3 4"
},
"output": {
"stdout": "0"
}
... | {
"tags": [],
"title": "p03625 AtCoder Beginner Contest 071 - Make a Rectangle"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntypedef long long ll;\n\nint main() {\n\tint N; cin >> N;\n\tvector<ll> A(N);\n\tfor (int i = 0; i < N; i++) cin >> A[i];\n\tsort(A.begin(), A.end());\n\tvector<ll> L;\n\tfor (int i = N - 1; i >= 0; i--) {\n\t\tif (i >= 1 && A[i] == A[i - 1]) {\n\t\t\tL.push_... |
p03785 AtCoder Grand Contest 011 - Airport Bus | Every day, N passengers arrive at Takahashi Airport. The i-th passenger arrives at time T_i.
Every passenger arrived at Takahashi airport travels to the city by bus. Each bus can accommodate up to C passengers. Naturally, a passenger cannot take a bus that departs earlier than the airplane arrives at the airport. Also... | [
{
"input": {
"stdin": "5 3 5\n1\n2\n3\n6\n12"
},
"output": {
"stdout": "3"
}
},
{
"input": {
"stdin": "6 3 3\n7\n6\n2\n8\n10\n6"
},
"output": {
"stdout": "3"
}
},
{
"input": {
"stdin": "5 10 5\n1\n4\n3\n7\n12"
},
"output": {
"st... | {
"tags": [],
"title": "p03785 AtCoder Grand Contest 011 - Airport Bus"
} | {
"cpp": "#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst int nmax=100005;\nint t[nmax];\nint n,c,k,lst,ans,i;\nint main()\n{\n cin>>n>>c>>k;\n for(i=1;i<=n;i++)\n {\n cin>>t[i];\n }\n sort(t+1,t+n+1);\n lst=1;ans=1;\n for(i=2;i<=n;i++)\n {\n if(t[i]-t[lst]>... |
p03953 AtCoder Grand Contest 006 - Rabbit Exercise | There are N rabbits on a number line. The rabbits are conveniently numbered 1 through N. The coordinate of the initial position of rabbit i is x_i.
The rabbits will now take exercise on the number line, by performing sets described below. A set consists of M jumps. The j-th jump of a set is performed by rabbit a_j (2≤... | [
{
"input": {
"stdin": "3\n1 -1 1\n2 2\n2 2"
},
"output": {
"stdout": "1.0\n-1.0\n1.0"
}
},
{
"input": {
"stdin": "3\n-1 0 2\n1 1\n2"
},
"output": {
"stdout": "-1.0\n1.0\n2.0"
}
},
{
"input": {
"stdin": "5\n0 1 3 6 10\n3 10\n2 3 4"
},
... | {
"tags": [],
"title": "p03953 AtCoder Grand Contest 006 - Rabbit Exercise"
} | {
"cpp": "#include<bits/stdc++.h>\n\nusing namespace std;\n\nint P, N, M, x[100009], vv[100009], ans[100009], p[100009], ap[100009];\nlong long K;\n\nint main ()\n{\n//freopen (\"input\", \"r\", stdin);\n//freopen (\"output\", \"w\", stdout);\n\nscanf (\"%d\", &N);\n//M = 4, back (1);\n//return 0;\nfor (int i=1; i<=N... |
AIZU/p00043 | # Puzzle
There is a puzzle to complete by combining 14 numbers from 1 to 9. Complete by adding another number to the given 13 numbers.
The conditions for completing the puzzle are
* You must have one combination of the same numbers.
* The remaining 12 numbers are 4 combinations of 3 numbers.
The combination of three... | [
{
"input": {
"stdin": "3649596966777\n6358665788577\n9118992346175\n9643871425498\n7755542764533\n1133557799246"
},
"output": {
"stdout": "2 3 5 8\n3 4\n1 2 3 4 5 6 7 8 9\n7 8 9\n1 2 3 4 6 7 8\n0"
}
},
{
"input": {
"stdin": "3649596966777\n4672433573317\n9118992346175\n9643... | {
"tags": [],
"title": "Puzzle"
} | {
"cpp": "#include<queue>\n#include<stack>\n#include<math.h>\n#include<cmath>\n#include<bitset>\n#include<stdio.h>\n#include<string>\n#include<map>\n#include<algorithm>\n#include<vector>\n#include<iostream>\n#include<utility>\nusing namespace std;\ntypedef long long ll;\ntypedef pair<int,int> P;\nvector<int> ans(20);... |
AIZU/p00175 | # Quaternary Notation
Decimal numbers are a common notation system currently in use and use ten symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 to represent all numbers.
Binary numbers are a popular notation in the computer world and use two symbols, 0 and 1, to represent all numbers.
Only the four numbers 0, 1, 2, and 3 a... | [
{
"input": {
"stdin": "7\n4\n0\n12\n10\n10000\n-1"
},
"output": {
"stdout": "13\n10\n0\n30\n22\n2130100"
}
},
{
"input": {
"stdin": "7\n6\n0\n12\n2\n00000\n-1"
},
"output": {
"stdout": "13\n12\n0\n30\n2\n0\n"
}
},
{
"input": {
"stdin": "7\n7\... | {
"tags": [],
"title": "Quaternary Notation"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define for_(i,a,b) for(int i=(a);i<(b);++i)\n#define for_rev(i,a,b) for(int i=(a);i>=(b);--i)\n\nvoid convert(int x) {\n\tif (x == 0) {\n\t\tcout << 0 << endl;\n\t\treturn;\n\t}\n\t\n\tint mul = 16777216;\n\tbool flag = false;\n\t\n\tfor_rev(i,12,0) {\n\t\t... |
AIZU/p00331 | # Sunrise and Sunset
The appearance of the sun is called "sunrise" and the hiding is called "sunset". What is the exact time when the sun is on the horizon?
As shown in the figure below, we will represent the sun as a circle and the horizon as a straight line. At this time, the time of "sunrise" and "sunset" of the s... | [
{
"input": {
"stdin": "3 3"
},
"output": {
"stdout": "1"
}
},
{
"input": {
"stdin": "-3 3"
},
"output": {
"stdout": "0"
}
},
{
"input": {
"stdin": "-4 3"
},
"output": {
"stdout": "-1"
}
},
{
"input": {
"stdin":... | {
"tags": [],
"title": "Sunrise and Sunset"
} | {
"cpp": "#include<bits/stdc++.h>\nusing namespace std;\n\nint main(){\n int H,R;\n cin>>H>>R;\n\n if(H + R > 0) cout<<\"1\"<<endl;\n else if(H + R == 0) cout<<\"0\"<<endl;\n else if(H + R < 0) cout<<\"-1\"<<endl;\n\n\n return 0;\n}\n\n",
"java": "import java.util.Scanner;\n\npublic class Main {\n\tp... |
AIZU/p00504 | # Gifts
problem
JOI, who came to Australia for a trip, enjoyed sightseeing in various places and finally returned to Japan. Now, JOI is in the town with the international airport where the return flight departs. The town is divided into north, south, east and west, and each section has roads, souvenir shops, houses, ... | [
{
"input": {
"stdin": "5 4 2\n...#\n.#.#\n.#73\n8##.\n...."
},
"output": {
"stdout": "11"
}
},
{
"input": {
"stdin": "5 4 1\n.#-.\n.#.%\n48#.\n8#\".\n...."
},
"output": {
"stdout": "20\n"
}
},
{
"input": {
"stdin": "5 4 3\n..#-\n#..$\n37#.\n.... | {
"tags": [],
"title": "Gifts"
} | {
"cpp": "#include<bits/stdc++.h>\n#define rep(i,n) for(int i=0;i<(int)(n);i++)\n#define range(x,l,r) (l<=x && x<r)\n#define chmax(a,b) (a = max(a,b))\nusing namespace std;\n\nconst int dy[] = {0,1,0,-1}, dx[] = {1,0,-1,0};\nint dp[50][50][4][1<<12];\nint vis[1<<12];\n\ninline int visit(int bit){\n if(vis[bit]>=0)re... |
AIZU/p00688 | # Factorization of Quadratic Formula
As the first step in algebra, students learn quadratic formulas and their factorization. Often, the factorization is a severe burden for them. A large number of students cannot master the factorization; such students cannot be aware of the elegance of advanced algebra. It might be ... | [
{
"input": {
"stdin": "2 5 2\n1 1 1\n10 -7 0\n1 0 -3\n0 0 0"
},
"output": {
"stdout": "2 1 1 2\nImpossible\n10 -7 1 0\nImpossible"
}
},
{
"input": {
"stdin": "3 10 3\n1 2 1\n10 -11 0\n1 0 1\n0 0 0"
},
"output": {
"stdout": "3 1 1 3\n1 1 1 1\n10 -11 1 0\nImposs... | {
"tags": [],
"title": "Factorization of Quadratic Formula"
} | {
"cpp": "#include <iostream>\n#include <algorithm>\n#include <cassert>\n#include <cctype>\n#include <complex>\n#include <cstdio>\n#include <map>\n#include <math.h>\n#include <queue>\n#include <set>\n#include <stack>\n#include <string>\n#include <vector>\nusing namespace std;\n\n#define rep(i,n) for(int i=0;i<n;i++)\... |
AIZU/p00830 | # Pathological Paths
Professor Pathfinder is a distinguished authority on the structure of hyperlinks in the World Wide Web. For establishing his hypotheses, he has been developing software agents, which automatically traverse hyperlinks and analyze the structure of the Web. Today, he has gotten an intriguing idea to ... | [
{
"input": {
"stdin": "5 6\n/home/ACM/index.html\n/ICPC/index.html\n/ICPC/general.html\n/ICPC/japanese/index.html\n/ICPC/secret/confidential/2005/index.html\n/home/ACM/\n/home/ICPC/../ACM/\n/ICPC/secret/\n/ICPC/secret/index.html\n/ICPC\n/ICPC/../ICPC/index.html\n/ICPC\n/ICPC/general.html\n/ICPC/japanese/.... | {
"tags": [],
"title": "Pathological Paths"
} | {
"cpp": "#include <iostream>\n#include <vector>\n#include <string>\n#include <cstring>\n#include <algorithm>\n#include <sstream>\n#include <map>\n#include <set>\n\n#define REP(i,k,n) for(int i=k;i<n;i++)\n#define rep(i,n) for(int i=0;i<n;i++)\n#define INF 1<<30\n#define pb push_back\n#define mp make_pair\n\nusing na... |
AIZU/p00961 | # Black or White
Problem E Black or White
Here lies a row of a number of bricks each painted either black or white. With a single stroke of your brush, you can overpaint a part of the row of bricks at once with either black or white paint. Using white paint, all the black bricks in the painted part become white while... | [
{
"input": {
"stdin": "4 4\nBWWB\nWBBW"
},
"output": {
"stdout": "2"
}
},
{
"input": {
"stdin": "1 5\nBWBV\nWBBW"
},
"output": {
"stdout": "1\n"
}
},
{
"input": {
"stdin": "1 31\nBWWB\nWBBW"
},
"output": {
"stdout": "1\n"
}
... | {
"tags": [],
"title": "Black or White"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\nusing pii = pair<int, int>;\n\nint main() {\n int n, k; cin >> n >> k;\n string s, t; cin >> s >> t;\n\n vector<int> sum(n + 1);\n for(int i = 0; i < n; ++i) {\n sum[i + 1] = sum[i] + (i == 0 || t[i - 1] != t[i]);\n }\n\n vector<int>... |
AIZU/p01094 | # Look for the Winner!
Look for the Winner!
The citizens of TKB City are famous for their deep love in elections and vote counting. Today they hold an election for the next chairperson of the electoral commission. Now the voting has just been closed and the counting is going to start. The TKB citizens have strong des... | [
{
"input": {
"stdin": "1\nA\n4\nA A B B\n5\nL M N L N\n6\nK K K K K K\n6\nX X X Y Z X\n10\nA A A B A C A C C B\n10\nU U U U U V V W W W\n0"
},
"output": {
"stdout": "A 1\nTIE\nTIE\nK 4\nX 5\nA 7\nU 8"
}
},
{
"input": {
"stdin": "1\nA\n4\nA A B B\n5\nL M M L N\n6\nK K K K K ... | {
"tags": [],
"title": "Look for the Winner!"
} | {
"cpp": "#include<iostream>\n#include<cstdio>\n#include<algorithm>\n#include<string>\n#include<vector>\n#include<queue>\n#include<set>\n#include<functional>\n#include<map>\n#define P pair<int,int>\nusing namespace std;\n\nint main() {\n\tint a;\n\twhile (cin >> a, a) {\n\t\tchar t[100];\n\t\tfor (int i = 0; i < a; i... |
AIZU/p01230 | # Can I go there?
At one point, there was a president who traveled around Japan to do business. One day he got a mysterious ticket. If you use the ticket, the train fare will be free regardless of the distance to the destination. However, when moving from the current station to the adjacent station is counted as one s... | [
{
"input": {
"stdin": "2 1 1\n1 2\n2 1 2\n1 2\n3 1 2\n1 2\n8 8 5778\n1 2\n2 3\n2 4\n3 5\n5 6\n6 7\n7 8\n4 8\n8 8 5777\n1 2\n2 3\n2 4\n3 5\n5 6\n6 7\n7 8\n4 8\n0 0 0"
},
"output": {
"stdout": "yes\nno\nno\nyes\nno"
}
},
{
"input": {
"stdin": "2 1 1\n1 2\n2 1 2\n1 4\n3 1 2\n1... | {
"tags": [],
"title": "Can I go there?"
} | {
"cpp": "#include<cstdio>\n#include<math.h>\n#include<algorithm>\n#include<vector>\n#include<queue>\n#include<string.h>\n \nusing namespace std;\n#define rep(i,n) for(int i=0;i<n;i++)\n#define INF 1001001001\n#define LLINF 1001001001001001001\n#define mp make_pair\n#define LLIandI pair<long long int , int>\n#define ... |
AIZU/p01364 | # Two-Wheel Buggy
International Car Production Company (ICPC), one of the largest automobile manufacturers in the world, is now developing a new vehicle called "Two-Wheel Buggy". As its name suggests, the vehicle has only two wheels. Quite simply, "Two-Wheel Buggy" is made up of two wheels (the left wheel and the righ... | [
{
"input": {
"stdin": "1 1\n180 90 2\n1 1\n180 180 20\n2 10\n360 -360 5\n-90 360 8\n3 2\n100 60 9\n-72 -72 10\n-45 -225 5\n0 0"
},
"output": {
"stdout": "3.00000\n3.00000\n0.00000\n62.83185\n12.00000\n0.00000\n-2.44505\n13.12132"
}
},
{
"input": {
"stdin": "1 1\n180 175 2\n... | {
"tags": [],
"title": "Two-Wheel Buggy"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\n\ntypedef long long ll;\n\n#define fi first\n#define se second\n#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)\n#define rep(i,n) repl(i,0,n)\n#define all(x) (x).begin(),(x).end()\n#define dbg(x) cout<<#x\"=\"<<x<<endl\n#define mmax(x,y) (x>y?x:y)\n#define... |
AIZU/p01546 | # Sports Days 2
The University of Aizu Elementary School (Aizu University and Small) is famous as one of Japan's leading competition programmer training schools. Of course, it is essential to practice the algorithm even when attending an athletic meet. Of course you, the director of the competitive programming departm... | [
{
"input": {
"stdin": "7 8 4000\n0 1 1\n1 0 1\n1 2 2\n2 3 2\n3 4 2\n5 4 3\n3 5 4\n5 6 5"
},
"output": {
"stdout": "3991"
}
},
{
"input": {
"stdin": "2 0 1"
},
"output": {
"stdout": "-1"
}
},
{
"input": {
"stdin": "3 9 89\n2 0 2\n1 0 3\n2 0 1\... | {
"tags": [],
"title": "Sports Days 2"
} | {
"cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nusing int64 = long long;\nconst int64 LINF = 1LL << 60;\nconst int INF = 1 << 26;\n\nint V, E, K;\nint64 g[26][150][150];\n\nint main() {\n fill_n(**g, 26 * 150 * 150, -LINF);\n\n cin >> V >> E >> K;\n for(int i = 0; i < V; i++) {\n g[0][i][i] = 0;\n... |
AIZU/p01702 | # Unknown Switches
Problem Statement
In the headquarter building of ICPC (International Company of Plugs & Connectors), there are $M$ light bulbs and they are controlled by $N$ switches. Each light bulb can be turned on or off by exactly one switch. Each switch may control multiple light bulbs. When you operate a swi... | [
{
"input": {
"stdin": "3 10 3\n000 0000000000\n110 0000001111\n101 1111111100\n2 2 0\n1 1 0\n2 1 1\n01 1\n11 11 10\n10000000000 10000000000\n11000000000 01000000000\n01100000000 00100000000\n00110000000 00010000000\n00011000000 00001000000\n00001100000 00000100000\n00000110000 00000010000\n00000011000 000... | {
"tags": [],
"title": "Unknown Switches"
} | {
"cpp": "#define _USE_MATH_DEFINES\n#include <iostream>\n#include <fstream>\n#include <sstream>\n#include <string>\n#include <cstring>\n#include <vector>\n#include <set>\n#include <map>\n#include <unordered_map>\n#include <queue>\n#include <deque>\n#include <stack>\n#include <algorithm>\n#include <bitset>\n#include ... |
AIZU/p01846 | # jfen
jfen
There is a one-person game to play on the H × W board. This game is a game to move 0 or 1 balls in each cell. You were playing this game and found it difficult to move the ball accurately from cell to cell because the ball is so round. So you decided to make a robot that would move the ball as instructed.... | [
{
"input": {
"stdin": "b1/1b\n1 1 1 2\nb5/bbbbbb\n2 4 1 4\nb2b2b/7\n1 4 2 4\n#"
},
"output": {
"stdout": "1b/1b\nb2b2/bbb1bb\nb5b/3b3"
}
},
{
"input": {
"stdin": "b1/1b\n1 1 1 2\nb5/bbbbbb\n2 8 0 5\nb2b2b/7\n1 4 2 6\n#"
},
"output": {
"stdout": "1b/1b\nb5/bbbb... | {
"tags": [],
"title": "jfen"
} | {
"cpp": "#include <iostream>\n#include <iomanip>\n#include <cstdio>\n#include <string>\n#include <cstring>\n#include <deque>\n#include <list>\n#include <queue>\n#include <stack>\n#include <vector>\n#include <utility>\n#include <algorithm>\n#include <map>\n#include <set>\n#include <complex>\n#include <cmath>\n#includ... |
AIZU/p01982 | # Generalized Leap Years
Generalized leap year
Normally, whether or not the year x is a leap year is defined as follows.
1. If x is a multiple of 400, it is a leap year.
2. Otherwise, if x is a multiple of 100, it is not a leap year.
3. Otherwise, if x is a multiple of 4, it is a leap year.
4. If not, it is not a le... | [
{
"input": {
"stdin": "3 1988 2014\n400\n100\n4\n1 1000 1999\n1\n2 1111 3333\n2\n2\n6 2000 3000\n5\n7\n11\n9\n3\n13\n0 0 0"
},
"output": {
"stdout": "7\n1000\n2223\n785"
}
},
{
"input": {
"stdin": "3 322 2014\n400\n100\n4\n1 1000 1999\n1\n2 1111 4526\n1\n2\n6 2000 3409\n5\n... | {
"tags": [],
"title": "Generalized Leap Years"
} | {
"cpp": "#include <cstdio>\n#include <iostream>\n#include <string>\n#include <vector>\n#include <sstream>\n#include <map>\n#include <set>\n#include <queue>\n#include <algorithm>\n#include <cmath>\n#include <cstring>\n#include <typeinfo>\n#include <numeric>\n#include <functional>\n#include <unordered_map>\n#include <... |
AIZU/p02128 | # Light
Problem
There are $ N $ streetlights on a two-dimensional square of $ W \ times H $.
Gaccho wants to start with $ (1,1) $ and go to $ (W, H) $.
Gaccho is afraid of dark places, so he only wants to walk in the squares that are brightened by the streetlights.
Initially, all streetlights only brighten the square... | [
{
"input": {
"stdin": "1 1 1\n1 1"
},
"output": {
"stdout": "0"
}
},
{
"input": {
"stdin": "5 10 3\n3 9\n2 8\n5 1"
},
"output": {
"stdout": "8"
}
},
{
"input": {
"stdin": "10 10 1\n6 6"
},
"output": {
"stdout": "10"
}
},
... | {
"tags": [],
"title": "Light"
} | {
"cpp": "#define __USE_MINGW_ANSI_STDIO 0\n#include <bits/stdc++.h>\n\nusing namespace std;\nusing ll = long long;\n#define int ll\nusing VI = vector<int>;\nusing VVI = vector<VI>;\nusing PII = pair<int, int>;\n\n#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)\n#define REP(i, n) FOR(i, 0, n)\n#define ALL(x) ... |
AIZU/p02269 | # Dictionary
Your task is to write a program of a simple dictionary which implements the following instructions:
* insert str: insert a string str in to the dictionary
* find str: if the distionary contains str, then print 'yes', otherwise print 'no'
Notes
Template in C
Constraints
* A string consists of 'A', 'C'... | [
{
"input": {
"stdin": "5\ninsert A\ninsert T\ninsert C\nfind G\nfind A"
},
"output": {
"stdout": "no\nyes"
}
},
{
"input": {
"stdin": "13\ninsert AAA\ninsert AAC\ninsert AGA\ninsert AGG\ninsert TTT\nfind AAA\nfind CCC\nfind CCC\ninsert CCC\nfind CCC\ninsert T\nfind TTT\nfin... | {
"tags": [],
"title": "Dictionary"
} | {
"cpp": "#include <iostream>\n#include <cstdio>\n#include <string>\n#include <set>\nusing namespace std;\n#define rep(x,to) for(int x=0; x<(to); (x)++)\n\n\nint main() {\n\tint n;\n\tchar s[10];\n\tstring t;\n\tset<string> Set;\n\tcin >> n;\n\trep(i,n) {\n\t\tscanf(\"%s\", s);\n\t\tif(s[0] == 'i') {\n\t\t\tcin >> t;... |
AIZU/p02416 | # Sum of Numbers
Write a program which reads an integer and prints sum of its digits.
Input
The input consists of multiple datasets. For each dataset, an integer x is given in a line. The number of digits in x does not exceed 1000.
The input ends with a line including single zero. Your program should not process ... | [
{
"input": {
"stdin": "123\n55\n1000\n0"
},
"output": {
"stdout": "6\n10\n1"
}
},
{
"input": {
"stdin": "123\n37\n1100\n0"
},
"output": {
"stdout": "6\n10\n2\n"
}
},
{
"input": {
"stdin": "123\n69\n1000\n0"
},
"output": {
"stdou... | {
"tags": [],
"title": "Sum of Numbers"
} | {
"cpp": "#include <cstdio>\n\nint main()\n{\n\tchar ch;\n\tint n = 0;\n\n\twhile ((ch = getchar())!= '0' || n != 0)\n\t{\n\t\tif (ch == '\\n')\n\t\t{\n\t\t\tprintf(\"%d\\n\",n);\n\t\t\tn = 0;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tch &= 0x0F;\n\t\t\tn += ch;\n\t\t}\n\t}\n\treturn 0;\n}",
"java": "import java.util.*;\n\npu... |
CodeChef/btreat | Its Chef's Birthday and his friends are demanding him for treat. So he went to the shop to buy N gift packets (one for each friend). Each gift packet bought from shop contains some number of chocolates in it.
Chef believes in equality i.e. all gift packets should contain equal number of chocolates in it. So Chef thou... | [
{
"input": {
"stdin": "2\n4\n5 2 4 5\n6\n5 2 4 1 7 5"
},
"output": {
"stdout": "2\n5"
}
}
] | {
"tags": [],
"title": "btreat"
} | {
"cpp": null,
"java": null,
"python": "import sys\n\nfor t in xrange(int(sys.stdin.readline())):\n\tn=int(sys.stdin.readline())\n\tarr=map(int,sys.stdin.readline().split())\n\ts=sum(arr)\n\tif s%n ==0:\n\t\tavg=s/n\n\t\tc=0\n\t\tfor i in xrange(n):\n\t\t\tc+=abs(avg-arr[i])\n\t\tprint c/2\n\telse:\n\t\tprint \"N... |
CodeChef/compiler | Lira is now very keen on compiler development. :)
She knows that one of the most important components of a compiler, is its parser.
A parser is, in simple terms, a software component that processes text, and checks it's semantic correctness, or, if you prefer, if the text is properly built.
As an example, in declaring... | [
{
"input": {
"stdin": "3\n<<>>\n><\n<>>>"
},
"output": {
"stdout": "4\n0\n2\n"
}
},
{
"input": {
"stdin": "3\n<<>>\n><\n>>><"
},
"output": {
"stdout": "4\n0\n0\n"
}
},
{
"input": {
"stdin": "3\n<<>>\n><\n>>?<"
},
"output": {
"st... | {
"tags": [],
"title": "compiler"
} | {
"cpp": null,
"java": null,
"python": "#!/usr/bin/env python2\n# -*- coding: utf-8 -*-\n\n## Codechef Practice: Easy\n## Compilers and parsers\n## tags: easy, may challenge 2014 (may14)\n\ntry:\n import psyco\n psyco.full()\nexcept ImportError:\n pass\n\n\ndef main(A):\n sol = 0 # max length\n qu... |
CodeChef/gdog | Tuzik is a little dog. But despite the fact he is still a puppy he already knows about the pretty things that coins are. He knows that for every coin he can get very tasty bone from his master. He believes that some day he will find a treasure and have loads of bones.
And finally he found something interesting. A woode... | [
{
"input": {
"stdin": "2\n5 2\n11 3"
},
"output": {
"stdout": "1\n2\n"
}
},
{
"input": {
"stdin": "2\n10 16\n14 18"
},
"output": {
"stdout": "10\n14\n"
}
},
{
"input": {
"stdin": "2\n10 7\n8 10"
},
"output": {
"stdout": "4\n8\n"... | {
"tags": [],
"title": "gdog"
} | {
"cpp": null,
"java": null,
"python": "t=int(raw_input())\nfor i in range(t):\n a,b=map(int,raw_input().split())\n max=int(0)\n if b>a:\n \tmax=a\n else:\n \tfor i in range(2,b+1):\n \t\tif a%i>max:\n \t\t\tmax=a%i\n print max"
} |
CodeChef/m3 | The new season of the Bytelandian Premier League (BPL) has started!
In the BPL, any two soccer teams play with each other exactly once. In each match, the winner earns 3 points and the loser earns no point. There is no draw (if the match is level after the two halves, two teams will take part in a penalty shootout to d... | [
{
"input": {
"stdin": "3\n3\n0 0 0 \n1 0 1 \n1 0 0 \n4\n0 1 1 0 \n0 0 2 0 \n0 2 0 0 \n1 1 1 0 \n5\n0 2 2 1 0 \n2 0 1 1 0 \n2 0 0 1 0 \n0 0 0 0 1 \n1 1 1 0 0"
},
"output": {
"stdout": "010\n0001\n11001"
}
}
] | {
"tags": [],
"title": "m3"
} | {
"cpp": null,
"java": null,
"python": "#!/usr/bin/python\n# Richard Lee\nimport os\nimport sys\n\ndef champion_possible(team_id, r_m, c_list):\n def team_sort(x, y):\n rx = r[x]\n ry = r[y]\n if rx == ry:\n return p[y] - p[x]\n return rx - ry\n \n def team_sort_r(x, y):\n r... |
CodeChef/prys01 | Problem Statement
N knights were sitting at a round table and having a serious talk. The knights are so tired, that they can’t even rotate their neck to talk to talk to the knights sitting on their sides. So, a knight can only talk to the knight sitting diametrically opposite to him on the table.
Given the number of kn... | [
{
"input": {
"stdin": "1\n6 2"
},
"output": {
"stdout": "5"
}
},
{
"input": {
"stdin": "1\n-4 -11"
},
"output": {
"stdout": "-13\n"
}
},
{
"input": {
"stdin": "1\n20 3"
},
"output": {
"stdout": "13\n"
}
},
{
"input":... | {
"tags": [],
"title": "prys01"
} | {
"cpp": null,
"java": null,
"python": "#chiragjn\nt=int(raw_input())\nfor i in xrange(t):\n\tn,m=map(int,raw_input().split())\n\tprint m+(n>>1) if m<=n/2 else m-(n>>1)"
} |
CodeChef/tidrice | Did you know that there are over 40,000 varieties of Rice in the world ? There are so many dishes that can be prepared with Rice too. A famous chef from Mumbai, Tid Gusto prepared a new dish and named it 'Tid Rice'. He posted the recipe in his newly designed blog for community voting, where a user can plus (+) or minus... | [
{
"input": {
"stdin": "3\n4\ntilak +\ntilak +\ntilak -\ntilak +\n3\nratna +\nshashi -\nratna -\n3\nbhavani -\nbhavani +\nbhavani -"
},
"output": {
"stdout": "1\n-2\n-1\n"
}
},
{
"input": {
"stdin": "3\n4\nuilak +\ntilak +\ntilak -\ntilaj +\n3\nratna +\nshathi -\nranta -\n3\... | {
"tags": [],
"title": "tidrice"
} | {
"cpp": null,
"java": null,
"python": "for testcases in xrange(int(raw_input())):\n n = int(raw_input() )\n Dic = {}\n for i in xrange(n) :\n user, score = map(str, raw_input().split())\n Dic[user] = score\n \n Scores = list( Dic.values() )\n ans = 0\n for point in Scores :\n ... |
Codeforces/1013/A | # Piles With Stones
There is a beautiful garden of stones in Innopolis.
Its most beautiful place is the n piles with stones numbered from 1 to n.
EJOI participants have visited this place twice.
When they first visited it, the number of stones in piles was x_1, x_2, …, x_n, correspondingly. One of the participants... | [
{
"input": {
"stdin": "5\n1 2 3 4 5\n2 1 4 3 5\n"
},
"output": {
"stdout": "Yes\n"
}
},
{
"input": {
"stdin": "5\n1 1 1 1 1\n1 0 1 0 1\n"
},
"output": {
"stdout": "Yes\n"
}
},
{
"input": {
"stdin": "3\n2 3 9\n1 7 9\n"
},
"output": {
... | {
"tags": [
"math"
],
"title": "Piles With Stones"
} | {
"cpp": "#include <bits/stdc++.h>\nint main() {\n int suma = 0, sumb = 0;\n int a, b;\n int n;\n scanf(\"%d\", &n);\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &a);\n suma += a;\n }\n for (int i = 1; i <= n; i++) {\n scanf(\"%d\", &b);\n sumb += b;\n }\n if (suma >= sumb)\n puts(\"Yes\")... |
Codeforces/1037/G | # A Game on Strings
Alice and Bob are playing a game on strings.
Initially, they have some string t. In one move the first player selects the character c present in t and erases all it's occurrences in t, thus splitting t into many smaller strings. The game then goes independently with each of the strings — to make t... | [
{
"input": {
"stdin": "aaccbdb\n2\n5 7\n1 7\n"
},
"output": {
"stdout": "Alice\nAlice\n"
}
},
{
"input": {
"stdin": "aaab\n2\n1 2\n1 4\n"
},
"output": {
"stdout": "Alice\nBob\n"
}
},
{
"input": {
"stdin": "mrxphlhblv\n10\n3 5\n5 5\n2 9\n3 7\n... | {
"tags": [
"games"
],
"title": "A Game on Strings"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar s[109999];\nint n;\nvector<int> ap[26], qz[26];\npair<pair<int, int>, pair<int, int> > qs[5800999];\nint qn = 0;\nbool cmp(pair<pair<int, int>, pair<int, int> > a,\n pair<pair<int, int>, pair<int, int> > b) {\n if (a.first.second != b.first.secon... |
Codeforces/1060/C | # Maximum Subrectangle
You are given two arrays a and b of positive integers, with length n and m respectively.
Let c be an n × m matrix, where c_{i,j} = a_i ⋅ b_j.
You need to find a subrectangle of the matrix c such that the sum of its elements is at most x, and its area (the total number of elements) is the lar... | [
{
"input": {
"stdin": "5 1\n5 4 2 4 5\n2\n5\n"
},
"output": {
"stdout": "1"
}
},
{
"input": {
"stdin": "3 3\n1 2 3\n1 2 3\n9\n"
},
"output": {
"stdout": "4"
}
},
{
"input": {
"stdin": "100 1\n1525 1915 925 1023 803 904 802 1943 954 23 258 505... | {
"tags": [
"binary search",
"implementation",
"two pointers"
],
"title": "Maximum Subrectangle"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long maxn = 2000 + 100;\nconst long long INF = 0x3f3f3f3f;\nlong long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }\nlong long lowbit(long long x) { return x & -x; }\nlong long a[maxn];\nlong long b[maxn];\nlong long ss[maxn];\nlon... |
Codeforces/1082/A | # Vasya and Book
Vasya is reading a e-book. The file of the book consists of n pages, numbered from 1 to n. The screen is currently displaying the contents of page x, and Vasya wants to read the page y. There are two buttons on the book which allow Vasya to scroll d pages forwards or backwards (but he cannot scroll ou... | [
{
"input": {
"stdin": "3\n10 4 5 2\n5 1 3 4\n20 4 19 3\n"
},
"output": {
"stdout": "4\n-1\n5\n"
}
},
{
"input": {
"stdin": "1\n123 123 123 123\n"
},
"output": {
"stdout": "0\n"
}
},
{
"input": {
"stdin": "1\n1000000000 5 999999999 1\n"
},... | {
"tags": [
"implementation",
"math"
],
"title": "Vasya and Book"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nstring to_string(string s) { return '\"' + s + '\"'; }\nstring to_string(const char* s) { return to_string((string)s); }\nstring to_string(bool b) { return (b ? \"true\" : \"false\"); }\ntemplate <typename A, typename B>\nstring to_string(pair<A, B> p) {\n re... |
Codeforces/1101/C | # Division and Union
There are n segments [l_i, r_i] for 1 ≤ i ≤ n. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group.
... | [
{
"input": {
"stdin": "3\n2\n5 5\n2 3\n3\n3 5\n2 3\n2 3\n3\n3 3\n4 4\n5 5\n"
},
"output": {
"stdout": "2 1 \n-1\n1 2 2 \n"
}
},
{
"input": {
"stdin": "3\n2\n3 5\n2 3\n3\n1 5\n1 5\n2 3\n3\n3 3\n4 4\n0 4\n"
},
"output": {
"stdout": "-1\n-1\n-1\n"
}
},
{
... | {
"tags": [
"sortings"
],
"title": "Division and Union"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long t;\n cin >> t;\n while (t--) {\n long long n;\n cin >> n;\n long long i, a[n];\n pair<pair<long long, long long>, long long> p[n];\n for (i = 0; i < n; i++) {\n cin >> p[i].first.first >> p[i].first.second;\n ... |
Codeforces/112/A | # Petya and Strings
Little Petya loves presents. His mum bought him two strings of the same size for his birthday. The strings consist of uppercase and lowercase Latin letters. Now Petya wants to compare those two strings lexicographically. The letters' case does not matter, that is an uppercase letter is considered e... | [
{
"input": {
"stdin": "aaaa\naaaA\n"
},
"output": {
"stdout": "0\n"
}
},
{
"input": {
"stdin": "abs\nAbz\n"
},
"output": {
"stdout": "-1\n"
}
},
{
"input": {
"stdin": "abcdefg\nAbCdEfF\n"
},
"output": {
"stdout": "1\n"
}
}... | {
"tags": [
"implementation",
"strings"
],
"title": "Petya and Strings"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string str;\n cin >> str;\n vector<char> a(str.begin(), str.end());\n cin >> str;\n vector<char> b(str.begin(), str.end());\n int res = 0;\n for (int i = 0; i < a.size(); i++) {\n if (a[i] >= 'a') a[i] += -'a' + 'A';\n if (b[i] >= '... |
Codeforces/114/B | # PFAST Inc.
When little Petya grew up and entered the university, he started to take part in АСМ contests. Later he realized that he doesn't like how the АСМ contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the te... | [
{
"input": {
"stdin": "3 0\nPasha\nLesha\nVanya\n"
},
"output": {
"stdout": "3\nLesha\nPasha\nVanya\n"
}
},
{
"input": {
"stdin": "3 1\nPetya\nVasya\nMasha\nPetya Vasya\n"
},
"output": {
"stdout": "2\nMasha\nPetya\n"
}
},
{
"input": {
"stdin"... | {
"tags": [
"bitmasks",
"brute force",
"graphs"
],
"title": "PFAST Inc."
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint a[17];\nchar s[20][20];\nint n, m;\nint chage(char *str) {\n for (int i = 0; i < n; i++)\n if (!strcmp(s[i], str)) return i;\n}\nint mapp[20][20];\nint chage2(int x) {\n int sum = 0;\n while (x) {\n x &= x - 1;\n sum++;\n }\n return sum;\n}\n... |
Codeforces/1170/G | # Graph Decomposition
You are given an undirected graph consisting of n vertices and m edges.
Recall that a cycle is a path that starts and ends in the same vertex. A cycle in a graph is called simple if it contains each vertex (except the starting and ending one) no more than once (the starting and the ending one is... | [
{
"input": {
"stdin": "6 9\n1 2\n2 3\n1 3\n2 4\n2 5\n4 5\n3 5\n3 6\n5 6\n"
},
"output": {
"stdout": "\nYES\n3\n4 2 5 4 2 \n4 3 6 5 3 \n4 1 3 2 1 \n"
}
},
{
"input": {
"stdin": "4 7\n1 1\n1 2\n2 3\n3 4\n4 1\n1 3\n1 3\n"
},
"output": {
"stdout": "\nYES\n3\n2 1 1... | {
"tags": [
"*special",
"graphs"
],
"title": "Graph Decomposition"
} | {
"cpp": null,
"java": null,
"python": null
} |
Codeforces/1189/E | # Count Pairs
You are given a prime number p, n integers a_1, a_2, …, a_n, and an integer k.
Find the number of pairs of indexes (i, j) (1 ≤ i < j ≤ n) for which (a_i + a_j)(a_i^2 + a_j^2) ≡ k mod p.
Input
The first line contains integers n, p, k (2 ≤ n ≤ 3 ⋅ 10^5, 2 ≤ p ≤ 10^9, 0 ≤ k ≤ p-1). p is guaranteed to be... | [
{
"input": {
"stdin": "6 7 2\n1 2 3 4 5 6\n"
},
"output": {
"stdout": "3"
}
},
{
"input": {
"stdin": "3 3 0\n0 1 2\n"
},
"output": {
"stdout": "1"
}
},
{
"input": {
"stdin": "3 3 0\n0 2 1\n"
},
"output": {
"stdout": "1"
}
... | {
"tags": [
"math",
"matrices",
"number theory",
"two pointers"
],
"title": "Count Pairs"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n long long int n, p, k;\n cin >> n >> p >> k;\n map<long long int, long long int> count;\n long long int ans = 0;\n long long int t;\n for (int i = 0; i < n; i++) {\n cin >> t;\n t = ((t * k) - (((t * t) % p) * ((t * t) % p) % p) + p)... |
Codeforces/1208/F | # Bits And Pieces
You are given an array a of n integers.
You need to find the maximum value of a_{i} | ( a_{j} \& a_{k} ) over all triplets (i,j,k) such that i < j < k.
Here \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND), and | denotes the [bitwise OR operation](https://... | [
{
"input": {
"stdin": "3\n2 4 6\n"
},
"output": {
"stdout": "6\n"
}
},
{
"input": {
"stdin": "4\n2 8 4 7\n"
},
"output": {
"stdout": "12\n"
}
},
{
"input": {
"stdin": "71\n25 25 91 91 61 57 99 25 93 89 99 29 31 25 29 89 93 89 91 27 95 95 25 5... | {
"tags": [
"bitmasks",
"dfs and similar",
"dp",
"greedy"
],
"title": "Bits And Pieces"
} | {
"cpp": "#include <bits/stdc++.h>\ntemplate <class T>\ninline void read(T &res) {\n res = 0;\n bool bo = 0;\n char c;\n while (((c = getchar()) < '0' || c > '9') && c != '-')\n ;\n if (c == '-')\n bo = 1;\n else\n res = c - 48;\n while ((c = getchar()) >= '0' && c <= '9')\n res = (res << 3) + (res... |
Codeforces/1227/E | # Arson In Berland Forest
The Berland Forest can be represented as an infinite cell plane. Every cell contains a tree. That is, contained before the recent events.
A destructive fire raged through the Forest, and several trees were damaged by it. Precisely speaking, you have a n × m rectangle map which represents the... | [
{
"input": {
"stdin": "3 6\nXXXXXX\nXXXXXX\nXXXXXX\n"
},
"output": {
"stdout": "1\n......\n.XXXX.\n......\n"
}
},
{
"input": {
"stdin": "10 10\n.XXXXXX...\n.XXXXXX...\n.XXXXXX...\n.XXXXXX...\n.XXXXXXXX.\n...XXXXXX.\n...XXXXXX.\n...XXXXXX.\n...XXXXXX.\n..........\n"
},
... | {
"tags": [
"binary search",
"graphs",
"graphs",
"shortest paths"
],
"title": "Arson In Berland Forest"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nbool start[1200001];\nvector<int> g[1200001];\nvector<int> startV;\nint cnt = 0;\nvector<int> bfs(vector<int> s) {\n vector<int> d(cnt + 1, -1);\n queue<int> q;\n for (auto i : s) q.push(i), d[i] = 0;\n while (!q.empty()) {\n int v = q.front();\n q.p... |
Codeforces/1250/D | # Conference Problem
A large-scale conference on unnatural sciences is going to be held soon in Berland! In total, n scientists from all around the world have applied. All of them have indicated a time segment when they will attend the conference: two integers l_i, r_i — day of arrival and day of departure.
Also, som... | [
{
"input": {
"stdin": "2\n4\n1 10 30\n5 6 30\n6 12 0\n1 1 0\n4\n1 2 1\n2 3 0\n3 4 0\n4 5 2\n"
},
"output": {
"stdout": "\n4\n2\n"
}
}
] | {
"tags": [
"dp"
],
"title": "Conference Problem"
} | {
"cpp": null,
"java": null,
"python": null
} |
Codeforces/126/B | # Password
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.
A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that... | [
{
"input": {
"stdin": "abcdabc\n"
},
"output": {
"stdout": "Just a legend\n"
}
},
{
"input": {
"stdin": "fixprefixsuffix\n"
},
"output": {
"stdout": "fix\n"
}
},
{
"input": {
"stdin": "aaabaabaaaaab\n"
},
"output": {
"stdout": "... | {
"tags": [
"binary search",
"dp",
"hashing",
"string suffix structures",
"strings"
],
"title": "Password"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int Alp = 26;\nconst int __PRECISION = 9;\nconst int inf = 1e9 + 8;\nconst long double PI = acos(-1);\nconst long double EPS = 1e-7;\nconst long long MOD = 1e9 + 7;\nconst long long MAXN = 2e5 + 5;\nconst long long ROOTN = 320;\nconst long long LOGN = 18... |
Codeforces/1292/A | # NEKO's Maze Game
[3R2 as DJ Mashiro - Happiness Breeze](https://open.spotify.com/track/2qGqK8GRS65Wlf20qUBEak)
[Ice - DJ Mashiro is dead or alive](https://soundcloud.com/iceloki/dj-mashiro-is-dead-or-alive)
NEKO#ΦωΦ has just got a new maze game on her PC!
The game's main puzzle is a maze, in the forms of a 2 × n ... | [
{
"input": {
"stdin": "5 5\n2 3\n1 4\n2 4\n2 3\n1 4\n"
},
"output": {
"stdout": "Yes\nNo\nNo\nNo\nYes\n"
}
},
{
"input": {
"stdin": "73034 53\n2 21523\n1 21522\n2 21523\n2 21521\n2 37146\n2 21521\n2 21521\n2 21521\n1 37145\n2 37146\n1 54737\n2 66924\n2 21521\n2 28767\n2 215... | {
"tags": [
"data structures",
"dsu",
"implementation"
],
"title": "NEKO's Maze Game"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nmt19937_64 rang(\n chrono::high_resolution_clock::now().time_since_epoch().count());\nint rng(int lim) {\n uniform_int_distribution<int> uid(0, lim - 1);\n return uid(rang);\n}\nconst int INF = 0x3f3f3f3f;\nconst int mod = 1e9 + 7;\nvoid solve() {\n int ... |
Codeforces/1312/B | # Bogosort
You are given an array a_1, a_2, ... , a_n. Array is good if for each pair of indexes i < j the condition j - a_j ≠ i - a_i holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
For example, if... | [
{
"input": {
"stdin": "3\n1\n7\n4\n1 1 3 5\n6\n3 2 1 5 6 4\n"
},
"output": {
"stdout": "7\n5 3 1 1\n6 5 4 3 2 1\n"
}
},
{
"input": {
"stdin": "1\n3\n4 2 1\n"
},
"output": {
"stdout": "4 2 1\n"
}
},
{
"input": {
"stdin": "3\n1\n7\n4\n1 1 3 5\n... | {
"tags": [
"constructive algorithms",
"sortings"
],
"title": "Bogosort"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n int t;\n cin >> t;\n while (t--) {\n int n;\n cin >> n;\n int i, x;\n vector<int> a;\n for (i = 0; i < n; i++) {\n cin >> x;\n a.push_back(x);\n }\n sort(a.begin(), a.end(), greater<int>());\n for (i = 0; i <... |
Codeforces/1334/F | # Strange Function
Let's denote the following function f. This function takes an array a of length n and returns an array. Initially the result is an empty array. For each integer i from 1 to n we add element a_i to the end of the resulting array if it is greater than all previous elements (more formally, if a_i > max... | [
{
"input": {
"stdin": "11\n4 1 3 3 7 8 7 9 10 7 11\n3 5 0 -2 5 3 6 7 8 2 4\n3\n3 7 10\n"
},
"output": {
"stdout": "YES\n20"
}
},
{
"input": {
"stdin": "6\n2 1 5 3 6 5\n3 -9 0 16 22 -14\n4\n2 3 5 6\n"
},
"output": {
"stdout": "NO"
}
},
{
"input": {
... | {
"tags": [
"binary search",
"data structures",
"dp",
"greedy"
],
"title": "Strange Function"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst long long N = 1000777;\nlong long n, m;\nlong long a[N];\nlong long p[N];\nlong long b[N];\nlong long t[4 * N];\nlong long t2[4 * N];\nmap<long long, long long> mp;\nlong long nl = 0;\nvoid build(long long v, long long tl, long long tr) {\n if (tl == tr... |
Codeforces/1355/A | # Sequence with Digits
Let's define the following recurrence: $$$a_{n+1} = a_{n} + minDigit(a_{n}) ⋅ maxDigit(a_{n}).$$$
Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes.
Your task is calculate a_{K} for given a... | [
{
"input": {
"stdin": "8\n1 4\n487 1\n487 2\n487 3\n487 4\n487 5\n487 6\n487 7\n"
},
"output": {
"stdout": "42\n487\n519\n528\n544\n564\n588\n628\n"
}
},
{
"input": {
"stdin": "1\n1 333\n"
},
"output": {
"stdout": "50\n"
}
},
{
"input": {
"st... | {
"tags": [
"brute force",
"implementation",
"math"
],
"title": "Sequence with Digits"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\npair<long long int, long long int> rec(long long int n) {\n long long int mini = 1e18, maxi = -1;\n while (n) {\n mini = min(mini, n % 10);\n maxi = max(maxi, n % 10);\n n /= 10;\n }\n return {mini, maxi};\n}\nint main() {\n long long int t;\n c... |
Codeforces/1374/F | # Cyclic Shifts Sorting
You are given an array a consisting of n integers.
In one move, you can choose some index i (1 ≤ i ≤ n - 2) and shift the segment [a_i, a_{i + 1}, a_{i + 2}] cyclically to the right (i.e. replace the segment [a_i, a_{i + 1}, a_{i + 2}] with [a_{i + 2}, a_i, a_{i + 1}]).
Your task is to sort ... | [
{
"input": {
"stdin": "5\n5\n1 2 3 4 5\n5\n5 4 3 2 1\n8\n8 4 5 2 3 6 7 3\n7\n5 2 1 6 4 7 3\n6\n1 2 3 3 6 4\n"
},
"output": {
"stdout": "0\n\n6\n3 1 3 2 2 3 \n13\n2 1 1 6 4 2 4 3 3 4 4 6 6 \n-1\n4\n3 3 4 4 \n"
}
},
{
"input": {
"stdin": "1\n100\n86 366 161 188 28 209 450 355... | {
"tags": [
"brute force",
"constructive algorithms",
"implementation",
"sortings"
],
"title": "Cyclic Shifts Sorting"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 200010;\nint T, n, mk, a[N];\nvector<int> res;\nvoid flip(int x) {\n int t = a[x + 2];\n a[x + 2] = a[x + 1];\n a[x + 1] = a[x];\n a[x] = t;\n}\nvoid solve(int x, int y) {\n if (a[x] == a[x + 2]) {\n flip(x);\n flip(x);\n res.push_b... |
Codeforces/1398/C | # Good Subarrays
You are given an array a_1, a_2, ... , a_n consisting of integers from 0 to 9. A subarray a_l, a_{l+1}, a_{l+2}, ... , a_{r-1}, a_r is good if the sum of elements of this subarray is equal to the length of this subarray (∑_{i=l}^{r} a_i = r - l + 1).
For example, if a = [1, 2, 0], then there are 3 go... | [
{
"input": {
"stdin": "3\n3\n120\n5\n11011\n6\n600005\n"
},
"output": {
"stdout": "3\n6\n1\n"
}
},
{
"input": {
"stdin": "11\n1\n0\n1\n1\n1\n2\n1\n3\n1\n4\n1\n5\n1\n6\n1\n7\n1\n8\n1\n9\n26\n11140000000090000000002111\n"
},
"output": {
"stdout": "0\n1\n0\n0\n0\... | {
"tags": [
"data structures",
"dp",
"math"
],
"title": "Good Subarrays"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nusing ll = long long;\nusing ld = long double;\nusing pii = pair<int, int>;\nvoid solve() {\n int n;\n cin >> n;\n string s;\n cin >> s;\n vector<int> a(n + 1);\n vector<int> pref(n + 1);\n for (int i = 0; i < n; i++) {\n int c = s[i] - '0';\n a[i... |
Codeforces/1421/D | # Hexagons
Lindsey Buckingham told Stevie Nicks ["Go your own way"](https://www.youtube.com/watch?v=6ul-cZyuYq4). Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
<image>
Nicks wishes to go from the... | [
{
"input": {
"stdin": "2\n-3 1\n1 3 5 7 9 11\n1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n"
},
"output": {
"stdout": "18\n1000000000000000000\n"
}
},
{
"input": {
"stdin": "1\n-1000000000 -1000000000\n10000 20000 30000 40000 500... | {
"tags": [
"brute force",
"constructive algorithms",
"greedy",
"implementation",
"math",
"shortest paths"
],
"title": "Hexagons"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid solve() {\n int x, y;\n cin >> x >> y;\n array<int64_t, 7> cost;\n for (int i = 1; i <= 6; ++i) {\n cin >> cost[i];\n }\n auto path_cost = [&](int64_t x, int64_t plus, int64_t minus) -> int64_t {\n return (x >= 0 ? plus : -minus) * x;\n };\n ... |
Codeforces/143/C | # Help Farmer
Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of sto... | [
{
"input": {
"stdin": "7\n"
},
"output": {
"stdout": "47 65\n"
}
},
{
"input": {
"stdin": "4\n"
},
"output": {
"stdout": "28 41\n"
}
},
{
"input": {
"stdin": "12\n"
},
"output": {
"stdout": "48 105\n"
}
},
{
"input":... | {
"tags": [
"brute force",
"math"
],
"title": "Help Farmer"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long n;\nint main() {\n scanf(\"%I64d\", &n);\n long long sol1, sol2, a, b, c, d;\n sol1 = sol2 = (n + 1) * 9;\n for (a = 1; a * a * a <= n; ++a) {\n if (n % a != 0) continue;\n for (b = a; a * b * b <= n; ++b) {\n if (n % (a * b) != 0) con... |
Codeforces/1466/H | # Finding satisfactory solutions
Getting so far in this contest is not an easy feat. By solving all the previous problems, you have impressed the gods greatly. Thus, they decided to spare you the story for this problem and grant a formal statement instead.
Consider n agents. Each one of them initially has exactly one... | [
{
"input": {
"stdin": "4\n2 1 3 4\n"
},
"output": {
"stdout": "\n27408\n"
}
},
{
"input": {
"stdin": "2\n2 1\n"
},
"output": {
"stdout": "\n1\n"
}
},
{
"input": {
"stdin": "3\n1 2 3\n"
},
"output": {
"stdout": "\n98\n"
}
}... | {
"tags": [
"combinatorics",
"dp",
"graphs",
"greedy",
"math"
],
"title": "Finding satisfactory solutions"
} | {
"cpp": "/***************************************************************\n\tFile name: CF1466H.cpp\n\tAuthor: ljfcnyali\n\tCreate time: 2021年01月06日 星期三 20时52分14秒\n***************************************************************/\n#include<bits/stdc++.h>\n\nusing namespace std;\n\n#define REP(i, a, b) for ( int i = (... |
Codeforces/1491/B | # Minimal Cost
There is a graph of n rows and 10^6 + 2 columns, where rows are numbered from 1 to n and columns from 0 to 10^6 + 1:
<image>
Let's denote the node in the row i and column j by (i, j).
Initially for each i the i-th row has exactly one obstacle — at node (i, a_i). You want to move some obstacles so tha... | [
{
"input": {
"stdin": "3\n2 3 4\n2 2\n2 3 4\n3 2\n2 4 3\n3 2\n"
},
"output": {
"stdout": "\n7\n3\n3\n"
}
},
{
"input": {
"stdin": "1\n2 4 2\n999999 1000000\n"
},
"output": {
"stdout": "2\n"
}
},
{
"input": {
"stdin": "1\n2 1000000000 1\n99999... | {
"tags": [
"brute force",
"math"
],
"title": "Minimal Cost"
} | {
"cpp": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define ll long long int\n\n\n\nint main()\n{\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n ll t;\n cin>>t;\n while(t--)\n {\n ll n,u,v,i;\n cin>>n>>u>>v;\n ll arr[n],diff[n-1];\n for(i=0;i<n;i++)\n {cin>>arr[i];\n } ... |
Codeforces/1513/E | # Cost Equilibrium
An array is called beautiful if all the elements in the array are equal.
You can transform an array using the following steps any number of times:
1. Choose two indices i and j (1 ≤ i,j ≤ n), and an integer x (1 ≤ x ≤ a_i). Let i be the source index and j be the sink index.
2. Decrease the i... | [
{
"input": {
"stdin": "5\n0 11 12 13 14\n"
},
"output": {
"stdout": "\n120"
}
},
{
"input": {
"stdin": "3\n1 2 3\n"
},
"output": {
"stdout": "\n6"
}
},
{
"input": {
"stdin": "4\n0 4 0 4\n"
},
"output": {
"stdout": "\n2"
}
... | {
"tags": [
"combinatorics",
"constructive algorithms",
"math",
"sortings"
],
"title": "Cost Equilibrium"
} | {
"cpp": "#include<bits/stdc++.h>\n#pragma GCC optimize(2)\nusing namespace std;\ntypedef long long LL;\ntypedef unsigned long long uLL;\ntypedef pair<int,int> pii;\ntypedef pair<LL,LL> pLL;\ntypedef pair<double,double> pdd;\nconst int N=2e5+5;\nconst int M=2e5+5;\nconst int inf=1e9+5;\nconst LL mod=1e9+7;\nconst dou... |
Codeforces/1540/D | # Inverse Inversions
You were playing with permutation p of length n, but you lost it in Blair, Alabama!
Luckily, you remember some information about the permutation. More specifically, you remember an array b of length n, where b_i is the number of indices j such that j < i and p_j > p_i.
You have the array b, and ... | [
{
"input": {
"stdin": "3\n0 0 0\n7\n2 1\n2 2\n2 3\n1 2 1\n2 1\n2 2\n2 3\n"
},
"output": {
"stdout": "1 2 3 2 1 3 \n"
}
},
{
"input": {
"stdin": "5\n0 1 2 3 4\n15\n2 1\n2 2\n1 2 1\n2 2\n2 3\n2 5\n1 3 0\n1 4 0\n2 3\n2 4\n2 5\n1 4 1\n2 3\n2 4\n2 5\n"
},
"output": {
... | {
"tags": [
"binary search",
"brute force",
"data structures"
],
"title": "Inverse Inversions"
} | {
"cpp": "#include <bits/stdc++.h>\ntypedef long long ll;\nusing namespace std;\n\nconst int MAXN=100100;\nint n;\nint b[MAXN],l[MAXN],r[MAXN],pos[MAXN],tr[MAXN];\nvector<int> v[MAXN];\n\nvoid add(int x,int d)\n{\n\twhile(x<=n)\n\t{\n\t\ttr[x]+=d;\n\t\tx+=x&(-x);\n\t}\n}\n\nint find(int x)\n{\n\tint ret=0;\n\tfor(int... |
Codeforces/168/C | # Wizards and Trolleybuses
In some country live wizards. They love to ride trolleybuses.
A city in this country has a trolleybus depot with n trolleybuses. Every day the trolleybuses leave the depot, one by one and go to the final station. The final station is at a distance of d meters from the depot. We know for the... | [
{
"input": {
"stdin": "3 10 10000\n0 10\n5 11\n1000 1\n"
},
"output": {
"stdout": "1000.500000000\n1000.500000000\n11000.050000000\n"
}
},
{
"input": {
"stdin": "1 2 26\n28 29\n"
},
"output": {
"stdout": "33.099019514\n"
}
},
{
"input": {
"st... | {
"tags": [
"implementation"
],
"title": "Wizards and Trolleybuses"
} | {
"cpp": "#include <bits/stdc++.h>\nconst long long mod = 1e9 + 7;\nusing namespace std;\ninline int read() {\n register int x = 0, f = 1, ch = getchar();\n while (!isdigit(ch)) {\n if (ch == '-') f = -1;\n ch = getchar();\n }\n while (isdigit(ch)) {\n x = x * 10 + ch - '0';\n ch = getchar();\n }\n ... |
Codeforces/189/A | # Cut Ribbon
Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:
* After the cutting each ribbon piece should have length a, b or c.
* After the cutting the number of ribbon pieces should be maximum.
Help Polycarpus and find the number of ... | [
{
"input": {
"stdin": "5 5 3 2\n"
},
"output": {
"stdout": "2\n"
}
},
{
"input": {
"stdin": "7 5 5 2\n"
},
"output": {
"stdout": "2\n"
}
},
{
"input": {
"stdin": "2 19 15 1\n"
},
"output": {
"stdout": "2\n"
}
},
{
"i... | {
"tags": [
"brute force",
"dp"
],
"title": "Cut Ribbon"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid solve() {\n int n, a, b, c;\n cin >> n >> a >> b >> c;\n int dp[4005] = {0};\n dp[a] = 1;\n dp[b] = 1;\n dp[c] = 1;\n for (int i = 1; i < n + 1; i++) {\n if (i - a >= 0) {\n if (dp[i - a]) dp[i] = max(dp[i], dp[i - a] + 1);\n }\n if (... |
Codeforces/212/A | # Privatization
There is a developed network of flights between Berland and Beerland. All of them belong to the Berland state company BerAvia. Each flight connects some Berland city with some Beerland city. For each flight airplanes fly in both directions.
Changes are coming to Berland — the state decided to privatiz... | [
{
"input": {
"stdin": "3 5 8 2\n1 4\n1 3\n3 3\n1 2\n1 1\n2 1\n1 5\n2 2\n"
},
"output": {
"stdout": "4\n1 2 1 1 2 1 1 2 "
}
},
{
"input": {
"stdin": "1 2 1 1\n1 1\n"
},
"output": {
"stdout": "0\n1 "
}
},
{
"input": {
"stdin": "3 5 8 3\n1 4\n3 ... | {
"tags": [
"flows",
"graphs"
],
"title": "Privatization"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, m, k, t, x, y;\nint h[210 << 1], tot;\nint dis[210 << 1], vis[210 << 1], pre[210 << 1], prd[210 << 1],\n q[210 * 210 * 4], ql, qr, S, T;\nint d[5010][2], dx[210], dy[210], c[5010], ans;\nstruct edge {\n int v, c, w, n;\n edge(int y = 0, int z = 0, ... |
Codeforces/236/A | # Boy or Girl
Those days, many boys use beautiful girls' photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in th... | [
{
"input": {
"stdin": "xiaodao\n"
},
"output": {
"stdout": "IGNORE HIM!\n"
}
},
{
"input": {
"stdin": "sevenkplus\n"
},
"output": {
"stdout": "CHAT WITH HER!\n"
}
},
{
"input": {
"stdin": "wjmzbmr\n"
},
"output": {
"stdout": "CH... | {
"tags": [
"brute force",
"implementation",
"strings"
],
"title": "Boy or Girl"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string s;\n char alphabet[26] = {};\n cin >> s;\n for (int i = 0; i < s.length(); ++i) {\n char temp = s[i];\n s[i] = tolower(temp);\n alphabet[s[i] - 'a'] = s[i];\n }\n int cnt = 0;\n for (int j = 0; j < 26; ++j) {\n if (alph... |
Codeforces/260/D | # Black and White Tree
The board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-directed graph is called a tree if it is connected and doesn't contain any cycles.
Each node of the graph is painted black or white in such a manner that there aren't two nodes of the same color, connect... | [
{
"input": {
"stdin": "6\n1 0\n0 3\n1 8\n0 2\n0 3\n0 0\n"
},
"output": {
"stdout": "6 1 0\n4 1 0\n4 3 2\n2 3 3\n5 3 3\n"
}
},
{
"input": {
"stdin": "3\n1 3\n1 2\n0 5\n"
},
"output": {
"stdout": "3 2 2\n3 1 3\n"
}
},
{
"input": {
"stdin": "20\... | {
"tags": [
"constructive algorithms",
"dsu",
"graphs",
"greedy",
"trees"
],
"title": "Black and White Tree"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T>\ninline T gcd(T a, T b) {\n return (b) == 0 ? (a) : gcd((b), ((a) % (b)));\n}\ntemplate <class T>\ninline T lcm(T a, T b) {\n return ((a) / gcd((a), (b)) * (b));\n}\ntemplate <class T>\ninline T BigMod(T Base, T power, T M = 1000000007) {\... |
Codeforces/284/D | # Cow Program
Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1, a2, ..., an of positive integers:
1. Initially, x = 1 and y = 0. If, after any step, x ≤ 0 or x > n, the program immediately terminat... | [
{
"input": {
"stdin": "3\n1 2\n"
},
"output": {
"stdout": "-1\n-1\n"
}
},
{
"input": {
"stdin": "4\n2 4 1\n"
},
"output": {
"stdout": "3\n6\n8\n"
}
},
{
"input": {
"stdin": "98\n94 24 17 92 275858941 58 91 57 13 468038892 42 195790073 4940057... | {
"tags": [
"dfs and similar",
"dp",
"graphs"
],
"title": "Cow Program"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long arr[200010];\nint mem1[2][200010];\nint vis1[2][200010];\nint run[2][200010];\nint n;\nbool get1(bool a, int b) {\n if (b <= 0 || b > n) return 1;\n if (run[a][b]) return 0;\n if (vis1[a][b]) return mem1[a][b];\n run[a][b] = 1;\n bool ret;\n if... |
Codeforces/309/D | # Tennis Rackets
Professional sport is more than hard work. It also is the equipment, designed by top engineers. As an example, let's take tennis. Not only should you be in great shape, you also need an excellent racket! In this problem your task is to contribute to the development of tennis and to help to design a re... | [
{
"input": {
"stdin": "3 0\n"
},
"output": {
"stdout": "9\n"
}
},
{
"input": {
"stdin": "4 0\n"
},
"output": {
"stdout": "24\n"
}
},
{
"input": {
"stdin": "10 1\n"
},
"output": {
"stdout": "210\n"
}
},
{
"input": {
... | {
"tags": [
"brute force",
"geometry"
],
"title": "Tennis Rackets"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst double gen3 = sqrt(3.0);\nint n, m;\nlong long calc() {\n long long ans = 0;\n for (int i = m + 1; i + m <= n && i <= (n + 1) / 2; ++i) {\n double xi = i / 2.0, yi = i * gen3 / 2;\n int sum = 0;\n for (int j = m + 1; j + m <= n; ++j) {\n ... |
Codeforces/332/B | # Maximum Absurdity
Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.
This time mr. Boos... | [
{
"input": {
"stdin": "6 2\n1 1 1 1 1 1\n"
},
"output": {
"stdout": "1 3\n"
}
},
{
"input": {
"stdin": "5 2\n3 6 1 1 6\n"
},
"output": {
"stdout": "1 4\n"
}
},
{
"input": {
"stdin": "4 1\n1 2 2 2\n"
},
"output": {
"stdout": "2 3... | {
"tags": [
"data structures",
"dp",
"implementation"
],
"title": "Maximum Absurdity"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint const maxN = 2e5 + 20;\nlong long n, k, a[maxN], mx = -1, ans1 = -1, ans2 = -1, x;\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cin >> n >> k;\n for (int i = (0); i < (n); i++) {\n cin >> x;\n a[max(0LL, i - k + 1)] += x;\n a[i... |
Codeforces/355/B | # Vasya and Public Transport
Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.
Public transport is not free. There are 4 types of tick... | [
{
"input": {
"stdin": "4 3 2 1\n1 3\n798\n1 2 3\n"
},
"output": {
"stdout": "1"
}
},
{
"input": {
"stdin": "100 100 8 100\n3 5\n7 94 12\n100 1 47 0 42\n"
},
"output": {
"stdout": "16"
}
},
{
"input": {
"stdin": "1 3 7 19\n2 3\n2 5\n4 4 4\n"
... | {
"tags": [
"greedy",
"implementation"
],
"title": "Vasya and Public Transport"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvector<long long> v;\nvector<long long> vv;\nset<long long> st;\nlong long x, y, z;\nstring s, s1, s2, s3, s4, s5;\nvoid memset_ll(long long a[], long long v, long long n) {\n for (long long i = 0; i < n; i++) a[i] = v;\n}\nvoid memset_s(char a[], char v, lon... |
Codeforces/379/C | # New Year Ratings Change
One very well-known internet resource site (let's call it X) has come up with a New Year adventure. Specifically, they decided to give ratings to all visitors.
There are n users on the site, for each user we know the rating value he wants to get as a New Year Present. We know that user i wan... | [
{
"input": {
"stdin": "1\n1000000000\n"
},
"output": {
"stdout": "1000000000 \n"
}
},
{
"input": {
"stdin": "3\n5 1 1\n"
},
"output": {
"stdout": "5 \n1 \n2 \n"
}
},
{
"input": {
"stdin": "10\n4 5 10 5 2 14 15 6 10 6\n"
},
"output": {... | {
"tags": [
"greedy",
"sortings"
],
"title": "New Year Ratings Change"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint64_t sumn(int64_t n) { return ((n) * (n + 1)) / 2; }\nint main() {\n int n;\n cin >> n;\n vector<pair<long long, long long> > a(n);\n vector<long long> b(n);\n for (int i = 0; i < n; i++) {\n cin >> a[i].first;\n a[i].second = i;\n }\n sort(a.b... |
Codeforces/39/J | # Spelling Check
Petya has noticed that when he types using a keyboard, he often presses extra buttons and adds extra letters to the words. Of course, the spell-checking system underlines the words for him and he has to click every word and choose the right variant. Petya got fed up with correcting his mistakes himsel... | [
{
"input": {
"stdin": "competition\ncodeforces\n"
},
"output": {
"stdout": "0\n"
}
},
{
"input": {
"stdin": "abdrakadabra\nabrakadabra\n"
},
"output": {
"stdout": "1\n3 "
}
},
{
"input": {
"stdin": "aa\na\n"
},
"output": {
"stdo... | {
"tags": [
"hashing",
"implementation",
"strings"
],
"title": "Spelling Check"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\npriority_queue<long long> pq;\nlong long i, j;\nchar ch;\nstring a, b;\nint main() {\n pq = priority_queue<long long>();\n long long c = 0;\n cin >> a >> b;\n for (i = 0; i < a.size(); i++) {\n if (a[i] != b[i]) {\n ch = a[i];\n long long p = ... |
Codeforces/426/A | # Sereja and Mugs
Sereja showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and n water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, ... | [
{
"input": {
"stdin": "3 4\n1 1 1\n"
},
"output": {
"stdout": "YES\n"
}
},
{
"input": {
"stdin": "3 4\n3 1 3\n"
},
"output": {
"stdout": "YES\n"
}
},
{
"input": {
"stdin": "3 4\n4 4 4\n"
},
"output": {
"stdout": "NO\n"
}
}... | {
"tags": [
"implementation"
],
"title": "Sereja and Mugs"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }\nint main() {\n int n, m;\n cin >> n >> m;\n int a[2222];\n for (int i = 0; i < n; i++) scanf(\"%d\", &a[i]);\n qsort(a, n, sizeof(a[0]), cmp);\n int flag = 1;\n int sum = 0;\n for... |
Codeforces/44/D | # Hyperdrive
In a far away galaxy there are n inhabited planets, numbered with numbers from 1 to n. They are located at large distances from each other, that's why the communication between them was very difficult until on the planet number 1 a hyperdrive was invented. As soon as this significant event took place, n -... | [
{
"input": {
"stdin": "4\n0 0 0\n0 0 1\n0 1 0\n1 0 0\n"
},
"output": {
"stdout": "1.707106781186547\n"
}
},
{
"input": {
"stdin": "8\n-10000 -10000 -10000\n-10000 -10000 10000\n-10000 10000 -10000\n-10000 10000 10000\n10000 -10000 -10000\n10000 -10000 10000\n10000 10000 -10... | {
"tags": [
"math"
],
"title": "Hyperdrive"
} | {
"cpp": "/* 2013-08-11 10:01:16.979212 */#include <cstdio>\n#include <cstring>\n#include <iostream>\n#include <cmath>\n#include <algorithm>\n#define sqr(x) ((x)*(x))\nusing namespace std;\nstruct data{\n int x,y,z;\n long double dis;\n}A[5005];\nlong double ans;\ninline bool operator < (data a,data b){\n re... |
Codeforces/470/H | # Array Sorting
Sorting arrays is traditionally associated with high-level languages. How hard can it be in FALSE? Sort the given array in non-descending order.
Input
The input consists of a single line of space-separated integers. The first number is n (1 ≤ n ≤ 10) — the size of the array. The following n numbers a... | [
{
"input": {
"stdin": "3 3 1 2\n"
},
"output": {
"stdout": "1 2 3 \n"
}
},
{
"input": {
"stdin": "7 12 2 3 44 5 60 2\n"
},
"output": {
"stdout": "2 2 3 5 12 44 60 \n"
}
}
] | {
"tags": [
"*special"
],
"title": "Array Sorting"
} | {
"cpp": null,
"java": null,
"python": null
} |
Codeforces/495/A | # Digital Counter
Malek lives in an apartment block with 100 floors numbered from 0 to 99. The apartment has an elevator with a digital counter showing the floor that the elevator is currently on. The elevator shows each digit of a number with 7 light sticks by turning them on or off. The picture below shows how the e... | [
{
"input": {
"stdin": "00\n"
},
"output": {
"stdout": "4\n"
}
},
{
"input": {
"stdin": "89\n"
},
"output": {
"stdout": "2\n"
}
},
{
"input": {
"stdin": "73\n"
},
"output": {
"stdout": "15\n"
}
},
{
"input": {
"... | {
"tags": [
"implementation"
],
"title": "Digital Counter"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint A[1000];\nint main() {\n int n, p, t, a, i;\n cin >> n;\n A[0] = 2;\n A[1] = 7;\n A[2] = 2;\n A[3] = 3;\n A[4] = 3;\n A[5] = 4;\n A[6] = 2;\n A[7] = 5;\n A[8] = 1;\n A[9] = 2;\n cout << A[n / 10] * A[n % 10];\n}\n",
"java": "import java.util... |
Codeforces/519/D | # A and B and Interesting Substrings
A and B are preparing themselves for programming contests.
After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.
A likes lowercase letters of the Latin alpha... | [
{
"input": {
"stdin": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa\n"
},
"output": {
"stdout": "2\n"
}
},
{
"input": {
"stdin": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab\n"
},
"output": {
"stdout": "2\n"
}
},
{
"in... | {
"tags": [
"data structures",
"dp",
"two pointers"
],
"title": "A and B and Interesting Substrings"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long power(long long a, long long b, long long m) {\n long long ans = 1;\n while (b) {\n if (b & 1) ans = (ans * a) % m;\n b /= 2;\n a = (a * a) % m;\n }\n return ans;\n}\nint main() {\n long long t = 1, b = 1;\n while (t--) {\n map<long... |
Codeforces/545/E | # Paths and Trees
Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.
Let's assume th... | [
{
"input": {
"stdin": "3 3\n1 2 1\n2 3 1\n1 3 2\n3\n"
},
"output": {
"stdout": "2\n1 2 "
}
},
{
"input": {
"stdin": "4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4\n"
},
"output": {
"stdout": "4\n4 2 3 "
}
},
{
"input": {
"stdin": "6 8\n1 2 30\n1 3 20\n2... | {
"tags": [
"graphs",
"greedy",
"shortest paths"
],
"title": "Paths and Trees"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 3e5 + 100;\nconst long long INF = 1LL << 60;\nstruct P {\n long long d, cost;\n int num, v;\n P(long long d = 0, long long cost = 0, int num = 0, int v = 0)\n : d(d), cost(cost), num(num), v(v) {}\n friend bool operator>(const P a, co... |
Codeforces/572/B | # Order Book
In this task you need to process a set of stock exchange orders and use them to create order book.
An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready ... | [
{
"input": {
"stdin": "6 2\nB 10 3\nS 50 2\nS 40 1\nS 50 6\nB 20 4\nB 25 10\n"
},
"output": {
"stdout": "S 50 8\nS 40 1\nB 25 10\nB 20 4\n"
}
},
{
"input": {
"stdin": "2 2\nS 1 1\nB 0 2\n"
},
"output": {
"stdout": "S 1 1\nB 0 2\n"
}
},
{
"input": {... | {
"tags": [
"data structures",
"greedy",
"implementation",
"sortings"
],
"title": "Order Book"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, k;\nmap<int, int> b, s;\nvoid print(map<int, int>::iterator it, int x) {\n if (x >= k || it == s.end()) return;\n print(++it, x + 1);\n it--;\n printf(\"S %d %d\\n\", it->first, it->second);\n}\nint main() {\n scanf(\"%d %d\", &n, &k);\n for (int ... |
Codeforces/593/E | # Strange Calculation and Cats
Gosha's universe is a table consisting of n rows and m columns. Both the rows and columns are numbered with consecutive integers starting with 1. We will use (r, c) to denote a cell located in the row r and column c.
Gosha is often invited somewhere. Every time he gets an invitation, he... | [
{
"input": {
"stdin": "3 3 3\n2 2 2 2\n1 3 3 5\n1 3 3 7\n"
},
"output": {
"stdout": "2\n42\n"
}
},
{
"input": {
"stdin": "4 5 5\n2 2 5 3\n2 2 4 6\n3 2 4 9\n1 4 4 13\n1 4 4 15\n"
},
"output": {
"stdout": "490902\n10598759\n"
}
},
{
"input": {
... | {
"tags": [
"dp",
"matrices"
],
"title": "Strange Calculation and Cats"
} | {
"cpp": "#include <bits/stdc++.h>\n#pragma comment(linker, \"/STACK:1000000000\")\nusing namespace std;\nconst int maxn = 25, mod = (int)1e9 + 7;\nunordered_map<int, long long> dp[maxn][maxn][maxn][maxn];\nbool us[maxn][maxn];\nint n, m;\nvoid clear() {\n for (int i = 1; i <= n; i++) {\n for (int j = 1; j <= m; ... |
Codeforces/615/C | # Running Track
A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating t. On planet AMI-1511 the c... | [
{
"input": {
"stdin": "ami\nno\n"
},
"output": {
"stdout": "-1\n"
}
},
{
"input": {
"stdin": "abc\ncbaabc\n"
},
"output": {
"stdout": "2\n3 1\n1 3\n"
}
},
{
"input": {
"stdin": "aaabrytaaa\nayrat\n"
},
"output": {
"stdout": "3\n... | {
"tags": [
"dp",
"greedy",
"strings",
"trees"
],
"title": "Running Track"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint dp[2][2110][2110];\nint main() {\n string s, t;\n cin >> s >> t;\n for (int i = s.size() - 1; i >= 0; i--) {\n for (int j = t.size() - 1; j >= 0; j--) {\n if (s[i] == t[j]) dp[0][i][j] = dp[0][i + 1][j + 1] + 1;\n if (s[s.size() - 1 - i] ==... |
Codeforces/634/E | # Preorder Test
For his computer science class, Jacob builds a model tree with sticks and balls containing n nodes in the shape of a tree. Jacob has spent ai minutes building the i-th ball in the tree.
Jacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not hav... | [
{
"input": {
"stdin": "4 2\n1 5 5 5\n1 2\n1 3\n1 4\n"
},
"output": {
"stdout": "1"
}
},
{
"input": {
"stdin": "5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3\n"
},
"output": {
"stdout": "3"
}
},
{
"input": {
"stdin": "2 2\n1 1000000\n1 2\n"
},
"o... | {
"tags": [
"binary search",
"dfs and similar",
"dp",
"graphs",
"greedy",
"trees"
],
"title": "Preorder Test"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 2e5 + 85 - 69;\nvector<int> adj[maxn];\nint a[maxn], dp[maxn], pd[maxn], sz[maxn], mx[maxn], mx2[maxn], par[maxn],\n sum[maxn], n, k;\nbool can[maxn];\nvoid calc(int u = 0, int p = -1) {\n par[u] = p;\n sz[u] = 1;\n for (auto v : adj[u])... |
Codeforces/663/C | # Graph Coloring
You are given an undirected graph that consists of n vertices and m edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, w... | [
{
"input": {
"stdin": "6 5\n1 3 R\n2 3 R\n3 4 B\n4 5 R\n4 6 R\n"
},
"output": {
"stdout": "2\n3 4 \n"
}
},
{
"input": {
"stdin": "3 3\n1 2 B\n3 1 R\n3 2 B\n"
},
"output": {
"stdout": "1\n2 \n"
}
},
{
"input": {
"stdin": "4 5\n1 2 R\n1 3 R\n2 ... | {
"tags": [
"dfs and similar",
"graphs"
],
"title": "Graph Coloring"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int maxn = 1e5 + 7;\nconst long long md = 1e9 + 7;\nconst int inf = 1e9;\nvector<pair<int, int> > G[maxn];\nbool vis[maxn];\nint val[maxn];\nbool inval;\nbool st[maxn][3];\npair<int, int> dfs(int x, int v, int m) {\n val[x] = v;\n vis[x] = true;\n pai... |
Codeforces/688/A | # Opponents
Arya has n opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the scho... | [
{
"input": {
"stdin": "4 5\n1101\n1111\n0110\n1011\n1111\n"
},
"output": {
"stdout": "2"
}
},
{
"input": {
"stdin": "2 2\n10\n00\n"
},
"output": {
"stdout": "2"
}
},
{
"input": {
"stdin": "4 1\n0100\n"
},
"output": {
"stdout": "... | {
"tags": [
"implementation"
],
"title": "Opponents"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint testCase;\nstring s;\nint iinf = 1e9 + 1;\nlong long inf = 1e16 + 1;\nvoid solve() {\n long long n;\n cin >> n;\n long long d;\n cin >> d;\n long long c = 0, ans = 0;\n for (int i = 0; i < d; i++) {\n cin >> s;\n if (count(s.begin(), s.end(), '... |
Codeforces/70/E | # Information Reform
Thought it is already the XXI century, the Mass Media isn't very popular in Walrusland. The cities get news from messengers who can only travel along roads. The network of roads in Walrusland is built so that it is possible to get to any city from any other one in exactly one way, and the roads' l... | [
{
"input": {
"stdin": "8 10\n2 5 9 11 15 19 20\n1 4\n1 3\n1 7\n4 6\n2 8\n2 3\n3 5\n"
},
"output": {
"stdout": "38\n1 2 1 1 1 1 1 2 "
}
},
{
"input": {
"stdin": "6 10\n1 4 7 11 13\n4 6\n3 4\n3 5\n1 2\n2 4\n"
},
"output": {
"stdout": "21\n4 4 4 4 4 4 \n"
}
... | {
"tags": [
"dp",
"implementation",
"trees"
],
"title": "Information Reform"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 514;\nvector<int> v[N];\nint n, k, w[N], as[N], g[N], f[N][N], d[N][N];\nvoid dfs(int x, int fa) {\n for (int i = (1); i <= (n); i++) f[x][i] = w[d[x][i]] + k;\n for (int y : v[x])\n if (y != fa) {\n dfs(y, x);\n for (int i = (1); ... |
Codeforces/730/L | # Expression Queries
A simplified arithmetic expression (SAE) is an arithmetic expression defined by the following grammar:
* <SAE> ::= <Number> | <SAE>+<SAE> | <SAE>*<SAE> | (<SAE>)
* <Number> ::= <Digit> | <Digit><Number>
* <Digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
In other words it's a correct ari... | [
{
"input": {
"stdin": "(01)\n1\n1 4\n"
},
"output": {
"stdout": "1\n"
}
},
{
"input": {
"stdin": "((1+2)*3+101*2)\n6\n8 14\n1 6\n2 10\n11 14\n5 5\n4 5\n"
},
"output": {
"stdout": "205\n-1\n10\n2\n2\n-1\n"
}
},
{
"input": {
"stdin": "1\n1\n1 1... | {
"tags": [
"data structures"
],
"title": "Expression Queries"
} | {
"cpp": "#include <bits/stdc++.h>\nusing std::max;\nusing std::min;\nusing std::sort;\nusing std::swap;\nusing std::vector;\nconst int maxn = 400007;\nconst int mod = 1e9 + 7;\nlong long fast_pow(long long b, int k) {\n long long s = 1;\n while (k) {\n if (k & 1) s = s * b % mod;\n b = b * b % mod;\n k >>... |
Codeforces/755/B | # PolandBall and Game
PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses.
You're given two lists of words familiar to PolandBall and EnemyBall. Can you determi... | [
{
"input": {
"stdin": "2 2\nkremowka\nwadowicka\nkremowka\nwiedenska\n"
},
"output": {
"stdout": "YES\n"
}
},
{
"input": {
"stdin": "5 1\npolandball\nis\na\ncool\ncharacter\nnope\n"
},
"output": {
"stdout": "YES\n"
}
},
{
"input": {
"stdin": ... | {
"tags": [
"binary search",
"data structures",
"games",
"greedy",
"sortings",
"strings"
],
"title": "PolandBall and Game"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nvoid solve() {\n int n, m, intercnt = 0;\n char aszBuf[1000];\n set<string> st;\n scanf(\"%d %d\", &n, &m);\n for (int i = 0; i < n; i++) {\n scanf(\"%s\", aszBuf);\n st.insert(aszBuf);\n }\n for (int i = 0; i < m; i++) {\n scanf(\"%s\", aszBuf... |
Codeforces/776/C | # Molly's Chemicals
Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.
Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potio... | [
{
"input": {
"stdin": "4 2\n2 2 2 2\n"
},
"output": {
"stdout": " 8\n"
}
},
{
"input": {
"stdin": "4 -3\n3 -6 -3 12\n"
},
"output": {
"stdout": "3\n"
}
},
{
"input": {
"stdin": "4 ... | {
"tags": [
"binary search",
"brute force",
"data structures",
"implementation",
"math"
],
"title": "Molly's Chemicals"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n int t = 1;\n while (t--) {\n long long int n, k;\n cin >> n >> k;\n long long int arr[n];\n long long int sum[n];\n sum[0] = 0;\n for (int i = 1; i <= n; i++) {\n... |
Codeforces/7/D | # Palindrome Degree
String s of length n is called k-palindrome, if it is a palindrome itself, and its prefix and suffix of length <image> are (k - 1)-palindromes. By definition, any string (even empty) is 0-palindrome.
Let's call the palindrome degree of string s such a maximum number k, for which s is k-palindrome.... | [
{
"input": {
"stdin": "abacaba\n"
},
"output": {
"stdout": "6\n"
}
},
{
"input": {
"stdin": "a2A\n"
},
"output": {
"stdout": "1\n"
}
},
{
"input": {
"stdin": "z\n"
},
"output": {
"stdout": "1\n"
}
},
{
"input": {
... | {
"tags": [
"hashing",
"strings"
],
"title": "Palindrome Degree"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconstexpr long long MX = 5000000;\nlong long base[] = {129, 137};\nlong long mod[] = {1479386893, 1928476349};\nlong long powBase[2][MX + 5];\nvoid ctPow() {\n powBase[0][0] = powBase[1][0] = 1;\n for (long long i = 1; i <= MX; i++) {\n powBase[0][i] = (p... |
Codeforces/821/A | # Okabe and Future Gadget Laboratory
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row... | [
{
"input": {
"stdin": "3\n1 1 2\n2 3 1\n6 4 1\n"
},
"output": {
"stdout": "Yes\n"
}
},
{
"input": {
"stdin": "3\n1 5 2\n1 1 1\n1 2 3\n"
},
"output": {
"stdout": "No\n"
}
},
{
"input": {
"stdin": "4\n1 1 1 1\n1 12 1 2\n4 4 1 3\n5 10 6 1\n"
... | {
"tags": [
"implementation"
],
"title": "Okabe and Future Gadget Laboratory"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n ios_base::sync_with_stdio(false);\n int ans = 0;\n int n;\n cin >> n;\n int ar[100][100];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n cin >> ar[i][j];\n }\n }\n bool b = 0;\n for (int i = 0; i < n; i++) ... |
Codeforces/847/A | # Union of Doubly Linked Lists
Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly ... | [
{
"input": {
"stdin": "7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0\n"
},
"output": {
"stdout": "4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0\n"
}
},
{
"input": {
"stdin": "100\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n... | {
"tags": [
"implementation"
],
"title": "Union of Doubly Linked Lists"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nint n, a[101][3], k, z;\nvector<int> v[100];\nbool f;\nint main(void) {\n cin >> n;\n for (int i = 0; i < n; i++) {\n cin >> a[i][0] >> a[i][2];\n a[i][1] = i + 1;\n }\n while (k < n) {\n f = false;\n for (int i = 0; i < n; i++)\n if (a[i]... |
Codeforces/868/C | # Qualification Rounds
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of n problems, and they want to select any non-empty subset of it as a problemset.
k experienced teams are participating in the contest. Some of these teams already k... | [
{
"input": {
"stdin": "3 2\n1 0\n1 1\n0 1\n"
},
"output": {
"stdout": "YES\n"
}
},
{
"input": {
"stdin": "5 3\n1 0 1\n1 1 0\n1 0 0\n1 0 0\n1 0 0\n"
},
"output": {
"stdout": "NO\n"
}
},
{
"input": {
"stdin": "2 3\n1 0 0\n0 0 1\n"
},
"o... | {
"tags": [
"bitmasks",
"brute force",
"constructive algorithms",
"dp"
],
"title": "Qualification Rounds"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nlong long MOD = 1e9 + 7;\nvector<int> v[4];\nbool g(int i, int j, int k) {\n vector<int> a = v[i], b = v[j], c = v[k];\n vector<int> d;\n set_intersection(b.begin(), b.end(), a.begin(), a.end(), back_inserter(d));\n a.clear();\n set_intersection(c.begin()... |
Codeforces/893/F | # Subtree Minimum Query
You are given a rooted tree consisting of n vertices. Each vertex has a number written on it; number ai is written on vertex i.
Let's denote d(i, j) as the distance between vertices i and j in the tree (that is, the number of edges in the shortest path from i to j). Also let's denote the k-blo... | [
{
"input": {
"stdin": "5 2\n1 3 2 3 5\n2 3\n5 1\n3 4\n4 1\n2\n1 2\n2 3\n"
},
"output": {
"stdout": "2\n5\n"
}
},
{
"input": {
"stdin": "3 3\n1 3 2\n1 2\n1 3\n2\n2 3\n1 1\n"
},
"output": {
"stdout": "2\n1\n"
}
},
{
"input": {
"stdin": "1 1\n1\... | {
"tags": [
"data structures",
"trees"
],
"title": "Subtree Minimum Query"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 1e5 + 4, sz = 320;\nint n, r, a[N], in[N], out[N], d[N];\nvector<int> g[N];\nvector<pair<int, int> > v, tree[4 * N];\nvoid dfs(int node, int parent = -1, int depth = 0) {\n d[node] = depth;\n in[node] = v.size();\n v.push_back({depth, a[node]}... |
Codeforces/915/C | # Permute Digits
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.
It is allowed to leave a as it is.
Input
The first line contains integer a (1 ≤ a ≤ 1018). The seco... | [
{
"input": {
"stdin": "4940\n5000\n"
},
"output": {
"stdout": "4940\n"
}
},
{
"input": {
"stdin": "3921\n10000\n"
},
"output": {
"stdout": "9321\n"
}
},
{
"input": {
"stdin": "123\n222\n"
},
"output": {
"stdout": "213\n"
}
... | {
"tags": [
"dp",
"greedy"
],
"title": "Permute Digits"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nchar a[20], b[20], res[20];\nint n, m;\ninline bool judge() {\n int pos = n;\n for (int i = 1; i <= n; ++i) {\n if (a[i] != '-') {\n res[pos] = a[i];\n pos--;\n }\n }\n return (strcmp(res + 1, b + 1) <= 0);\n}\nint main() {\n scanf(\"%s\",... |
Codeforces/938/D | # Buy a Ticket
Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well.
There are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be u... | [
{
"input": {
"stdin": "4 2\n1 2 4\n2 3 7\n6 20 1 25\n"
},
"output": {
"stdout": "6 14 1 25 "
}
},
{
"input": {
"stdin": "3 3\n1 2 1\n2 3 1\n1 3 1\n30 10 20\n"
},
"output": {
"stdout": "12 10 12 "
}
},
{
"input": {
"stdin": "7 7\n1 6 745325\n2... | {
"tags": [
"data structures",
"graphs",
"shortest paths"
],
"title": "Buy a Ticket"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\nconst int N = 2e5 + 5;\nstruct graph {\n int to, next;\n long long val;\n} gr[N << 2];\nint n, m, cnt, head[N];\nlong long dis[N];\nvoid border(int u, int v, long long w) {\n gr[++cnt].to = v;\n gr[cnt].val = w;\n gr[cnt].next = head[u];\n head[u] = cnt;... |
Codeforces/963/D | # Frequency of String
You are given a string s. You should answer n queries. The i-th query consists of integer k_i and string m_i. The answer for this query is the minimum length of such a string t that t is a substring of s and m_i has at least k_i occurrences as a substring in t.
A substring of a string is a conti... | [
{
"input": {
"stdin": "aaaaa\n5\n3 a\n3 aa\n2 aaa\n3 aaaa\n1 aaaaa\n"
},
"output": {
"stdout": "3\n4\n4\n-1\n5\n"
}
},
{
"input": {
"stdin": "abbb\n7\n4 b\n1 ab\n3 bb\n1 abb\n2 bbb\n1 a\n2 abbb\n"
},
"output": {
"stdout": "-1\n2\n-1\n3\n-1\n1\n-1\n"
}
},... | {
"tags": [
"hashing",
"string suffix structures",
"strings"
],
"title": "Frequency of String"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\ntemplate <class T1, class T2>\nvoid chmin(T1& a, T2 b) {\n if (a > b) a = b;\n}\ntemplate <class T1, class T2>\nvoid chmax(T1& a, T2 b) {\n if (a < b) a = b;\n}\nusing Pi = pair<int, int>;\nusing Tapris = tuple<int, int, int>;\nusing vint = vector<int>;\ncon... |
Codeforces/990/G | # GCD Counting
You are given a tree consisting of n vertices. A number is written on each vertex; the number on vertex i is equal to a_i.
Let's denote the function g(x, y) as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex x to vertex y (including these two ... | [
{
"input": {
"stdin": "4\n9 16 144 6\n1 3\n2 3\n4 3\n"
},
"output": {
"stdout": "1 1\n2 1\n3 1\n6 2\n9 2\n16 2\n144 1\n"
}
},
{
"input": {
"stdin": "6\n1 2 4 8 16 32\n1 6\n6 3\n3 4\n4 2\n6 5\n"
},
"output": {
"stdout": "1 6\n2 5\n4 6\n8 1\n16 2\n32 1\n"
}
... | {
"tags": [
"divide and conquer",
"dp",
"dsu",
"number theory",
"trees"
],
"title": "GCD Counting"
} | {
"cpp": "#include <bits/stdc++.h>\nnamespace chtholly {\nchar buf[1 << 23], *p1 = buf, *p2 = buf;\ninline int read() {\n int x = 0, f = 1;\n char c = getchar();\n for (; !isdigit(c); c = getchar()) f ^= c == '-';\n for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ '0');\n return f ? x : -x;\n}\nt... |
HackerEarth/aprileasy-the-one-with-the-queries-3 | You are given a string S and Q query strings (q1, q2, ... , qQ). For each query string, report whether or not it is a subsequence of S.
Input :
The first line contains a string S.
The next line contains a single integer, Q.
The following Q lines each contain 1 query string qi.
Output :
Output Q lines. On the i^th... | [
{
"input": {
"stdin": "hello\n5\nworld\nlo\nelo\nhl\nol\n\nSAMPLE"
},
"output": {
"stdout": "No\nYes\nYes\nYes\nNo\n"
}
},
{
"input": {
"stdin": "hello\n5\nwolrd\nlo\nflo\nhl\nol\n\nSAMPLE"
},
"output": {
"stdout": "No\nYes\nNo\nYes\nNo\n"
}
},
{
"... | {
"tags": [],
"title": "aprileasy-the-one-with-the-queries-3"
} | {
"cpp": null,
"java": null,
"python": "'''\n# Read input from stdin and provide input before running code\n\nname = raw_input('What is your name?\\n')\nprint 'Hi, %s.' % name\n'''\ndef issubstr(substr, mystr, start_index=0):\n for letter in substr:\n start_index = mystr.find(letter, start_index) + 1\n ... |
HackerEarth/chandu-and-his-playing-area | Little chandu is very fond of playing games. Recently, He found a few straws each of length 1 inches in the store room. He took all of them and decided to mark a rectangular area on the floor with straws and warn rest of the family members to not to enter that area so that he can play in peace. He wants to maximize tha... | [
{
"input": {
"stdin": "3\n6\n9\n11\n\nSAMPLE"
},
"output": {
"stdout": "2\n4\n6\n"
}
},
{
"input": {
"stdin": "100\n1207244\n255313\n56\n83269\n638451\n347617\n29369\n233941\n61\n7937\n85\n6\n80\n2485\n7\n6394\n92\n80883\n278\n14467\n9\n7\n2018983\n2607\n916\n18515\n3600328... | {
"tags": [],
"title": "chandu-and-his-playing-area"
} | {
"cpp": null,
"java": null,
"python": "T=input()\nfor i in range(0,T):\n\tN=input()\n\tlength=N/4\n\tN=N-length*2\n\tbreadth=N/2\n\tprint length*breadth"
} |
HackerEarth/divide-apples | N boys are sitting in a circle. Each of them have some apples in their hand. You find that the total number of the apples can be divided by N. So you want to divide the apples equally among all the boys. But they are so lazy that each one of them only wants to give one apple to one of the neighbors at one step. Calcula... | [
{
"input": {
"stdin": "4\n1 3 9 7\n\nSAMPLE"
},
"output": {
"stdout": "8\n"
}
},
{
"input": {
"stdin": "4\n1 3 9 7"
},
"output": {
"stdout": "8\n"
}
},
{
"input": {
"stdin": "4\n1 3 9 7\n\nEAPLLS"
},
"output": {
"stdout": "8\n"
... | {
"tags": [],
"title": "divide-apples"
} | {
"cpp": null,
"java": null,
"python": "N=input()\ns=raw_input().split(' ')\nl=[]\ncount=0\nmiddle_one=0\nz=0\np=0\nnegative=[0]*N\nsss=[0]*N\nfor i in range(N):\n\tl.append(int(s[i]))\n\tcount+=int(s[i])\ntot=count/N\nfor i in range(N):\n\tnegative[i]=l[i]-tot\nfor i in range(N):\n\tsss[i]=p\n\tp+=negative[i]\ns... |
HackerEarth/good-points | You are given non-negative integer N. Find N pairwise different points on the Euclidean plane with integer coordinates in the corresponding order and the following properties:
1) the distance between any two consecutive points in the order is equal for the all pairs of consecutive points 2) the angle created by any t... | [
{
"input": {
"stdin": "4\n\nSAMPLE"
},
"output": {
"stdout": "YES\n0 0\n0 1\n1 1\n1 0\n"
}
},
{
"input": {
"stdin": "5"
},
"output": {
"stdout": "NO\n"
}
},
{
"input": {
"stdin": "8"
},
"output": {
"stdout": "NO\n"
}
},
... | {
"tags": [],
"title": "good-points"
} | {
"cpp": null,
"java": null,
"python": "'''\n# Read input from stdin and provide input before running code\n\nname = raw_input('What is your name?\\n')\nprint 'Hi, %s.' % name\n'''\ntest=1\nfor i in range(test):\n\ta=int(raw_input())\n\tif a >4 or a==3 :\n\t\tprint \"NO\"\n\t\tbreak;\n\telse:\n\t\tprint \"YES\\n0... |
HackerEarth/little-achraf-in-who-wants-to-be-a-millionaire | “Can you answer this million dollar question ?” said the presenter to Little Achraf.
“Yes”, he responded.
“Ok … here is the question”.
We define a sequence A of strings of order L as follows:
A[n] = \sum\limits_{i = 0}^{L-1} A[n-L+i]; n ≥ L+1; where addition refers to string concatenation
A[n] ∈ [a-z A-Z 0-9]; ... | [
{
"input": {
"stdin": "2 2\n1 3 A\n3 5 B\n\nSAMPLE"
},
"output": {
"stdout": "1\n"
}
},
{
"input": {
"stdin": "4 11\n72 11 L\n1 3 Y\n2 5 d\n1 4 v\n70 12 B\n5 6 C\n72 10 f\n141 12 s\n1 3 n\n12646 19 L\n1 5 x"
},
"output": {
"stdout": "0\n"
}
},
{
"i... | {
"tags": [],
"title": "little-achraf-in-who-wants-to-be-a-millionaire"
} | {
"cpp": null,
"java": null,
"python": "a,d,fl,gl={},[0]*100000,0,[0]*102\nl,m=map(int,raw_input().split())\nfor i in range(1,l+1): \n\td[i]=1 \ntemp,lim,k=l+1,10**15,i+1\nwhile k<500:\n\td[k]=temp=sum(d[k-l:k])\n\tk+=1\n\tif temp <= lim: final=k\ndef find(p,value):\n\tif value <= l: return value-1\n\tpo=0\n\tans... |
HackerEarth/monks-love-for-food | Our monk loves food. Hence,he took up position of a manager at Sagar,a restaurant that serves people with delicious food packages. It is a very famous place and people are always queuing up to have one of those packages. Each package has a cost associated with it. The packages are kept as a pile.
The job of a manager... | [
{
"input": {
"stdin": "6\n1\n2 5\n2 7\n2 9\n1\n1\n\nSAMPLE"
},
"output": {
"stdout": "No Food\n9\n7\n"
}
},
{
"input": {
"stdin": "2\n1\n1"
},
"output": {
"stdout": "No Food\nNo Food\n"
}
},
{
"input": {
"stdin": "6\n1\n2 0\n2 7\n2 18\n1\n1\n... | {
"tags": [],
"title": "monks-love-for-food"
} | {
"cpp": null,
"java": null,
"python": "query=input()\ntype_list=[]\nfor i in range(query):\n\ttyp=raw_input()\n\tif typ[0]==\"2\":\n\t\ttype_list.append(typ[2:])\n\tif typ[0]==\"1\":\n\t\tif len(type_list)==0:\n\t\t\tprint \"No Food\"\n\t\telse:\n\t\t\tres=type_list[-1]\n\t\t\tprint res\n\t\t\ttype_list.pop()"
} |
HackerEarth/power-matrix | You are given a square matrix M and a positive integer N. You will have to compute M raised to the power N. (that is, M multiplied with itself N times.)
Input
First line of input is T ( number of test-cases) .
First line of each test-case contains two integer M , N where M is size of square array that you have to ex... | [
{
"input": {
"stdin": "2\n2 3\n1 0 \n1 1 \n3 3\n1 0 4 \n1 2 2 \n0 4 4\n\nSAMPLE"
},
"output": {
"stdout": "1 0 \n3 1 \n17 112 116 \n15 88 100 \n28 144 160"
}
}
] | {
"tags": [],
"title": "power-matrix"
} | {
"cpp": null,
"java": null,
"python": "def matrix_mult(A, B , m):\n C = [[0 for x in range(m)] for x in range(m)]\n for i in range(m):\n for j in range(m):\n for k in range(m):\n C[i][k] = (C[i][k] + A[i][j] * B[j][k]) % 1000000007\n return C\n\ndef fast_exponentiation(A, m,n):\n if n == 1:\n ... |
HackerEarth/sachins-love-letter-a | Sachin wants to give a love letter to his girlfriend on valentines day. He
is having a circular piece of paper of radius "r".
He wants to use rectangular piece of paper for letter writing whose length and breadth are integers. In how many ways can he do that.
NOTE : Rectangle of a * b and b * a are considered as di... | [
{
"input": {
"stdin": "12"
},
"output": {
"stdout": "424"
}
},
{
"input": {
"stdin": "245"
},
"output": {
"stdout": "424\n"
}
},
{
"input": {
"stdin": "8"
},
"output": {
"stdout": "424\n"
}
},
{
"input": {
"std... | {
"tags": [],
"title": "sachins-love-letter-a"
} | {
"cpp": null,
"java": null,
"python": "print 424"
} |
HackerEarth/sum-of-numbers-9 | Given an array of N elements, check if it is possible to obtain a sum of S, by choosing some (or none) elements of the array and adding them.
Input:
First line of the input contains number of test cases T. Each test case has three lines.
First line has N, the number of elements in array.
Second line contains N space s... | [
{
"input": {
"stdin": "3\n5\n3 2 0 7 -1\n8\n3\n-1 3 3\n4\n3\n4 -5 1\n5\n\nSAMPLE"
},
"output": {
"stdout": "YES\nNO\nYES\n"
}
},
{
"input": {
"stdin": "50\n13\n34423 -366726 -147520 -584399 615107 415987 -236590 906765 385780 -12338 -827677 -83330 3716\n-481201\n15\n868821 ... | {
"tags": [],
"title": "sum-of-numbers-9"
} | {
"cpp": null,
"java": null,
"python": "import math\ndef check(k,s,x):\n\tsu=0\n\ti=0\n\twhile(k!=0):\n\t\tif(k%2==1):\n\t\t\tsu=su+x[i]\n\t\ti=i+1\n k=int(k/2)\n\tif(s==su):\n\t\treturn 1\n\telse:\n\t\treturn 0\n\n\ntc=int(raw_input())\nfor k in range(0,tc):\n flag=0\n m=0\n i... |
HackerEarth/way-to-follow-on-grid | You are situated in an N dimensional grid at position (x1,x2,...,xN). The dimensions of the grid are (D1,D2,...DN). In one step, you can walk one step ahead or behind in any one of the N dimensions. (So there are always 2×N possible different moves). In how many ways can you take M steps such that you do not leave the ... | [
{
"input": {
"stdin": "5\n1 287\n44\n78\n1 236\n25\n87\n1 122\n41\n63\n1 260\n7\n64\n1 127\n3\n73\n\nSAMPLE"
},
"output": {
"stdout": "38753340\n587915072\n644474045\n423479916\n320130104"
}
},
{
"input": {
"stdin": "5\n1 287\n44\n78\n1 236\n25\n87\n1 122\n41\n63\n1 260\n7\... | {
"tags": [],
"title": "way-to-follow-on-grid"
} | {
"cpp": null,
"java": null,
"python": "MOD = 1000000007\ndef solve(n, m, x, d):\n\tmat = [[0 for i in range(n + 1)] for j in range(m + 1)]\n\tfor i in range(n):\n\t\tmat1 = [[0 for j in range(m + 1)] for k in range(d[i] + 1)]\n\t\tD = d[i]\n\t\tfor j in range(1, D + 1):\n\t\t\tmat1[j][0] = 1\n\t\tfor j in range(... |
p02615 AtCoder Beginner Contest 173 - Chat in a Circle | Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.
The N players will arrive at the place one by one in some order. To ma... | [
{
"input": {
"stdin": "4\n2 2 1 3"
},
"output": {
"stdout": "7"
}
},
{
"input": {
"stdin": "7\n1 1 1 1 1 1 1"
},
"output": {
"stdout": "6"
}
},
{
"input": {
"stdin": "4\n1 1 26 0"
},
"output": {
"stdout": "28\n"
}
},
{
... | {
"tags": [],
"title": "p02615 AtCoder Beginner Contest 173 - Chat in a Circle"
} | {
"cpp": "#include <bits/stdc++.h>\nusing namespace std;\n#define rep(i,n) for(int i=0;i<(n);i++)\n\nint main(){\n int player[200005];//心地よさ\n int n;\n long long ans = 0;\n cin >> n;\n rep(i, n){\n cin >> player[i];\n }\n sort(player, player+n);\n\n rep(i, n-1){\n ans += player[n-1-(i+1)/2];\n }\n\n\n ... |
p02746 Panasonic Programming Contest 2020 - Fractal Shortest Path | For a non-negative integer K, we define a fractal of level K as follows:
* A fractal of level 0 is a grid with just one white square.
* When K > 0, a fractal of level K is a 3^K \times 3^K grid. If we divide this grid into nine 3^{K-1} \times 3^{K-1} subgrids:
* The central subgrid consists of only black squares.
* Ea... | [
{
"input": {
"stdin": "2\n4 2 7 4\n9 9 1 9"
},
"output": {
"stdout": "5\n8"
}
},
{
"input": {
"stdin": "2\n2 4 8 4\n3 3 1 10"
},
"output": {
"stdout": "8\n9\n"
}
},
{
"input": {
"stdin": "2\n3 2 6 4\n4 3 2 4"
},
"output": {
"std... | {
"tags": [],
"title": "p02746 Panasonic Programming Contest 2020 - Fractal Shortest Path"
} | {
"cpp": "#include <bits/stdc++.h>\n#include <iomanip>\nusing namespace std;\n#define reps(i,s,n) for(int i = s; i < n; i++)\n#define rep(i,n) reps(i,0,n)\n#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)\n#define Rrep(i,n) Rreps(i,n,0)\n#define ALL(a) a.begin(), a.end()\n#define fi first\n#define se second\ntype... |
p02881 AtCoder Beginner Contest 144 - Walk on Multiplication Table | Takahashi is standing on a multiplication table with infinitely many rows and columns.
The square (i,j) contains the integer i \times j. Initially, Takahashi is standing at (1,1).
In one move, he can move from (i,j) to either (i+1,j) or (i,j+1).
Given an integer N, find the minimum number of moves needed to reach a ... | [
{
"input": {
"stdin": "10000000019"
},
"output": {
"stdout": "10000000018"
}
},
{
"input": {
"stdin": "50"
},
"output": {
"stdout": "13"
}
},
{
"input": {
"stdin": "10"
},
"output": {
"stdout": "5"
}
},
{
"input": {
... | {
"tags": [],
"title": "p02881 AtCoder Beginner Contest 144 - Walk on Multiplication Table"
} | {
"cpp": "#include <bits/stdc++.h>\n#define ll long long\nll c;\nll ans=(ll)1<<(ll)60;\nint main(){\n\tstd::cin>>c;\n\tfor(ll i=1;i*i<=c;++i){\n\t\tif (c%i)continue;\n\t\tans=std::min(ans,i-1+c/i-1);\n\t}printf(\"%lld\",ans);\n\treturn 0;\n}",
"java": "import java.util.*;\npublic class Main{\n public static void m... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.