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
1295
A
A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit...
[ "greedy" ]
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int n; cin>>n; if(n%2!=0){ cout<<7; n-=3; } while(n>0){ cout<<1; n-=2; } cout<<'\n'; } return 0; }
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; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, p; cin>>n>>m>>p; vector<int> A(n); vector<int> B(m); int mina = n; int minb = m; for(int i = 0; i<n; i++){ cin>>A[i]; } for(int i = 0; i<m; i++){ cin>>B[i...
cpp
1295
D
D. Same GCDstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers aa and mm. Calculate the number of integers xx such that 0≤x<m0≤x<m and gcd(a,m)=gcd(a+x,m)gcd(a,m)=gcd(a+x,m).Note: gcd(a,b)gcd(a,b) is the greatest common divisor of aa and bb.I...
[ "math", "number theory" ]
#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...
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> using namespace std; int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); long long s; cin>>s; while(s--) { long long l; cin>>l; long long s[l+2]; for(long long q=0;q<l;q++) cin>>s[q]; sort(s,s+l,greater<long long>()); for(long long q=0;q<l...
cpp
1296
B
B. Food Buyingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMishka wants to buy some food in the nearby shop. Initially; he has ss burles on his card. Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer numb...
[ "math" ]
#include<bits/stdc++.h> using namespace std; int main() { long long t,s; cin>>t; while(t--){ cin>>s;int sum=s; while(s>9){ int k=s%10; s=s/10; sum=sum+s; s=s+k; } cout<<sum<<endl; } return 0; }
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" ]
// Author : حسن #include <bits/stdc++.h> using namespace std; #define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define rall(s) s.rbegin(),s.rend() #define all(s) s.begin(),s.end() #define pb push_back #define fi first #define se second #define ll long long #define ld long double #define YES c...
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<bits/stdc++.h> using namespace std; int main(){ int a,b,c; int n; cin>>n; while(n--){ int ans=0; cin>>a>>b>>c; if(a)a--,ans++; if(b)b--,ans++; if(c)c--,ans++; if(a<c)swap(a,c); if(a<b)swap(a,b); if(a&&b)a--,b--,ans++; if(a&&c)a--,c--,ans++; if(b&&c)b--,c--,ans++; if(a&&b&&c)ans++; cout<<ans<<endl;} }
cpp
1301
C
C. Ayoub's functiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAyoub thinks that he is a very smart person; so he created a function f(s)f(s), where ss is a binary string (a string which contains only symbols "0" and "1"). The function f(s)f(s) is equal to the nu...
[ "binary search", "combinatorics", "greedy", "math", "strings" ]
#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
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> #define ll long long using namespace std; const ll mod = 1e9+7; bool cmp(pair<ll,ll>a,pair<ll,ll>b) { return a.second < b.second; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n; cin >> n; vector<ll>a(n+1),pref(n+1,0); ...
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; #define ll long long #define mod 1e9 + 7 string a = "abcdefghijklmnopqrstuvwxyz"; void dfs(string& k, char x, map<char, vector<char>>& mp, map<char, bool>& visited){ if(x < 'a' || x > 'z') return; k += x; visited[x] = true; // if(mp[x][0] == '...
cpp
1296
A
A. Array with Odd Sumtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn integers.In one move, you can choose two indices 1≤i,j≤n1≤i,j≤n such that i≠ji≠j and set ai:=ajai:=aj. You can perform such moves any number of times (poss...
[ "math" ]
#include <iostream> #include <string> const int SIZE = 2000; int main() { int t, n, a, odd_count; std::cin >> t; std::string arr[SIZE]; std::string s; bool has_even, has_odd; int temp[300]; for (int i = 0; i < t; i++) { std::cin >> n; odd_count = 0;...
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; #define int long long #define PII pair<int,int> #define x first #define y second const int N=1e5+10; void solve() { int n,x,y; cin>>n>>x>>y; int t=(x+y+1)-n; int a=min(max(t,1ll),n); int b=min(x+y-1,n); cout<<a<<' '<<b<<endl; return; } si...
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" ]
/* Code by Poojan Patel (poojanpatel2712) */ #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define tt ll testcase; cin>>testcase; while(testcase--) #define ic(n) ll n; cin>>n #define arr(a,n) ll a[n]; for(ll i=0; ...
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" ]
// Akash Singh #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; //Make less_equal for multiset // find_by_order(k) returns...
cpp
1292
E
E. Rin and The Unknown Flowertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMisoilePunch♪ - 彩This is an interactive problem!On a normal day at the hidden office in A.R.C. Markland-N; Rin received an artifact, given to her by the exploration captain Sagar.After much...
[ "constructive algorithms", "greedy", "interactive", "math" ]
//llllljy #include<bits/stdc++.h> using namespace std; const int maxn=105; int t,n,vis[maxn],czc; void solve1(){ int x,y; memset(vis,0,sizeof(vis)); printf("? CH\n");fflush(stdout);scanf("%d",&x); for(int i=1;i<=x;i++)scanf("%d",&y),vis[y]=1,vis[y+1]=3; printf("? CC\n");fflush(stdout);scanf("%d",&x); f...
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; typedef long long ll; //#define int long long //#define endl '\n' const int N=200009; const int INF=0x3f3f3f3f; const int mod=998244353; inline int read(){ int s=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch...
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 const ll MM = 998244353, N = 1e5 + 10, MAX = 1e15; using namespace std; vector<ll>v[200], v1[200]; int solve() { int n; cin >> n; string s, t; cin >> s >> t; for (int i = 0; i < n; i++) { v[s[i]].emplace_back(i + 1); v1[t[i]].emplace_back(i + 1); ...
cpp
1307
G
G. Cow and Exercisetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFarmer John is obsessed with making Bessie exercise more!Bessie is out grazing on the farm; which consists of nn fields connected by mm directed roads. Each road takes some time wiwi to cross. She is ...
[ "flows", "graphs", "shortest paths" ]
#include <bits/stdc++.h> using namespace std; #define rep(i, s, e) for (int i = s; i <= e; ++i) #define drep(i, s, e) for (int i = s; i >= e; --i) #define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout) #define pv(a) cout << #a << " = " << a << endl #define pa(a, l, r) cout << #a " : "; r...
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" ]
// LUOGU_RID: 99754622 #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod=998244353; const int N=2e5+5; ll x[N], y[N]; void solve(){ cin>>x[0]>>y[0]; ll ax,ay,bx,by; cin>>ax>>ay>>bx>>by; int tot; for(int i=1;;i++){ __i...
cpp
1304
C
C. Air Conditionertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGildong owns a bulgogi restaurant. The restaurant has a lot of customers; so many of them like to make a reservation before visiting it.Gildong tries so hard to satisfy the customers that he even memor...
[ "dp", "greedy", "implementation", "sortings", "two pointers" ]
#include <bits/stdc++.h> #define sys ios_base::sync_with_stdio(0);cin.tie(0); #define mod 1000000007 using namespace std; //#pragma comment(linker, "/STACK:268435456"); #define count_setbits(n) __builtin_popcount(n) #define fixed cout<<fixed<<setprecision(16) #define count_bits(n) ((ll)log2(n))+1 #define no_of...
cpp
1312
F
F. Attack on Red Kingdomtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe Red Kingdom is attacked by the White King and the Black King!The Kingdom is guarded by nn castles, the ii-th castle is defended by aiai soldiers. To conquer the Red Kingdom, the Kings have t...
[ "games", "two pointers" ]
#include "bits/stdc++.h" using namespace std; typedef long long ll; #define deb(...) logger(#__VA_ARGS__, __VA_ARGS__) template<typename ...Args> void logger(string vars, Args&&... values) { cout << vars << " = "; string delim = ""; (..., (cout << delim << values, delim = ", ")); cout << '\n'; } ...
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" ]
// LUOGU_RID: 98466313 #include <bits/stdc++.h> #include <utility> namespace atcoder { namespace internal { // @param m `1 <= m` // @return x mod m constexpr long long safe_mod(long long x, long long m) { x %= m; if (x < 0) x += m; return x; } // Fast modular multiplication by barre...
cpp
1290
D
D. Coffee Varieties (hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. You can find the easy version in the Div. 2 contest. Both versions only differ in the number of times you can ask your friend to taste coffee.Th...
[ "constructive algorithms", "graphs", "interactive" ]
#include <bits/stdc++.h> #define int long long using namespace std; const int N = 1e3 + 42, INF = 1e18 + 42; bool eq[N]; int n, k, nb = 0; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; if(k == 1) k = 2; for(int i = 1; i < 2*...
cpp
1313
C1
C1. Skyscrapers (easy version)time limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is an easier version of the problem. In this version n≤1000n≤1000The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the constructio...
[ "brute force", "data structures", "dp", "greedy" ]
#include <bits/stdc++.h> using namespace std; #define int long long int32_t main() { int n; cin >> n; vector<int>a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } //pick max vector<int> c(n); int ans = 0; for (int i = 0; i < n; i++) { //max is i ...
cpp
1307
G
G. Cow and Exercisetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFarmer John is obsessed with making Bessie exercise more!Bessie is out grazing on the farm; which consists of nn fields connected by mm directed roads. Each road takes some time wiwi to cross. She is ...
[ "flows", "graphs", "shortest paths" ]
#include<bits/stdc++.h> #define fi first #define se second using namespace std; const int _=100,inf=1e9; int S,T,n,m,Q,head[_],ne[_*_],to[_*_],w[_*_],c[_*_],tot=1,x; int dis[_],vis[_],p[_]; vector<pair<int,int>>ans; void add(int x,int y,int z,int a){ ne[++tot]=head[x],head[x]=tot,to[tot]=y,w[tot]=z,c[tot]=a; ...
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" ]
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #ifndef ATCODER_INTERNAL_BITOP_HPP #define ATCODER_INTERNAL_BITOP_HPP 1 #ifdef _MSC_VER #include <intrin.h> #include<cassert> #endif namespace atcoder { namespace internal { int ceil_pow2(int n) { i...
cpp
1323
B
B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n×mn×m formed by following rule: ci,j=ai⋅bjci,j=ai⋅bj (i.e....
[ "binary search", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define ll long long ll a[40001],b[40001],n,m,i,j,k,ans=0; int main() { cin>>n>>m>>k; for(i=0;i<n;i++){cin>>a[i];if(a[i]) a[i]+=a[i-1];} for(i=0;i<m;i++){cin>>b[i];if(b[i]) b[i]+=b[i-1];} for(i=1;i<=n;i++) if(k%i==0){ ll u=i,v=k/i,x=0,y=0l; for(j=0;j<n;j++) if(a[j]>=u) x++...
cpp
1301
F
F. Super Jabertime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJaber is a superhero in a large country that can be described as a grid with nn rows and mm columns, where every cell in that grid contains a different city.Jaber gave every city in that country a specifi...
[ "dfs and similar", "graphs", "implementation", "shortest paths" ]
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define debug(x) cerr << #x << " " << x << "\n" #define debugs(x) cerr << #x << " " << x << " " using namespace std; typedef long long ll; typedef pair <short, short> pii; const ll NMAX = 1002; const ll VMAX = 41; const ll INF = (1LL << 59); const l...
cpp
1311
E
E. Construct the Binary Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and dd. You need to construct a rooted binary tree consisting of nn vertices with a root at the vertex 11 and the sum of depths of all vertices equals to dd.A t...
[ "brute force", "constructive algorithms", "trees" ]
/// What are you doing now? Just go f*cking code now dude? #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define TASK "codin" //#define int long long using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long llu; #define IO ios_base::sync_with_stdio(false);cin.tie(0...
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" ]
#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0); cin.tie(0) #define ff first #define ss second #define pb push_back #define pf push_front #define all(s) s.begin(),s.end() typedef long long ll; const double pi = acos(-1),ef=1e-9; const ll N=100+5,M=1e9+7; ll n,a[N],mm[N]...
cpp
1323
B
B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n×mn×m formed by following rule: ci,j=ai⋅bjci,j=ai⋅bj (i.e....
[ "binary search", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; void count(vector<int> &src, vector<long long> &dest){ int i = 0; int n = src.size(); while (i < n){ int j = i; while (j < n && src[j] == 1){ j++; } int len_ones = (j - i); int cnt = 1; ...
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<iostream> using namespace std; int t,a,b,c,n; main(){for(cin>>t;t--;puts((a+b+c+n)%3||(a+b+c+n)/3<max(a,max(b,c))?"NO":"YES")) cin>>a>>b>>c>>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> //#include <atcoder/all> #define f first #define s second # define FAST() ios_base::sync_with_stdio(false); cin.tie(NULL); #define IN() freopen("collectingofficer.in", "r", stdin); #define OUT() freopen("output.txt", "w", stdout) ; /* وَمَا تَوْفِيقِي إِ...
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<bits/stdc++.h> using namespace std; #define int long long #define double long long struct Point{ double x,y; Point(double x=0,double y=0):x(x),y(y){} }; typedef Point Vector; Vector operator - (Point A, Point B){return Vector(A.x - B.x, A.y - B.y);} Vector operator * (Vector A, double p){ret...
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; typedef long long ll; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; using vi = vc<int>; using vl = vc<ll>; using vvi = vc<vi>; using vvl = vc<vl>; using pii = pair<int, int>; using pll = pair<ll, ll>; #define inf 0x3f3f3f3f #define...
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" ]
/** * author: tourist * created: 02.02.2020 17:12:43 **/ #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int len = (int) s.size(); vector<int> nxt(len + 1); for (int i = len - 2; i >= 0; i--) { if...
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" ]
#line 1 "library/my_template.hpp" #if defined(LOCAL) #include <my_template_compiled.hpp> #else #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<ll, ll>; using vi = vector<ll>; using u32 = unsigned int;...
cpp
1307
F
F. Cow and Vacationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie is planning a vacation! In Cow-lifornia; there are nn cities, with n−1n−1 bidirectional roads connecting them. It is guaranteed that one can reach any city from any other city. Bessie is consi...
[ "dfs and similar", "dsu", "trees" ]
#include<bits/stdc++.h> #define Mx 18 using namespace std; int n,k,r,tot,edge_t=0,Q; int la[400002],rt[400002],col[400002],dep[400002]; int fa[400002][22]; vector<int> vec[200002]; typedef pair<int,int> P;P p; queue<P> q; struct aaa { int to,nx; }edge[800002]; inline void add_edge(int x,int y) { edge[+...
cpp
1294
F
F. Three Paths on a Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an unweighted tree with nn vertices. Recall that a tree is a connected undirected graph without cycles.Your task is to choose three distinct vertices a,b,ca,b,c on this tree such t...
[ "dfs and similar", "dp", "greedy", "trees" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector <int> vi; typedef vector <string> vs; typedef pair <int, int> pii; typedef vector<pii > vpii; #define MP make_pair #define F first #define S second #define REVERSE(a) reverse (a.begin(), a.end()) #define ALL(a) a.begin(), a.end() #def...
cpp
1293
B
B. JOE is on TV!time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output3R2 - Standby for ActionOur dear Cafe's owner; JOE Miller, will soon take part in a new game TV-show "1 vs. nn"!The game goes in rounds, where in each round the host asks JOE and his opponents a common q...
[ "combinatorics", "greedy", "math" ]
#include <bits/stdc++.h> #include <iostream> #include <vector> #include <iomanip> using namespace std; typedef long long ll; #define vi vector<int> int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); double a=0; int n; cin>>n; for(int i=1;i<=n;i++) a=a+(double)1/i; ...
cpp
1288
A
A. Deadlinetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAdilbek was assigned to a special project. For Adilbek it means that he has nn days to run a special program and provide its results. But there is a problem: the program needs to run for dd days to calculate...
[ "binary search", "brute force", "math", "ternary search" ]
//NEWBIE CONTESTANT // copy right © 12/2/2022 to ESLAM SAYED (ESLAM 7215) Educational DIV2--> 143 #include <bits/stdc++.h> using namespace std ; #define clo cout << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl; #define NB iostream::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr...
cpp
1303
F
F. Number of Componentstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a matrix n×mn×m, initially filled with zeroes. We define ai,jai,j as the element in the ii-th row and the jj-th column of the matrix.Two cells of the matrix are connected if they sh...
[ "dsu", "implementation" ]
#include<bits/stdc++.h> using namespace std; int n, m, q; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; vector <int> p, rk; vector <vector <int> > A; int get(int a) { if(p[a] == a) { return a; } else { return p[a] = get(p[a]); } } bool unite(int a, int b) { a = get(a); b = get(b);...
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" ]
// LUOGU_RID: 100367685 #include <bits/stdc++.h> #define endl '\n' #define fi first #define se second #define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k)) #define forn(i,n) for (int i = 0; i < n; i++) #define forr(i,a,b) for (int i = a; i <= b; i++) #define all(v) v.begin(), v.end() #define pb push_back using...
cpp
1307
C
C. Cow and Messagetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However; Bessie is sure that there is a secret message hidden inside.The text is a string ss of lowercase Latin letter...
[ "brute force", "dp", "math", "strings" ]
#include<bits/stdc++.h> using namespace std; #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define MOD 1000000007 #define MOD1 998244353 #define ln "\n" #define pb push_back #define ppb pop_back #define mp make_pair #define ff first #define ss second #define PI 3.141592653...
cpp
1305
A
A. Kuroni and the Giftstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni has nn daughters. As gifts for them, he bought nn necklaces and nn bracelets: the ii-th necklace has a brightness aiai, where all the aiai are pairwise distinct (i.e. all aiai are differen...
[ "brute force", "constructive algorithms", "greedy", "sortings" ]
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; int b[n]; for (int j = 0; j < 2; j++) { for (int i=0; i<n; i++) cin>>b[i]; sort(b, b + n); for (int k = 0; k < n; k++) cout<<b[k]<<" "; printf("\n"); } } return 0; }
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 <iostream> #include <vector> #include <queue> #include <unordered_map> #include <unordered_set> #include <map> #include <set> #include <cmath> #include <algorithm> #include <functional> #include <cassert> using namespace std; #define int long long int gauss(int x) { return x*(x+1)/2; } voi...
cpp
1295
D
D. Same GCDstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers aa and mm. Calculate the number of integers xx such that 0≤x<m0≤x<m and gcd(a,m)=gcd(a+x,m)gcd(a,m)=gcd(a+x,m).Note: gcd(a,b)gcd(a,b) is the greatest common divisor of aa and bb.I...
[ "math", "number theory" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; ll euler_phi(ll n) { ll ans = n; for (ll i = 2; i * i <= n; i++) if (n % i == 0) { ans = ans / i * (i - 1); while (n % i == 0) n /= i; } if (n > 1) ans = ans / n * (n - 1); return ans; } auto solve...
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;i--) #define f...
cpp
1323
B
B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n×mn×m formed by following rule: ci,j=ai⋅bjci,j=ai⋅bj (i.e....
[ "binary search", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define all(v) ((v).begin()), ((v).end()) #define rall(v) ((v).rbegin()), ((v).rend()) #define Py cout<<"YES"<<endl; #define Pn cout<<"NO"<<end...
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" ]
#ifdef LOCAL #define _GLIBCXX_DEBUG #define _GLIBCXX_DEBUG_PEDANTIC #endif //#define NOGNU #ifndef LOCAL #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #endif #pragma GCC target("avx2") // #pragma GCC target("sse4") // #pragma GCC target("sse2") #pragma GCC target("...
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: 102246479 #include <bits/stdc++.h> using namespace std; namespace vbzIO { char ibuf[(1 << 20) + 1], *iS, *iT; #if ONLINE_JUDGE #define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin), (iS == iT ? EOF : *iS++) : *iS++) #else #define gh() getchar() #endif #define p...
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; #define fio ios_base::sync_with_stdio(0); cin.tie(0); #define int long long #define vt vector #define pb push_back #define eb emplace_back #define INF 0x3f3f3f3f3f3f3f3f #define PI pair<int, int> #define rep(i, from, to) for (int i = from; i <= to; ++i) #pragma GCC...
cpp
1288
E
E. Messenger Simulatortime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp is a frequent user of the very popular messenger. He's chatting with his friends all the time. He has nn friends, numbered from 11 to nn.Recall that a permutation of size nn is an array o...
[ "data structures" ]
//#pragma GCC optomize ("Ofast") //#pragma GCC optomize ("unroll-loops") //#pragma GCC target ("avx,avx2,fma") #include <bits/stdc++.h> #define F first #define S second #define ll long long #define all(x) (x.begin(), x.end()); #define uint unsigned int #define pb push_back #define ios ios_base::sync_with...
cpp
1304
F2
F2. Animal Observation (hard version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the constraint on kk.Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The ...
[ "data structures", "dp", "greedy" ]
#include <bits/stdc++.h> // v.erase( unique(all(v)) , v.end() ) -----> removes duplicates and resizes the vector as so using namespace std; #define ll long long #define lld long double const lld pi = 3.14159265358979323846; #define pb push_back #define pf push_front #define all(a) a.begin(),a.end() #defin...
cpp
1299
A
A. Anu Has a Functiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAnu has created her own function ff: f(x,y)=(x|y)−yf(x,y)=(x|y)−y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)−6=15−6=9f(11,6)=(11|6)−6=15−6=9. It can be proved that for an...
[ "brute force", "greedy", "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
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 <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; #define rep(i, a, b) for(ll i = a; i < b; i++) #define rrep(i, a, b) for(ll i = a; i >= b; i--) const ll inf = 4e18; int main(void) { cin.tie(0); ios::sync_with_stdio(0); ll t; cin >> t; while(t--) { ...
cpp
1322
E
E. Median Mountain Rangetime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBerland — is a huge country with diverse geography. One of the most famous natural attractions of Berland is the "Median mountain range". This mountain range is nn mountain peaks, located on one...
[ "data structures" ]
#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; const int F = 3; vector<int> a(n+F+F, 0); for(int i = F; i < n+F; i++) cin >> a[i]; for(int i = 2; i < F; i++) a[i] = a[F]; for(int i = n+F; i < n+F+F-2; i++) a[i] = ...
cpp
1301
E
E. Nanosofttime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWarawreh created a great company called Nanosoft. The only thing that Warawreh still has to do is to place a large picture containing its logo on top of the company's building.The logo of Nanosoft can be des...
[ "binary search", "data structures", "dp", "implementation" ]
#include<iostream> #include<cstring> using namespace std; using LL = long long; const int maxn = 505; int s1[4][maxn][maxn], a[maxn][maxn]; int s2[255][maxn][maxn]; int sum(int s[maxn][maxn], int l1, int r1, int l2, int r2){ return s[l2][r2] - s[l2][r1 - 1] - s[l1 - 1][r2] + s[l1 - 1][r1 - 1]; } int m...
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; int t,n,x,s,A; int main() { cin>>t; while(t--&&cin>>n) { A=s=0; for(int i=1;i<=n;i++) { cin>>x; if(x==0) A++,s++; s+=x; } cout<<A+(s==0)<<'\n'; } return 0; }
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" ]
// Problem: Happy New Year // Contest: Luogu // URL: https://www.luogu.com.cn/problem/CF1313D // Memory Limit: 500 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) //不回家了,我们去鸟巢! #include<bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GC...
cpp
1305
B
B. Kuroni and Simple Stringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNow that Kuroni has reached 10 years old; he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifical...
[ "constructive algorithms", "greedy", "strings", "two pointers" ]
#include<bits/stdc++.h> #define maxn 1001 using namespace std; int ans[maxn],tot; char s[maxn]; int main(){ int l,r,len,i; scanf("%s",s+1); len=strlen(s+1); l=1,r=len; while(1){ while(l<=len&&s[l]==')')l++; while(1<=r&&s[r]=='(')r--; if(l>r)break; ans[++tot]=l,ans[++tot]=r; l++,r--; } if(!tot)printf("0\n"); else{ print...
cpp
13
C
C. Sequencetime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes to play very much. And most of all he likes to play the following game:He is given a sequence of N integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. ...
[ "dp", "sortings" ]
#include <bits/stdc++.h> using namespace std; #define int long long #define fir first #define sec second #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); int32_t main(){ int n,ans=0; cin >> n; priority_queue<int> pq; for(int i=0;i<n;i++){ int x; cin >> x; pq.push(x);...
cpp
1290
D
D. Coffee Varieties (hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. You can find the easy version in the Div. 2 contest. Both versions only differ in the number of times you can ask your friend to taste coffee.Th...
[ "constructive algorithms", "graphs", "interactive" ]
#include <bits/stdc++.h> //#pragma GCC target("sse,sse2,avx2") //#pragma GCC optimize("unroll-loops,O3") #define rep(i,l,r) for (int i = l; i < r; i++) #define repr(i,r,l) for (int i = r; i >= l; i--) #define X first #define Y second #define all(x) (x).begin() , (x).end() #define pb push_back #define endl '\n' #define ...
cpp
1288
A
A. Deadlinetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAdilbek was assigned to a special project. For Adilbek it means that he has nn days to run a special program and provide its results. But there is a problem: the program needs to run for dd days to calculate...
[ "binary search", "brute force", "math", "ternary search" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> v; ll n, d; int main() { long long t; cin >> t; while (t--) { cin >> n >> d; if (n >= d) { cout << "YES" << endl; } else { ...
cpp
1320
C
C. World of Darkraft: Battle for Azathothtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputRoma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.Roma has a choice to buy exactly one of nn diff...
[ "brute force", "data structures", "sortings" ]
#include <bits/stdc++.h> #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define FIO ios::sync_with_stdio(false); cin.tie(nullptr) #define TC(t) int t; cin >> t; for(int i = 1; i <= t; i++) #def...
cpp
1312
G
G. Autocompletiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a set of strings SS. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type...
[ "data structures", "dfs and similar", "dp" ]
#include <bits/stdc++.h> using namespace std; const int N=1e6+10; int st[N]; int rs[N],dp[N]; int e[N][27]; vector<pair<int,int>> stk; int Rank; void dfs(int u) { Rank+=st[u]; if(stk.size()&&st[u]) dp[u]=min(dp[u],stk.back().second+Rank); //if(u==1)cout<<stk.size()<<endl; //if(u) ...
cpp
1323
B
B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n×mn×m formed by following rule: ci,j=ai⋅bjci,j=ai⋅bj (i.e....
[ "binary search", "greedy", "implementation" ]
/* ゆめうつつ */ #include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; #define int long long typedef long long ll; typedef pair<int, int> pii; const ll mod = 1e9 + 7; const ll inf = 2e9 + 7; const ll maxn = 2e5 + 5; #define pb push_back #define fi first #define se second #define endl '\n' ...
cpp
1324
A
A. Yet Another Tetris Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given some Tetris field consisting of nn columns. The initial height of the ii-th column of the field is aiai blocks. On top of these columns you can place only figures of size 2×12...
[ "implementation", "number theory" ]
#include <bits/stdc++.h> typedef long long ll; #define endl '\n' const int N = 234234; ll n, T; ll nums[N]; void solve() { std::cin >> n; ll odd = 0, even = 0; for (int i = 0; i < n; i++) { int a; std::cin >> a; if (a & 1) { odd ++; } else even ++;...
cpp
1305
G
G. Kuroni and Antihypetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni isn't good at economics. So he decided to found a new financial pyramid called Antihype. It has the following rules: You can join the pyramid for free and get 00 coins. If you are already...
[ "bitmasks", "brute force", "dp", "dsu", "graphs" ]
// LUOGU_RID: 97972407 #include<stdio.h> #include<bits/stdc++.h> #define fir first #define sec second #define all(x) begin(x),end(x) using namespace std; typedef long long ll; typedef unsigned uint; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef __int128 int128; typedef ...
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" ]
#include<bits/stdc++.h> #include<unordered_set> using namespace std; typedef long long ll; vector<ll> bulbs; unordered_map<string,int> mp; ll min(ll a,ll b) { if(a<b) return a; return b; } // odd--->0 int util(ll odd,ll even,ll i,ll prev) { string temp=to_string(odd)+"_"+to_string(...
cpp
1285
E
E. Delete a Segmenttime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn segments on a OxOx axis [l1,r1][l1,r1], [l2,r2][l2,r2], ..., [ln,rn][ln,rn]. Segment [l,r][l,r] covers all points from ll to rr inclusive, so all xx such that l≤x≤rl≤x≤r.Segments can be ...
[ "brute force", "constructive algorithms", "data structures", "dp", "graphs", "sortings", "trees", "two pointers" ]
// LUOGU_RID: 92581424 #include <bits/stdc++.h> using namespace std; const int N = 4e5 + 6; int T, n, m, ans, cnt[N], mx; pair<int, int> q[N]; set<int> st; inline void solve() { scanf("%d", &n); ans = m = 0; st.clear(); mx = -1; for (int i = 1; i <= n; i++) { int l, r; scanf("%d%d", &l, &...
cpp
1296
E2
E2. String Coloring (hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is a hard version of the problem. The actual problems are different; but the easy version is almost a subtask of the hard version. Note that the constraints and the output format a...
[ "data structures", "dp" ]
#include <bits/stdc++.h> using namespace std; const int MAXN=200010; int n,ans,dp[MAXN],mx[27]; char c[MAXN]; int main () { scanf("%d%s",&n,c+1); for (int i=n;i>=1;i--) { dp[i]=mx[c[i]-'a']+1; for (int j=c[i]-'a'+1;j<=25;j++) {mx[j]=max(mx[j],dp[i]);} ans=max(ans,dp[i]); } printf("%d\n",ans); for (int i=1;i<n;i++) {pr...
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> using namespace std; #define int long long #define mod 998244353 int check(int a[],int p,int n) { int ans=0; for(int i=0;i<n;i++) { if(a[i]<p){ans+=(p-a[i]);continue;} int x=a[i]; ans+=min(x%p,p-x%p); } return ans...
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; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, p; cin >> n >> m >> p; vector<int> a(n), b(m); for (int i = 0; i < n; ++i) { cin >> a[i]; } fo...
cpp
1311
E
E. Construct the Binary Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and dd. You need to construct a rooted binary tree consisting of nn vertices with a root at the vertex 11 and the sum of depths of all vertices equals to dd.A t...
[ "brute force", "constructive algorithms", "trees" ]
#include <bits/stdc++.h> #include <fstream> #include <ext/rope> //header with rope using namespace __gnu_cxx; #define pb push_back #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_poli...
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; #define pb emplace_back 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...
cpp
1323
B
B. Count Subrectanglestime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n×mn×m formed by following rule: ci,j=ai⋅bjci,j=ai⋅bj (i.e....
[ "binary search", "greedy", "implementation" ]
#include <bits/stdc++.h> #pragma GCC optimize("O3,Ofast,fast-math") using namespace std; #define int long long vector <int> a, b; int ac(int x) { int ans = 0; int now = 0; for (int i = 0; i < a.size(); i++) { if (a[i]) now++; else now = 0; if (now >= x) ans++; } ...
cpp
1304
F2
F2. Animal Observation (hard version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the constraint on kk.Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The ...
[ "data structures", "dp", "greedy" ]
// LUOGU_RID: 101625819 #pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define int long long int read() { int s=0; char c=getchar(); while(c<'0' || c>'9') c=getchar(); while(c>='0' && c<='9') s=s*10+c-'0',c=getchar(); return s; } int n,m,k; int a[55][20010],b[55][20010]; int ...
cpp
1141
B
B. Maximal Continuous Resttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputEach day in Berland consists of nn hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence a1,a2,…,ana1,a2,…,an (each aiai is either 00 or 11)...
[ "implementation" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vb = vector<bool>; using vll = vector<ll>; using vs = vector<string>; #define ALL(v) v.begin(), v.end() #define SORT(v) sort(ALL(v)) #define SZ(v) int(v....
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
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; 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 i = 1; i <=...
cpp
1299
A
A. Anu Has a Functiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAnu has created her own function ff: f(x,y)=(x|y)−yf(x,y)=(x|y)−y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)−6=15−6=9f(11,6)=(11|6)−6=15−6=9. It can be proved that for an...
[ "brute force", "greedy", "math" ]
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define sz(x) (int)(x).size() #define pb push_back #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(...
cpp
1299
C
C. Water Balancetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn water tanks in a row, ii-th of them contains aiai liters of water. The tanks are numbered from 11 to nn from left to right.You can perform the following operation: choose some subsegment [l...
[ "data structures", "geometry", "greedy" ]
#include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define mk make_pair #define lowbit(x) (x&(-x)) #define pb emplace_back #define pr pair<int,int> #define let const auto const int N=1e6+5; int read(){ int x=0,f=1; char c=getchar(); while(('0'>c||c>'9')&&c!='-...
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> #define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i) #define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i) #define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to) typedef long long ll; using std::cin; using std::cout; usin...
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> using namespace std; #define int long long typedef long long ll; const double PI = acos(-1.0); const int maxn = 2e5 + 7; int n,m, a[maxn], b[maxn], pos[maxn], val[maxn]; struct segment_tree { struct tree { int l, r, lazy, mi; }tr[maxn * 4]; inl...
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 i64 = long long; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } std::vector<int> s(n + 1); for (int i = 0; i < n; i++) { s[i + 1] = s...
cpp
1320
B
B. Navigation Systemtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe map of Bertown can be represented as a set of nn intersections, numbered from 11 to nn and connected by mm one-way roads. It is possible to move along the roads from any intersection to any othe...
[ "dfs and similar", "graphs", "shortest paths" ]
#include <bits/stdc++.h> using namespace std; vector<int> distances (int s, int n ,vector<vector<int>> & adj) { vector<bool> visited(n, false); vector<int> dist(n, -1); queue<int> Q; dist[s] = 0; visited[s] = true; Q.push(s); while(!Q.empty()) { int v = Q.front(); Q.pop(...
cpp
1294
F
F. Three Paths on a Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an unweighted tree with nn vertices. Recall that a tree is a connected undirected graph without cycles.Your task is to choose three distinct vertices a,b,ca,b,c on this tree such t...
[ "dfs and similar", "dp", "greedy", "trees" ]
#include<iostream> #include <bits/stdc++.h> using namespace std; template<class container> void print(container v) { for (auto& it : v) cout << it << ' ' ;cout <<endl;} using ll = long long; using ull = unsigned long long; using ld = long double; #define nd "\n" #define all(x) (x).begin(), (x).end() #define po...
cpp
1311
B
B. WeirdSorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn.You are also given a set of distinct positions p1,p2,…,pmp1,p2,…,pm, where 1≤pi<n1≤pi<n. The position pipi means that you can swap elements a[pi]a[pi] and a[pi+1]a[pi+...
[ "dfs and similar", "sortings" ]
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' #define sp ' ' #define pb push_back #define assert(x,a) if(x){cout << a << endl;return;} #define sortv(x)sort(x.begin(),x.end()) #define revev(x)reverse(x.begin(),x.end()) const ll mod = 1000000007; vector<vector<int>>graph; ...
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> using namespace std; const int N = 505; int a[N]; int f[N][N]; int dp[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; f[i][i] = a[i]; } for (int len = 2; len...
cpp
1311
B
B. WeirdSorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn.You are also given a set of distinct positions p1,p2,…,pmp1,p2,…,pm, where 1≤pi<n1≤pi<n. The position pipi means that you can swap elements a[pi]a[pi] and a[pi+1]a[pi+...
[ "dfs and similar", "sortings" ]
#include<bits/stdc++.h> int i,j,k,n,m,a[100];int main(){ for(scanf("%*d");~scanf("%d%d",&n,&m);i=j=0){std::set<int>p; for(;i<n;i++)scanf("%d",a+i);while(m--)scanf("%d",&i),p.insert(i); for(k=1;k<=n;k++)if(!p.count(k))std::sort(a+j,a+k),j=k; puts(std::is_sorted(a,a+n)?"YES":"NO"); }}
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 <bits/stdc++.h> using namespace std; #define ll long long #define fr(i,a,b,k) for(int i=a;i<b;i+=k) #define frrev(i,a,b,k) for(int i=a;i>b;i-=k) #define NO cout<<"NO\n" #define YES cout<<"YES\n" #define V vector<ll int> #define VP vector<pair<ll int,ll int>> #define MP map<ll int,ll int> #define ...
cpp
1141
C
C. Polycarp Restores Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each number from 11 to nn exactly once. For example, the following arrays are permutations: [3,1,2][3,1,2...
[ "math" ]
#include<iostream> #include<iterator> #include<ranges> #include<vector> #include<algorithm> void solve_test_case(); int main() { unsigned t{ 1 }; do solve_test_case(); while (--t); } void solve_test_case() { unsigned length_of_permutation; std::cin >> length_of_permutation; std::vecto...
cpp
1316
A
A. Grade Allocationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputnn students are taking an exam. The highest possible score at this exam is mm. Let aiai be the score of the ii-th student. You have access to the school database which stores the results of all studen...
[ "implementation" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endo '\n' #define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); /*int isprime(int N){ if(N<2 || (!(N&1) && N!=2)) return 0; for(int i=3; i*i<=N; i+=2){ if(!(N%i)) return 0; } r...
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" ]
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; using pii = pair<int, int>; void upd(pii &x, const pii &y) { auto &[a, b] = x; const auto &[c, d] = y; x = {max(a, c), min(b, d)}; } int n, m, val[N], repr[N], dis[N]; vector<int> G[N]; int fa[N][20], ord[N], dep[N]; int find(i...
cpp
1287
B
B. Hypersettime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shape...
[ "brute force", "data structures", "implementation" ]
#include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; using namespace std; const ll mod = 998244353; const int mm = 3e5 + 10; map<string,int>mp; string a[1505]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n,k; cin>>n>>k; for(int i=1;i...
cpp
1323
A
A. Even Subset Sum Problemtime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by 22) or determine that there is no such subse...
[ "brute force", "dp", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; int main() { int t; int n,a[105],tag; cin>>t; while(t--) { tag=0; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } if(n==1&&a[1]%2==1) cout<<-1<<endl; else { if(a[1]%2==0) { cout<<1<<endl<<1<<...
cpp
1296
C
C. Yet Another Walking Robottime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot on a coordinate plane. Initially; the robot is located at the point (0,0)(0,0). Its path is described as a string ss of length nn consisting of characters 'L', 'R', 'U', 'D'....
[ "data structures", "implementation" ]
#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
1304
B
B. Longest Palindrometime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputReturning back to problem solving; Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings "pop", "noon", "x", and "...
[ "brute force", "constructive algorithms", "greedy", "implementation", "strings" ]
#include <bits/stdc++.h> using namespace std; #define ll long long #define fr(i,a,b,k) for(int i=a;i<b;i+=k) #define frrev(i,a,b,k) for(int i=a;i>=b;i-=k) #define NO cout<<"NO\n" #define YES cout<<"YES\n" #define V vector<ll int> #define VP vector<pair<ll int,ll int>> #define MP map<ll int,ll int> #define...
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> using namespace std; int main() { int t,m,n; int tag; cin>>t; while(t--) { tag=0; cin>>n>>m; if(n%m==0) cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
cpp