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
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" ]
// LUOGU_RID: 102287254 // #pragma GCC optimize("O3,unroll-loops") // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include <bits/stdc++.h> #include <ext/rope> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #inc...
cpp
1316
C
C. Primitive Primestime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt is Professor R's last class of his teaching career. Every time Professor R taught a class; he gave a special problem for the students to solve. You being his favourite student, put your heart in...
[ "constructive algorithms", "math", "ternary search" ]
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define all(v) ((v).begin()), ((v).end()) #define siz(v) ((int)((v).size())) #define clr(v, d) memset(v, d, sizeof(v)) #define rep(i, v) for (int i = 0; i <...
cpp
1311
F
F. Moving Pointstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn points on a coordinate axis OXOX. The ii-th point is located at the integer point xixi and has a speed vivi. It is guaranteed that no two points occupy the same coordinate. All nn points mo...
[ "data structures", "divide and conquer", "implementation", "sortings" ]
#include<bits/stdc++.h> using namespace std; long long n,a[200005],ans; pair<long long,long long>q[200005]; int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) { long long x; cin>>x; q[i]=make_pair(x,a[i]); } sort(a+1,a+n+1); sort(q+1,q+n+1); for(int i=1;i<=n;i++) ans+=(lower_bound(a+1,a+n+1,q[...
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; void solve(){ int n, m, k; // n is elements, m is place in line, k is number of people to control cin >> n >> m >> k; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; if (k >= (m - 1)){ int ans = 0; for ...
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> #define range(i,n) for(int i= 0; i < (n); i++) typedef long long int ll; #define arr(a,n) for(int i=0;i<(n);i++) cin>>a[i]; #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define int ll #define in insert #define endl '\...
cpp
1288
F
F. Red-Blue Graphtime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a bipartite graph: the first part of this graph contains n1n1 vertices, the second part contains n2n2 vertices, and there are mm edges. The graph can contain multiple edges.Initially, each...
[ "constructive algorithms", "flows" ]
#include<bits/stdc++.h> using namespace std; const int N = 443; int n1, n2, m, r, b; string s1, s2; int u[N]; int v[N]; struct edge { int y, c, f, cost; edge() {}; edge(int y, int c, int f, int cost) : y(y), c(c), f(f), cost(cost) {}; }; int bal[N][N]; int s, t, oldS, oldT, V; vecto...
cpp
1295
F
F. Good Contesttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn online contest will soon be held on ForceCoders; a large competitive programming platform. The authors have prepared nn problems; and since the platform is very popular, 998244351998244351 coder from ...
[ "combinatorics", "dp", "probabilities" ]
//#pragma GCC optimize("Ofast", "unroll-loops") //#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4", "avx") #include <bits/stdc++.h> #define int long long #define ll long long #define ull unsigned long long #define clr(f, n) memset(f, 0, sizeof(int) * (n)) #define cpy(f, g, n) memcpy(f, g, sizeof(int) * ...
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> #define int long long using namespace std; struct point{ int x,y; }; struct line{ point p1,p2; }a[4]; int len_line_pow(line a){//求线段长度的平方 return (a.p1.x-a.p2.x)*(a.p1.x-a.p2.x)+(a.p1.y-a.p2.y)*(a.p1.y-a.p2.y); } bool check_same_point(point p1,point p2){//判断两点是否...
cpp
1321
A
A. Contest for Robotstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, a...
[ "greedy" ]
#include<bits/stdc++.h> #include<stdio.h> using namespace std; #define ll long long #define scl(n) cin>>n; #define scc(c) cin>>c; #define fr(i,n) for (ll i=0;i<n;i++) #define fr1(i,n) for(ll i=1;i<=n;i++) #define pfl(x) printf("%l...
cpp
1299
B
B. Aerodynamictime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas are going to build a polygon spaceship. You're given a strictly convex (i. e. no three points are collinear) polygon PP which is defined by coordinates of its vertices. Define P(x,y)P...
[ "geometry" ]
#include<iostream> using namespace std; int main() { int n,i; cin>>n; if(n%2) { cout<<"NO"<<endl; return 0; } pair<int,int>a[n],p,temp; for(i=0;i<n;i++)cin>>a[i].first>>a[i].second; p={a[0].first+a[n/2].first,a[0].second+a[n/2].second}; for(i=1;i<n/2;i++){temp={a[i].first+a[i...
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" ]
// LUOGU_RID: 101656980 // just for fun #include <bits/stdc++.h> #define rep(i, x, y) for (int i = (x); i <= (y); i+=1) #define epr(i, x) for (int i = head[x]; i; i = nxt[i]) #define per(i, x, y) for (int i = (x); i >= (y); i-=1) #define DC int T = gi <int> (); while (T--) #define eb emplace_back #define ep emplace #d...
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" ]
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define AquA cin.tie(0);ios_base::sync_with_stdio(0); #define fs first #define sc second #define p_q priority_queue #define int long long using namespace std; vector<int> findz(string a){ int n=a.size(); vector<int> re(n); re[0]=n; int...
cpp
1290
E
E. Cartesian Tree time limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIldar is the algorithm teacher of William and Harris. Today; Ildar is teaching Cartesian Tree. However, Harris is sick, so Ildar is only teaching William.A cartesian tree is a rooted tree, that can be...
[ "data structures" ]
#include <bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2") #define int long long using namespace std; namespace IO { 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++) :...
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> #include <ext/pb_ds/assoc_container.hpp> #define ll long long using namespace std; using namespace __gnu_pbds; typedef pair<int, int> node; typedef tree<node, null_type, less<node>,rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int N=3e5+5; const ll MOD=1e9+7,MAX=1e18...
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 <iostream> #include <vector> #include <queue> #include <unordered_map> #include <unordered_set> #include <map> #include <set> #include <cmath> #include <algorithm> #include <functional> using namespace std; #define int long long void solve() { int n,mod; cin>>n>>mod; vector<int> fac...
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> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> using namespace std; using namespace __gnu_pbds; // less<int> -> increasing order, greater<int>->decreasing order, less_equal<int> -> work as multiset typedef tree<pair<long long int, ...
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; typedef long long ll; typedef pair<int, int> PII; typedef pair<double, double> PDD; const int N = 1e6 + 10; vector<int>h[N]; int ask(int u, int v) { cout << "? " << u << " " << v << '\n'; int t; cin >> t; return t; } int ans, st[N]; int p[10], ok; void dfs...
cpp
1321
A
A. Contest for Robotstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, a...
[ "greedy" ]
//This is coded by VIDYARTH GS //#include <bits/stdc++.h> #include <iostream> #include <complex> #include <queue> #include <set> #include <unordered_set> #include <list> #include <chrono> #include <random> #include <iostream> #include <algorithm> #include <cmath> #include <string> #include <vector> #inc...
cpp
1313
D
D. Happy New Yeartime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBeing Santa Claus is very difficult. Sometimes you have to deal with difficult situations.Today Santa Claus came to the holiday and there were mm children lined up in front of him. Let's number them fr...
[ "bitmasks", "dp", "implementation" ]
// LUOGU_RID: 97931742 #include <cstdio> #include <cstring> #include <algorithm> using std::max; const int MAXN = 1e5 + 5; int n, m, k; int vis[MAXN << 1]; int f[1000]; int tot; struct Seg { int x, id; Seg() {} Seg(const int& X, const int& I) { x = X, id = I; } bool op...
cpp
1290
E
E. Cartesian Tree time limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIldar is the algorithm teacher of William and Harris. Today; Ildar is teaching Cartesian Tree. However, Harris is sick, so Ildar is only teaching William.A cartesian tree is a rooted tree, that can be...
[ "data structures" ]
#include <bits/stdc++.h> #define int long long using namespace std; const int maxn=150005,inf=1e18; int n,a[maxn],pos[maxn]; struct vl {int sum,mx,sc,cnt; vl(){sum=0;mx=sc=-inf;cnt=0;} }; inline vl merge(vl a,vl b) { vl ret; ret.sum=a.sum+b.sum; ret.mx=max(a.mx,b.mx); ret.sc=-inf; ret.cnt=0; if...
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; using i64=long long; using u64=unsigned long long; using db=double; using pii=pair<int,int>; using vi=vector<int>; template<typename T> inline T read(){ T x=0,f=0;char ch=getchar(); while(!isdigit(ch)) f|=(ch=='-'),ch=getchar(); while(isdigit(ch)) x=x*10+(ch-...
cpp
1325
E
E. Ehab's REAL Number Theory Problemtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa of length nn that has a special condition: every element in this array has at most 7 divisors. Find the length of the shortest non-empty subsequence of this...
[ "brute force", "dfs and similar", "graphs", "number theory", "shortest paths" ]
#include <bits/stdc++.h> using namespace std; const int MN=8e4; int N, at=170, dist[MN], ans = MN; bool pvis[1001], vis[MN]; set<int> start; vector<int> prime, vals, adj[MN]; unordered_map<int, int> ind; // (value, index it maps to) queue<pair<int, pair<int, int> > > next1; // (distance, (node, parent)) void ...
cpp
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; typedef long long ll; int dx[4] = {-1, 0, 0, 1}; int dy[4] = { 0,-1, 1, 0}; int n,m,Q,a[305][305],x[2000005],y[2000005],c[2000005],ans[2000005]; vector <int> vec[305][305]; int pre[2000005]; int fa[4000005]; int getf(int x){ if(x == fa[x]) return x; retur...
cpp
1288
B
B. Yet Another Meme Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers AA and BB, calculate the number of pairs (a,b)(a,b) such that 1≤a≤A1≤a≤A, 1≤b≤B1≤b≤B, and the equation a⋅b+a+b=conc(a,b)a⋅b+a+b=conc(a,b) is true; conc(a,b)conc(a,b)...
[ "math" ]
#include<bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; using namespace std; const ll mod = 998244353; const int mm = 2e5 + 10; void solve(){ ll a,b; cin>>a>>b; ll ans=0; for(int i=10;i-1<=b;i*=10){ ans+=a; }cout<<ans<<'\n'; } int main(){ std::ios::sync_with_stdio(false); ...
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 <iostream> #include <stack> using namespace std; #define int long long int ans[500005]; int n, a[500005]; int suml[500005]; int sumr[500005]; stack<int> st; signed main(){ scanf("%lld", &n); st.push(0); a[0] = -999999999; for(int i = 1; i <= n; i++) scanf("%lld", &a[i]); for(int i = 1; i <= ...
cpp
1299
D
D. Around the Worldtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas are planning 144144 trips around the world.You are given a simple weighted undirected connected graph with nn vertexes and mm edges with the following restriction: there isn't a...
[ "bitmasks", "combinatorics", "dfs and similar", "dp", "graphs", "graphs", "math", "trees" ]
#include<bits/stdc++.h> using namespace std; struct edge{ int x,y,z; }t[1000011]; const int mod=1000000007; int n,m,sum,x[200011],y[200011],z[200011],h[200011],f[200011],val[200011],ans,p[11],idx[200011],cnt,g[511][11],son[511][511],idd[41],ss[2][2][511],la,col[200011],dep[200011],vis[41]; bool fl[200011]; que...
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> #define ms(x, v) memset(x, v, sizeof(x)) #define il __attribute__((always_inline)) #define U(i,l,r) for(int i(l),END##i(r);i<=END##i;++i) #define D(i,r,l) for(int i(r),END##i(l);i>=END##i;--i) using namespace std; typedef unsigned long long ull; typedef long long ll; template <typenam...
cpp
1304
F1
F1. Animal Observation (easy 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" ]
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //#include<boost/algorithm/string.hpp> //pragmas #pragma GCC optimize("O3") //types #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define ll long long int #define ull unsigned long long int #define vec vector<...
cpp
1294
E
E. Obtain a Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a rectangular matrix of size n×mn×m consisting of integers from 11 to 2⋅1052⋅105.In one move, you can: choose any element of the matrix and change its value to any integer between ...
[ "greedy", "implementation", "math" ]
#include<bits/stdc++.h> using namespace std; const int N=1e6+10,R=1e6; const int mod=1e9+7,inf=1e9; void solve() { int n,m; cin>>n>>m; vector<vector<int> >a(n,vector<int>(m)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; } } long long ans...
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> using namespace std; #define int long long typedef int ll; typedef pair<ll,ll> pi; const int N=2e5+10; pi isinter(pi a,pi b) { if (a.first > b.first)swap(a, b); if (a.first == b.first && b.second >= a.second)swap(a, b); // nested if (a.second >= b.second) { ret...
cpp
1285
F
F. Classical?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array aa, consisting of nn integers, find:max1≤i<j≤nLCM(ai,aj),max1≤i<j≤nLCM(ai,aj),where LCM(x,y)LCM(x,y) is the smallest positive integer that is divisible by both xx and yy. For example, LCM(6,8...
[ "binary search", "combinatorics", "number theory" ]
#include <bits/stdc++.h> constexpr int N = 1e5 + 10; signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<std::vector<int>> divs(N); std::vector<int> mu(N); for (int i = 1; i < N; ++i) { mu[i] += i == 1; divs[i].emplace_back(...
cpp
1141
E
E. Superhero Battletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA superhero fights with a monster. The battle consists of rounds; each of which lasts exactly nn minutes. After a round ends, the next round starts immediately. This is repeated over and over again.E...
[ "math" ]
#include <bits/stdc++.h> using namespace std; #define int long long #define sz(x) (int)((x).size()) #define all(x) (x).begin(), (x).end() #define db(x) cout << "[" << #x << "]: " << x << '\n'; const int INF = 2e18; struct SegmentTree { int merge(const int& left_child, const int& right_child) { ...
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" ]
// LUOGU_RID: 102109617 #include <bits/stdc++.h> using i64 = long long; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n, h, l, r; std::cin >> n >> h >> l >> r; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } std::vector<int> dp(h, -1); dp[0] = 0; ...
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> #define speed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ll long long #define pb push_back #define mp make_pair #define f first #define s second using namespace std; const int N = 2001; int n, p, cnt = 0, mx = 0; bool f = 1; int main(){ speed; cin >> n...
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> #include<vector> #include<algorithm> using namespace std; using LL = long long; const int maxn = 505; int s[4][maxn][maxn], a[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];...
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" ]
// LUOGU_RID: 102570642 #include <bits/stdc++.h> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const LL mod = 1e9 + 7; const int N = 100005; int a[N]; int main() { int _; cin >> _; while (_--) { int n, m; cin >> n >> m; int mx = 0; int ok = 0; ...
cpp
1299
B
B. Aerodynamictime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas are going to build a polygon spaceship. You're given a strictly convex (i. e. no three points are collinear) polygon PP which is defined by coordinates of its vertices. Define P(x,y)P...
[ "geometry" ]
#include<iostream> using namespace std; int main() { int n,i; cin>>n; if(n%2) { cout<<"NO"<<endl; return 0; } pair<int,int>a[n],p,temp; for(i=0;i<n;i++)cin>>a[i].first>>a[i].second; p={a[0].first+a[n/2].first,a[0].second+a[n/2].second}; for(i=1;i<n/2;i++){temp={a[i].first+a[i+n/2].first,a[i].second+a[i+n/2].second};if...
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> #define Source ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL); #define ll long long #define int long long #define ld long double #define Endl '\n' //#define t int t;cin>>t;while(t--) #define all(x) x.begin(),x.end() #define allr(x) x.rbegin(),x.rend() #define sz(a) (in...
cpp
1290
A
A. Mind Controltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou and your n−1n−1 friends have found an array of integers a1,a2,…,ana1,a2,…,an. You have decided to share it in the following way: All nn of you stand in a line in a particular order. Each minute, the p...
[ "brute force", "data structures", "implementation" ]
#include<bits/stdc++.h> using namespace std; const int maxn=1e4+10; int arr[maxn]; int main(){ int t; cin>>t; while(t--){ int n,m,k; cin>>n>>m>>k; for(int i=1;i<=n;++i) cin>>arr[i]; if(k>=m) k=m-1; int ans=-1,len1=n-k,len2=len1-(m-k-1); for(int i=...
cpp
1324
C
C. Frog Jumpstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a frog staying to the left of the string s=s1s2…sns=s1s2…sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00). Each character of ss is either 'L' or 'R'. It...
[ "binary search", "data structures", "dfs and similar", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 int main(){ ios_base::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); int T=1; cin>>T; while(T--) { int ans=0,dis=1; string s; cin>>s; char c; for(int i=0;i<s.size();i++){ ...
cpp
1141
G
G. Privatization of Roads in Treelandtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTreeland consists of nn cities and n−1n−1 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are righ...
[ "binary search", "constructive algorithms", "dfs and similar", "graphs", "greedy", "trees" ]
#include<bits/stdc++.h> #define ll long long int #define pa pair<int,int> #define f first #define s second #define sz 200005 #define vec array<int,4> using namespace std; std::vector<int> adj[sz]; int deg[sz],ans[sz],deg1[sz]; int r; map<pa,int>mp; void solve(int node,int par,int clr) { for(int u:adj[node]) { ...
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> #define ll long long int #define pii pair<ll,ll> using namespace std ; void solve() { ll n,g,b ; cin>>n>>g>>b ; // ll good_days = n/2/g*g + (ll)(ceil(n/2.0) - n/2/g*g); // ll bad_days = ((n/2/g-1)*b) + (ceil(n/2.0) - n/2/g*g != 0 ? b : 0); // ll rem_days...
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<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { int num,t; int a[2][1000]; scanf("%d",&t); while(t--) { scanf("%d",&num); for(int j=0;j<2;j++) for(int i=0;i<num;i++) { scanf("%d",&a[j][i]); } sort(a[0],a[0]+num); sort(a[1],a[1]+num); for(int j=...
cpp
1292
B
B. Aroma's Searchtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTHE SxPLAY & KIVΛ - 漂流 KIVΛ & Nikki Simmons - PerspectivesWith a new body; our idol Aroma White (or should we call her Kaori Minamiya?) begins to uncover her lost past through the OS space.The space can...
[ "brute force", "constructive algorithms", "geometry", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; int main() { int64_t x0, y0, ax, ay, bx, by, xs, ys, t; cin >> x0 >> y0 >> ax >> ay >> bx >> by; cin >> xs >> ys >> t; vector<int64_t> x(1, x0), y(1, y0); int64_t LIMIT = (1LL << 62) - 1; while ((LIMIT - bx) / ax >= x.back() && (LIMIT - by) / ...
cpp
1303
D
D. Fill The Bagtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a bag of size nn. Also you have mm boxes. The size of ii-th box is aiai, where each aiai is an integer non-negative power of two.You can divide boxes into two parts of equal size. Your goal is t...
[ "bitmasks", "greedy" ]
#include <bits/stdc++.h> #define N 100005 #define wr cout << "Continue debugging\n"; #define all(x) (x).begin(), (x).end() #define ll long long int #define pii pair <int, int> #define pb push_back #define ff first #define ss second using namespace std; int t, cnt[N]; int logs(int x){ int res = 0; whi...
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" ]
/* God loves You */ #include<bits/stdc++.h> using namespace std; #define Sowrav_Nath ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define f(i,n) for(int i=0;i<int(n);i++) #define f1(i,n) for(int i=1;i<=n;i++) #define rf(i,n) for(int i=int(n)-1;i>=0;i--) ...
cpp
1303
C
C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arrange...
[ "dfs and similar", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define ll long long ll N = 1000000007; // int dp[200001]; string func(int i,vector<vector<int>> &adj,int pr){ string str=""; str.push_back('a'+i); for(auto ch:adj[i]){ if(ch==pr)continue; str=str+func(ch,adj,i); } return str; } int main() ...
cpp
1287
A
A. Angry Studentstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt's a walking tour day in SIS.Winter; so tt groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another.Initially, some students are an...
[ "greedy", "implementation" ]
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); int t; cin >> t; while(t--){ int n; string s; cin >> n >> s; int cnt = 0; int res = 0; ...
cpp
1290
E
E. Cartesian Tree time limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIldar is the algorithm teacher of William and Harris. Today; Ildar is teaching Cartesian Tree. However, Harris is sick, so Ildar is only teaching William.A cartesian tree is a rooted tree, that can be...
[ "data structures" ]
#include<bits/stdc++.h> using namespace std; #define ll long long #define N 200010 #define inf (1e9) ll n; ll a[N]; ll ans[N]; ll pos[N]; struct sgt{ ll sum[N<<2],tag1[N<<2],tag2[N<<2],mx[N<<2],smx[N<<2],cn[N<<2],cnmx[N<<2]; inline void pushup(ll x){ sum[x]=sum[x<<1]+sum[x<<1|1]; cn[x]=cn[x<<1]+cn[x...
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> using namespace std; int main() { int t; cin>>t; while(t--) { int n,i,even=0,odd=0; cin>>n; int a[n]; for( i=0;i<n;i++) cin>>a[i]; for(i=0;i<n;i++) { if(a[i]%2==0) ...
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; #define X first #define Y second #define int long long #define sz(a) (int)a.size() #define ll long long const int N = 2e5; const int T = 20; int n, q; vector <int> g[N]; vector <pair <int, int> > g1[N]; int h[N]; int used[N]; int up[T][N]; int tin[N], tout[N];...
cpp
1286
D
D. LCCtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn infinitely long Line Chillland Collider (LCC) was built in Chillland. There are nn pipes with coordinates xixi that are connected to LCC. When the experiment starts at time 0, ii-th proton flies from the ii-th...
[ "data structures", "math", "matrices", "probabilities" ]
// LUOGU_RID: 97675100 #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 barrett...
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" ]
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set; #define endl '\n' #define ilihg ios_base::sync_with_...
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 <bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long #define Endl '\n' #define ENDL '\n' #define enld '\n' #define bitsN(n) __builtin_popcount(n) #define bitsN_ll(n) __builtin_popcountll(n) #define lz(n) __builtin_clz(n) #define lzll(n) __builtin_clzll(n) #define tz(n) __bui...
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; int tt, tc; using ll = long long; void solve() { int n; cin >> n; string s; cin >> s; vector<int> freq(27, INT_MAX); freq[0] = INT_MIN; reverse(s.begin(), s.end()); vector<int> ans; for (char c : s) { int j = c - ...
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 <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
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> #include <bitset> #include <iostream> #define lana_del_rey ios_base::sync_with_stdio(false);cin.tie(NULL) #define ll long long int #define clr(v,d ) memset(v, d, sizeof(v)) #define deb(x) cout<<#x<<"="<<x<<endl using namespace std; const ll Mod = 1000000007; const ll N = 2002000; ...
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> #define F first #define S second #define pb push_back #define ppb pop_back #define ep insert #define endl '\n' #define elif else if #define pow pwr #define sqrt sqrtt #define int long long typedef unsigned long long ull; using namespace std; const int N=1005; vector<int> adj[N]; ...
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<iostream> #include<vector> #include<set> #include<queue> #include<map> #include<algorithm> #include<numeric> #include<cmath> #include<cstring> #include<bitset> #include<iomanip> #include<random> #include<fstream> #include<complex> #include<time.h> #include<stack> using namespace std; #define endl "\n" #define ...
cpp
1141
E
E. Superhero Battletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA superhero fights with a monster. The battle consists of rounds; each of which lasts exactly nn minutes. After a round ends, the next round starts immediately. This is repeated over and over again.E...
[ "math" ]
// ~BhupinderJ #include <bits/stdc++.h> using namespace std; #define endl "\n" #define spc <<" "<< #define pii pair<int, int> #define vvi vector<vector<int>> #define all(t) t.begin(), t.end() #define int long long const int Mod = 1e9+7; const int N = 1e5+1; void SOLVE(){ int H, n; cin >> H >> n; v...
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> #include <ext/random> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define GET(n, i) (((n) >> (i)) bitand 1) #define LAST(n) ((n) bitand (-(n))) #define RANGE(x, y, i, j) (i > -1 and i < x and j > -1 and j < ...
cpp
1305
E
E. Kuroni and the Score Distributiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni is the coordinator of the next Mathforces round written by the "Proof by AC" team. All the preparation has been done; and he is discussing with the team about the score distrib...
[ "constructive algorithms", "greedy", "implementation", "math" ]
#include <bits/stdc++.h> // #pragma GCC optimize(2) #define N 5005 // #define mod 998244353 // #define endl '\n' #define int long long // #define double long double #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using n...
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 pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define eb emplace_back #define ef emplace_front #define lowbit(x) (x & (-x)) #define ti chrono::system_clock::now().time_since_epoch().count() #define Fin(x) freopen(x, "r", stdi...
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> #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") #pragma GCC optimize("fast-math") #pragma GCC optimize ("unroll-loops,Ofast,O3") #pragma GCC target("avx,av...
cpp
1324
D
D. Pair of Topicstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe next lecture in a high school requires two topics to be discussed. The ii-th topic is interesting by aiai units for the teacher and by bibi units for the students.The pair of topics ii and jj (i<ji...
[ "binary search", "data structures", "sortings", "two pointers" ]
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template<class T> using ordset = tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>; #define ar array #define ll long lo...
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 <vector> #include <algorithm> #include <bits/stdc++.h> #include <iterator> #include <map> using namespace std; bool sortbysec(const pair<char,int> &a, const pair<char,int> &b) { return (a.first > b.first); } bool isPalindrome(string S) { // Stores the rev...
cpp
1291
B
B. Array Sharpeningtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou're given an array a1,…,ana1,…,an of nn non-negative integers.Let's call it sharpened if and only if there exists an integer 1≤k≤n1≤k≤n such that a1<a2<…<aka1<a2<…<ak and ak>ak+1>…>anak>ak+1>…>an. ...
[ "greedy", "implementation" ]
#include <bits/stdc++.h> // #include "Algorithms.h" #define long long long const int Mod = 1e9 + 7, N = 1e3 + 1; void Solution() { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } int l = 0, r = n; while (l < n && a[l] >= l) l++; whil...
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" ]
// LUOGU_RID: 101764554 //#pragma GCC optimize(3) #include<iostream> #include<climits> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<complex> #include<random> #include<chrono> #define int long l...
cpp
1301
D
D. Time to Runtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBashar was practicing for the national programming contest. Because of sitting too much in front of the computer without doing physical movements and eating a lot Bashar became much fatter. Bashar is going...
[ "constructive algorithms", "graphs", "implementation" ]
#include <bits/stdc++.h> #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
1294
C
C. Product of Three Numberstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given one integer number nn. Find three distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c and a⋅b⋅c=na⋅b⋅c=n or say that it is impossible to do it.If there are several answers, yo...
[ "greedy", "math", "number theory" ]
// Online C++ compiler to run C++ program online #include <iostream> #define k long long unsigned using namespace std; int main() { int t;cin>>t; while(t--){ k n,a=1,b=1,c=1;cin>>n; for(k i=2;i*i<=n;i++){ if(n%i==0){ a=i; n/=a; ...
cpp
1305
E
E. Kuroni and the Score Distributiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKuroni is the coordinator of the next Mathforces round written by the "Proof by AC" team. All the preparation has been done; and he is discussing with the team about the score distrib...
[ "constructive algorithms", "greedy", "implementation", "math" ]
#include<bits/stdc++.h> using namespace std; int n,m,t=1; int a[5002]; int main() { for(scanf("%d%d",&n,&m);t<=n && m>=(t-1)/2;++t)a[t]=t,m-=(t-1)/2; if(t>n && m)return 0&puts("-1"); if(m)a[t]=2*t-2*m-1,++t; for(;t<=n;++t)a[t]=n*n+t*n; for(int i=1;i<=n;++i)printf("%d ",a[i]); return 0; }
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; typedef long long ll; #define v(li) vector<ll> #define vp(li) vector<pair<ll, ll>> #define m(li) map<ll, ll> #define s(li) set<ll> #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define ss second #define ff first #define f(i, n) 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; deque<string>k; int main() { ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); int n, m, ans = 0; string q = ""; cin >> n >> m; map<string, int>mp; for(int i = 1; i <= n;i++) { string s, t; cin >> s; ...
cpp
1294
E
E. Obtain a Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a rectangular matrix of size n×mn×m consisting of integers from 11 to 2⋅1052⋅105.In one move, you can: choose any element of the matrix and change its value to any integer between ...
[ "greedy", "implementation", "math" ]
#include "bits/stdc++.h" #define pf printf #define sf scanf #define db double #define ll long long #define siz 20000001 #define MAXN 100001 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); ll mod=1000000007; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll powerwithmod(...
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> using namespace std; int main() { float n,ans=0; cin>>n; while(n>0) { ans+= 1/n; n--; } cout<<ans<<endl; }
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; #define ll long long int ll ans[100000]; ll depth=0; int main() { ll t; cin>>t; while(t--) { ll n; cin>>n; ll arr[n]; set<ll>s; vector<ll>v(2*n+1); for(int i=0;i<n;i++) { cin>>arr[i]; v[arr[i]]=1; } ll flag=0; ...
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> #define ll long long #define pb push_back using namespace std; int main() { ll n,m;cin>>n>>m; vector<string> vn,vm; for(ll i=0;i<n;i++) { string x;cin>>x;vn.pb(x);} for(ll i=0;i<m;i++) { string x;cin>>x;vm.pb(x);} ll q;cin>>q; while(q--){ ll x;cin>>x; cout<<vn[(x-1+n)...
cpp
1324
C
C. Frog Jumpstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a frog staying to the left of the string s=s1s2…sns=s1s2…sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00). Each character of ss is either 'L' or 'R'. It...
[ "binary search", "data structures", "dfs and similar", "greedy", "implementation" ]
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") #pragma GCC optimize("fast-math") #pragma GCC optimize ("unroll-loops,Ofast,O3") #pragma GCC target("avx,av...
cpp
1291
A
A. Even But Not Eventime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's define a number ebne (even but not even) if and only if its sum of digits is divisible by 22 but the number itself is not divisible by 22. For example, 1313, 12271227, 185217185217 are ebne num...
[ "greedy", "math", "strings" ]
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int n,even=0,odd=0; cin>>n; string s; cin>>s; for(int i=0;i<n;i++){ if(s[i]%2==0){ even++; } else{ od...
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" ]
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; long long t,n,k,i,j; string s,a,b,c; int main() { scanf("%lld",&t); for(j=1;j<=t;j=j+1) { scanf("%lld",&n);cin>>s;a=s;k=1; for(i=2;i<=n;i=i+1) { c=s.substr(0,i-1); if((n-i)%2==1)b=s.substr(i-1,n-i+1)+c; if((n-i)%2==0){reverse(c.begin(),c.end()...
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 <iostream> #include <vector> using namespace std; using ll = long long; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree< ll, null_type, less<>, rb_tree_tag, tree_order_statistics_n...
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" ]
#include <bits/stdc++.h> #define ll long long using namespace std; ll lcm(ll a, ll b){ return (a*b)/__gcd(a,b); } void solve(){ ll x; cin>>x; ll res1 = x; ll res2 = 1; ll res = x; for(ll i = 1; i*i <= x; i++){ if(x % i == 0){ if(lcm(i, x/i) == x){ if(res > max(x/i, i)){ res = max(i, x/i); res...
cpp
1321
A
A. Contest for Robotstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp is preparing the first programming contest for robots. There are nn problems in it, and a lot of robots are going to participate in it. Each robot solving the problem ii gets pipi points, a...
[ "greedy" ]
#include <bits/stdc++.h> #include <time.h> #include <random> #include <conio.h> typedef long double ld; typedef long long ll; #define rall(a) a.rbegin(), a.rend() #define all(a) a.begin(), a.end() #define accepdet cout << "YES\n" #define error cout << "NO\n" /// author: University ITMO using namespa...
cpp
1315
B
B. Homecomingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter a long party Petya decided to return home; but he turned out to be at the opposite end of the town from his home. There are nn crossroads in the line in the town, and there is either the bus or the tr...
[ "binary search", "dp", "greedy", "strings" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { cin.tie(0);cin.sync_with_stdio(0); cout.tie(0);cout.sync_with_stdio(0); int t= 1; cin >>t ; while (t--) { int a , b , p ; cin >> a >> b >> p ; string s ; cin >...
cpp
1310
E
E. Strange Functiontime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputLet's define the function ff of multiset aa as the multiset of number of occurences of every number, that is present in aa.E.g., f({5,5,1,2,5,2,3,3,9,5})={1,1,2,2,4}f({5,5,1,2,5,2,3,3,9,5})={1,1,2,2,...
[ "dp" ]
#include<bits/stdc++.h> #define ll long long #define gmax(x,y) x=max(x,y) #define gmin(x,y) x=min(x,y) #define F first #define S second #define P pair #define FOR(i,a,b) for(int i=a;i<=b;i++) #define rep(i,a,b) for(int i=a;i<b;i++) #define V vector #define RE return #define ALL(a) a.begin(),a.end() #define ...
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" ]
#include <bits/stdc++.h> using namespace std; //#define int long long typedef pair<int,int> PII; typedef long long LL; const int INF=0x3f3f3f3f; const long long LNF=0x3f3f3f3f3f3f3f3f; const int mod=998244353; const int N=400010,M=N<<1; inline int read(){ int f=1,x=0;char s=getchar(); while(s<'0'||s>'9'){...
cpp
1286
F
F. Harry The Pottertime limit per test9 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputTo defeat Lord Voldemort; Harry needs to destroy all horcruxes first. The last horcrux is an array aa of nn integers, which also needs to be destroyed. The array is considered destroyed if all its e...
[ "brute force", "constructive algorithms", "dp", "fft", "implementation", "math" ]
#include <bits/stdc++.h> #include <immintrin.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimize("Ofast,unroll-loops") #define ll long long #define int ll #define ull unsigned ll #define ld long double #define rep(a) rep1(i,a) #define rep1(i,a) rep2(i,0,a) #define rep2(i,b,a) for(int i=...
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> #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
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 <iostream> using namespace std; int main() { int n; string s, res = ""; cin >> n >> s; char c = 'a', d = 'a'; for (int i = 0; i < n; i++) { if (s[i] >= c) { res += "0"; c = s[i]; } else if (s[i] >= d) { res += "1"; d = s[i]; } else { cout << "NO"; return 0; } } cout << "YES" << endl << res; }
cpp
1292
B
B. Aroma's Searchtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTHE SxPLAY & KIVΛ - 漂流 KIVΛ & Nikki Simmons - PerspectivesWith a new body; our idol Aroma White (or should we call her Kaori Minamiya?) begins to uncover her lost past through the OS space.The space can...
[ "brute force", "constructive algorithms", "geometry", "greedy", "implementation" ]
#include<bits/stdc++.h> #define int long long using namespace std; struct node{ int x,y; }p[100]; int dist(int a,int b,int c,int d){ if(a<c) swap(a,c); if(b<d) swap(b,d); return a-c+b-d; } signed main(){ int x,y,a,b,c,d,sx,sy,t,m=1,cnt=0,ans=-1; scanf("%lld%lld%lld%lld%lld%lld%lld%lld%lld",&x,&y,&a,&...
cpp
1320
D
D. Reachable Stringstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn this problem; we will deal with binary strings. Each character of a binary string is either a 0 or a 1. We will also deal with substrings; recall that a substring is a contiguous subsequence of a...
[ "data structures", "hashing", "strings" ]
#include <bits/stdc++.h> using namespace std; #define int long long #define N 200001 int P = 7; int M1 = 1000000009, M2 = 1000000007; int Power(int x, int y, int M){ int res = 1; while (y){ if (y % 2){ res *= x; res %= M; } x *= x; x %= M...
cpp
1291
B
B. Array Sharpeningtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou're given an array a1,…,ana1,…,an of nn non-negative integers.Let's call it sharpened if and only if there exists an integer 1≤k≤n1≤k≤n such that a1<a2<…<aka1<a2<…<ak and ak>ak+1>…>anak>ak+1>…>an. ...
[ "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int T, n, a[N]; int main () { scanf ("%d", &T); while (T --) { scanf ("%d", &n); for (int i = 1; i <= n; i ++) scanf ("%d", &a[i]); int l = 1, r = n; while (a[l + 1] >= l) l ++; while (a[r - 1] >= n - r + 1) r --; if (l >= r) puts ("Yes"); else puts ...
cpp
1299
D
D. Around the Worldtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas are planning 144144 trips around the world.You are given a simple weighted undirected connected graph with nn vertexes and mm edges with the following restriction: there isn't a...
[ "bitmasks", "combinatorics", "dfs and similar", "dp", "graphs", "graphs", "math", "trees" ]
#include<bits/stdc++.h> using namespace std; #define I inline int #define V inline void #define FOR(i,a,b) for(int i=a;i<=b;i++) #define ROF(i,a,b) for(int i=a;i>=b;i--) #define REP(u) for(int i=h[u],v;v=e[i].t,i;i=e[i].n) const int N=1e5+1,M=400,mod=1e9+7,bit[]={1,3,7,15,31}; V check(int&x){x-=mod,x+=x>>31&mod...
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> 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 #define rep(i,a,b) for(long long i = a; i...
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 <iostream> #include <vector> #include <algorithm> using namespace std; int operation(int); int main() { int t,s,value; cin>>t; vector<int> testcases; for (int i = 0; i <t; i++) { cin>>value; testcases.push_back(value); } for (int i = 0; i <t; i++) ...
cpp
1300
B
B. Assigning to Classestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputReminder: the median of the array [a1,a2,…,a2k+1][a1,a2,…,a2k+1] of odd number of elements is defined as follows: let [b1,b2,…,b2k+1][b1,b2,…,b2k+1] be the elements of the array in the sorted ord...
[ "greedy", "implementation", "sortings" ]
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0), ios::sync_with_stdio(0); int t, n; cin >> t; while (t--) { cin >> n; vector<int> a(2*n); for (int i = 0; i < 2*n; i++) cin >> a[i]; sort(a.begin(), a.end()); cout << (a[n] - a[n-1]) << '\n'; } }
cpp
1299
B
B. Aerodynamictime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGuy-Manuel and Thomas are going to build a polygon spaceship. You're given a strictly convex (i. e. no three points are collinear) polygon PP which is defined by coordinates of its vertices. Define P(x,y)P...
[ "geometry" ]
#include <iostream> #include <vector> int n; std::vector<int> x, y; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> n; if (n % 2 == 1) { std::cout << "NO\n"; return 0; } x.resize(n); y.resize(n); for (int i = 0; i < n; ++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" ]
#include <bits/stdc++.h> using namespace std; using ll=long long; bool isPrime(ll n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (ll i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) ...
cpp
1292
C
C. Xenon's Attack on the Gangstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputINSPION FullBand Master - INSPION INSPION - IOLITE-SUNSTONEOn another floor of the A.R.C. Markland-N; the young man Simon "Xenon" Jackson, takes a break after finishing his project early (...
[ "combinatorics", "dfs and similar", "dp", "greedy", "trees" ]
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(),(x).end() #define rep(i,n) for(int i=0;i<(n);i++) #define rep3(i,m,n) for(int i=(m);i<(n);i++) #define foreach(x,a) for(auto& (x) : (a) ) #define endl '\n' #define dump(x) cout << #x << " = " << (x) << endl; #define YES(n) cout << ((n) ...
cpp
1304
E
E. 1-Trees and Queriestime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputGildong was hiking a mountain; walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?Then...
[ "data structures", "dfs and similar", "shortest paths", "trees" ]
#include <bits/stdc++.h> using namespace std; #define int int64_t int n; const int LOG = 20; const int N = 1e5 + 10; vector<int> g[N]; int depth[N]; int LCA[N][LOG]; void dfs(int node, int parent) { depth[node] = depth[parent] + 1; LCA[node][0] = parent; for(int child : g[node]) { i...
cpp