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
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" ]
#include<bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> edge(n, 0); vector<pair<int, int> > s; int ans[n]; for (int i = 1, u, v; i < n; i++) { cin >> v >> u; v--; u--; s.emplace_back(v, u); edge[u]++; edge[v]++; } int j = 0; int k = n - 2; for ...
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" ]
#include <bits/stdc++.h> using namespace std; typedef int64_t ll; struct Vec { ll x, y; Vec(ll x = 0, ll y = 0): x(x), y(y) {} Vec operator - (const Vec& o) const { return Vec(x - o.x, y - o.y); } ll operator ^ (const Vec& o) const { return x * o.y - y * o.x; } bool up() { return y > 0 or (y == 0 a...
cpp
1310
C
C. Au Pont Rougetime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVK just opened its second HQ in St. Petersburg! Side of its office building has a huge string ss written on its side. This part of the office is supposed to be split into mm meeting rooms in such way th...
[ "binary search", "dp", "strings" ]
#include "bits/stdc++.h" using namespace std; #define all(x) begin(x),end(x) template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>...
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" ]
//shenoda adel #include <iostream> #include <iomanip> #include <cmath> #include <string> #include<stack> #include<queue> #include <algorithm> #include<set> #include<map> #include<iterator> #include <vector> #include <sstream> #define ll long long #define endl "\n" #define fx(x) fixed<<setprecision(x) #d...
cpp
13
A
A. Numberstime limit per test1 secondmemory limit per test64 megabytesinputstdinoutputstdoutLittle Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18.Now he wonders what is an average value of ...
[ "implementation", "math" ]
#include <iostream> #include <vector> using namespace std; int gcd(int a, int b) { if(b==0){ return a; } else{ return gcd(b, a % b); } } int main(){ int a; int tmp; int sum; int n = 0; cin >> a; int d = a - 2; vector<int> base; vector<int> sums; ...
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> #define ll long long #define fr(i,a,b) for(int i = a ; i < b ; i++) #define read(x) int x ; cin>>x #define yy cout<<"YES"<<endl; #define nn cout<<"NO"<<endl; using namespace std; void init_code(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.t...
cpp
1324
F
F. Maximum White Subtreetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of nn vertices. A tree is a connected undirected graph with n−1n−1 edges. Each vertex vv of this tree has a color assigned to it (av=1av=1 if the vertex vv is whi...
[ "dfs and similar", "dp", "graphs", "trees" ]
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0)->sync_with_stdio(0); int n, u, v; cin >> n; vector<vector<int>> tree(n + 1); int color[n + 1]; int ans[n + 1]; int dp[n + 1]; for (int i = 1; i <= n; ++i) { cin >> color[i]; if (color[i] == 0) color[i]...
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" ]
/** * @file 1322E.cpp * @author Macesuted (i@macesuted.moe) * @date 2023-01-06 * * @copyright Copyright (c) 2023 * */ #include <bits/stdc++.h> using namespace std; #ifndef LOCAL #define endl '\n' #endif bool mem1; #define maxn 500005 typedef pair<int, int> pii; int n, a[maxn], ans2[ma...
cpp
1285
D
D. Dr. Evil Underscorestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; as a friendship gift, Bakry gave Badawy nn integers a1,a2,…,ana1,a2,…,an and challenged him to choose an integer XX such that the value max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X) is minimum possible, whe...
[ "bitmasks", "brute force", "dfs and similar", "divide and conquer", "dp", "greedy", "strings", "trees" ]
using namespace std; #include <bits/stdc++.h> #define int long long #define repp(n) for(int i=0;i<n;i++) #define mod 1000000007 void solve() { int n;cin>>n; int a[n+1],ans=0;repp(n)cin>>a[i+1]; vector<int> now(n);repp(n)now[i]=i+1; vector<vector<int>> vv,temp1,temp2; vv.push_back(...
cpp
1296
E1
E1. String Coloring (easy version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an easy 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 ...
[ "constructive algorithms", "dp", "graphs", "greedy", "sortings" ]
#include<bits/stdc++.h> using namespace std; int main() { int n; char f1, f2; string s; cin>>n; cin>>s; f1 = 'a'; f2 = 'a'; for(int i=0;i<=n-1;++i) { if(s[i]>=f1) { f1 = s[i]; s[i] = '0'; } else if(s[i]>=f2) { f2 = s[i]; s[i] = '1'; } else { cout<<"NO"; ...
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> #include<iostream> #include<set> #include<map> #include<iterator> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> ///Defines..... #define ll long long #define lli long long int #define pb push_back #define mp ...
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 <bits/stdc++.h> #define int long long using namespace std; const int INF=505; int n,m,q; char a[INF][INF]; mt19937 rnd(chrono::system_clock().now().time_since_epoch().count()); int gen(int l,int r) {return rnd()%(r-l+1);} int vis[INF],p[INF],b[INF][INF],ans[INF][INF]; int f[INF][INF][13][13]; int Ge...
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<iostream> #include<vector> #include<set> #include<map> #include<algorithm> #include<queue> #include<string> using namespace std; #define int long long int #define MAXN 105 #define nd second #define st first #define pb push_back #define fio ios_base::sync_with_stdio(false);cin.tie(NULL) typedef vector<int> vi...
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> #define R(i,a,b) for(int i=(a),i##E=(b);i<=i##E;i++) #define L(i,a,b) for(int i=(b),i##E=(a);i>=i##E;i--) using namespace std; int n,m; vector<int>e[555555]; inline void ns() { cout<<"-1"<<endl; exit(0); } struct dsu { int fa[555555]; bool v[555555]; inline void clear() { ...
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> using namespace std; const int maxn=600005; const int mod=(1e9+7); int n,m,i,j,t,k,s,ls[maxn],tls,a[maxn],b[maxn]; int pla[maxn],chg[maxn],two[maxn]; int sum[maxn<<2],cnt[maxn<<2],lef[maxn<<2],rig[maxn<<2]; inline int Pow(int x,int y) { int ret=1; while (y) { if (y&1) ret=1ll*...
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" ]
// oooo /* har chi delet mikhad bebar ~ gitar o ba khodet nabar! ~ ;Amoo_Hasan; */ /* link: */ #include<bits/stdc++.h> #pragma GCC optimize("O3,no-stack-protector,unroll-loops,Ofast") #pragma GCC target("avx2,fma") using namespace std; typedef long long ll; typedef long double ld; #define S...
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<cstdio> #include<vector> #include<algorithm> #include<cstdlib> #include<utility> #include<queue> #include<stack> #include<map> struct edge{int x,y,d;edge(int x=0,int y=0,int d=0){this->x=x,this->y=y,this->d=d;}}; struct tree { std::vector<int> a[200005]; int n,rt,dep[200005],fa[200005],sz[200005]...
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" ]
#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
1294
B
B. Collecting Packagestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a robot in a warehouse and nn packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0,0)(0,0). The ii-th package is ...
[ "implementation", "sortings" ]
// Problem: B. Collecting Packages // Contest: Codeforces - Codeforces Round #615 (Div. 3) // URL: https://codeforces.com/problemset/problem/1294/B // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) #include <iostream> #include <bits/stdc++.h> #include <numeric>...
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" ]
#include<bits/stdc++.h> using namespace std; int const M=200200;struct node{int to,next;}node[M<<1]; int i,n,m,u,v,num,B,dep[M],head[M];bool flag[M];vector<int> f,Ans; int read(){ int x=0;char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar(); return x; }...
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> typedef long long ll; typedef unsigned long long ull; #define rep(i, a, b) for(int i = (a); i <= (b); i ++) #define per(i, a, b) for(int i = (a); i >= (b); i --) #define Ede(i, u) for(int i = head[u]; i; i = e[i].nxt) using namespace std; #define eb emplace_back typedef pair<int, int> ...
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; typedef long long ll; inline int read(){ int f=1,r=0;char c=getchar(); while(!isdigit(c))f^=c=='-',c=getchar(); while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar(); return f?r:-r; } #define pii pair<int,int> #define fi first #define se second const int N=2e5+7; struct Edge{...
cpp
1284
A
A. New Year and Namingtime limit per test1 secondmemory limit per test1024 megabytesinputstandard inputoutputstandard outputHappy new year! The year 2020 is also known as Year Gyeongja (경자년; gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Ko...
[ "implementation", "strings" ]
#include<bits/stdc++.h> using namespace std; const int maxn=25; int n,m,q; string s[maxn],t[maxn]; int main() { cin>>n>>m; for(int i=0; i<n; i++) cin>>s[i]; for(int i=0; i<m; i++) cin>>t[i]; int q; cin>>q; while(q--) { int x; cin>>x; ...
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("O3,unroll-loops") #include<bits/stdc++.h> using namespace std; typedef long long ll; #define Thank_Queue_Spiderman ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define BITS sizeof(int) * 8 #define TestCase ll T; cin >> T int arr[1000000]; bool flag = false; int ma...
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> #define R(i,a,b) for(int i=(a),i##E=(b);i<=i##E;i++) #define L(i,a,b) for(int i=(b),i##E=(a);i>=i##E;i--) using namespace std; int n,m; vector<int>e[555555]; inline void ns() { cout<<"-1"<<endl; exit(0); } struct dsu { int fa[555555]; bool v[555555]; inline void clear() { ...
cpp
1307
B
B. Cow and Friendtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play! More specifically; he wants to get from (0,0)(0,0) to (x,0)(x,0) by...
[ "geometry", "greedy", "math" ]
#include<bits/stdc++.h> using namespace std; typedef long long ll; vector<int> func(vector<vector<int>> &roads, int x) { int n = roads.size(); queue<int> q; vector<int> dist(n, 1e9); q.push(x); dist[x] = 0; while (q.size()) { int a = q.front(); q.pop(); int d...
cpp
1316
B
B. String Modificationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has a string ss of length nn. He decides to make the following modification to the string: Pick an integer kk, (1≤k≤n1≤k≤n). For ii from 11 to n−k+1n−k+1, reverse the substring s[i:i+k−1]s...
[ "brute force", "constructive algorithms", "implementation", "sortings", "strings" ]
/* 奇偶判断由来: 首先,反转次数为 cnt = n-k+1 若奇偶性相同,则 cnt 为奇数,即 s 的前 k 个字符最终是反的 若奇偶性不同,则最终还是正序的 因为一次反转等价于把前 k 个字符 reverse */ #include <iostream> using namespace std; void solve() { int n; cin >> n; string s; cin >> s; int ans = 1; string res = s; for(int i=2; i<=n; i++) { string...
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" ]
#pragma GCC optimize("O3,unroll-loops") #include<bits/stdc++.h> using namespace std; typedef long long ll; #define Thank_Queue_Spiderman ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define BITS sizeof(int) * 8 #define TestCase ll T; cin >> T int arr[1000000]; bool flag = false; int ma...
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 vi std::vector<int> #define N 805 int n; vi V[N<<2]; int a[N<<2]; inline int qry1(int i,int l,int r,int x,int y){ printf("? %d ",(r-l+1)<<1); for(int j=l;j<=r;j++) if(j!=i) printf("%d %d ",V[j][0],V[j][1]); printf("%d %d\n",x,y),fflush(stdout); int res; scanf("%d...
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" ]
/* author :- sairaj */ #pragma GCC optimize("O1") #include<bits/stdc++.h> using namespace std; #define all(a) a.begin(),a.end() #define sortv(a) sort(all(a)) #define sortvg(a) sort(all(a),greater<>()); #define int long long #define endl "\n" #define SPEED {ios_base::sync_with_stdio(false);cin.tie(NUL...
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" ]
#include <iostream> #include <string> using namespace std; int main() { int a; string b; cin >> a; cin >> b; cout << a + 1; return 0; }
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> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int len = (int) s.size(); int pref = 0; int suf = 0; for (int i = 0; i < len; i++) { suf += (s[i] == ')'); } int cut = -1; for (int i = 0; i <= len; i++) ...
cpp
1311
D
D. Three Integerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three integers a≤b≤ca≤b≤c.In one move, you can add +1+1 or −1−1 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero)...
[ "brute force", "math" ]
#include <bits/stdc++.h> #define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout) #define all(a) a.begin() , a.end() #define F first #define S second using namespace std; using ll = long long; co...
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> #define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0) #define all(x) (x).begin(), (x).end() #define int long long #define mp make_pair #define endl '\n' using namespace std; typedef unsigned long long ULL; typedef long long ll; typedef pair<int, int> pii; typedef pair...
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" ]
#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; int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n; cin>>n; string a; cin>>a; int l=0,r=0; for(auto i:a){ i...
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; typedef long long ll; ll MOD = 1e9 + 7; ll ans = 0; ll dfs(ll x, vector<vector<ll>> &roads, vector<ll> &visited) { if (visited[x]) return 0; visited[x] = true; ll depth = 0; for (ll child : roads[x]) depth = max(depth, dfs(child...
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; typedef long long LL; void solve() { int n; cin >> n; vector<int> a(n + 1); set<int> st; for(int i = 1; i <= n; i++) cin >> a[i], st.insert(i), st.insert(n + i); vector<int> ans(2 * n + 1); for(int i = 1; i <= n; i++) st.erase(...
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" ]
// BEGIN BOILERPLATE CODE #include <bits/stdc++.h> using namespace std; #ifdef KAMIRULEZ const bool kami_loc = true; const long long kami_seed = 69420; #else const bool kami_loc = false; const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count(); #endif const string kami_fi =...
cpp
1310
C
C. Au Pont Rougetime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVK just opened its second HQ in St. Petersburg! Side of its office building has a huge string ss written on its side. This part of the office is supposed to be split into mm meeting rooms in such way th...
[ "binary search", "dp", "strings" ]
//______________"In The Name Of GOD"______________ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair <ll, ll> pll; typedef pair <int, int> pii; const int lg = 20; const int mod = 1e9 + 7; const ll inf = 2e18 + 100; const int maxn = 1e3...
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 <bits/stdc++.h> const long long mod = 1000001; #define ll long long int #define in(k) \ int k; \ cin >> k #define i(a, b) \ int a, b; \ cin >> a >> b #define inp(arr, n) \ int arr[n]; \ for (int i = 0; i < n; i++) \ cin >> arr[i] #define...
cpp
1316
D
D. Nash Matrixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNash designed an interesting yet simple board game where a player is simply required to follow instructions written on the cell where the player currently stands. This board game is played on the n×nn×n b...
[ "constructive algorithms", "dfs and similar", "graphs", "implementation" ]
#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
1285
C
C. Fadi and LCMtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Osama gave Fadi an integer XX, and Fadi was wondering about the minimum possible value of max(a,b)max(a,b) such that LCM(a,b)LCM(a,b) equals XX. Both aa and bb should be positive integers.LCM(a,b)L...
[ "brute force", "math", "number theory" ]
// Problem: C. Fadi and LCM // Contest: Codeforces - Codeforces Round #613 (Div. 2) // URL: https://codeforces.com/problemset/problem/1285/C // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) //clear adj and visited vector declared globally after each test case /...
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 int s; cin>>s; while(s--) { long long int n; cin>>n; long long int count=0; long long int p = n; while(p>=10) { count+=p/10; p = p/10 + p%10; ...
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<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
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 LL = long long; [[maybe_unused]] constexpr int mod = 1e9 + 7, N = 3e5 + 10; LL a[N], n, sum[N]; void solve() { std::cin >> n; for (int i = 1; i <= n; ++i) { std::cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } std::map<LL, int> c1; for (int i = 1; i <= n; ++i) ...
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> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace __gnu_pbds; using namespace std; /***************************************************************************************************************/ template ...
cpp
1284
F
F. New Year and Social Networktime limit per test4 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputDonghyun's new social network service (SNS) contains nn users numbered 1,2,…,n1,2,…,n. Internally, their network is a tree graph, so there are n−1n−1 direct connections between each user....
[ "data structures", "graph matchings", "graphs", "math", "trees" ]
// LUOGU_RID: 102062513 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; typedef pair<int,int> P; const int N=3e5+5; struct edge{ int v,nx; }e[N<<1],g[N<<1]; int n,ne,cnt,fe[N],fg[N],p[N],sz[N],son[N],deep[N],fa[N],top[N],id[N],...
cpp
1288
C
C. Two Arraystime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm. Calculate the number of pairs of arrays (a,b)(a,b) such that: the length of both arrays is equal to mm; each element of each array is an integer between 11 and nn (in...
[ "combinatorics", "dp" ]
#include<bits/stdc++.h> #define eps 1e-9 #define emailam ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define endl '\n' #define ll long long #define ull unsigned long long #define pb push_back #define all(a) a.begin(),a.end() #define pf push_front #define fi first #define se...
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" ]
/** * author: magnus_hegdahl * created: 01.02.2023 00:25 * problem: E2. String Coloring (hard version) * url: https://codeforces.com/contest/1296/problem/E2 */ #include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr)->sync_with_stdio(false); int n; string s; cin >> n >> s; vec...
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; #define int long long int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); int t; cin>>t; while(t--) { int n; cin>>n; int a[n],b[n]; for(int i=0;i<n;i++) { cin>>a[i]; } for(int i=0;i<n;i++) { ...
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 endl "\n" #define int long long typedef long long LL; typedef pair<int,int> PII; void solve(){ int n,x,y; cin>>n>>x>>y; int mn=(x+y<n+1)?1:min(n,x+y-n+1); int mx=min(n,x+y-1); cout<<mn<<" "<<mx<<endl; } signed main() { ios::sync_with_stdi...
cpp
1322
A
A. Unusual Competitionstime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputA bracketed sequence is called correct (regular) if by inserting "+" and "1" you can get a well-formed mathematical expression from it. For example; sequences "(())()", "()" and "(()(()))" are cor...
[ "greedy" ]
#include <bits/stdc++.h> using namespace std; #define int long long int int32_t main() { int n; cin >> n; string s; cin >> s; int cur = 0, ans=0, sum=0, k = 0; for(int i=0; i<n; i++){ if(s[i]==')'){ cur--; } else{ cur++; } ...
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> #define int long long using namespace std; inline int read(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch))f^=ch=='-',ch=getchar(); while(isdigit(ch))x=x*10+(ch^48),ch=getchar(); return f?x:-x; } const int N=150005; int K[N],B[N],tot,n,a[N],ans; vector<int> T[N]; int calc(i...
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 Dattebayo() ios::sync_with_stdio(0),cin.tie(nullptr),cout.tie(nullptr) #define all(v) ((v).begin()), ((v).end()) #define YES cout<<"YES\n" #define NO cout<<"NO\n" #define ll long long #define F first #define S second #define endl "\n" using namespace std; ///Where there's a wi...
cpp
1322
B
B. Presenttime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputCatherine received an array of integers as a gift for March 8. Eventually she grew bored with it; and she started calculated various useless characteristics for it. She succeeded to do it for each one she cam...
[ "binary search", "bitmasks", "constructive algorithms", "data structures", "math", "sortings" ]
// LUOGU_RID: 101016299 #include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int N = 2e5 + 10; void solve() { int n; cin >> n; vector<int> a(n + 1); for(int i = 1; i <= n; i++) cin >> a[i]; int ans = 0; auto cal = [&](vector<int> &v, int b) { int cnt = 0; for(int ...
cpp
1284
D
D. New Year and Conferencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputFilled with optimism; Hyunuk will host a conference about how great this new year will be!The conference will have nn lectures. Hyunuk has two candidate venues aa and bb. For each of the nn l...
[ "binary search", "data structures", "hashing", "sortings" ]
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define INF 0x3f3f3f3f #define f first #define s second #define rep(i,a,b) for (int i=a; i<b; ++i) #define setpr(x) cout<<setprecision(x)<<fixed #define sz(v) int(v.size()) #define all(v) v.begin(),v.end() typedef long long ll; typedef ...
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" ]
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #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; template<class key, class cmp = std::less<key>> using op_set = tree<k...
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" ]
/* わんわん……わんだほーいっ☆ Wonderhoy! */ #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef double DB; char buf[1<<21],*p1=buf,*p2=buf; #define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++) LL read() { LL x=0,f=1; char c=getchar(); while(c<'0' || c>'9') f=...
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" ]
#include <iostream> #include <set> #include <algorithm> using namespace std; typedef pair<int, int> pii; const int N = 2 * 100 * 1000 +10; pii seg[N]; int dp_last, dp, big, cmp; void Solve() { int n; cin>>n; for(int i=1; i<=n; i++) cin>>seg[i].first>>seg[i].second; sort(seg+1, seg...
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; using ll = long long; using ull = unsigned long long; using uint = unsigned int; using vi = vector<int>; using pi = pair<int, int>; #define FAST_IO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) bool is_prime(ll x) { if (x <= 1) return 0; ...
cpp
1325
A
A. EhAb AnD gCdtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a positive integer xx. Find any such 22 positive integers aa and bb such that GCD(a,b)+LCM(a,b)=xGCD(a,b)+LCM(a,b)=x.As a reminder, GCD(a,b)GCD(a,b) is the greatest integer that divides both...
[ "constructive algorithms", "greedy", "number theory" ]
#include<bits/stdc++.h> #include<vector> using namespace std; int main(){ int t; cin>>t; while(t--){ long n; cin>>n; cout<<n-1<<" "<<1<<endl; } return 0; }
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; #define jeno_joyer_khuda_thake int main (void) #define kaj_shesh return 0 #define sf scanf #define pf printf #define ssf ...
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" ]
/* _/ _/ _/_/_/ _/ _/ _/ _/_/_/_/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/_/_/ _/ _/ _/ _/ _/ _/ ...
cpp
1305
D
D. Kuroni and the Celebrationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.After getting AC after 13 Time Limit Exceeded verdicts on a geometry problem; Kuroni went to an Italian restaurant to celebrate this holy achievement. Unfortun...
[ "constructive algorithms", "dfs and similar", "interactive", "trees" ]
#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>; using vsi = vector<si>; using qii = queue<ii>; ll n; vsi g; si valid; void dfs(int u, int p) { valid.erase(u); ...
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 const int INF = 1e15; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int boyut; cin >> boyut; vector<int> dizi(boyut + 1); set <int> dizi2; for(int i = 1; i <= boyut; i++) { cin >> dizi[i]; dizi2.inse...
cpp
1290
C
C. Prefix Enlightenmenttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn lamps on a line, numbered from 11 to nn. Each one has an initial state off (00) or on (11).You're given kk subsets A1,…,AkA1,…,Ak of {1,2,…,n}{1,2,…,n}, such that the intersection of...
[ "dfs and similar", "dsu", "graphs" ]
#include <bits/stdc++.h> using namespace std; #define int long long #define N 300001 int parent[N], Size[N], val[N]; int c[2][N]; vector<int> Comp[N]; int st[N]; int tot = 0; void init_dsu(int n){ for (int i = 1; i <= n; i++){ parent[i] = i; Comp[i].push_back(i); Size[i] =...
cpp
1301
A
A. Three Stringstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three strings aa, bb and cc of the same length nn. The strings consist of lowercase English letters only. The ii-th letter of aa is aiai, the ii-th letter of bb is bibi, the ii-th letter of...
[ "implementation", "strings" ]
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> using namespace std; typedef long long ll; typedef long double ldb; t...
cpp
1313
E
E. Concatenation with intersectiontime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputVasya had three strings aa, bb and ss, which consist of lowercase English letters. The lengths of strings aa and bb are equal to nn, the length of the string ss is equal to mm. Vasya d...
[ "data structures", "hashing", "strings", "two pointers" ]
//Then #include <bits/stdc++.h> #define pb push_back #define fi first #define se second #define faster ios_base::sync_with_stdio(0); cin.tie(0); #define int long long using namespace std; using ll = long long; using ld = long double; using pii = pair <int, int>; mt19937_64 Rand(chrono::steady_clock::now().ti...
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; int main(){ int n; cin >> n; pair<int, int> sc, dp; vector<int> areas(4,0); for(int i=0; i<n; i++){ cin >> sc.first >> sc.second >> dp.first >> dp.second; areas[0] = (dp.first * sc.second); areas[1] = (sc.first * (sc.second - dp.second - 1)); ...
cpp
1296
D
D. Fight with Monsterstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn monsters standing in a row numbered from 11 to nn. The ii-th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal...
[ "greedy", "sortings" ]
#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
1322
A
A. Unusual Competitionstime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputA bracketed sequence is called correct (regular) if by inserting "+" and "1" you can get a well-formed mathematical expression from it. For example; sequences "(())()", "()" and "(()(()))" are cor...
[ "greedy" ]
#include<bits/stdc++.h> #define ll long long #define pb push_back #define pob pop_back #define pf push_front #define pof pop_front #define fi first #define se second #define ull unsigned long long #define ld long double #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define in freo...
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<iostream> #include<cstring> #include<random> #include<cmath> using namespace std; using LL = long long; using ULL = unsigned long long; const int maxn = 2e5 + 5; static const ULL mod = (1ull << 61) - 1; ULL power[maxn]; mt19937_64 rnd(random_device{}()); uniform_int_distribution<ULL> dist(100, mod -...
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 <stdio.h> #include <random> #include <time.h> #include <set> #define int long long std::mt19937 random_pool(time(NULL)); std::set<int> PrimeFactor; int n, a[200001], m = 1e18; inline void Decompose(int x) { if(x <= 0) return; for(register int i = 2; i * i <= x; ++ i) if(!(x % i))...
cpp
1321
C
C. Remove Adjacenttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss consisting of lowercase Latin letters. Let the length of ss be |s||s|. You may perform several operations on this string.In one operation, you can choose some index ii and re...
[ "brute force", "constructive algorithms", "greedy", "strings" ]
#include<bits/stdc++.h> #define ll long long int #define debug(x) cout<<"x = "<<x<<endl; using namespace std; int fx[]= {1,-1,0,0,1,-1,1,-1}; int fy[]= {0,0,1,-1,1,-1,-1,1}; const ll MOD = 1e9+7; void solve(int tt) { int n; cin>>n; string s; cin>>s; vector<int>a[26]; for(int i=0; i<n; i++)a...
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> using namespace std; #define int long long #define fr(i, a, b) for (int i = a; i < b; i++) #define frr(i, a, b) for (int i = a; i >= b; i--) #define pb push_back #define ff first #define ss second #define pr pair<int,int> // #define M 1000000007 #define l...
cpp
1316
E
E. Team Buildingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice; the president of club FCB, wants to build a team for the new volleyball tournament. The team should consist of pp players playing in pp different positions. She also recognizes the importance of ...
[ "bitmasks", "dp", "greedy", "sortings" ]
#pragma GCC optimize ("O3") #include <bits/stdc++.h> #define ll long long #define endl '\n' #define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); using namespace std; const int OO = 0x3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 7; int n, p, ...
cpp
1324
E
E. Sleeping Scheduletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVova had a pretty weird sleeping schedule. There are hh hours in a day. Vova will sleep exactly nn times. The ii-th time he will sleep exactly after aiai hours from the time he woke up. You can assu...
[ "dp", "implementation" ]
#include <bits/stdc++.h> using namespace std; /*-----------------*/ #define ll long long #define ld long double #define ever (;;) #define all(v) v.begin(),v.end() #define allr(v) v.rbegin(),v.rend() #define PB push_back #define F first #define S second #define MOD 1000000007 #define Dracarys ios_base::sync_...
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 <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <string> #include <vector> using namespace std; #define all(x) (x).begin(), (x).end() #defin...
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> #define REP(i,n) for(int i = 0; i< (n);i++) #define all(x) (x).begin(), (x).end() #define f first #define s second #define SZ(a) (int)a.size() #define bit(x,i)(((x)>>(i))&1) typedef long long ll; using namespace std; ll m; void solve(){ ll n; cin >> n >> m; vector<ll> v(n); ll ans = 1; ...
cpp
1292
A
A. NEKO's Maze Gametime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or aliveNEKO#ΦωΦ has just got a new maze game on her PC!The game's main puzzle is a maze; in the forms of a 2×n2×n rectangle grid. NEKO...
[ "data structures", "dsu", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(),x.end() #define pb push_back #define ll long long #define repp(n) for(int i=0;i<n;i++) //typedef __int128 lll; const ll MOD=998244353; const ll mod = 1e9+7; #define ret(a) cout<<a<<"\n"; return void solve(){ ll n,q; cin>>n>...
cpp
1292
A
A. NEKO's Maze Gametime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output3R2 as DJ Mashiro - Happiness Breeze Ice - DJ Mashiro is dead or aliveNEKO#ΦωΦ has just got a new maze game on her PC!The game's main puzzle is a maze; in the forms of a 2×n2×n rectangle grid. NEKO...
[ "data structures", "dsu", "implementation" ]
#include <bits/stdc++.h> using namespace std; int main() { int n, q; scanf("%d%d", &n, &q); set<pair<int,int>> cells; // O(log) per operation int bad_nei = 0; for(int i = 0; i < q; i++) { int row, col; scanf("%d%d", &row, &col); bool was_forbidden = cells.count({row, col}); for(int r = row - 1; ...
cpp
1307
E
E. Cow and Treatstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a successful year of milk production; Farmer John is rewarding his cows with their favorite treat: tasty grass!On the field, there is a row of nn units of grass, each with a sweetness sisi. Farmer...
[ "binary search", "combinatorics", "dp", "greedy", "implementation", "math" ]
#include<iostream> #include<cstring> #include<vector> #include<array> #include<cassert> using namespace std; using LL = long long; const int maxn = 5005; int s[maxn][maxn]; vector<pair<int, int> > p[maxn]; template<const int T> struct ModInt { const static int mod = T; int x; ModInt(int x = ...
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; const int MAX = 5e5 + 5; int arr[MAX], ldp[MAX], rdp[MAX], ans[MAX]; signed main(){ //ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; for(int i = 1; i <= n; ++i) cin >> arr[i]; stack<int> stk; fo...
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" ]
# include <bits/stdc++.h> using namespace std; int T; int n,m,f[60]; inline char get_(int x) { return x==1 ? 'C' : x==2 ? 'H' : 'O';} int main() { scanf("%d",&T); while (T--) { memset(f,0,sizeof(f)); scanf("%d",&n); printf("? CC\n"), fflush(stdout); scanf("%d",&m); for (int x,i=1;i<=m;i++) scanf("%d",&x)...
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<bits/stdc++.h> #define int long long #define fa(i,a,n) for(int i=a;i<n;i++) #define pb push_back #define bp pop_back #define mp make_pair #define all(v) v.begin(),v.end() #define vi vector<int> #define Utaval ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; void solve(){ ...
cpp
1311
D
D. Three Integerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three integers a≤b≤ca≤b≤c.In one move, you can add +1+1 or −1−1 to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero)...
[ "brute force", "math" ]
#include<bits/stdc++.h> #define nl "\n" #define fi first #define se second #define pi 3.14159 #define ll long long #define odd(a) (a&1) #define even(a) !(a&1) #define Mod 1'000'000'007 #define INF 2'000'000'000 #define sz(x) int(x.size()) #define charToInt(s) (s - '0') #define ull unsigned long long #def...
cpp
1290
F
F. Making Shapestime limit per test5 secondsmemory limit per test768 megabytesinputstandard inputoutputstandard outputYou are given nn pairwise non-collinear two-dimensional vectors. You can make shapes in the two-dimensional plane with these vectors in the following fashion: Start at the origin (0,0)(0,0). Choose a ve...
[ "dp" ]
#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& char sss; const i...
cpp
1295
C
C. Obtain The Stringtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two strings ss and tt consisting of lowercase Latin letters. Also you have a string zz which is initially empty. You want string zz to be equal to string tt. You can perform the followi...
[ "dp", "greedy", "strings" ]
#include<bits/stdc++.h> using namespace std; #define int long long const int mod = 1e9 + 7; //const int mod = 998244353; #define PII pair<long long, long long> #define x first #define y second #define pi 3.14159265359 int qpow(int a, int b) { int res = 1; while (b != 0) { if (b & 1)res...
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> long long t,a,m,ans; int main() { for(scanf("%lld",&t);t--;) { scanf("%lld%lld",&a,&m);ans=m/=std::gcd(a,m); for(long long i=2;i*i<=m;i++)if(m%i==0) for(ans-=ans/i;m%i==0;m/=i); printf("%lld\n",ans-(m>1)*ans/m); } }
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" ]
#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
1320
A
A. Journey Planningtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTanya wants to go on a journey across the cities of Berland. There are nn cities situated along the main railroad line of Berland, and these cities are numbered from 11 to nn. Tanya plans her journey...
[ "data structures", "dp", "greedy", "math", "sortings" ]
#include <bits/stdc++.h> using namespace std; #define int long long #define yes cout<<"YES"<<"\n" #define no cout<<"NO"<<"\n" #define fori(i, a, n) for (int i = a; i < n; i++) #define ford(i, n, a) for (int i = n-1; i >= a; i--) #define mod 1000000007 #define PI 3.141592653589793238462 #define line cout<<"\n...
cpp
1284
F
F. New Year and Social Networktime limit per test4 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputDonghyun's new social network service (SNS) contains nn users numbered 1,2,…,n1,2,…,n. Internally, their network is a tree graph, so there are n−1n−1 direct connections between each user....
[ "data structures", "graph matchings", "graphs", "math", "trees" ]
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/trie_policy.hpp> // #include <ext/rope> using namespace std; // using namespace __gnu_cxx; // using namespace __gnu_pbds; void Hollwo_Pelw(); signed main(){ #ifndef hollwo_pelw_local if (fopen(".inp", "r")) a...
cpp
1286
B
B. Numbers on Treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEvlampiy was gifted a rooted tree. The vertices of the tree are numbered from 11 to nn. Each of its vertices also has an integer aiai written on it. For each vertex ii, Evlampiy calculated cici — the n...
[ "constructive algorithms", "data structures", "dfs and similar", "graphs", "greedy", "trees" ]
#include<bits/stdc++.h> using namespace std; const int MAXN = 2.1e3; int N; int P[MAXN]; vector<int> ch[MAXN]; int C[MAXN]; vector<int> dfs(int cur) { vector<int> ans; for (int nxt : ch[cur]) { auto v = dfs(nxt); ans.insert(ans.end(), v.begin(), v.end()); } if (C[cur] > int(ans.size())) { co...
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<bits/stdc++.h> using namespace std; typedef long long ll; ll a[500000]; void solve() { ll n;string s; cin>>n>>s; map<pair<ll,ll>,ll >m; ll x=0,y=0,mi=3e9,dis,l,r; s='0'+s; m[{0,0}]=0; for(int i=1;i<=n;i++) { if(s[i]=='L')x--; if(s[i]=='R')x++; if(s[i]=='U')y--; if(s[i]=='D')y++; if(m.count({x,y})) { ll juli=i...
cpp
1286
C2
C2. Madhouse (Hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem is different with easy version only by constraints on total answers lengthIt is an interactive problemVenya joined a tour to the madhouse; in which orderlies play with patients th...
[ "brute force", "constructive algorithms", "hashing", "interactive", "math" ]
#include<bits/stdc++.h> #define ll long long #define pb push_back #define mkp make_pair #define vi vector<int> #define pii pair<int,int> #define SZ(x) ((int)x.size()) #define FI(n) FastIO::read(n) #define FO(n) FastIO::write(n) #define ull unsigned long long #define mst(a,b) memset(a,b,sizeof(a)) #define foR...
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; #define ll long long int main (){ int t; cin >> t; while (t--){ int a , b ; cin >> a >> b; if (a==b) cout<<0<<endl; else cout<< 1 + int((a < b) ^ ((b - a) & 1)) << endl; } }
cpp
1290
F
F. Making Shapestime limit per test5 secondsmemory limit per test768 megabytesinputstandard inputoutputstandard outputYou are given nn pairwise non-collinear two-dimensional vectors. You can make shapes in the two-dimensional plane with these vectors in the following fashion: Start at the origin (0,0)(0,0). Choose a ve...
[ "dp" ]
#include<bits/stdc++.h> #define ll long long #define fir first #define sec second #define pii pair<int,int> using namespace std; const int maxn=6; const int inf=0x3f3f3f3f; const int mod=998244353; struct mint { int val; mint():val(0) {} mint(ll tval) { if(-mod<=tval&&tval<2*mod) { if(tval>=mo...
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> #define For(i,a,b) for(register int i=(a);i<=(b);++i) #define Rep(i,a,b) for(register int i=(a);i>=(b);--i) #define int long long using namespace std; inline int read() { char c=getchar();int x=0;bool f=0; for(;!isdigit(c);c=getchar())f^=!(c^45); for(;isdigit(c);c=getchar())...
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> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> #define endl '\n' #define int long long #define all(a) a.begin(...
cpp
1303
B
B. National Projecttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYour company was appointed to lay new asphalt on the highway of length nn. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip...
[ "math" ]
#include <bits/stdc++.h> #include <algorithm> #include <iostream> #include <numeric> #include <cmath> #include <conio.h> #include <iomanip> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ll long long #define ld long do...
cpp