contest_id
stringclasses
33 values
problem_id
stringclasses
14 values
statement
stringclasses
181 values
tags
listlengths
1
8
code
stringlengths
21
64.5k
language
stringclasses
3 values
1307
A
A. Cow and Haybalestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of nn haybale piles on the farm. The ii-th pile contains aiai haybales. However, Farmer John has just left for vac...
[ "greedy", "implementation" ]
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") #pragma GCC optimize("fast-math") #pragma GCC optimize ("unroll-loops,Ofast,O3") #pragma GCC target("avx,av...
cpp
1322
D
D. Reality Showtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputA popular reality show is recruiting a new cast for the third season! nn candidates numbered from 11 to nn have been interviewed. The candidate ii has aggressiveness level lili, and recruiting this candi...
[ "bitmasks", "dp" ]
// LUOGU_RID: 91777092 #include<cmath> #include<cstdio> #include<bitset> #include<iostream> #include<algorithm> #include<queue> #include<cstring> #include<set> #include<vector> #include<map> #include<random> #include<ctime> #include<unordered_map> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy....
cpp
1301
B
B. Motarack's Birthdaytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputDark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array aa of nn non-negative integers.Dark created that array 10001000 years ago, so so...
[ "binary search", "greedy", "ternary search" ]
#include<iostream> #include<cstring> #include<vector> #include<map> #include<queue> #include<unordered_map> #include<cmath> #include<cstdio> #include<algorithm> #include<set> #include<cstdlib> #include<stack> #include<ctime> #define forin(i,a,n) for(int i=a;i<=n;i++) #define forni(i,n,a) for(int i=n;i>=a;i--) #define f...
cpp
1313
B
B. Different Rulestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNikolay has only recently started in competitive programming; but already qualified to the finals of one prestigious olympiad. There going to be nn participants, one of whom is Nikolay. Like any good o...
[ "constructive algorithms", "greedy", "implementation", "math" ]
#include <iostream> #include <map> #include <vector> #define ll int using namespace std; int32_t main() { ll t; cin >> t; for (ll i = 0; i < t; i++){ ll n, x, y; cin >> n >> x >> y; ll res1, res2; res1 = min(std::max(1, x + y - n + 1), n); res2 = min(x + y - 1, n);...
cpp
1141
D
D. Colored Bootstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn left boots and nn right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings ll and rr, both of length nn. The...
[ "greedy", "implementation" ]
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define ff first #define sc second #define pb push_back #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; const ll mod = 1e9 + 7; void solve(){ int n; cin >> n; string a, b; cin >> a >> b...
cpp
1312
A
A. Two Regular Polygonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm (m<nm<n). Consider a convex regular polygon of nn vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) an...
[ "geometry", "greedy", "math", "number theory" ]
#include <iostream> #include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int n,m; cin>>n>>m; if(n%m==0){ cout<<"YES"<<endl; } else{ cout<<"NO"<<endl; } } }
cpp
1313
D
D. Happy New Yeartime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBeing Santa Claus is very difficult. Sometimes you have to deal with difficult situations.Today Santa Claus came to the holiday and there were mm children lined up in front of him. Let's number them fr...
[ "bitmasks", "dp", "implementation" ]
// LUOGU_RID: 97884369 # include <bits/stdc++.h> using namespace std; int n,m,K,w[200010],f[200010][300],g[300],tn[300][300]; pair<int,int> ok[100010]; vector<int> op[200010]; vector<pair<int,int> > kl; int main() { // freopen("in.in","r",stdin); // freopen("out.out","w",stdout); scanf("%d%d%d",&n,&m,&K); ...
cpp
1305
H
H. Kuroni the Private Tutortime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAs a professional private tutor; Kuroni has to gather statistics of an exam. Kuroni has appointed you to complete this important task. You must not disappoint him.The exam consists of nn ques...
[ "binary search", "greedy" ]
#include<bits/stdc++.h> template <typename _Tp>void read(_Tp &x){ char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar(); x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar(); if(f)x=-x; } template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(arg...
cpp
1311
A
A. Add Odd or Subtract Eventime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers aa and bb.In one move, you can change aa in the following way: Choose any positive odd integer xx (x>0x>0) and replace aa with a+xa+x; choose any positiv...
[ "greedy", "implementation", "math" ]
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b; cin >> a >> b; if (b > a) { int x = b - a; if (x % 2 == 0) { cout << 2 << endl; } else { cout << 1 << endl; } } else if (a == b) { cout << 0 << endl; } else ...
cpp
1311
A
A. Add Odd or Subtract Eventime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers aa and bb.In one move, you can change aa in the following way: Choose any positive odd integer xx (x>0x>0) and replace aa with a+xa+x; choose any positiv...
[ "greedy", "implementation", "math" ]
#include <bits/stdc++.h> using namespace std; int main() { // your code goes here int t; cin>>t; while(t--){ int a,b; cin>>a>>b; if(a<b){ int x=b-a; if(x%2==0){ cout<<2<<endl; } else{ cout<<1<<endl; } } else if(a==b){ cout<<0<<endl; } else{ int h=a-...
cpp
1303
G
G. Sum of Prefix Sumstime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWe define the sum of prefix sums of an array [s1,s2,…,sk][s1,s2,…,sk] as s1+(s1+s2)+(s1+s2+s3)+⋯+(s1+s2+⋯+sk)s1+(s1+s2)+(s1+s2+s3)+⋯+(s1+s2+⋯+sk).You are given a tree consisting of nn vertices. Eac...
[ "data structures", "divide and conquer", "geometry", "trees" ]
#include<bits/stdc++.h> using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_real_distribution<> pp(0.0,1.0); #define int long long #define pii pair<int,int> #define maxn 150005 const int inf=1e18; struct node{ int sum,len0,len1,cnt; }; struct line{ mu...
cpp
1285
A
A. Mezo Playing Zomatime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Mezo is playing a game. Zoma, a character in that game, is initially at position x=0x=0. Mezo starts sending nn commands to Zoma. There are two possible commands: 'L' (Left) sets the position...
[ "math" ]
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> #define pb push_back using namespace std; long long t, n, a[1000001]; string s; int main(){ cin >> t; cin >> s; cout << t + 1; }
cpp
13
E
E. Holestime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for one person with following rules:There are N holes located in a single row and numbered from left to right with numbers from 1 to N. ...
[ "data structures", "dsu" ]
#include <bits/stdc++.h> using namespace std; /*<DEBUG>*/ #define tem template <typename #define can_shift(_X_, ...) enable_if_t<sizeof test<_X_>(0) __VA_ARGS__ 8, debug&> operator<<(T i) #define _op debug& operator<< tem C > auto test(C *x) -> decltype(cerr << *x, 0LL); tem C > char test(...); tem C > struct itr{C b...
cpp
1290
B
B. Irreducible Anagramstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call two strings ss and tt anagrams of each other if it is possible to rearrange symbols in the string ss to get a string, equal to tt.Let's consider two strings ss and tt which are anagram...
[ "binary search", "constructive algorithms", "data structures", "strings", "two pointers" ]
#include<cstdio> int a[200100][30],Q,l,r; char S[200100]; int main(){ scanf("%s",S+1); for(int i=1;S[i];i++){ for(int j=0;j<26;j++) a[i][j]=a[i-1][j]; a[i][S[i]-'a']++; } scanf("%d",&Q); while(Q--){ scanf("%d%d",&l,&r); int cnt=0; for(int i=0;i<26;i++) if(a[l-1][i]!=a[r][i])cnt++; if(cnt>2||S[l]!=S[r]||l==r)puts("Yes"...
cpp
1312
A
A. Two Regular Polygonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm (m<nm<n). Consider a convex regular polygon of nn vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) an...
[ "geometry", "greedy", "math", "number theory" ]
#include <bits/stdc++.h> #define ll long long #define sz(x) (x).size() #define all(x) (x).begin(), (x).end() #define tv ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; ll n,m,t; void x(){ int a,b; cin>>a>>b; if(a<b)swap(a,b); if(a%b==0)cout<<"YES\n"; else cou...
cpp
1284
C
C. New Year and Permutationtime limit per test1 secondmemory limit per test1024 megabytesinputstandard inputoutputstandard outputRecall that the permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a ...
[ "combinatorics", "math" ]
#include <bits/stdc++.h> #define all(x) x.begin(), x.end() #define r_all(x) x.rbegin(), x.rend() #define sz(x)(ll) x.size() #define g_max(x, y) x = max(x, y) #define g_min(x, y) x = min(x, y) #define rsz(a, n) a.resize(n) #define ass(a, n) a.assign(n, 0) #define YES() cout << "YES\n" #define Yes cout << "Yes\n" #defin...
cpp
1286
E
E. Fedya the Potter Strikes Backtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFedya has a string SS, initially empty, and an array WW, also initially empty.There are nn queries to process, one at a time. Query ii consists of a lowercase English letter cici and a n...
[ "data structures", "strings" ]
// LUOGU_RID: 101230917 #include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <string> #pragma GCC optimize("Ofast") #pragma GCC target("avx,sse2,sse3,sse4,mmx,arch=cannonlake,tune=cannonlake") #define query(pos) (a[*lower_bound(stk + 1, stk + 1 + r, (pos))]) #define endl '\n' using std...
cpp
1304
D
D. Shortest and Longest LIStime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGildong recently learned how to find the longest increasing subsequence (LIS) in O(nlogn)O(nlog⁡n) time for a sequence of length nn. He wants to test himself if he can implement it correctly,...
[ "constructive algorithms", "graphs", "greedy", "two pointers" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll tc = 1; cin >> tc; while (tc--) { ll n; ...
cpp
1325
B
B. CopyCopyCopyCopyCopytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEhab has an array aa of length nn. He has just enough free time to make a new array consisting of nn copies of the old array, written back-to-back. What will be the length of the new array's longe...
[ "greedy", "implementation" ]
#include<bits/stdc++.h> #include<iostream> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #include<math.h> #include<algorithm> #include<vector> #include<stdbool.h> using std::cout; const int N = 3000008; using namespace std; int main(){ ll t; ...
cpp
1284
B
B. New Year and Ascent Sequencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputA sequence a=[a1,a2,…,al]a=[a1,a2,…,al] of length ll has an ascent if there exists a pair of indices (i,j)(i,j) such that 1≤i<j≤l1≤i<j≤l and ai<ajai<aj. For example, the sequence [0,2,0,...
[ "binary search", "combinatorics", "data structures", "dp", "implementation", "sortings" ]
#include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define ll long long #define MOD 1000000007 using namespace std; int main() { ll n,cou=0,ans=0; cin >> n; vector<ll> arr1,arr2; for(ll i=0;i<n;i++){ ll x,mx=-1,mn=1000000,f=0; ...
cpp
1141
F2
F2. Same Sum Blocks (Hard)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is given in two editions; which differ exclusively in the constraints on the number nn.You are given an array of integers a[1],a[2],…,a[n].a[1],a[2],…,a[n]. A block is a sequence ...
[ "data structures", "greedy" ]
#include <bits/stdc++.h> using namespace std; const int N = 1505; int sums[N]; void solve() { int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> sums[i]; sums[i] += sums[i - 1]; } unordered_map<int, vector<pair<int, int>>> pos; for (int j = 1; j <=...
cpp
1313
A
A. Fast Food Restauranttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTired of boring office work; Denis decided to open a fast food restaurant.On the first day he made aa portions of dumplings, bb portions of cranberry juice and cc pancakes with condensed milk.The ...
[ "brute force", "greedy", "implementation" ]
#include<iostream> using namespace std; int main(){ int t; cin >> t; for (int i=0;i<t;i++){ int count=0; int arr[3]; for(int j=0;j<3;j++){ cin >> arr[j]; } int max; int min; int mid; max=arr[0]; min=arr[0]; ...
cpp
1141
E
E. Superhero Battletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA superhero fights with a monster. The battle consists of rounds; each of which lasts exactly nn minutes. After a round ends, the next round starts immediately. This is repeated over and over again.E...
[ "math" ]
#include <bits/stdc++.h> #define nl << "\n" #define spc << " " << #define pt cout << using namespace std; typedef long long ll; #define int long long #define pii pair<int, int> int mod = 1e9 + 7; int MAX = 1e9 + 1; int MIN = -1e9; double eps = 1e-7; int dieTime(int req,vector<int> &arr,int n){ for(i...
cpp
1325
D
D. Ehab the Xorcisttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven 2 integers uu and vv, find the shortest array such that bitwise-xor of its elements is uu, and the sum of its elements is vv.InputThe only line contains 2 integers uu and vv (0≤u,v≤1018)(0≤u,v≤1...
[ "bitmasks", "constructive algorithms", "greedy", "number theory" ]
#include<bits/stdc++.h> #define endl '\n' #define iloveds std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); #define all(a) a.begin(),a.end() #define int long long using namespace std; typedef long long ll ; const int N = 1e5 + 100; ll u, v; signed main(){ iloveds; cin >> u >> v; ll delta = v - u...
cpp
1315
A
A. Dead Pixeltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputScreen resolution of Polycarp's monitor is a×ba×b pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates (x,y)(x,y) (0≤x<a,0≤y<b0≤x<a,0≤y<b). You can consider columns of pixels to ...
[ "implementation" ]
#include <bits/stdc++.h> using namespace std; #define ll long lon int main() { ios::sync_with_stdio(0); cout.tie(0), cin.tie(0); int t; cin >> t; while (t--) { int a, b, x, y; cin >> a >> b >> x >> y; cout << max({x * b, a * y, (a - x - 1) * b, a * (b - y - 1)...
cpp
13
B
B. Letter Atime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. Th...
[ "geometry", "implementation" ]
#include <stdio.h> #include <string.h> #include <iostream> #include <cmath> using namespace std; int fang(int a,int b,int c,int d,int e,int f)//判断是否在三点是否共线 { if((long long)(f-b)*(c-a)==(long long)(d-b)*(e-a)) return 1; return 0; } int xie(int x,int y,int a,int b,int p,int q)//判断两线段的夹角是否在0~90之间 { if(...
cpp
1324
D
D. Pair of Topicstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe next lecture in a high school requires two topics to be discussed. The ii-th topic is interesting by aiai units for the teacher and by bibi units for the students.The pair of topics ii and jj (i<ji...
[ "binary search", "data structures", "sortings", "two pointers" ]
#include <bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ; template<class T> using ordered_multi_set = t...
cpp
1325
F
F. Ehab's Last Theoremtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt's the year 5555. You have a graph; and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either.Given a connected graph w...
[ "constructive algorithms", "dfs and similar", "graphs", "greedy" ]
// LUOGU_RID: 102449126 #include<bits/stdc++.h> using namespace std; #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define eb emplace_back #define ef emplace_front #define lowbit(x) (x & (-x)) #define ti chrono::system_clock::now().time_since_epoch().count() #define Fin...
cpp
1284
B
B. New Year and Ascent Sequencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputA sequence a=[a1,a2,…,al]a=[a1,a2,…,al] of length ll has an ascent if there exists a pair of indices (i,j)(i,j) such that 1≤i<j≤l1≤i<j≤l and ai<ajai<aj. For example, the sequence [0,2,0,...
[ "binary search", "combinatorics", "data structures", "dp", "implementation", "sortings" ]
#include <bits/stdc++.h> #define int long long using namespace std; int n, k, i, j, x, ans, t[8000321]; vector<int> g[123123], v; void update(int v, int tl, int tr, int pos) { if (tl == tr) { t[v]++; return; } int tm = (tl + tr) / 2; if (pos <= tm) update(v + v, tl, tm, pos); else update(v + v + ...
cpp
1315
B
B. Homecomingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a long party Petya decided to return home; but he turned out to be at the opposite end of the town from his home. There are nn crossroads in the line in the town, and there is either the bus or the tr...
[ "binary search", "dp", "greedy", "strings" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { cin.tie(0), ios::sync_with_stdio(0); ll t, a, b, p; cin >> t; while (t--) { cin >> a >> b >> p; string s; cin >> s; int n = s.size(); int k = n, c = s[n-2]; if (c == 'A') p -= a; else p...
cpp
1313
C2
C2. Skyscrapers (hard version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is a harder version of the problem. In this version n≤500000n≤500000The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the constru...
[ "data structures", "dp", "greedy" ]
#include<bits/stdc++.h> #define int long long using namespace std; int n; const int maxn=500010; int a[maxn]; stack<int> aft,bfr; int f[maxn],b[maxn]; int sum,s[maxn][2],arr[maxn]; signed main(){ cin >> n; for(int i = 0;i < n;i++) scanf("%d",&a[i]); for(int i = 0;i < n;i++){ while(!aft.empty() and a[i...
cpp
1295
E
E. Permutation Separationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a permutation p1,p2,…,pnp1,p2,…,pn (an array where each integer from 11 to nn appears exactly once). The weight of the ii-th element of this permutation is aiai.At first, you sepa...
[ "data structures", "divide and conquer" ]
#include<bits/stdc++.h> #define int long long using namespace std; int const N=200010; int n,p[N],a[N],f[N]; struct segtree{ #define ls(x) ((x)<<1) #define rs(x) ((x)<<1|1) #define mid (((l)+(r))>>1) static int const T=N<<2; int tr[T],lt[T]; void update(int x){ tr[x]=min(tr[ls(x)],tr[rs(x)]); } voi...
cpp
1313
A
A. Fast Food Restauranttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTired of boring office work; Denis decided to open a fast food restaurant.On the first day he made aa portions of dumplings, bb portions of cranberry juice and cc pancakes with condensed milk.The ...
[ "brute force", "greedy", "implementation" ]
#include <iostream> #include <map> #include <vector> int main() { int t; std::cin >> t; int a, b, c, a1, b1, c1; for (int i = 0; i < t; i++){ std::cin >> a >> b >> c; int N = 3, res = 0; std::vector<int> sets = {1, 2, 3, 4, 5, 6, 7}; do { a1 = a; ...
cpp
1320
D
D. Reachable Stringstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn this problem; we will deal with binary strings. Each character of a binary string is either a 0 or a 1. We will also deal with substrings; recall that a substring is a contiguous subsequence of a...
[ "data structures", "hashing", "strings" ]
#include<bits/stdc++.h> //#define feyn #define int long long #define ll __int128 using namespace std; const int N=200010; const int mod=1e15+7; inline void read(int &wh){ wh=0;int f=1;char w=getchar(); while(w<'0'||w>'9'){if(w=='-')f=-1;w=getchar();} while(w<='9'&&w>='0'){wh=wh*10+w-'0';w=getchar(...
cpp
1301
D
D. Time to Runtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBashar was practicing for the national programming contest. Because of sitting too much in front of the computer without doing physical movements and eating a lot Bashar became much fatter. Bashar is going...
[ "constructive algorithms", "graphs", "implementation" ]
#include <bits/stdc++.h> using namespace std; int main() { vector <pair<int, string>> way, ans; int n, m, k, cur = 0; cin >> n >> m >> k; if (k > 4 * n * m - 2 * (n + m)) { cout << "NO\n"; return 0; } cout << "YES\n"; for (int i = 0; i < n - 1; ++i) { if(m...
cpp
1296
F
F. Berland Beautytime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn railway stations in Berland. They are connected to each other by n−1n−1 railway sections. The railway network is connected, i.e. can be represented as an undirected tree.You have a map of ...
[ "constructive algorithms", "dfs and similar", "greedy", "sortings", "trees" ]
#include <bits/stdc++.h> using namespace std; const int N = 5005; int n; basic_string<int> e[N]; int d[N], p[N], w[N]; void dfs(int x) { for (int y : e[x]) { if (y == p[x]) continue; p[y] = x; d[y] = d[x] + 1; dfs(y); } } void vis(int a, int b, auto f) { while (a != b) { if (d[a] > d[b]) swap(a, b); f(b); b = p[b]; } ...
cpp
1290
E
E. Cartesian Tree time limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIldar is the algorithm teacher of William and Harris. Today; Ildar is teaching Cartesian Tree. However, Harris is sick, so Ildar is only teaching William.A cartesian tree is a rooted tree, that can be...
[ "data structures" ]
// LUOGU_RID: 101572938 #include <bits/stdc++.h> #define int long long using namespace std; const int maxn=150005,inf=1e18; int n,a[maxn],pos[maxn]; struct vl {int sum,mx,sc,cnt; vl(){sum=0;mx=sc=-inf;cnt=0;} }; inline vl merge(vl a,vl b) { vl ret; ret.sum=a.sum+b.sum; ret.mx=max(a.mx,b.mx); ...
cpp
1304
E
E. 1-Trees and Queriestime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputGildong was hiking a mountain; walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?Then...
[ "data structures", "dfs and similar", "shortest paths", "trees" ]
#include <iostream> #include <vector> using namespace std; using ll = long long; typedef vector<ll> vi; typedef vector<vi> vvi; ll INF = 2e9; ll n, q, u, v; vi a, d; vvi g, anc; void dfs(ll u, ll p) { if (p != -1) { a[u] = p; d[u] = d[p] + 1; } for (auto z:g[u]) { if (z == p) c...
cpp
1295
E
E. Permutation Separationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a permutation p1,p2,…,pnp1,p2,…,pn (an array where each integer from 11 to nn appears exactly once). The weight of the ii-th element of this permutation is aiai.At first, you sepa...
[ "data structures", "divide and conquer" ]
#include<bits/stdc++.h> typedef long long int ll; using namespace std; typedef vector <ll> Row; typedef vector <Row> Matrix; #define T while(t--) #define TASHLEEF ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define all(x) x.begin() , x.end() #define debv(v) for(auto p : v) cout << p << " "; #def...
cpp
1310
B
B. Double Eliminationtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe biggest event of the year – Cota 2 world championship "The Innernational" is right around the corner. 2n2n teams will compete in a double-elimination format (please, carefully read problem stat...
[ "dp", "implementation" ]
#include <bits/stdc++.h> using ul = std::uint32_t; using li = std::int32_t; using ull = std::uint64_t; using ll = std::int64_t; using lf = double; using llf = long double; using uss = std::uint8_t; using us = std::uint16_t; const ul maxn = 17; li ans[1 << maxn][2][2]; bool sp[1 << maxn]; ul n, k;...
cpp
1284
E
E. New Year and Castle Constructiontime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputKiwon's favorite video game is now holding a new year event to motivate the users! The game is about building and defending a castle; which led Kiwon to think about the following puz...
[ "combinatorics", "geometry", "math", "sortings" ]
#ifdef MY_LOCAL #include "D://competitive_programming/debug/debug.h" #define debug(x) cerr << "[" << #x<< "]:"<<x<<"\n" #else #define debug(x) #endif #define REP(i, n) for(int i = 0; i < n; i ++) #define REPL(i,m, n) for(int i = m; i < n; i ++) #define SORT(arr) sort(arr.begin(), arr.end()) #define LSOne(S) (...
cpp
1286
D
D. LCCtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn infinitely long Line Chillland Collider (LCC) was built in Chillland. There are nn pipes with coordinates xixi that are connected to LCC. When the experiment starts at time 0, ii-th proton flies from the ii-th...
[ "data structures", "math", "matrices", "probabilities" ]
// LUOGU_RID: 92552980 #include <bits/stdc++.h> #define P 998244353ll #define ll long long using namespace std; namespace QYB { void exgcd(ll a, ll b, ll &x, ll &y) { if (!b) return x = 1, y = 0, void(); exgcd(b, a % b, y, x); y -= a / b * x; } ll inv(ll a) { ll x, y; exgcd(a, P, x, y); ...
cpp
1305
F
F. Kuroni and the Punishmenttime limit per test2.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni is very angry at the other setters for using him as a theme! As a punishment; he forced them to solve the following problem:You have an array aa consisting of nn positive integers. ...
[ "math", "number theory", "probabilities" ]
#include <bits/stdc++.h> #define fi first #define se second #define faster ios_base::sync_with_stdio(0); cin.tie(0); #define pb push_back using namespace std; using ll = long long; using pii = pair <int, int>; mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count()); const int maxN = 2e5 + 1; c...
cpp
1292
D
D. Chaotic V.time limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputÆsir - CHAOS Æsir - V."Everything has been planned out. No more hidden concerns. The condition of Cytus is also perfect.The time right now...... 00:01:12......It's time."The emotion samples are now suffici...
[ "dp", "graphs", "greedy", "math", "number theory", "trees" ]
#include<bits/stdc++.h> #define N 5000 #define M 5000005 using namespace std; int tot,n,pr[N+5],cnt,s[N+5][705],son[M],a[M],v[N+5],p[N+5][705]; long long asw,sum[M]; bool vi[N+5]; struct nood { int to,next,v; } g[M]; inline void add(int x,int y,int z) { g[++cnt].v=z,g[cnt].to=y,g[cnt].next=a[x],a[x]=cnt; ...
cpp
1286
E
E. Fedya the Potter Strikes Backtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFedya has a string SS, initially empty, and an array WW, also initially empty.There are nn queries to process, one at a time. Query ii consists of a lowercase English letter cici and a n...
[ "data structures", "strings" ]
// S2OJ Submission #67633 @ 1675042177406 #include<bits/stdc++.h> #define int long long using namespace std; const int N = 6e5 + 5; int n, w, s[N], mn[N<<2], ne[N], fa[N][26], sum; map<int, int> mp;char op; __int128 ans; void pushup(int x){ mn[x] = min(mn[x<<1], mn[x<<1|1]); } void update(int x, int l, in...
cpp
1290
A
A. Mind Controltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou and your n−1n−1 friends have found an array of integers a1,a2,…,ana1,a2,…,an. You have decided to share it in the following way: All nn of you stand in a line in a particular order. Each minute, the p...
[ "brute force", "data structures", "implementation" ]
#include<bits/stdc++.h> using namespace std; #define int long long const int N = 1e6 + 10; int a[N]; int n, m, k; void solve() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) cin >> a[i]; k = min(k,m-1); int ans = 0; for (int i = 0; i <= k; i++) { int minx = 1e18; for (int j = 0; j <= m - k - 1; ...
cpp
1294
D
D. MEX maximizingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array [0,0,1,0,2][0,0,1,0,2] MEX equals to 33 because numbers 0,10,1 and 22 are prese...
[ "data structures", "greedy", "implementation", "math" ]
#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q, x, y, ans; map<int, int>mp; cin>>q>>x; ans = 0; for(int i=1;i<=q;++i) { cin>>y; ++mp[y%x]; while(mp[ans%x]) { --mp[ans%x]; ++ans; } cout<<ans<<"\n"; } ret...
cpp
1303
A
A. Erasing Zeroestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a cont...
[ "implementation", "strings" ]
//saw tutorial #include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { string s; cin>>s; int n=s.length(),i,j,k,l,m,c=0; for(i=0; i<n; i++) { if(s[i]=='1') { k=i; ...
cpp
1303
G
G. Sum of Prefix Sumstime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWe define the sum of prefix sums of an array [s1,s2,…,sk][s1,s2,…,sk] as s1+(s1+s2)+(s1+s2+s3)+⋯+(s1+s2+⋯+sk)s1+(s1+s2)+(s1+s2+s3)+⋯+(s1+s2+⋯+sk).You are given a tree consisting of nn vertices. Eac...
[ "data structures", "divide and conquer", "geometry", "trees" ]
#include <bits/stdc++.h> using namespace std; // clang-format off namespace { enum operand_type_enum : uint32_t { RAW = 0, COMPOSED = 1 << 1, REQUIRE_PARAM = 1 << 2, }; template <class T> concept has_operand_type = requires() { { T::operand_type } -> convertible_to<uint32_t>; }; template <class T> class operand_type...
cpp
1286
F
F. Harry The Pottertime limit per test9 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputTo defeat Lord Voldemort; Harry needs to destroy all horcruxes first. The last horcrux is an array aa of nn integers, which also needs to be destroyed. The array is considered destroyed if all its e...
[ "brute force", "constructive algorithms", "dp", "fft", "implementation", "math" ]
// LUOGU_RID: 102585203 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 22 ll n; ll p[N],a[N]; ll f[1<<N]; ll cnn[1<<N]; bool vis[1<<N]; inline ll suan(ll x){ if(f[x]!=-1)return f[x]; ll an=(vis[x]); for(int i=(x-1)&x;i;i=(i-1)&x){ if(!vis[x^i])continue; if(cnn[i]/2+1<=...
cpp
1291
B
B. Array Sharpeningtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou're given an array a1,…,ana1,…,an of nn non-negative integers.Let's call it sharpened if and only if there exists an integer 1≤k≤n1≤k≤n such that a1<a2<…<aka1<a2<…<ak and ak>ak+1>…>anak>ak+1>…>an. ...
[ "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> fin(n); for (int i = 0; i < n; ++i) cin >> fin[i]; int pref_num = -1, suff_num = n; for (int i ...
cpp
1294
C
C. Product of Three Numberstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given one integer number nn. Find three distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c and a⋅b⋅c=na⋅b⋅c=n or say that it is impossible to do it.If there are several answers, yo...
[ "greedy", "math", "number theory" ]
#include <iostream> #include <cmath> using std::cin, std::cout; int main(){ int t,n,n1,m,a,b,x,y,c; cin >> t; while(t--){ cin>> n; n1 = n; b=0,a=0,x=0,y=0,m=3; if(n%2 == 0){ a = 2; while(n%2 == 0){ n /= 2; ...
cpp
1311
A
A. Add Odd or Subtract Eventime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers aa and bb.In one move, you can change aa in the following way: Choose any positive odd integer xx (x>0x>0) and replace aa with a+xa+x; choose any positiv...
[ "greedy", "implementation", "math" ]
#include<bits/stdc++.h> using namespace std; int main() { int t,i; cin >> t; while(t--) { int a,b; cin >> a>> b; if(a==b) cout << 0 << endl; else if(b>a) { if(a%2==0 && b%2!=0) cout << 1 << endl; else if(a%2!=0 && b%2==0...
cpp
1325
B
B. CopyCopyCopyCopyCopytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEhab has an array aa of length nn. He has just enough free time to make a new array consisting of nn copies of the old array, written back-to-back. What will be the length of the new array's longe...
[ "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define ll long long int void solve() { int n; cin >> n; set<int> s; for (int i = 0; i < n; i++) ...
cpp
1303
C
C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arrange...
[ "dfs and similar", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { string a,b=""; cin>>a; bitset<26>mp; int i=0; bool flag=true; for(auto c:a) if(mp[c-'a']==1) { if(i<b.size()-1&&b[i+1]==c) i++; else if(i!=0 &&b[i-1]==c) i--; else flag=false; } else { mp[c-'a']=1; if(i==b.size()-1) b+=c,i++; else i...
cpp
1312
E
E. Array Shrinkingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a1,a2,…,ana1,a2,…,an. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements ai=ai+1ai=ai+1 (if there is at least one such...
[ "dp", "greedy" ]
#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define ll long long #define f first #define s second using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_nod...
cpp
1316
C
C. Primitive Primestime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt is Professor R's last class of his teaching career. Every time Professor R taught a class; he gave a special problem for the students to solve. You being his favourite student, put your heart in...
[ "constructive algorithms", "math", "ternary search" ]
#include<bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#define pbds tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> //using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef double db; typedef long double lld...
cpp
13
D
D. Trianglestime limit per test2 secondsmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to draw. He drew N red and M blue points on the plane in such a way that no three points lie on the same line. Now he wonders what is the number of distinct triangles with vertices in red points which do no...
[ "dp", "geometry" ]
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; typedef long long ll; typedef pair<int,int>P; const int INF=0x3f3f3f3f,maxn=505; const double eps=1e-9; struct Point ...
cpp
1315
C
C. Restoring Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a sequence b1,b2,…,bnb1,b2,…,bn. Find the lexicographically minimal permutation a1,a2,…,a2na1,a2,…,a2n such that bi=min(a2i−1,a2i)bi=min(a2i−1,a2i), or determine that it is impossib...
[ "greedy" ]
#include <bits/stdc++.h> using namespace std; // Vamos Abhinav // NO OBSTACLE IS BIGGER THAN HUMAN CAPABILITIES #define fo(i, n) for (i = 0; i < n; i++) #define FO(i, n) for (i = 1; i <= n; i++) #define ll long long #define PI 3.1415926535897932384626 #define pb push_back #define endl '\n' const int mod = 59...
cpp
1325
E
E. Ehab's REAL Number Theory Problemtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this...
[ "brute force", "dfs and similar", "graphs", "number theory", "shortest paths" ]
#include <bits/stdc++.h> using namespace std; const int MN=8e4; int N, at=170, dist[MN], ans = MN; bool pvis[1001], vis[MN]; set<int> start; vector<int> prime, vals, adj[MN]; unordered_map<int, int> ind; // (value, index it maps to) queue<pair<int, pair<int, int> > > next1; // (distance, (node, parent)) void ...
cpp
1300
A
A. Non-zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,ana1,a2,…,an]. In one step they can add 11 to any element of the array. Formally, in one step they can choose any integer index ii (1≤i≤n1≤i≤n) a...
[ "implementation", "math" ]
#include <bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #define dbg(x) \ cerr << #x << " "; \ _print(x); \ cerr << endl; #else #define dbg(x) ; #endif typedef long long ll; void _print(ll t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t...
cpp
1285
F
F. Classical?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array aa, consisting of nn integers, find:max1≤i<j≤nLCM(ai,aj),max1≤i<j≤nLCM(ai,aj),where LCM(x,y)LCM(x,y) is the smallest positive integer that is divisible by both xx and yy. For example, LCM(6,8...
[ "binary search", "combinatorics", "number theory" ]
// LUOGU_RID: 101755148 #pragma GCC optimize(3) #pragma GCC target("avx") #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma...
cpp
1312
A
A. Two Regular Polygonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm (m<nm<n). Consider a convex regular polygon of nn vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) an...
[ "geometry", "greedy", "math", "number theory" ]
#include <iostream> using namespace std; int main() { int t; cin>>t; while(t--) { int n,m; cin>>n>>m; if(n%m==0) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
cpp
1315
B
B. Homecomingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a long party Petya decided to return home; but he turned out to be at the opposite end of the town from his home. There are nn crossroads in the line in the town, and there is either the bus or the tr...
[ "binary search", "dp", "greedy", "strings" ]
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--){ int a, b, p; cin >> a >> b >> p; string s; cin >> s; int n = s.length(); int cost = 0, ans = n; for(int i=n-2; i>=0; i--){ if(i==0){ ...
cpp
1312
B
B. Bogosorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a1,a2,…,ana1,a2,…,an. Array is good if for each pair of indexes i<ji<j the condition j−aj≠i−aij−aj≠i−ai holds. Can you shuffle this array so that it becomes good? To shuffle an array m...
[ "constructive algorithms", "sortings" ]
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define elif else if #define accepted cout << "YES\n" #define error cout << "NO\n" using namespace std; const ll N = 2e10; const ll mod = 1e9 + 7; void func() ...
cpp
1286
E
E. Fedya the Potter Strikes Backtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFedya has a string SS, initially empty, and an array WW, also initially empty.There are nn queries to process, one at a time. Query ii consists of a lowercase English letter cici and a n...
[ "data structures", "strings" ]
// S2OJ Submission #67658 @ 1675085389162 #include <algorithm> #include <iostream> #include <vector> #include <cstdio> #include <map> #define int long long #define maxn 600005 using namespace std; inline int read(){ int x = 0, flag = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '...
cpp
1316
F
F. Battalion Strengthtime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn officers in the Army of Byteland. Each officer has some power associated with him. The power of the ii-th officer is denoted by pipi. As the war is fast approaching, the General would ...
[ "data structures", "divide and conquer", "probabilities" ]
#include <bits/stdc++.h> #define st first #define nd second using lint = int64_t; constexpr int mod = int(1e9) + 7; constexpr int inf = 0x3f3f3f3f; constexpr int ninf = 0xcfcfcfcf; constexpr lint linf = 0x3f3f3f3f3f3f3f3f; const long double pi = acosl(-1.0); // Returns -1 if a < b, 0 if a = b and 1 if a > b. ...
cpp
13
D
D. Trianglestime limit per test2 secondsmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to draw. He drew N red and M blue points on the plane in such a way that no three points lie on the same line. Now he wonders what is the number of distinct triangles with vertices in red points which do no...
[ "dp", "geometry" ]
//https://codeforces.com/problemset/problem/13/D #include<bits/stdc++.h> #define VI vector<int> typedef long double db; using namespace std; const db eps=1e-20; const db PI = acos(-1.); int sign(db x) {return x<-eps?-1:x>eps;} int cmp(db x, db y) {return sign(x-y);} struct Point{ db x,y; Point(){} ...
cpp
1310
A
A. Recommendationstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVK news recommendation system daily selects interesting publications of one of nn disjoint categories for each user. Each publication belongs to exactly one category. For each category ii batch algori...
[ "data structures", "greedy", "sortings" ]
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; pair<long long int,int> p[n+1]; for(int i=0; i<n; i++){ cin>>p[i].first; } for(int i=0; i<n; i++) cin>>p[i].second; sort(p,p+n); p[n].first=2*(pow(10,9)); p[n].second=0; long long int ans=0; priority_queue<int> q; q.push(p[0].second); long long in...
cpp
1311
C
C. Perform the Combotime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou want to perform the combo on your opponent in one popular fighting game. The combo is the string ss consisting of nn lowercase Latin letters. To perform the combo, you have to press all buttons ...
[ "brute force" ]
#include <iostream> #include <vector> #include <map> #include <string.h> #include <math.h> #include <set> #include <bits/stdc++.h> using namespace std; #define ll long long #define cy cout << "YES\n" #define cn cout << "NO\n" #define nl "\n" #define fi first #define se second #define MOD 1000000007 #...
cpp
1304
A
A. Two Rabbitstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBeing tired of participating in too many Codeforces rounds; Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller ...
[ "math" ]
#include <iostream> #include <fstream> #include <stack> #include <set> #include <map> #include <stack> #include <vector> #include <queue> #include <string> #include <algorithm> #include <numeric> #include <cmath> #include <array> #include <bitset> #include <queue> #include <cstring> #include <iomanip> ...
cpp
1284
G
G. Seollaltime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputIt is only a few days until Seollal (Korean Lunar New Year); and Jaehyun has invited his family to his garden. There are kids among the guests. To make the gathering more fun for the kids, Jaehyun is going t...
[ "graphs" ]
#include <bits/stdc++.h> using namespace std; const int maxn = 810; int T, n, m, fa[maxn], d[maxn], pre[maxn], lim[maxn], eid[22][22][2]; bool bad[maxn], mark[maxn], vis[maxn], in1[maxn], in2[maxn], ban[maxn]; vector<int> G[maxn]; char str[22][22], ans[42][42]; int id(int x, int y) { return (x - 1) * m ...
cpp
1321
A
A. Contest for Robotstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, a...
[ "greedy" ]
#include <bits/stdc++.h> using namespace std; //------------------------------------------------------- void fast(){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} //------------------------------------------------------- #define endl '\n' #define pb push_back #define all(x) (x).begin(),(x).en...
cpp
1296
F
F. Berland Beautytime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn railway stations in Berland. They are connected to each other by n−1n−1 railway sections. The railway network is connected, i.e. can be represented as an undirected tree.You have a map of ...
[ "constructive algorithms", "dfs and similar", "greedy", "sortings", "trees" ]
#include <iostream> #include <vector> #include <bitset> #include <map> #include <queue> #include <stack> #include <algorithm> #include <cmath> #include <set> #include <cstring> #include <array> #pragma GCC optimize(2) #include <bits/stdc++.h> #define rep(i,from,to) for(int i=from;i<to;i++) #define ite2(x,...
cpp
1325
C
C. Ehab and Path-etic MEXstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of nn nodes. You want to write some labels on the tree's edges such that the following conditions hold: Every label is an integer between 00 and n−2n−2 inclusiv...
[ "constructive algorithms", "dfs and similar", "greedy", "trees" ]
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int>pi; typedef pair<ll, ll>pl; typedef vector<pi>vpi; typedef vector<pl>vpl; typedef vector<vi> vvi; typedef vector<vl...
cpp
1141
F1
F1. Same Sum Blocks (Easy)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is given in two editions; which differ exclusively in the constraints on the number nn.You are given an array of integers a[1],a[2],…,a[n].a[1],a[2],…,a[n]. A block is a sequence ...
[ "greedy" ]
#include <bits/stdc++.h> using namespace std; #define sz(s) (int)(s.size()) #define all(v) v.begin(),v.end() #define clr(d, v) memset(d,v,sizeof(d)) #define ll long long void file() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } int main() { file(); int n;...
cpp
1294
A
A. Collecting Coinstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp has three sisters: Alice; Barbara, and Cerene. They're collecting coins. Currently, Alice has aa coins, Barbara has bb coins and Cerene has cc coins. Recently Polycarp has returned from the ...
[ "math" ]
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll t; cin >> t; while (t--) { ll a, b, c, n; cin >> a >> b >> c >> n; ll sum = a + b + c + n; if ((sum % 3 == 0) && (sum / 3 >= a) && (sum / 3 >= b) && (sum / 3 >= c)) ...
cpp
1305
C
C. Kuroni and Impossible Calculationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTo become the king of Codeforces; Kuroni has to solve the following problem.He is given nn numbers a1,a2,…,ana1,a2,…,an. Help Kuroni to calculate ∏1≤i<j≤n|ai−aj|∏1≤i<j≤n|ai−aj|. As re...
[ "brute force", "combinatorics", "math", "number theory" ]
#include "bits/stdc++.h" using namespace std; #define ll long long #define pb push_back #define pob pop_back() #define mp make_pair #define add_m(a, b,mod) (((a % mod) + (b % mod)) % mod) #define sub_m(a, b,mod) (((a % mod) - (b % mod) + mod) % mod) #define mul_m(a, b,mod) (((a % mod) * (b % mod)) % mod) ...
cpp
1316
C
C. Primitive Primestime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt is Professor R's last class of his teaching career. Every time Professor R taught a class; he gave a special problem for the students to solve. You being his favourite student, put your heart in...
[ "constructive algorithms", "math", "ternary search" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = tuple<int, int>; using vi = vector<ll>; using vii = vector<ii>; using vvi = vector<vi>; using si = set<ll>; int main() { cin.tie(0), ios::sync_with_stdio(0); ll n, m, p, a, b; cin >> n >> m >> p; int k = -1, l = -1; ...
cpp
1299
E
E. So Meantime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is interactive.We have hidden a permutation p1,p2,…,pnp1,p2,…,pn of numbers from 11 to nn from you, where nn is even. You can try to guess it using the following queries:?? kk a1a1 a2a2 …… akak.I...
[ "interactive", "math" ]
#include<bits/stdc++.h> #define ll long long #define rep(i, x, y) for(int i = (x), stOyny = (y); i <= stOyny; ++i) #define irep(i, x, y) for(int i = (x), stOyny = (y); i >= stOyny; --i) #define pb emplace_back #define pii pair<int, int> #define vint vector<int> #define fi first #define se second #define let co...
cpp
1325
B
B. CopyCopyCopyCopyCopytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEhab has an array aa of length nn. He has just enough free time to make a new array consisting of nn copies of the old array, written back-to-back. What will be the length of the new array's longe...
[ "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; set<int> s; for (int i = 0, x; i < n; i++) { cin >> x; s.insert(x); } cout << s.size() << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve()...
cpp
1312
D
D. Count the Arraystime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYour task is to calculate the number of arrays such that: each array contains nn elements; each element is an integer from 11 to mm; for each array, there is exactly one pair of equal elements; f...
[ "combinatorics", "math" ]
#include<bits/stdc++.h> using namespace std; typedef long long int ll; #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int,null_type, less<int>,rb_tree_tag, tree_or...
cpp
1286
F
F. Harry The Pottertime limit per test9 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputTo defeat Lord Voldemort; Harry needs to destroy all horcruxes first. The last horcrux is an array aa of nn integers, which also needs to be destroyed. The array is considered destroyed if all its e...
[ "brute force", "constructive algorithms", "dp", "fft", "implementation", "math" ]
#include<bits/stdc++.h> #define mset(a,b) memset((a),(b),sizeof((a))) #define rep(i,l,r) for(int i=(l);i<=(r);i++) #define dec(i,l,r) for(int i=(r);i>=(l);i--) #define cmax(a,b) (((a)<(b))?(a=b):(a)) #define cmin(a,b) (((a)>(b))?(a=b):(a)) #define Next(k) for(int x=head[k];x;x=li[x].next) #define vc vector #define ar a...
cpp
1288
B
B. Yet Another Meme Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers AA and BB, calculate the number of pairs (a,b)(a,b) such that 1≤a≤A1≤a≤A, 1≤b≤B1≤b≤B, and the equation a⋅b+a+b=conc(a,b)a⋅b+a+b=conc(a,b) is true; conc(a,b)conc(a,b)...
[ "math" ]
#include<bits/stdc++.h> using namespace std; main(){long long t,a,b;for(cin>>t;t--;cout<<(int)log10(b+1)*a<<endl)cin>>a>>b;}
cpp
1294
C
C. Product of Three Numberstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given one integer number nn. Find three distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c and a⋅b⋅c=na⋅b⋅c=n or say that it is impossible to do it.If there are several answers, yo...
[ "greedy", "math", "number theory" ]
#include<iostream> #include<vector> #include<set> #include<queue> #include<map> #include<algorithm> #include<numeric> #include<cmath> #include<cstring> #include<bitset> #include<iomanip> #include<random> #include<fstream> #include<complex> #include<time.h> #include<stack> using namespace std; #define endl "\n" #define ...
cpp
1299
D
D. Around the Worldtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas are planning 144144 trips around the world.You are given a simple weighted undirected connected graph with nn vertexes and mm edges with the following restriction: there isn't a...
[ "bitmasks", "combinatorics", "dfs and similar", "dp", "graphs", "graphs", "math", "trees" ]
#include<bits/stdc++.h> #define mod 1000000007 using namespace std; int n,m,t=0,edge_t=0,rt_t=0,i0=0,i1=1,ans=0; int la[100002],rt[100002],pre[100002],deg[100002],sum[100002]; int num[382][382],f[2][382]; bool u[100002]; vector<int> Vec; vector<int> vec[382],vec2[100002]; unordered_map<int,int> mp; struct aaa...
cpp
1292
B
B. Aroma's Searchtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTHE SxPLAY & KIVΛ - 漂流 KIVΛ & Nikki Simmons - PerspectivesWith a new body; our idol Aroma White (or should we call her Kaori Minamiya?) begins to uncover her lost past through the OS space.The space can...
[ "brute force", "constructive algorithms", "geometry", "greedy", "implementation" ]
#include <cstdio> #include <cmath> #include <vector> int main() { long long x0, y0, ax, ay, bx, by; long long xs, ys, t; scanf("%lld %lld %lld %lld %lld %lld", &x0, &y0, &ax, &ay, &bx, &by); scanf("%lld %lld %lld", &xs, &ys, &t); long long xl = xs + t, yl = ys + t; using Point = std...
cpp
1294
D
D. MEX maximizingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array [0,0,1,0,2][0,0,1,0,2] MEX equals to 33 because numbers 0,10,1 and 22 are prese...
[ "data structures", "greedy", "implementation", "math" ]
#include <bits/stdc++.h> #define ll long long int #define srv(v) sort(v.begin(),v.end()) #define rrv(s1) sort(s1.begin(),s1.end(),greater<int>()) #define str string #define sz size() #define dv(v) vector<ll> v #define ds(s) set<ll> s #define dm(mp) map<ll,ll> mp #define debug(x) cout<<#x<<" "<<x<<endl #defin...
cpp
1313
B
B. Different Rulestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNikolay has only recently started in competitive programming; but already qualified to the finals of one prestigious olympiad. There going to be nn participants, one of whom is Nikolay. Like any good o...
[ "constructive algorithms", "greedy", "implementation", "math" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; void solve(){ ll n,x,y; cin>>n>>x>>y; cout<<max(1ll,min(x+y-n+1,n))<<' '; cout<<min(x+y-1,n)<<' '; cout<<'\n'; } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t = 1; cin>>t; ...
cpp
1301
B
B. Motarack's Birthdaytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputDark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array aa of nn non-negative integers.Dark created that array 10001000 years ago, so so...
[ "binary search", "greedy", "ternary search" ]
#include<iostream> #include<iterator> #include<algorithm> #include<bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #include "local_dbg.cpp" #else #define debug(...) 101; #endif typedef long long int ll; typedef long double ld; typedef std::vector<int> vi; typedef std::vector<ll> vll; typed...
cpp
1291
A
A. Even But Not Eventime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's define a number ebne (even but not even) if and only if its sum of digits is divisible by 22 but the number itself is not divisible by 22. For example, 1313, 12271227, 185217185217 are ebne num...
[ "greedy", "math", "strings" ]
#include <bits/stdc++.h> #pragma GCC optimize ("unroll-loops") #pragma GCC optimize ("O3") #define ll long long #define st string #define ull unsigned ll #define pii pair <int, int> #define pll pair <ll, ll> #define pb push_back #define ins insert #define F first #define S second //#define int ll using nam...
cpp
1313
D
D. Happy New Yeartime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBeing Santa Claus is very difficult. Sometimes you have to deal with difficult situations.Today Santa Claus came to the holiday and there were mm children lined up in front of him. Let's number them fr...
[ "bitmasks", "dp", "implementation" ]
/* #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimize("unroll-loops") */ #include<bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define eb emplace_back #define PI 3.14159265359 #define fi first #define se second #define ...
cpp
1322
F
F. Assigning Farestime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputMayor of city M. decided to launch several new metro lines during 2020. Since the city has a very limited budget; it was decided not to dig new tunnels but to use the existing underground network.The ...
[ "dp", "trees" ]
// LUOGU_RID: 98244252 #include <bits/stdc++.h> using namespace std; const int N=5e5+5; int n,m,k,sz[N],d[N],so[N],tp[N],fa[N],s[N],h[N],c[N*2],b[N*2],p[N*2],f[N*2]; vector<int> e[N],g[N]; inline void cmi(int&x,int y) {if(x>y) x=y;} inline void cmx(int&x,int y) {if(x<y) x=y;} int getf(int x) {return f[x]==x?x:f[...
cpp
1284
B
B. New Year and Ascent Sequencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputA sequence a=[a1,a2,…,al]a=[a1,a2,…,al] of length ll has an ascent if there exists a pair of indices (i,j)(i,j) such that 1≤i<j≤l1≤i<j≤l and ai<ajai<aj. For example, the sequence [0,2,0,...
[ "binary search", "combinatorics", "data structures", "dp", "implementation", "sortings" ]
#include<iostream> #include<cstring> #include<vector> #include<map> #include<queue> #include<unordered_map> #include<cmath> #include<cstdio> #include<algorithm> #include<set> #include<cstdlib> #include<stack> #include<ctime> #define forin(i,a,n) for(int i=a;i<=n;i++) #define forni(i,n,a) for(int i=n;i>=a;...
cpp
1292
F
F. Nora's Toy Boxestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSIHanatsuka - EMber SIHanatsuka - ATONEMENTBack in time; the seven-year-old Nora used to play lots of games with her creation ROBO_Head-02, both to have fun and enhance his abilities.One day, Nora's a...
[ "bitmasks", "combinatorics", "dp" ]
// LUOGU_RID: 92191453 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> using namespace std; #define fi first #define sc second #define mkp make_pair #define pii pair<int,int> typedef long long ll; const int N=65,M=(1<<12)+5,oo=1e9,mod=1e9+7; inline int read() ...
cpp
1288
B
B. Yet Another Meme Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers AA and BB, calculate the number of pairs (a,b)(a,b) such that 1≤a≤A1≤a≤A, 1≤b≤B1≤b≤B, and the equation a⋅b+a+b=conc(a,b)a⋅b+a+b=conc(a,b) is true; conc(a,b)conc(a,b)...
[ "math" ]
/// endless ? #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long double ld; #define ll long long #define F first #define S second #define pii pair...
cpp
1320
E
E. Treeland and Virusestime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are nn cities in Treeland connected with n−1n−1 bidirectional roads in such that a way that any city is reachable from any other; in other words, the graph of cities and roads is a tree. Tr...
[ "data structures", "dfs and similar", "dp", "shortest paths", "trees" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define all(a) begin(a), end(a) #define len(a) int((a).size()) namespace rn = ranges; namespace vw = rn::views; #ifdef LOCAL #include "debug.h" #else #define dbg(...) #define dprint(...) #def...
cpp
1310
D
D. Tourismtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMasha lives in a country with nn cities numbered from 11 to nn. She lives in the city number 11. There is a direct train route between each pair of distinct cities ii and jj, where i≠ji≠j. In total there are ...
[ "dp", "graphs", "probabilities" ]
#include <bits/stdc++.h> using namespace std; typedef double db; #define int long long #define fi first #define se second #define mk make_pair #define pb emplace_back #define poly vector<int> #define Bt(a) bitset<a> #define bc __builtin_popcount #define pc putchar #define ci const int& const int mod = 1e9+...
cpp
1286
A
A. Garlandtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVadim loves decorating the Christmas tree; so he got a beautiful garland as a present. It consists of nn light bulbs in a single row. Each bulb has a number from 11 to nn (in arbitrary order), such that all th...
[ "dp", "greedy", "sortings" ]
//#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3") #include "bits/stdc++.h" #define F first #define S second #define me "| Temirlan |" #define ALL(a) a.begin() , a.end() #define OK cerr << "---------------------------OK...
cpp
1293
A
A. ConneR and the A.R.C. Markland-Ntime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSakuzyo - ImprintingA.R.C. Markland-N is a tall building with nn floors numbered from 11 to nn. Between each two adjacent floors in the building, there is a staircase connecting them.I...
[ "binary search", "brute force", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define cin(vec) \ for (auto &i : vec) \ cin >> i #define cout(vec) \ for (auto &i : vec) \ cout << i << " "; \ cout << "\n"; #define sz(x) int(x.size()) #define pb(x) push_back(x) #define Num_of_Digits(n) ((int)log10(n) + 1) #define fi firs...
cpp