contest_id
stringclasses
33 values
problem_id
stringclasses
14 values
statement
stringclasses
181 values
tags
sequencelengths
1
8
code
stringlengths
21
64.5k
language
stringclasses
3 values
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 tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex vv is the last different from vv vertex on the path from the root to the vertex vv. The depth of the vertex vv is the length of the path from the root to the vertex vv. Children of vertex vv are all vertices for which vv is the parent. The binary tree is such a tree that no vertex has more than 22 children.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.The only line of each test case contains two integers nn and dd (2≤n,d≤50002≤n,d≤5000) — the number of vertices in the tree and the required sum of depths of all vertices.It is guaranteed that the sum of nn and the sum of dd both does not exceed 50005000 (∑n≤5000,∑d≤5000∑n≤5000,∑d≤5000).OutputFor each test case, print the answer.If it is impossible to construct such a tree, print "NO" (without quotes) in the first line. Otherwise, print "{YES}" in the first line. Then print n−1n−1 integers p2,p3,…,pnp2,p3,…,pn in the second line, where pipi is the parent of the vertex ii. Note that the sequence of parents you print should describe some binary tree.ExampleInputCopy3 5 7 10 19 10 18 OutputCopyYES 1 2 1 3 YES 1 2 3 3 9 9 2 1 6 NO NotePictures corresponding to the first and the second test cases of the example:
[ "brute force", "constructive algorithms", "trees" ]
// 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 #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair <int, int> PII; typedef pair <LL, int> PLI; typedef pair <int, LL> PIL; typedef pair <LL, LL> PLL; template <typename T> inline T gi() { T x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();} while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return f * x; } template <typename T> inline void chkmax(T &x, const T &y) {x = x > y ? x : y;} template <typename T> inline void chkmin(T &x, const T &y) {x = x < y ? x : y;} const int N = 5003, M = N << 1, INF = 0x3f3f3f3f; int n, d; int cnt[N]; vector <int> vec[N]; inline void solve() { n = gi <int> (), d = gi <int> (); int cc = 0, mn = 0, mx = 1ll * n * (n - 1) / 2, mxd = 0; rep(i, 0, n) cnt[i] = 0, vec[i].clear(); rep(i, 0, 20) { cnt[i] = min(n - cc, 1 << i); mn += min(n - cc, 1 << i) * i; cc += min(n - cc, 1 << i); if (cc == n) {mxd = i; break;} } if (d < mn || d > mx) return (void)puts("NO"); puts("YES"); int dlt = d - mn, pp = mxd; per(i, mxd, 1) { int o = cnt[i]; rep(j, 1, o - 1) { --cnt[i]; int now = i; while (dlt && cnt[now]) ++now, --dlt; ++cnt[now], chkmax(pp, now); if (!dlt) break; } if (!dlt) break; } int oid = 0; rep(i, 0, pp) rep(j, 1, cnt[i]) vec[i].eb(++oid); // cout << i << ' ' << j << ' ' << cnt[i] << ' ' << oid << endl; rep(i, 1, pp) { int zz = 0, oc = 0; rep(j, 1, cnt[i]) { printf("%d ", vec[i - 1][zz]); // cout << "{ " << oc << ' ' << zz << "}\n"; if (oc == 0) ++oc; else oc = 0, ++zz; } } puts(""); } int main() { //freopen(".in", "r", stdin); freopen(".out", "w", stdout); DC solve(); return !!0; }
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 to fill the bag completely.For example, if n=10n=10 and a=[1,1,32]a=[1,1,32] then you have to divide the box of size 3232 into two parts of size 1616, and then divide the box of size 1616. So you can fill the bag with boxes of size 11, 11 and 88.Calculate the minimum number of divisions required to fill the bag of size nn.InputThe first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.The first line of each test case contains two integers nn and mm (1≤n≤1018,1≤m≤1051≤n≤1018,1≤m≤105) — the size of bag and the number of boxes, respectively.The second line of each test case contains mm integers a1,a2,…,ama1,a2,…,am (1≤ai≤1091≤ai≤109) — the sizes of boxes. It is guaranteed that each aiai is a power of two.It is also guaranteed that sum of all mm over all test cases does not exceed 105105.OutputFor each test case print one integer — the minimum number of divisions required to fill the bag of size nn (or −1−1, if it is impossible).ExampleInputCopy3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 OutputCopy2 -1 0
[ "bitmasks", "greedy" ]
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { long long m; cin >> m; int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long sum = accumulate(a.begin(), a.end(), 0LL); if (sum < m) { cout << -1 << '\n'; } else { int ans = 0; vector<int> bits; for (int bit = 0; bit < 63; bit++) { if ((1LL << bit) & m) { bits.push_back(bit); } } reverse(bits.begin(), bits.end()); sort(a.begin(), a.end()); reverse(a.begin(), a.end()); long long cur = 0; while ((int) a.size() > 0) { auto x = a.back(); a.pop_back(); cur += x; if (bits.empty()) { break; } if (cur >= (1LL << bits.back())) { if (cur & (1LL << bits.back())) { cur -= (1LL << bits.back()); bits.pop_back(); } else { cur -= x; int cValue = x; while (cValue > (1LL << bits.back())) { ans += 1; cValue /= 2; a.push_back(cValue); } assert(cValue == (1LL << bits.back())); bits.pop_back(); } } } assert(bits.empty()); cout << ans << '\n'; } } return 0; }
cpp
1311
C
C. Perform the Combotime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou want to perform the combo on your opponent in one popular fighting game. The combo is the string ss consisting of nn lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in ss. I.e. if s=s="abca" then you have to press 'a', then 'b', 'c' and 'a' again.You know that you will spend mm wrong tries to perform the combo and during the ii-th try you will make a mistake right after pipi-th button (1≤pi<n1≤pi<n) (i.e. you will press first pipi buttons right and start performing the combo from the beginning). It is guaranteed that during the m+1m+1-th try you press all buttons right and finally perform the combo.I.e. if s=s="abca", m=2m=2 and p=[1,3]p=[1,3] then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'.Your task is to calculate for each button (letter) the number of times you'll press it.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.Then tt test cases follow.The first line of each test case contains two integers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, 1≤m≤2⋅1051≤m≤2⋅105) — the length of ss and the number of tries correspondingly.The second line of each test case contains the string ss consisting of nn lowercase Latin letters.The third line of each test case contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pi<n1≤pi<n) — the number of characters pressed right during the ii-th try.It is guaranteed that the sum of nn and the sum of mm both does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105, ∑m≤2⋅105∑m≤2⋅105).It is guaranteed that the answer for each letter does not exceed 2⋅1092⋅109.OutputFor each test case, print the answer — 2626 integers: the number of times you press the button 'a', the number of times you press the button 'b', ……, the number of times you press the button 'z'.ExampleInputCopy3 4 2 abca 1 3 10 5 codeforces 2 8 3 2 9 26 10 qwertyuioplkjhgfdsazxcvbnm 20 10 1 2 3 5 10 5 9 4 OutputCopy4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2 NoteThe first test case is described in the problem statement. Wrong tries are "a"; "abc" and the final try is "abca". The number of times you press 'a' is 44, 'b' is 22 and 'c' is 22.In the second test case, there are five wrong tries: "co", "codeforc", "cod", "co", "codeforce" and the final try is "codeforces". The number of times you press 'c' is 99, 'd' is 44, 'e' is 55, 'f' is 33, 'o' is 99, 'r' is 33 and 's' is 11.
[ "brute force" ]
//#include<bits/stdc++.h> #include<algorithm> #include<cstdio> #include<sstream> #include<cstdlib> #include<cctype> #include<cmath> #include<set> #include<queue> #include<stack> #include<list> #include<iostream> #include<fstream> #include<numeric> #include<string> #include<vector> #include<cstring> #include<map> #include<iterator> #include<deque> #include<climits> #include<complex> #define EPS 1E-9 #define PI acos(-1) #define SQR(n) (n*n) using namespace std; #define M 1000000007 #define INF (1<<30)-1+(1<<30) #define fast ios::sync_with_stdio();cin.tie(nullptr) #define all(v) (v).begin(), (v).end() #define ll long long #define f0(i, n) for(int (i) = 0; i < (n); i++) #define f1(i, n) for(int (i) = 1; i <= (n); i++) #define fn0(i, n) for(int (i) = (n)-1;(i)>=0;i--) #define fn1(i, n) for(int (i) = (n);(i)>=1;i--) #define endl "\n" #define pb push_back #define UNIQUE(v) (v).erase(unique(all(v)), (v).end()) #define sz() size() #define UB upper_bound #define LB lower_bound #define pll pair<ll, ll> #define mxe(a,n) (*max_element(a,a+n)) #define mne(a,n) (*min_element(a,a+n)) #define countbit(x) __builtin_popcount(x) #define P printf //moves int dx[] = { -1,+0,+1,+0,-1,-1,+1,+1}; int dy[] = { +0,-1,+0,+1,+1,-1,+1,-1}; //bit int Set(int N,int pos){ return N=N | (1<<pos);} int Reset(int N,int pos){ return N= N & ~(1<<pos);} bool Check(int N,int pos){ return (bool)(N & (1<<pos));} template< class T, class X > inline T togglebit(T a, X i) { T t=1;return (a^(t<<i)); } //fn template <typename T> T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;} template <typename T> T ModInv (T b,T m){return BigMod(b,m-2,m);} template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1); else return SQR(POW(B,P/2));} template <typename T> T Dis(T x1,T y1,T x2, T y2){return sqrt( SQR(x1-x2) + SQR(y1-y2) );} template <typename T> T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));} template <typename T> T DIFF(T a,T b) { T d = a-b;if(d<0)return -d;else return d;} template <typename T> T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);} template <typename T> T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));} template <typename T> void pall(T a){for(int i=0;i<a.size();i++){cout<<a[i]<<" ";}cout<<endl;} template <typename T> void pall(T a[], T n){for(int i=0;i<n;i++){cout<<a[i]<<" ";}cout<<endl;} template <typename T> void yes(T a){if(a==1)printf("YES\n");else printf("Yes\n");} template <typename T> void no(T a){if(a==1)printf("NO\n");else printf("No\n");} string s, str; void solve(int tc){ ll n, m, x; cin >> n >> m; ll arr[n+1][26]; memset(arr, 0, sizeof(arr)); cin >> s; ll tot[26]; memset(tot, 0, sizeof(tot)); f0(i, n){ ll x = s[i] - 'a'; tot[x]++; f0(j, 26){ arr[i+1][j] = tot[j]; } } ll ans[26]; memset(ans, 0ll, sizeof(ans)); for(int i=0;i<m;i++){ cin >> x; for(int j=0;j<26;j++){ ans[j] += arr[x][j]; } } for(int i=0;i<n;i++){ ll x = s[i]-'a'; ans[x]++; } for(int i=0;i<26;i++){ cout << ans[i] <<" "; } cout << endl; } int main(){ int tc = 1, cas_no = 0; cin >> tc; while(tc--){cas_no++; solve(cas_no); } return 0; }
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 to quit programming after the national contest and he is going to become an actor (just like his father); so he should lose weight.In order to lose weight, Bashar is going to run for kk kilometers. Bashar is going to run in a place that looks like a grid of nn rows and mm columns. In this grid there are two one-way roads of one-kilometer length between each pair of adjacent by side cells, one road is going from the first cell to the second one, and the other road is going from the second cell to the first one. So, there are exactly (4nm−2n−2m)(4nm−2n−2m) roads.Let's take, for example, n=3n=3 and m=4m=4. In this case, there are 3434 roads. It is the picture of this case (arrows describe roads):Bashar wants to run by these rules: He starts at the top-left cell in the grid; In one move Bashar may go up (the symbol 'U'), down (the symbol 'D'), left (the symbol 'L') or right (the symbol 'R'). More formally, if he stands in the cell in the row ii and in the column jj, i.e. in the cell (i,j)(i,j) he will move to: in the case 'U' to the cell (i−1,j)(i−1,j); in the case 'D' to the cell (i+1,j)(i+1,j); in the case 'L' to the cell (i,j−1)(i,j−1); in the case 'R' to the cell (i,j+1)(i,j+1); He wants to run exactly kk kilometers, so he wants to make exactly kk moves; Bashar can finish in any cell of the grid; He can't go out of the grid so at any moment of the time he should be on some cell; Bashar doesn't want to get bored while running so he must not visit the same road twice. But he can visit the same cell any number of times. Bashar asks you if it is possible to run by such rules. If it is possible, you should tell him how should he run.You should give him aa steps to do and since Bashar can't remember too many steps, aa should not exceed 30003000. In every step, you should give him an integer ff and a string of moves ss of length at most 44 which means that he should repeat the moves in the string ss for ff times. He will perform the steps in the order you print them.For example, if the steps are 22 RUD, 33 UUL then the moves he is going to move are RUD ++ RUD ++ UUL ++ UUL ++ UUL == RUDRUDUULUULUUL.Can you help him and give him a correct sequence of moves such that the total distance he will run is equal to kk kilometers or say, that it is impossible?InputThe only line contains three integers nn, mm and kk (1≤n,m≤5001≤n,m≤500, 1≤k≤1091≤k≤109), which are the number of rows and the number of columns in the grid and the total distance Bashar wants to run.OutputIf there is no possible way to run kk kilometers, print "NO" (without quotes), otherwise print "YES" (without quotes) in the first line.If the answer is "YES", on the second line print an integer aa (1≤a≤30001≤a≤3000) — the number of steps, then print aa lines describing the steps.To describe a step, print an integer ff (1≤f≤1091≤f≤109) and a string of moves ss of length at most 44. Every character in ss should be 'U', 'D', 'L' or 'R'.Bashar will start from the top-left cell. Make sure to move exactly kk moves without visiting the same road twice and without going outside the grid. He can finish at any cell.We can show that if it is possible to run exactly kk kilometers, then it is possible to describe the path under such output constraints.ExamplesInputCopy3 3 4 OutputCopyYES 2 2 R 2 L InputCopy3 3 1000000000 OutputCopyNO InputCopy3 3 8 OutputCopyYES 3 2 R 2 D 1 LLRR InputCopy4 4 9 OutputCopyYES 1 3 RLD InputCopy3 4 16 OutputCopyYES 8 3 R 3 L 1 D 3 R 1 D 1 U 3 L 1 D NoteThe moves Bashar is going to move in the first example are: "RRLL".It is not possible to run 10000000001000000000 kilometers in the second example because the total length of the roads is smaller and Bashar can't run the same road twice.The moves Bashar is going to move in the third example are: "RRDDLLRR".The moves Bashar is going to move in the fifth example are: "RRRLLLDRRRDULLLD". It is the picture of his run (the roads on this way are marked with red and numbered in the order of his running):
[ "constructive algorithms", "graphs", "implementation" ]
#include <bits/stdc++.h> #define int long long using namespace std; int n, m, k, i, j; vector<pair<int, string>> ans; signed main() { cin.tie(0)->sync_with_stdio(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif cin >> n >> m >> k; if (k > 4 * n * m - 2 * n - 2 * m) { cout << "NO"; return 0; } ans.push_back({m - 1, "R"}); ans.push_back({n - 1, "D"}); ans.push_back({m - 1, "L"}); ans.push_back({n - 1, "U"}); for (i = 0; i < n - 2; i++) { ans.push_back({1, "D"}); ans.push_back({m - 1, "R"}); ans.push_back({m - 1, "L"}); } ans.push_back({1, "D"}); for (i = 0; i < m - 2; i++) { ans.push_back({1, "R"}); ans.push_back({n - 1, "U"}); ans.push_back({n - 1, "D"}); } ans.push_back({1, "R"}); ans.push_back({n - 1, "U"}); ans.push_back({m - 1, "L"}); for (i = 0; i < ans.size(); i++) if (!ans[i].first) ans.erase(ans.begin() + i); for (i = 0; i < ans.size(); i++) { if (k - ans[i].first <= 0) { ans[i].first = k; cout << "YES\n" << i + 1; for (j = 0; j <= i; j++) cout << "\n" << ans[j].first << " " << ans[j].second; return 0; } k -= ans[i].first; } cout << "YES\n" << ans.size(); for (j = 0; j < ans.size(); j++) cout << "\n" << ans[j].first << " " << ans[j].second; }
cpp
1312
A
A. Two Regular Polygonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm (m<nm<n). Consider a convex regular polygon of nn vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). Examples of convex regular polygons Your task is to say if it is possible to build another convex regular polygon with mm vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.The next tt lines describe test cases. Each test case is given as two space-separated integers nn and mm (3≤m<n≤1003≤m<n≤100) — the number of vertices in the initial polygon and the number of vertices in the polygon you want to build.OutputFor each test case, print the answer — "YES" (without quotes), if it is possible to build another convex regular polygon with mm vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon and "NO" otherwise.ExampleInputCopy2 6 3 7 3 OutputCopyYES NO Note The first test case of the example It can be shown that the answer for the second test case of the example is "NO".
[ "geometry", "greedy", "math", "number theory" ]
#include<bits/stdc++.h> #define ll long long using namespace std; double pi=3.1415926535897932384626433832795; int fuc(int n){ int sum=0; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; sum+=n%10; n/=10; return sum; } int func(int n){ int cnt; for(int i=2;i<=sqrt(n);i++){ if(n%i==0){ n/=i; return i; } } return n; } string toBinary(int n) { string r; while(n!=0) { if(n%2==0){ r+='0'; n/=2; } else { r+='1'; n/=2; } } return r; } ///__builtin_popcount(ar[i]^ar[b]); bool prime(int n){ for(int i=2;i<=sqrt(n);i++){ if(n%i==0){ return false; } } return true; } int func(int n,int m){ int ans=1e9; for(int i=1;i<=sqrt(n);i++){ if(n%i==0){ if(i<=m){ ans=min(ans,n/i); } if(n/i<=m){ ans=min(ans,i); } } } return ans; } /// long long ar[int(1e6)]; void solve () { int n,m; cin>>n>>m; if(n%m==0){ cout<< "YES\n"; } else{ cout << "NO\n"; } } int main () { //for(long long i=1;i<=sqrt(1e12);i++){ar[i]=1;} //for( long long i=2;i<=sqrt(1e12);i++){if(ar[i]!=0){ar[i]=i*i;for( long long j=i*i;j<=sqrt(1e12);j+=i){ar[j]=0;}}} int t; cin>>t; while(t--){ solve (); } }
cpp
1312
D
D. Count the Arraystime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYour task is to calculate the number of arrays such that: each array contains nn elements; each element is an integer from 11 to mm; for each array, there is exactly one pair of equal elements; for each array aa, there exists an index ii such that the array is strictly ascending before the ii-th element and strictly descending after it (formally, it means that aj<aj+1aj<aj+1, if j<ij<i, and aj>aj+1aj>aj+1, if j≥ij≥i). InputThe first line contains two integers nn and mm (2≤n≤m≤2⋅1052≤n≤m≤2⋅105).OutputPrint one integer — the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353998244353.ExamplesInputCopy3 4 OutputCopy6 InputCopy3 5 OutputCopy10 InputCopy42 1337 OutputCopy806066790 InputCopy100000 200000 OutputCopy707899035 NoteThe arrays in the first example are: [1,2,1][1,2,1]; [1,3,1][1,3,1]; [1,4,1][1,4,1]; [2,3,2][2,3,2]; [2,4,2][2,4,2]; [3,4,3][3,4,3].
[ "combinatorics", "math" ]
#include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // everything related to pbds // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // typedef int node; // change type here // typedef tree<node, null_type, less<node>, // rb_tree_tag, tree_order_statistics_node_update> // this is push_backds // ordered_set; using ll = long long; using vi = vector<long long>; using vpi = vector<pair<ll, ll>>; typedef long double ld; #define all(x) (x).begin(), (x).end() #define rall(a) (a).rbegin(), (a).rend() #define lb lower_bound #define ps(x, y) fixed << setprecision(y) << x #define int long long ll INF = 1e18; ll MOD = 998244353; int dx[] = {0, 0, -1, 1}; int dy[] = {-1, 1, 0, 0}; // useful when dealing with points ll maxx(vi &a) { return (*max_element(a.begin(), a.end())); } ll minn(vi &a) { return (*min_element(a.begin(), a.end())); } long long gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // Function to return LCM of two numbers long long lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } bool isPrime(ll n) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } long long mod(long long x) { return ((x % MOD + MOD) % MOD); } long long add(long long a, long long b) { return mod(mod(a) + mod(b)); } long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); } void input(vi &a) { int n = a.size(); for (int i = 0; i < n; i++) { cin >> a[i]; } } void print(vi &a) { for (auto x : a) { cout << x << " "; } cout << endl; } int stringToInt(string s) { stringstream geek(s); int x = 0; geek >> x; return x; } bool isPowerOfTwo(ll n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } bool isPalindrome(string s) { int n = s.size(); for (int i = 0; i < n; i++) { if (s[i] != s[n - i - 1]) { return false; } } return true; } string binaryTransformation(long long x) { if (x == 0) return ""; else { string s = binaryTransformation(x / 2); s.push_back(char('0' + x % 2)); return s; } } ll power(ll a, ll b, ll mod) { if (b == 0) { return 1; } ll ans = power(a, b / 2, mod); ans *= ans; ans %= mod; if (b % 2) { ans *= a; } return ans % mod; } double power(double a, int b) { if (b == 0) { return 1; } double t = power(a, b / 2); if (b & 1) { return t * t * a; } else { return t * t; } } int modularInverse(int number, int mod) { return power(number, mod - 2, mod); } // it should be 1LL and not 1 // write more tests // google if stuck // take a walk if stuck // precompute first const int N = 200005; int fact[N], invfact[N]; int pow(int a, int b, int m) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } int modinv(int k) { return pow(k, MOD - 2, MOD); } void precompute() { fact[0] = fact[1] = 1; for (int i = 2; i < N; i++) { fact[i] = fact[i - 1] * i; fact[i] %= MOD; } invfact[N - 1] = modinv(fact[N - 1]); for (int i = N - 2; i >= 0; i--) { invfact[i] = invfact[i + 1] * (i + 1); invfact[i] %= MOD; } } int nCr(int x, int y) { if (y > x) return 0; int num = fact[x]; num *= invfact[y]; num %= MOD; num *= invfact[x - y]; num %= MOD; return num; } void solve() { // learned a lot from this problem ,lol // relearned stars and bars // basically we have 2*m positions and n candidates for each position // this can be rewritten as x1 + x2+x3+...xn= 2*m, here xi>=0 // so when we place a bar it means that many positions for that number int n, m; cin >> n >> m; if (n == 2) { cout << 0 << endl; return; } int ans = mul(nCr(m, n - 1), n - 2); ans = mul(ans, power(2, n - 3, MOD)); cout << ans << endl; } // you should look down sometimes // https://github.com/Manjunath0408/CPSnippets int32_t main() { // freopen("pump.in", "r", stdin); // freopen("pump.out", "w", stdout); /* stuff you should look for * int overflow, array bounds * special cases (n==1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH * IF STUCK ON A QUESTION, MOVE TO THE NEXT ONE */ ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; precompute(); // cin >> t; for (int i = 1; i <= t; i++) { solve(); } return 0; }
cpp
1141
E
E. Superhero Battletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA superhero fights with a monster. The battle consists of rounds; each of which lasts exactly nn minutes. After a round ends, the next round starts immediately. This is repeated over and over again.Each round has the same scenario. It is described by a sequence of nn numbers: d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106). The ii-th element means that monster's hp (hit points) changes by the value didi during the ii-th minute of each round. Formally, if before the ii-th minute of a round the monster's hp is hh, then after the ii-th minute it changes to h:=h+dih:=h+di.The monster's initial hp is HH. It means that before the battle the monster has HH hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 00. Print -1 if the battle continues infinitely.InputThe first line contains two integers HH and nn (1≤H≤10121≤H≤1012, 1≤n≤2⋅1051≤n≤2⋅105). The second line contains the sequence of integers d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106), where didi is the value to change monster's hp in the ii-th minute of a round.OutputPrint -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer kk such that kk is the first minute after which the monster is dead.ExamplesInputCopy1000 6 -100 -200 -300 125 77 -4 OutputCopy9 InputCopy1000000000000 5 -1 0 0 0 0 OutputCopy4999999999996 InputCopy10 4 -3 -6 5 4 OutputCopy-1
[ "math" ]
#include <bits/stdc++.h> using namespace std; #define E 1e-9 #define PI 3.141592653589793238462 #define F first #define S second #define PB push_back #define EB emplace_back #define MP make_pair #define INF 1e18 #define MOD 1000000007 #define SZ(a) int((a).size()) #define setbits(a) (__builtin_popcountll(a)) #define right(a) (__builtin_ctzll(a)) #define left(a) (__builtin_clzll(a)) #define parity(a) (__builtin_parityll(a)) #define msb(a) (__lg(a)) #define lsb(a) ((ll)(log2(a & -a))) typedef std::numeric_limits<double> dbl; typedef long long ll; #define REP(i, a, b) for (ll i = a; i < b; i++) typedef vector<ll> vi; typedef pair<ll, ll> pi; typedef pair<ll, pi> pii; ll power(ll a, ll n) { ll ans = 1ll; while (n > 0) { int last_bit = (n & 1ll); if (last_bit) { ans = ans * a; } a = a * a; n = n >> 1ll; } return ans; } //*********************DSU*********************** #define MAXV 150001 vector<ll> Parent(MAXV), Rank(MAXV); void Init(int n) { REP(i, 1, n + 1) { Parent[i] = i; Rank[i] = 1; } } int Root(int x) { if (Parent[x] != x) Parent[x] = Root(Parent[x]); return Parent[x]; } void Union(int x, int y) { int rx = Root(x), ry = Root(y); if (rx == ry) return; if (Rank[rx] >= Rank[ry]) { Parent[ry] = rx; Rank[rx] += Rank[ry]; } else { Parent[rx] = ry; Rank[ry] += Rank[rx]; } } //**********************END************************* int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll h, n; cin >> h >> n; vi d(n); REP(i, 0, n) cin >> d[i]; vi prefix(n + 1, 0); prefix[1] = d[0]; REP(i, 1, n) prefix[i + 1] = d[i] + prefix[i]; ll mi = INF; ll mi_idx = -1; REP(i, 1, n + 1) { if (mi > prefix[i] && prefix[i] < 0) { mi = prefix[i]; mi_idx = i; } } if (mi_idx == (-1)) cout << mi_idx << '\n'; else { if (h <= (-1ll * mi)) { ll ans = mi_idx; for (ll j = mi_idx - 1; j > 0; j--) { if (h <= (-1ll * prefix[j])) { ans = j; } } cout << ans << '\n'; } else if (prefix[n] >= 0) cout << "-1\n"; else { h += mi; ll div = (h / abs(prefix[n])); ll rem = (h % abs(prefix[n])); ll ans = (div * 1ll * n) + mi_idx; if (rem == 0) { cout << ans << '\n'; } else { ll i = (mi_idx) % (n); while (rem > 0) { ans++; rem += d[i]; i = (i + 1) % (n); } cout << ans << '\n'; } } } return 0; }
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. Each vertex ii has an integer aiai written on it. We define the value of the simple path from vertex uu to vertex vv as follows: consider all vertices appearing on the path from uu to vv, write down all the numbers written on these vertices in the order they appear on the path, and compute the sum of prefix sums of the resulting sequence.Your task is to calculate the maximum value over all paths in the tree.InputThe first line contains one integer nn (2≤n≤1500002≤n≤150000) — the number of vertices in the tree.Then n−1n−1 lines follow, representing the edges of the tree. Each line contains two integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n, ui≠viui≠vi), denoting an edge between vertices uiui and vivi. It is guaranteed that these edges form a tree.The last line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤1061≤ai≤106).OutputPrint one integer — the maximum value over all paths in the tree.ExampleInputCopy4 4 2 3 2 4 1 1 3 3 7 OutputCopy36 NoteThe best path in the first example is from vertex 33 to vertex 11. It gives the sequence [3,3,7,1][3,3,7,1], and the sum of prefix sums is 3636.
[ "data structures", "divide and conquer", "geometry", "trees" ]
#include "bits/stdc++.h" signed main() { using namespace std; std::ios::sync_with_stdio(false); std::cin.tie(nullptr); #ifdef DEBUG freopen("Input.txt", "r", stdin); #endif static int n; std::cin >> n; static std::vector<std::vector<int>> g(n); for (int i = 0; i < n - 1; i++) { int u, v; std::cin >> u >> v; u--, v--; g[u].emplace_back(v); g[v].emplace_back(u); } static std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } static constexpr int64_t Inf = int64_t(1) << 60; static constexpr int64_t MaxVal = int64_t(1) << 40; struct line_t { int64_t a, b; int64_t f(int64_t x) { return a * x + b; } }; struct node_t { line_t line{0, -Inf}; node_t *left = nullptr, *right = nullptr; }; static std::vector<bool> deleted(n); static std::vector<int> siz(n); static std::vector<int64_t> pref(n); static int current_size, current_centroid; static node_t *root; static int64_t res = 0; struct function { static void update(node_t *&ptr, int64_t l, int64_t r, line_t line) { if (ptr == nullptr) { ptr = new node_t{line}; return; } int64_t m = (l + r) >> 1; bool left = line.f(l) < ptr -> line.f(l); bool mid = line.f(m) < ptr -> line.f(m); if (!mid) { std::swap(ptr -> line, line); } if (l + 1 == r) { return; } if (left ^ mid) { update(ptr -> left, l, m, line); } else { update(ptr -> right, m, r, line); } } static int64_t query(node_t *ptr, int64_t l, int64_t r, int64_t x) { if (ptr == nullptr) { return -Inf; } if (l + 1 == r) { return ptr -> line.f(x); } int64_t m = (l + r) >> 1; if (x < m) { return std::max(ptr -> line.f(x), query(ptr -> left, l, m, x)); } else { return std::max(ptr -> line.f(x), query(ptr -> right, m, r, x)); } } static void clear(node_t *ptr) { if (ptr -> left != nullptr) { clear(ptr -> left); } if (ptr -> right != nullptr) { clear(ptr -> right); } delete(ptr); } static void prepare(int u, int p) { siz[u] = 1; for (int v : g[u]) { if (!deleted[v] and v != p) { prepare(v, u); siz[u] += siz[v]; } } } static int find_centroid(int u, int p) { for (int v : g[u]) { if (!deleted[v] and v != p and siz[v] > current_size / 2) { return find_centroid(v, u); } } return u; } static void dfs(int u, int p, int64_t sum) { pref[u] += a[u]; res = std::max(res, (sum += pref[u])); for (int v : g[u]) { if (!deleted[v] and v != p) { pref[v] = pref[u]; dfs(v, u, sum); } } } static void modify(int u, int p, int64_t sum, int h) { update(root, 0, MaxVal, line_t{h, sum}); for (int v : g[u]) { if (!deleted[v] and v != p) { modify(v, u, sum + pref[v] - a[current_centroid], h + 1); } } } static void set_result(int u, int p, int64_t sum, int h) { res = std::max(res, sum + query(root, 0, MaxVal, pref[u])); for (int v : g[u]) { if (!deleted[v] and v != p) { set_result(v, u, sum + int64_t(h + 2) * a[v], h + 1); } } } static void decomposite(int u) { prepare(u, -1), current_size = siz[u]; current_centroid = u = find_centroid(u, -1); pref[u] = 0, dfs(u, -1, 0); root = new node_t{line_t{0, 0}}; for (int v : g[u]) { if (!deleted[v]) { set_result(v, u, a[u] + a[v] * 2, 1); modify(v, u, a[v], 1); } } std::reverse(g[u].begin(), g[u].end()); clear(root), root = new node_t{line_t{0, 0}}; for (int v : g[u]) { if (!deleted[v]) { set_result(v, u, a[u] + a[v] * 2, 1); modify(v, u, a[v], 1); } } clear(root); deleted[u] = true; for (int v : g[u]) { if (!deleted[v]) { decomposite(v); } } } }; function::decomposite(0); std::cout << res << "\n"; return 0; }
cpp
1307
A
A. Cow and Haybalestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of nn haybale piles on the farm. The ii-th pile contains aiai haybales. However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices ii and jj (1≤i,j≤n1≤i,j≤n) such that |i−j|=1|i−j|=1 and ai>0ai>0 and apply ai=ai−1ai=ai−1, aj=aj+1aj=aj+1. She may also decide to not do anything on some days because she is lazy.Bessie wants to maximize the number of haybales in pile 11 (i.e. to maximize a1a1), and she only has dd days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile 11 if she acts optimally!InputThe input consists of multiple test cases. The first line contains an integer tt (1≤t≤1001≤t≤100)  — the number of test cases. Next 2t2t lines contain a description of test cases  — two lines per test case.The first line of each test case contains integers nn and dd (1≤n,d≤1001≤n,d≤100) — the number of haybale piles and the number of days, respectively. The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1000≤ai≤100)  — the number of haybales in each pile.OutputFor each test case, output one integer: the maximum number of haybales that may be in pile 11 after dd days if Bessie acts optimally.ExampleInputCopy3 4 5 1 0 3 2 2 2 100 1 1 8 0 OutputCopy3 101 0 NoteIn the first test case of the sample; this is one possible way Bessie can end up with 33 haybales in pile 11: On day one, move a haybale from pile 33 to pile 22 On day two, move a haybale from pile 33 to pile 22 On day three, move a haybale from pile 22 to pile 11 On day four, move a haybale from pile 22 to pile 11 On day five, do nothing In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile 22 to pile 11 on the second day.
[ "greedy", "implementation" ]
#include "bits/stdc++.h" #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>; #ifdef LOCAL #include "debug.h" #endif const int64_t mod = (1 > 0 ? 998244353 : 1e9 + 7); const int inf32 = 0x3f3f3f3f; const int64_t inf64 = (int64_t) 4e18; #define fi first #define se second int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, d; cin >> n >> d; vector <int> a(n); for (int i = 0;i < n;i++) { cin >> a[i]; } for (int i = 1;i < n;i++) { int add = min(d / i, a[i]); d -= add * i; a[i] -= add; a[0] += add; } cout << a[0] << '\n'; } }
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 following operation to achieve this: append any subsequence of ss at the end of string zz. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if z=acz=ac, s=abcdes=abcde, you may turn zz into following strings in one operation: z=acacez=acace (if we choose subsequence aceace); z=acbcdz=acbcd (if we choose subsequence bcdbcd); z=acbcez=acbce (if we choose subsequence bcebce). Note that after this operation string ss doesn't change.Calculate the minimum number of such operations to turn string zz into string tt. InputThe first line contains the integer TT (1≤T≤1001≤T≤100) — the number of test cases.The first line of each testcase contains one string ss (1≤|s|≤1051≤|s|≤105) consisting of lowercase Latin letters.The second line of each testcase contains one string tt (1≤|t|≤1051≤|t|≤105) consisting of lowercase Latin letters.It is guaranteed that the total length of all strings ss and tt in the input does not exceed 2⋅1052⋅105.OutputFor each testcase, print one integer — the minimum number of operations to turn string zz into string tt. If it's impossible print −1−1.ExampleInputCopy3 aabce ace abacaba aax ty yyt OutputCopy1 -1 3
[ "dp", "greedy", "strings" ]
#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(0); cin.tie(0); typedef long long int ll; const int N = 1e5+9; void solve() { string s1, s2; cin >> s1 >> s2; map<char, vector<int>> mp; for(int i=0;i<s1.size();i++) { mp[s1[i]].push_back(i+1); } int cnt = 1, val = 1; for(int i=0;i<s2.size();i++) { if(mp.find(s2[i]) == mp.end()) { cout << -1 << "\n"; return; } else { auto pos = lower_bound(mp[s2[i]].begin(), mp[s2[i]].end(), val); if(pos == mp[s2[i]].end()) { cnt++; val = *lower_bound(mp[s2[i]].begin(), mp[s2[i]].end(), 0)+1; } else { val = *pos+1; } //cout << val << "\n"; } } cout << cnt << "\n"; } int main() { FAST; int t = 1, cs = 1; cin >> t; while(t--) { //cout << "Case " << cs++ << ":\n"; solve(); } 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 any nonnegative numbers xx and yy value of f(x,y)f(x,y) is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array [a1,a2,…,an][a1,a2,…,an] is defined as f(f(…f(f(a1,a2),a3),…an−1),an)f(f(…f(f(a1,a2),a3),…an−1),an) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?InputThe first line contains a single integer nn (1≤n≤1051≤n≤105).The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109). Elements of the array are not guaranteed to be different.OutputOutput nn integers, the reordering of the array with maximum value. If there are multiple answers, print any.ExamplesInputCopy4 4 0 11 6 OutputCopy11 6 4 0InputCopy1 13 OutputCopy13 NoteIn the first testcase; value of the array [11,6,4,0][11,6,4,0] is f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9.[11,4,0,6][11,4,0,6] is also a valid answer.
[ "brute force", "greedy", "math" ]
#include <bits/stdc++.h> using namespace std; #define pb push_back //typedef __int128 lll; #define ll long long #define all(x) x.begin(),x.end() #define repp(n) for(int i=0;i<n;i++) const ll MOD=998244353; const ll mod = 1e9 +7; map<ll,ll> mp; void f(ll x){ ll bit = 0; while(x>0){ if(x&1){ mp[bit]++; } x>>=1; bit++; } } void solve(){ ll n; cin>>n; ll a[n]; repp(n)cin>>a[i]; repp(n){ f(a[i]); } ll ans = 0; ll id= 0; for(int i=0;i<n;i++){ ll temp = a[i]; ll cnt = 0; map<ll,ll> ok; ok = mp; ll bit = 0; while(temp>0){ if(temp&1)ok[bit]--; if(temp&1 && ok[bit]==0){ cnt+= pow(2,bit); } temp>>=1; bit++; } if(cnt>ans){ ans= cnt; id = i; } } swap(a[id],a[0]); repp(n)cout<<a[i]<<" "; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); // int t; // cin>>t; // while(t--) solve(); return 0; }
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.InputThe first line contains the single integer TT (1≤T≤501≤T≤50) — the number of test cases.Next TT lines contain test cases — one per line. Each line contains two integers aa and mm (1≤a<m≤10101≤a<m≤1010).OutputPrint TT integers — one per test case. For each test case print the number of appropriate xx-s.ExampleInputCopy3 4 9 5 10 42 9999999967 OutputCopy6 1 9999999966 NoteIn the first test case appropriate xx-s are [0,1,3,4,6,7][0,1,3,4,6,7].In the second test case the only appropriate xx is 00.
[ "math", "number theory" ]
# include<bits/stdc++.h> using namespace std; typedef long long int ll; int main () { ll t; cin>>t; while(t--) { ll a,n; cin>>a>>n; n/=__gcd(a,n); ll ans=n; for(ll i=2;i*i<=n;i++) { if(n%i==0) { while(n%i==0) { n/=i; } ans=ans/i*(i-1); } } if(n>1) { ans=ans/n*(n-1); } cout<<ans<<endl; } return 0; }
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 lectures, the speaker specified two time intervals [sai,eai][sai,eai] (sai≤eaisai≤eai) and [sbi,ebi][sbi,ebi] (sbi≤ebisbi≤ebi). If the conference is situated in venue aa, the lecture will be held from saisai to eaieai, and if the conference is situated in venue bb, the lecture will be held from sbisbi to ebiebi. Hyunuk will choose one of these venues and all lectures will be held at that venue.Two lectures are said to overlap if they share any point in time in common. Formally, a lecture held in interval [x,y][x,y] overlaps with a lecture held in interval [u,v][u,v] if and only if max(x,u)≤min(y,v)max(x,u)≤min(y,v).We say that a participant can attend a subset ss of the lectures if the lectures in ss do not pairwise overlap (i.e. no two lectures overlap). Note that the possibility of attending may depend on whether Hyunuk selected venue aa or venue bb to hold the conference.A subset of lectures ss is said to be venue-sensitive if, for one of the venues, the participant can attend ss, but for the other venue, the participant cannot attend ss.A venue-sensitive set is problematic for a participant who is interested in attending the lectures in ss because the participant cannot be sure whether the lecture times will overlap. Hyunuk will be happy if and only if there are no venue-sensitive sets. Determine whether Hyunuk will be happy.InputThe first line contains an integer nn (1≤n≤1000001≤n≤100000), the number of lectures held in the conference.Each of the next nn lines contains four integers saisai, eaieai, sbisbi, ebiebi (1≤sai,eai,sbi,ebi≤1091≤sai,eai,sbi,ebi≤109, sai≤eai,sbi≤ebisai≤eai,sbi≤ebi).OutputPrint "YES" if Hyunuk will be happy. Print "NO" otherwise.You can print each letter in any case (upper or lower).ExamplesInputCopy2 1 2 3 6 3 4 7 8 OutputCopyYES InputCopy3 1 3 2 4 4 5 6 7 3 4 5 5 OutputCopyNO InputCopy6 1 5 2 9 2 4 5 8 3 6 7 11 7 10 12 16 8 11 13 17 9 12 14 18 OutputCopyYES NoteIn second example; lecture set {1,3}{1,3} is venue-sensitive. Because participant can't attend this lectures in venue aa, but can attend in venue bb.In first and third example, venue-sensitive set does not exist.
[ "binary search", "data structures", "hashing", "sortings" ]
#include<bits/stdc++.h> using namespace std; const int N=2e5+10; using ull=unsigned long long; ull S[N],val[N]; const ull h1=1237123,h2=19260817; ull H(ull x) {return x*x*x*h1+h2;}ull f(ull x){return H(x&((1<<31)-1))+H(x>>31);} void solve() { int n;scanf("%d",&n); vector<array<int,3>>a1,a2; for(int i=1;i<=n;++i) { val[i]=f(i); int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d); a1.push_back({a,0,i});a1.push_back({b,1,i}); a2.push_back({c,0,i});a2.push_back({d,1,i}); } sort(a1.begin(),a1.end());sort(a2.begin(),a2.end()); ull now=0; for(auto [pos,type,id]:a1)if(type)now^=val[id];else S[id]^=now; reverse(a1.begin(),a1.end());now=0; for(auto [pos,type,id]:a1)if(!type)now^=val[id];else S[id]^=now; now=0; for(auto [pos,type,id]:a2)if(type)now^=val[id];else S[id]^=now; reverse(a2.begin(),a2.end());now=0; for(auto [pos,type,id]:a2)if(!type)now^=val[id];else S[id]^=now; for(int i=1;i<=n;++i)if(S[i]){printf("NO\n");return ;} printf("YES\n"); } int main() { int T=1; //scanf("%d",&T); while(T--)solve(); }
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 impossible.InputEach test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100).The first line of each test case consists of one integer nn — the number of elements in the sequence bb (1≤n≤1001≤n≤100).The second line of each test case consists of nn different integers b1,…,bnb1,…,bn — elements of the sequence bb (1≤bi≤2n1≤bi≤2n).It is guaranteed that the sum of nn by all test cases doesn't exceed 100100.OutputFor each test case, if there is no appropriate permutation, print one number −1−1.Otherwise, print 2n2n integers a1,…,a2na1,…,a2n — required lexicographically minimal permutation of numbers from 11 to 2n2n.ExampleInputCopy5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 OutputCopy1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
[ "greedy" ]
#define RAGHAV_PATEL ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); #include <bits/stdc++.h> #include <algorithm> using namespace std; #define int long long int void solve() { int n; cin >> n; vector<int> v(n); vector<int> fv((2 * n) + 1); for (int i = 0; i < n; i++) { cin >> v[i]; fv[v[i]] = 1; } vector<int> pq; for (int i = 1; i <= 2 * n; i++) { if (fv[i] == 0) pq.push_back(i); } vector<int> ans; int flag = 0; for (int i = 0; i < n; i++) { ans.push_back(v[i]); int temp = v[i]; while (true) { int x = upper_bound(pq.begin(), pq.end(), temp) - pq.begin(); if (x == pq.size()) { cout << -1 << "\n"; return; } else if (fv[pq[x]] == 1) temp=pq[x]; else { ans.push_back(pq[x]); fv[pq[x]] = 1; break; } } } for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; cout << "\n"; } signed main() { RAGHAV_PATEL int t, i; cin >> t; // t = 1; while (t) { solve(); t--; } } /* int l = ans.size(); while(l>=0&&ans[l]==0) l--; */
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 other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection vv to another intersection uu is the path that starts in vv, ends in uu and has the minimum length among all such paths.Polycarp lives near the intersection ss and works in a building near the intersection tt. Every day he gets from ss to tt by car. Today he has chosen the following path to his workplace: p1p1, p2p2, ..., pkpk, where p1=sp1=s, pk=tpk=t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from ss to tt.Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection ss, the system chooses some shortest path from ss to tt and shows it to Polycarp. Let's denote the next intersection in the chosen path as vv. If Polycarp chooses to drive along the road from ss to vv, then the navigator shows him the same shortest path (obviously, starting from vv as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection ww instead, the navigator rebuilds the path: as soon as Polycarp arrives at ww, the navigation system chooses some shortest path from ww to tt and shows it to Polycarp. The same process continues until Polycarp arrives at tt: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1,2,3,4][1,2,3,4] (s=1s=1, t=4t=4): Check the picture by the link http://tk.codeforces.com/a.png When Polycarp starts at 11, the system chooses some shortest path from 11 to 44. There is only one such path, it is [1,5,4][1,5,4]; Polycarp chooses to drive to 22, which is not along the path chosen by the system. When Polycarp arrives at 22, the navigator rebuilds the path by choosing some shortest path from 22 to 44, for example, [2,6,4][2,6,4] (note that it could choose [2,3,4][2,3,4]); Polycarp chooses to drive to 33, which is not along the path chosen by the system. When Polycarp arrives at 33, the navigator rebuilds the path by choosing the only shortest path from 33 to 44, which is [3,4][3,4]; Polycarp arrives at 44 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 22 rebuilds in this scenario. Note that if the system chose [2,3,4][2,3,4] instead of [2,6,4][2,6,4] during the second step, there would be only 11 rebuild (since Polycarp goes along the path, so the system maintains the path [3,4][3,4] during the third step).The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?InputThe first line contains two integers nn and mm (2≤n≤m≤2⋅1052≤n≤m≤2⋅105) — the number of intersections and one-way roads in Bertown, respectively.Then mm lines follow, each describing a road. Each line contains two integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) denoting a road from intersection uu to intersection vv. All roads in Bertown are pairwise distinct, which means that each ordered pair (u,v)(u,v) appears at most once in these mm lines (but if there is a road (u,v)(u,v), the road (v,u)(v,u) can also appear).The following line contains one integer kk (2≤k≤n2≤k≤n) — the number of intersections in Polycarp's path from home to his workplace.The last line contains kk integers p1p1, p2p2, ..., pkpk (1≤pi≤n1≤pi≤n, all these integers are pairwise distinct) — the intersections along Polycarp's path in the order he arrived at them. p1p1 is the intersection where Polycarp lives (s=p1s=p1), and pkpk is the intersection where Polycarp's workplace is situated (t=pkt=pk). It is guaranteed that for every i∈[1,k−1]i∈[1,k−1] the road from pipi to pi+1pi+1 exists, so the path goes along the roads of Bertown. OutputPrint two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.ExamplesInputCopy6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 OutputCopy1 2 InputCopy7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 OutputCopy0 0 InputCopy8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 OutputCopy0 3
[ "dfs and similar", "graphs", "shortest paths" ]
#include <bits/stdc++.h> using namespace std; #define MAX (int) 2e5 #define INF INT_MAX int distanceTo[MAX]; map<int, vector<int>> bertown1; map<int, vector<int>> bertown2; void BFS(int source) { int parent; set<int> visited; queue<int> toVisit; toVisit.push(source); visited.insert(source); while(!toVisit.empty()) { parent = toVisit.front(); for(int child : bertown2[parent]) { if(visited.count(child) == 0) { distanceTo[child] = distanceTo[parent] + 1; toVisit.push(child); visited.insert(child); } } toVisit.pop(); } } int main() { int n; int m; int a; int b; int k; int p; cin >> n; cin >> m; for(int i = 0; i < m; i++) { cin >> a; cin >> b; a--; b--; bertown1[a].push_back(b); bertown2[b].push_back(a); } int minCount = 0; int maxCount = 0; vector<int> actions; cin >> k; for(int i = 0; i < k; i++) { cin >> p; actions.push_back(--p); } BFS(actions[k - 1]); int prev; int curr; for(int i = 1; i < k; i++) { prev = actions[i - 1]; curr = actions[i]; if(distanceTo[curr] >= distanceTo[prev]) { minCount++; maxCount++; } else { for(int child : bertown1[prev]) { if((child != curr) && (distanceTo[child] == distanceTo[curr])) { maxCount++; break; } } } } cout << minCount << " " << maxCount << "\n"; }
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 to quit programming after the national contest and he is going to become an actor (just like his father); so he should lose weight.In order to lose weight, Bashar is going to run for kk kilometers. Bashar is going to run in a place that looks like a grid of nn rows and mm columns. In this grid there are two one-way roads of one-kilometer length between each pair of adjacent by side cells, one road is going from the first cell to the second one, and the other road is going from the second cell to the first one. So, there are exactly (4nm−2n−2m)(4nm−2n−2m) roads.Let's take, for example, n=3n=3 and m=4m=4. In this case, there are 3434 roads. It is the picture of this case (arrows describe roads):Bashar wants to run by these rules: He starts at the top-left cell in the grid; In one move Bashar may go up (the symbol 'U'), down (the symbol 'D'), left (the symbol 'L') or right (the symbol 'R'). More formally, if he stands in the cell in the row ii and in the column jj, i.e. in the cell (i,j)(i,j) he will move to: in the case 'U' to the cell (i−1,j)(i−1,j); in the case 'D' to the cell (i+1,j)(i+1,j); in the case 'L' to the cell (i,j−1)(i,j−1); in the case 'R' to the cell (i,j+1)(i,j+1); He wants to run exactly kk kilometers, so he wants to make exactly kk moves; Bashar can finish in any cell of the grid; He can't go out of the grid so at any moment of the time he should be on some cell; Bashar doesn't want to get bored while running so he must not visit the same road twice. But he can visit the same cell any number of times. Bashar asks you if it is possible to run by such rules. If it is possible, you should tell him how should he run.You should give him aa steps to do and since Bashar can't remember too many steps, aa should not exceed 30003000. In every step, you should give him an integer ff and a string of moves ss of length at most 44 which means that he should repeat the moves in the string ss for ff times. He will perform the steps in the order you print them.For example, if the steps are 22 RUD, 33 UUL then the moves he is going to move are RUD ++ RUD ++ UUL ++ UUL ++ UUL == RUDRUDUULUULUUL.Can you help him and give him a correct sequence of moves such that the total distance he will run is equal to kk kilometers or say, that it is impossible?InputThe only line contains three integers nn, mm and kk (1≤n,m≤5001≤n,m≤500, 1≤k≤1091≤k≤109), which are the number of rows and the number of columns in the grid and the total distance Bashar wants to run.OutputIf there is no possible way to run kk kilometers, print "NO" (without quotes), otherwise print "YES" (without quotes) in the first line.If the answer is "YES", on the second line print an integer aa (1≤a≤30001≤a≤3000) — the number of steps, then print aa lines describing the steps.To describe a step, print an integer ff (1≤f≤1091≤f≤109) and a string of moves ss of length at most 44. Every character in ss should be 'U', 'D', 'L' or 'R'.Bashar will start from the top-left cell. Make sure to move exactly kk moves without visiting the same road twice and without going outside the grid. He can finish at any cell.We can show that if it is possible to run exactly kk kilometers, then it is possible to describe the path under such output constraints.ExamplesInputCopy3 3 4 OutputCopyYES 2 2 R 2 L InputCopy3 3 1000000000 OutputCopyNO InputCopy3 3 8 OutputCopyYES 3 2 R 2 D 1 LLRR InputCopy4 4 9 OutputCopyYES 1 3 RLD InputCopy3 4 16 OutputCopyYES 8 3 R 3 L 1 D 3 R 1 D 1 U 3 L 1 D NoteThe moves Bashar is going to move in the first example are: "RRLL".It is not possible to run 10000000001000000000 kilometers in the second example because the total length of the roads is smaller and Bashar can't run the same road twice.The moves Bashar is going to move in the third example are: "RRDDLLRR".The moves Bashar is going to move in the fifth example are: "RRRLLLDRRRDULLLD". It is the picture of his run (the roads on this way are marked with red and numbered in the order of his running):
[ "constructive algorithms", "graphs", "implementation" ]
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <random> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define AboTaha_on_da_code ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define X first #define Y second const int dx[8]={0, 0, 1, -1, 1, -1, -1, 1}, dy[8]={1, -1, 0, 0, 1, -1, 1, -1}; const int M = 1e9+7, M2 = 998244353; const double EPS = 1e-9; void burn(int tc) { int n, m, k; cin >> n >> m >> k; if (4*n*m-2*n-2*m < k) return void (cout << "NO"); cout << "YES\n"; vector <int> cnt; vector <string> path; int cur = 0; while(cur < n && k) { if (m > 1) { if (cur + 1 < n) { if (k < 3) { if (k == 2) cnt.push_back(1), path.emplace_back("RD"); else cnt.push_back(1), path.emplace_back("R"); k = 0; } else { if (k <= 3*m-3) { cnt.push_back(k/3), path.emplace_back("RDU"); k%=3; if (k == 2) cnt.push_back(1), path.emplace_back("RD"); else if (k == 1) cnt.push_back(1), path.emplace_back("R"); k = 0; } else cnt.push_back(m-1), path.emplace_back("RDU"), k-=3*m-3; } } else { if (k <= m-1) cnt.push_back(k), path.emplace_back("R"), k = 0; else cnt.push_back(m-1), path.emplace_back("R"), k-=m-1; } if (!k) continue; if (k <= m-1) cnt.push_back(k), path.emplace_back("L"), k = 0; else cnt.push_back(m-1), path.emplace_back("L"), k-=m-1; } if (!k) continue; if (cur+1 < n) cnt.push_back(1), path.emplace_back("D"), k--; else if (cur+1 == n) { if (k <= n-1) cnt.push_back(k), path.emplace_back("U"), k = 0; else cnt.push_back(n-1), path.emplace_back("U"), k-=n-1; } cur++; } assert(!k); cout << cnt.size() << '\n'; for (int i = 0; i < cnt.size(); i++) { cout << cnt[i] << ' ' << path[i] << '\n'; } } int main() { AboTaha_on_da_code // freopen("Ain.txt", "r", stdin); // freopen("Aout.txt", "w", stdout); int T = 1; // cin >> T; for (int i = 1; i <= T; i++) { // cout << "Case #" << i << ": "; burn(i); cout << '\n'; } return 0; }
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 all over the world is going to solve them.For each problem, the authors estimated the number of people who would solve it: for the ii-th problem, the number of accepted solutions will be between lili and riri, inclusive.The creator of ForceCoders uses different criteria to determine if the contest is good or bad. One of these criteria is the number of inversions in the problem order. An inversion is a pair of problems (x,y)(x,y) such that xx is located earlier in the contest (x<yx<y), but the number of accepted solutions for yy is strictly greater.Obviously, both the creator of ForceCoders and the authors of the contest want the contest to be good. Now they want to calculate the probability that there will be no inversions in the problem order, assuming that for each problem ii, any integral number of accepted solutions for it (between lili and riri) is equally probable, and all these numbers are independent.InputThe first line contains one integer nn (2≤n≤502≤n≤50) — the number of problems in the contest.Then nn lines follow, the ii-th line contains two integers lili and riri (0≤li≤ri≤9982443510≤li≤ri≤998244351) — the minimum and maximum number of accepted solutions for the ii-th problem, respectively.OutputThe probability that there will be no inversions in the contest can be expressed as an irreducible fraction xyxy, where yy is coprime with 998244353998244353. Print one integer — the value of xy−1xy−1, taken modulo 998244353998244353, where y−1y−1 is an integer such that yy−1≡1yy−1≡1 (mod(mod 998244353)998244353).ExamplesInputCopy3 1 2 1 2 1 2 OutputCopy499122177 InputCopy2 42 1337 13 420 OutputCopy578894053 InputCopy2 1 1 0 0 OutputCopy1 InputCopy2 1 1 1 1 OutputCopy1 NoteThe real answer in the first test is 1212.
[ "combinatorics", "dp", "probabilities" ]
#include <bits/stdc++.h> using namespace std; typedef long long int ll; # define mod 998244353 ll a[1000],b[1000],c[1000]; ll dp[1010][1010],g[1010],inv[1010]; ll qp(ll base, ll pow) { ll ans=1; while(pow) { if(pow&1) ans=ans*base%mod; pow>>=1; base=base*base%mod; } return ans; } int main() { for(int i=1;i<=1000;i++) { inv[i]=qp((ll)i,mod-2); } int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]>>b[i]; b[i]++; } int len=0; for(int i=1;i<=n;i++) { len++; c[len]=a[i]; len++; c[len]=b[i]; } sort(c+1,c+1+len); len=unique(c+1,c+1+len)-c-1; for(int i=1;i<=n;i++) { a[i]=lower_bound(c+1,c+1+len,a[i])-c; b[i]=lower_bound(c+1,c+1+len,b[i])-c; } for(int i=0;i<=1000;i++) { dp[0][i]=1; } for(int i=1;i<=n;i++) { for(int j=a[i];j<b[i];j++) { ll len1=c[j+1]-c[j]; //当前数字个数 g[1]=len1; for(int k=2;k<=i;k++) { g[k]=g[k-1]*(ll)(len1+k-1)%mod*inv[k]%mod; } for(int k=i-1;k>=0;k--) { if(!(j>=a[k+1]&&j+1<=b[k+1])) break; dp[i][j]=(dp[i][j]+dp[k][j+1]*g[i-k]%mod)%mod; } } for(int j=len;j>=1;j--) { dp[i][j]=(dp[i][j]+dp[i][j+1])%mod; } } ll ans=dp[n][1]; //cout<<ans<<endl; for(int i=1;i<=n;i++) { ans=(ans*qp(c[b[i]]-c[a[i]],mod-2)%mod)%mod; } cout<<ans; return 0; }
cpp
1312
C
C. Adding Powerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSuppose you are performing the following algorithm. There is an array v1,v2,…,vnv1,v2,…,vn filled with zeroes at start. The following operation is applied to the array several times — at ii-th step (00-indexed) you can: either choose position pospos (1≤pos≤n1≤pos≤n) and increase vposvpos by kiki; or not choose any position and skip this step. You can choose how the algorithm would behave on each step and when to stop it. The question is: can you make array vv equal to the given array aa (vj=ajvj=aj for each jj) after some step?InputThe first line contains one integer TT (1≤T≤10001≤T≤1000) — the number of test cases. Next 2T2T lines contain test cases — two lines per test case.The first line of each test case contains two integers nn and kk (1≤n≤301≤n≤30, 2≤k≤1002≤k≤100) — the size of arrays vv and aa and value kk used in the algorithm.The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10160≤ai≤1016) — the array you'd like to achieve.OutputFor each test case print YES (case insensitive) if you can achieve the array aa after some step or NO (case insensitive) otherwise.ExampleInputCopy5 4 100 0 0 0 0 1 2 1 3 4 1 4 1 3 2 0 1 3 3 9 0 59049 810 OutputCopyYES YES NO NO YES NoteIn the first test case; you can stop the algorithm before the 00-th step, or don't choose any position several times and stop the algorithm.In the second test case, you can add k0k0 to v1v1 and stop the algorithm.In the third test case, you can't make two 11 in the array vv.In the fifth test case, you can skip 9090 and 9191, then add 9292 and 9393 to v3v3, skip 9494 and finally, add 9595 to v2v2.
[ "bitmasks", "greedy", "implementation", "math", "number theory", "ternary search" ]
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define vl vector<ll> #define pl pair<ll, ll> #define vi vector<int> #define pi pair<int, int> #define ff first #define ss second #define pb push_back #define ppb pop_back #define mp make_pair #define all(x) x.begin(), x.end() #define fl(i, a, b) for (ll i = a; i <= b; i++) #define bfl(i, k, n) for (ll i = k; i > n; i--) #define prDouble(d) cout << fixed << setprecision(10) << d #define deb(x) cerr << #x << " = " << x << " " #define endl "\n" #define int long long #define inf 1e18 const int mod = 1000000007; double epsilon = 1e-6; /*-----------------------------------------------------------------------------------------*/ void init_code() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE; } int modularExponentiation(int x,int n,int M){ int result=1; while(n>0){ if(n % 2 ==1) result=(result * x)%M; x=(x*x)%M; n=n/2; } return result; } int modInverse(int A,int M){ return modularExponentiation(A,M-2,M); } void solve() { int n,k; cin>>n>>k; int arr[n]; for(int i=0;i<n;i++) cin>>arr[i]; set<int> st; for(int i=0;i<n;i++){ if(arr[i]==0) continue; int val = arr[i]; int idx = 0; while(val>0){ idx++; val/=k; } //idx++; deb(idx); val = arr[i]; while(val>0){ int den = pow(k,idx); if(den>val){idx--; continue;} int quo = val/den; //deb(den); if(quo==0) {idx--; continue;} if(quo>1 or st.find(idx)!=st.end()){cout<<"NO"<<endl; return;} st.insert(idx); val = val - den; idx--; //deb(val); } } cout<<"YES"<<endl; return; } signed main() { init_code(); auto start_time = std::chrono::high_resolution_clock::now(); int T; cin>>T; //T = 1; while (T--) { solve(); } auto stop_time = std::chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::microseconds>(stop_time - start_time); #ifndef ONLINE_JUDGE cerr << "Time: " << duration.count() / 1000. << " ms" << endl; #endif return 0; }
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. Each user can reach every other users by using some sequence of direct connections. From now on, we will denote this primary network as T1T1.To prevent a possible server breakdown, Donghyun created a backup network T2T2, which also connects the same nn users via a tree graph. If a system breaks down, exactly one edge e∈T1e∈T1 becomes unusable. In this case, Donghyun will protect the edge ee by picking another edge f∈T2f∈T2, and add it to the existing network. This new edge should make the network be connected again. Donghyun wants to assign a replacement edge f∈T2f∈T2 for as many edges e∈T1e∈T1 as possible. However, since the backup network T2T2 is fragile, f∈T2f∈T2 can be assigned as the replacement edge for at most one edge in T1T1. With this restriction, Donghyun wants to protect as many edges in T1T1 as possible.Formally, let E(T)E(T) be an edge set of the tree TT. We consider a bipartite graph with two parts E(T1)E(T1) and E(T2)E(T2). For e∈E(T1),f∈E(T2)e∈E(T1),f∈E(T2), there is an edge connecting {e,f}{e,f} if and only if graph T1−{e}+{f}T1−{e}+{f} is a tree. You should find a maximum matching in this bipartite graph.InputThe first line contains an integer nn (2≤n≤2500002≤n≤250000), the number of users. In the next n−1n−1 lines, two integers aiai, bibi (1≤ai,bi≤n1≤ai,bi≤n) are given. Those two numbers denote the indices of the vertices connected by the corresponding edge in T1T1.In the next n−1n−1 lines, two integers cici, didi (1≤ci,di≤n1≤ci,di≤n) are given. Those two numbers denote the indices of the vertices connected by the corresponding edge in T2T2. It is guaranteed that both edge sets form a tree of size nn.OutputIn the first line, print the number mm (0≤m<n0≤m<n), the maximum number of edges that can be protected.In the next mm lines, print four integers ai,bi,ci,diai,bi,ci,di. Those four numbers denote that the edge (ai,bi)(ai,bi) in T1T1 is will be replaced with an edge (ci,di)(ci,di) in T2T2.All printed edges should belong to their respective network, and they should link to distinct edges in their respective network. If one removes an edge (ai,bi)(ai,bi) from T1T1 and adds edge (ci,di)(ci,di) from T2T2, the network should remain connected. The order of printing the edges or the order of vertices in each edge does not matter.If there are several solutions, you can print any.ExamplesInputCopy4 1 2 2 3 4 3 1 3 2 4 1 4 OutputCopy3 3 2 4 2 2 1 1 3 4 3 1 4 InputCopy5 1 2 2 4 3 4 4 5 1 2 1 3 1 4 1 5 OutputCopy4 2 1 1 2 3 4 1 3 4 2 1 4 5 4 1 5 InputCopy9 7 9 2 8 2 1 7 5 4 7 2 4 9 6 3 9 1 8 4 8 2 9 9 5 7 6 1 3 4 6 5 3 OutputCopy8 4 2 9 2 9 7 6 7 5 7 5 9 6 9 4 6 8 2 8 4 3 9 3 5 2 1 1 8 7 4 1 3
[ "data structures", "graph matchings", "graphs", "math", "trees" ]
#include<bits/stdc++.h> using namespace std; #define ll long long #define N 250100 ll n; ll du[N]; queue<ll>q; ll v[N<<1],fir[N],nxt[N<<1],cnt=0; inline void add(ll x,ll y){ v[++cnt]=y;nxt[cnt]=fir[x];fir[x]=cnt; return ; } bool vis[N]; ll fa[N]; inline ll getf(ll x){ if(fa[x]==x)return x; return fa[x]=getf(fa[x]); } ll faa[19][N],dep[N]; inline void dfs(ll x,ll ff){ faa[0][x]=ff;dep[x]=dep[ff]+1; for(int i=1;i<=18;i++)faa[i][x]=faa[i-1][faa[i-1][x]]; for(int i=fir[x];i;i=nxt[i]){ ll vi=v[i];if(vi==ff)continue; dfs(vi,x); }return ; } inline ll lca(ll x,ll y){ if(dep[x]<dep[y])swap(x,y); for(int i=18;i>=0;i--)if(dep[faa[i][x]]>=dep[y])x=faa[i][x]; if(x==y)return x; for(int i=18;i>=0;i--)if(faa[i][x]!=faa[i][y])x=faa[i][x],y=faa[i][y]; return faa[0][x]; } inline ll gt(ll x,ll y){ ll nw=x; for(int i=0;i<=18;i++){ if(y&(1<<i))nw=faa[i][nw]; }return nw; } inline void del(ll x,ll y){ ll o=lca(x,y); if(getf(o)==x){ ll p=dep[y]-dep[o]; ll l=0,r=p,mid,an; while(l<=r){ mid=(l+r)>>1; if(getf(gt(y,mid))==x)an=mid,r=mid-1; else l=mid+1; }ll g=gt(y,an),u=gt(y,an-1); cout<<g<<' '<<u;fa[x]=u; }else{ ll nw=x; for(int i=18;i>=0;i--){ if(getf(faa[i][nw])==x)nw=faa[i][nw]; }cout<<nw<<' '<<faa[0][nw]; ll g=getf(faa[0][nw]); fa[x]=g; } cout<<' '<<x<<' '<<y<<'\n' ; return ; } vector<ll>e[N]; int main() { // freopen("test1.in","r",stdin); //freopen(".in","r",stdin); //freopen("test1.out","w",stdout); ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n;for(int i=1;i<=n;i++)fa[i]=i;cout<<n-1<<'\n'; for(int i=1;i<n;i++){ ll u,v;cin>>u>>v;add(u,v);add(v,u); }for(int i=1;i<n;i++){ ll u,v;cin>>u>>v;du[u]++;du[v]++;e[u].push_back(v);e[v].push_back(u); }for(int i=1;i<=n;i++)if(du[i]==1)q.push(i); dfs(1,0); while(!q.empty()){ ll o=q.front();q.pop();vis[o]=1; for(auto vi:e[o]){ if(vis[vi])continue; del(o,vi); du[vi]--;if(du[vi]==1)q.push(vi); } } return 0; }
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×1 (i.e. the height of this figure is 22 blocks and the width of this figure is 11 block). Note that you cannot rotate these figures.Your task is to say if you can clear the whole field by placing such figures.More formally, the problem can be described like this:The following process occurs while at least one aiai is greater than 00: You place one figure 2×12×1 (choose some ii from 11 to nn and replace aiai with ai+2ai+2); then, while all aiai are greater than zero, replace each aiai with ai−1ai−1. And your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.The next 2t2t lines describe test cases. The first line of the test case contains one integer nn (1≤n≤1001≤n≤100) — the number of columns in the Tetris field. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100), where aiai is the initial height of the ii-th column of the Tetris field.OutputFor each test case, print the answer — "YES" (without quotes) if you can clear the whole Tetris field and "NO" otherwise.ExampleInputCopy4 3 1 1 3 4 1 1 2 1 2 11 11 1 100 OutputCopyYES NO YES YES NoteThe first test case of the example field is shown below:Gray lines are bounds of the Tetris field. Note that the field has no upper bound.One of the correct answers is to first place the figure in the first column. Then after the second step of the process; the field becomes [2,0,2][2,0,2]. Then place the figure in the second column and after the second step of the process, the field becomes [0,0,0][0,0,0].And the second test case of the example field is shown below:It can be shown that you cannot do anything to end the process.In the third test case of the example, you first place the figure in the second column after the second step of the process, the field becomes [0,2][0,2]. Then place the figure in the first column and after the second step of the process, the field becomes [0,0][0,0].In the fourth test case of the example, place the figure in the first column, then the field becomes [102][102] after the first step of the process, and then the field becomes [0][0] after the second step of the process.
[ "implementation", "number theory" ]
#include <iostream> #include <ranges> #include <algorithm> #include <numeric> #include <vector> #include <array> #include <set> #include <map> #include <bit> #include <sstream> #include <list> #include <stack> #include <queue> typedef long long integerType; typedef integerType z; typedef std::vector<integerType> v; typedef std::vector<v> V; typedef std::set<integerType> s; typedef std::multiset<integerType> S; typedef std::string r; typedef std::vector<r> R; typedef std::vector<bool> b; struct RHEXAOCrocks { RHEXAOCrocks() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); } template<class T> operator T() const { T n; std::cin >> n; return n; } std::string operator()() { std::string s; std::cin >> s; return s; } v operator()(integerType n) { v vec(n); for (auto& n : vec) std::cin >> n; return vec; } R operator()(integerType n, char del) { R vec(n); char ch; std::cin >> ch; vec[0].push_back(ch); int c; for (integerType i{}; i < n;++i) while ((c = std::cin.get()) != del) vec[i].push_back(c); return vec; } V operator()(integerType r, integerType c) { V tab(r, v(c)); for (auto& e : tab) for (auto& n : e) std::cin >> n; return tab; } template<class T> RHEXAOCrocks& operator[](const T& t) { std::cout << t; return *this; } RHEXAOCrocks& operator[](const R& a) { for (auto& e : a) std::cout << e << '\n'; return *this; } template<class Ty, template<typename T, typename A = std::allocator<T>> class C> RHEXAOCrocks& operator[](const C<Ty>& c) { for (auto Cc{ c.begin() }; Cc != c.end(); std::cout << *Cc << " \n"[++Cc == c.end()]); return *this; } template<class Ty, template<typename T, typename A = std::allocator<T>> class C> RHEXAOCrocks& operator[](const std::vector<C<Ty>>& c) { for (auto& e : c) for (auto E{ e.begin() }; E != e.end(); )std::cout << *E << " \n"[++E == e.end()]; return *this; } }o; #define i(b,e, ...) for(integerType i : std::views::iota(b,e) __VA_ARGS__) #define _i(b,e, ...) for(integerType i : std::views::iota(b,e) | std::views::reverse __VA_ARGS__) #define j(b,e, ...) for(integerType j : std::views::iota(b,e) __VA_ARGS__) #define _j(b,e, ...) for(integerType j : std::views::iota(b,e) | std::views::reverse __VA_ARGS__) #define k(b,e, ...) for(integerType k : std::views::iota(b,e) __VA_ARGS__) #define _k(b,e, ...) for(integerType k : std::views::iota(b,e) | std::views::reverse __VA_ARGS__) #define l(b,e, ...) for(integerType l : std::views::iota(b,e) __VA_ARGS__) #define _l(b,e, ...) for(integerType l : std::views::iota(b,e) | std::views::reverse __VA_ARGS__) #define Aa for(auto A{ a.begin() }, a_end{ a.end() }; A != a_end; ++A) #define Bb for(auto B{ b.begin() }, b_end{ b.end() }; B != b_end; ++B) #define Cc for(auto C{ c.begin() }, c_end{ c.end() }; C != c_end; ++C) #define Dd for(auto D{ d.begin() }, d_end{ d.end() }; D != d_end; ++D) #define z(a) (integerType)((a).size()) #define ff(...) auto f = [&](__VA_ARGS__, const auto& f) #define f(...) f(__VA_ARGS__, f) #define gg(...) auto g = [&](__VA_ARGS__, const auto& g) #define g(...) g(__VA_ARGS__, g) #define hh(...) auto h = [&](__VA_ARGS__, const auto& h) #define h(...) h(__VA_ARGS__, h) using namespace std; namespace ra = ranges; inline void nwmn(integerType nw, integerType& mn) { mn -= ((nw) < mn) * (mn - (nw)); } inline void nwmx(integerType nw, integerType& mx) { mx += ((nw) > mx) * ((nw)-mx); } z power(z a, z b, z M) { z r{ 1 }; do { if (b & 1) (r *= a) %= M; (a *= a) %= M; } while (b >>= 1); return r; } #define w(...) if(__VA_ARGS__) #define _ else #define W(...) while(__VA_ARGS__) #define A(...) for(auto __VA_ARGS__) #define O while(true) void solve_test_case() { z n{ o }, s{}; v a{ o(n) }; i(0, n) s += a[i] & 1; o[s == n || s == 0 ? "YES\n" : "NO\n"]; } int main() { z t{ o }; while (t--) solve_test_case(); }
cpp
1299
C
C. Water Balancetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are nn water tanks in a row, ii-th of them contains aiai liters of water. The tanks are numbered from 11 to nn from left to right.You can perform the following operation: choose some subsegment [l,r][l,r] (1≤l≤r≤n1≤l≤r≤n), and redistribute water in tanks l,l+1,…,rl,l+1,…,r evenly. In other words, replace each of al,al+1,…,aral,al+1,…,ar by al+al+1+⋯+arr−l+1al+al+1+⋯+arr−l+1. For example, if for volumes [1,3,6,7][1,3,6,7] you choose l=2,r=3l=2,r=3, new volumes of water will be [1,4.5,4.5,7][1,4.5,4.5,7]. You can perform this operation any number of times.What is the lexicographically smallest sequence of volumes of water that you can achieve?As a reminder:A sequence aa is lexicographically smaller than a sequence bb of the same length if and only if the following holds: in the first (leftmost) position where aa and bb differ, the sequence aa has a smaller element than the corresponding element in bb.InputThe first line contains an integer nn (1≤n≤1061≤n≤106) — the number of water tanks.The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — initial volumes of water in the water tanks, in liters.Because of large input, reading input as doubles is not recommended.OutputPrint the lexicographically smallest sequence you can get. In the ii-th line print the final volume of water in the ii-th tank.Your answer is considered correct if the absolute or relative error of each aiai does not exceed 10−910−9.Formally, let your answer be a1,a2,…,ana1,a2,…,an, and the jury's answer be b1,b2,…,bnb1,b2,…,bn. Your answer is accepted if and only if |ai−bi|max(1,|bi|)≤10−9|ai−bi|max(1,|bi|)≤10−9 for each ii.ExamplesInputCopy4 7 5 5 7 OutputCopy5.666666667 5.666666667 5.666666667 7.000000000 InputCopy5 7 8 8 10 12 OutputCopy7.000000000 8.000000000 8.000000000 10.000000000 12.000000000 InputCopy10 3 9 5 5 1 7 5 3 8 7 OutputCopy3.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 5.000000000 7.500000000 7.500000000 NoteIn the first sample; you can get the sequence by applying the operation for subsegment [1,3][1,3].In the second sample, you can't get any lexicographically smaller sequence.
[ "data structures", "geometry", "greedy" ]
#include<bits/stdc++.h> using namespace std; const int o=1e6+3; int n,b[o],q; double a[o]; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cin>>n; for (int i=1,y,z;i<=n;i++){ double x; cin>>z,x=z,y=1; while (q&&a[q]/b[q]>x/y) x+=a[q],y+=b[q],q--; a[++q]=x,b[q]=y; } for (int i=1;i<=q;i++) for (int j=1;j<=b[i];j++) cout<<fixed<<setprecision(10)<<a[i]/b[i]<<"\n"; return 0; }
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 to bb hp.You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 00.The fight with a monster happens in turns. You hit the monster by aa hp. If it is dead after your hit, you gain one point and you both proceed to the next monster. Your opponent hits the monster by bb hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster. You have some secret technique to force your opponent to skip his turn. You can use this technique at most kk times in total (for example, if there are two monsters and k=4k=4, then you can use the technique 22 times on the first monster and 11 time on the second monster, but not 22 times on the first monster and 33 times on the second monster).Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.InputThe first line of the input contains four integers n,a,bn,a,b and kk (1≤n≤2⋅105,1≤a,b,k≤1091≤n≤2⋅105,1≤a,b,k≤109) — the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.The second line of the input contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109), where hihi is the health points of the ii-th monster.OutputPrint one integer — the maximum number of points you can gain if you use the secret technique optimally.ExamplesInputCopy6 2 3 3 7 10 50 12 1 8 OutputCopy5 InputCopy1 1 100 99 100 OutputCopy1 InputCopy7 4 2 1 1 3 5 4 2 7 6 OutputCopy6
[ "greedy", "sortings" ]
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define S second #define F first #define pb push_back using namespace std; typedef long long ll; const int N = 2e5 + 123; ll n, a, b, k; ll c[N]; int main() { cin >> n >> a >> b >> k; for(int i = 1; i <= n; i++) { cin >> c[i]; c[i] = c[i] % (a + b); if(c[i] == 0) c[i] = a + b; if(c[i] % a != 0) { c[i] = c[i] / a; } else { c[i] = c[i] / a - 1; } } ll ans = 0; sort(c + 1, c + n + 1); for(int i = 1; i <= n; i++) { if(k - c[i] < 0) break; ans++; k -= c[i]; } cout << ans; }
cpp
1312
G
G. Autocompletiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a set of strings SS. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions: if the current string is tt, choose some lowercase Latin letter cc and append it to the back of tt, so the current string becomes t+ct+c. This action takes 11 second; use autocompletion. When you try to autocomplete the current string tt, a list of all strings s∈Ss∈S such that tt is a prefix of ss is shown to you. This list includes tt itself, if tt is a string from SS, and the strings are ordered lexicographically. You can transform tt into the ii-th string from this list in ii seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type. What is the minimum number of seconds that you have to spend to type each string from SS?Note that the strings from SS are given in an unusual way.InputThe first line contains one integer nn (1≤n≤1061≤n≤106).Then nn lines follow, the ii-th line contains one integer pipi (0≤pi<i0≤pi<i) and one lowercase Latin character cici. These lines form some set of strings such that SS is its subset as follows: there are n+1n+1 strings, numbered from 00 to nn; the 00-th string is an empty string, and the ii-th string (i≥1i≥1) is the result of appending the character cici to the string pipi. It is guaranteed that all these strings are distinct.The next line contains one integer kk (1≤k≤n1≤k≤n) — the number of strings in SS.The last line contains kk integers a1a1, a2a2, ..., akak (1≤ai≤n1≤ai≤n, all aiai are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set SS — formally, if we denote the ii-th generated string as sisi, then S=sa1,sa2,…,sakS=sa1,sa2,…,sak.OutputPrint kk integers, the ii-th of them should be equal to the minimum number of seconds required to type the string saisai.ExamplesInputCopy10 0 i 1 q 2 g 0 k 1 e 5 r 4 m 5 h 3 p 3 e 5 8 9 1 10 6 OutputCopy2 4 1 3 3 InputCopy8 0 a 1 b 2 a 2 b 4 a 4 b 5 c 6 d 5 2 3 4 7 8 OutputCopy1 2 2 4 4 NoteIn the first example; SS consists of the following strings: ieh, iqgp, i, iqge, ier.
[ "data structures", "dfs and similar", "dp" ]
#include <bits/stdc++.h> using namespace std; int ch[1000005][26],dfnow,id[1000005],ans[1000005],dep[1000005]; multiset<int> s; void dfs(int u) { int nw=dep[u]-dfnow; if(id[u]) { ans[id[u]]=min(dep[u],*s.begin()+dfnow+1); dep[u]=min(dep[u],ans[id[u]]); nw=dep[u]-dfnow; ++dfnow; } s.insert(nw); for(int i=0;i<26;i++) if(ch[u][i]) dep[ch[u][i]]=dep[u]+1,dfs(ch[u][i]); s.erase(s.find(nw)); } signed main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i=1;i<=n;i++) { int fa; char c; cin >> fa >> c; ch[fa][c-'a']=i; } int m; cin >> m; for(int i=1;i<=m;i++) { int x; cin >> x; id[x]=i; } dfs(0); for(int i=1;i<=m;i++) cout << ans[i] << " "; return 0; }
cpp
1285
B
B. Just Eat It!time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputToday; Yasser and Adel are at the shop buying cupcakes. There are nn cupcake types, arranged from 11 to nn on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type ii is an integer aiai. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.On the other hand, Adel will choose some segment [l,r][l,r] (1≤l≤r≤n)(1≤l≤r≤n) that does not include all of cupcakes (he can't choose [l,r]=[1,n][l,r]=[1,n]) and buy exactly one cupcake of each of types l,l+1,…,rl,l+1,…,r.After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice.For example, let the tastinesses of the cupcakes be [7,4,−1][7,4,−1]. Yasser will buy all of them, the total tastiness will be 7+4−1=107+4−1=10. Adel can choose segments [7],[4],[−1],[7,4][7],[4],[−1],[7,4] or [4,−1][4,−1], their total tastinesses are 7,4,−1,117,4,−1,11 and 33, respectively. Adel can choose segment with tastiness 1111, and as 1010 is not strictly greater than 1111, Yasser won't be happy :(Find out if Yasser will be happy after visiting the shop.InputEach test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). The description of the test cases follows.The first line of each test case contains nn (2≤n≤1052≤n≤105).The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109), where aiai represents the tastiness of the ii-th type of cupcake.It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.OutputFor each test case, print "YES", if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel's choice. Otherwise, print "NO".ExampleInputCopy3 4 1 2 3 4 3 7 4 -1 3 5 -5 5 OutputCopyYES NO NO NoteIn the first example; the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.In the second example; Adel will choose the segment [1,2][1,2] with total tastiness 1111, which is not less than the total tastiness of all cupcakes, which is 1010.In the third example, Adel can choose the segment [3,3][3,3] with total tastiness of 55. Note that Yasser's cupcakes' total tastiness is also 55, so in that case, the total tastiness of Yasser's cupcakes isn't strictly greater than the total tastiness of Adel's cupcakes.
[ "dp", "greedy", "implementation" ]
#include<bits/stdc++.h> #define ll long long #define el '\n' using namespace std; void solve(){ int n; cin >> n; vector<ll>a(n + 1), pre(n + 1); for (int i = 1; i <= n; i++){ cin >> a[i]; } for (int i = 1; i <= n; i++){ pre[i] += pre[i - 1] + a[i]; } ll j = 0, x = -1, mx = INT_MIN; bool ok = false, okk = false; for (int i = 1; i <= n - 1; i++){ if (pre[i] < pre[i + 1] && pre[i] <= 0){ ok = true; j = i; } if (pre[i] > pre[i + 1] && pre[i + 1] <= 0){ ok = true; j = i + 1; } else if ((pre[i] < pre[i + 1])||(pre[i] > pre[i + 1] && pre[i + 1] > 0)){ if (ok) x = i + 1; else x = i; } if (x != -1){ okk = true; mx = max(mx, pre[x] - pre[j]); x = -1; } } if (!okk)mx = pre[1]; if (pre[n] > mx)cout << "YES" << el; else cout << "NO" << el; } int main () { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; cin >> t; while(t--){ solve(); } }
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 positive even integer yy (y>0y>0) and replace aa with a−ya−y. You can perform as many such operations as you want. You can choose the same numbers xx and yy in different moves.Your task is to find the minimum number of moves required to obtain bb from aa. It is guaranteed that you can always obtain bb from aa.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.Then tt test cases follow. Each test case is given as two space-separated integers aa and bb (1≤a,b≤1091≤a,b≤109).OutputFor each test case, print the answer — the minimum number of moves required to obtain bb from aa if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain bb from aa.ExampleInputCopy5 2 3 10 10 2 4 7 4 9 3 OutputCopy1 0 2 2 1 NoteIn the first test case; you can just add 11.In the second test case, you don't need to do anything.In the third test case, you can add 11 two times.In the fourth test case, you can subtract 44 and add 11.In the fifth test case, you can just subtract 66.
[ "greedy", "implementation", "math" ]
#include <bits/stdc++.h> #define hello ios_base::sync_with_stdio(false); #define world cin.tie(NULL); using namespace std; int main(){ hello world int n; cin>>n; int A[n]; for (int i = 0; i < n; i++) { int a,b; cin>>a>>b; if (a==b) { A[i]=0; } else if (a>b) { if (a%2==0 && b%2==0) { A[i]=1; } else if (a%2==1 && b%2==1) { A[i]=1; } else { A[i]=2; } } else{ if (a%2==0 && b%2==0) { A[i]=2; } else if (a%2==1 && b%2==1) { A[i]=2; } else { A[i]=1; } } } for (int i = 0; i < n; i++) { cout<<A[i]<<endl; } return 0; }
cpp
1303
C
C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arranged in some order.Polycarp uses the same password ss on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in ss, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in ss, so, for example, the password cannot be password (two characters s are adjacent).Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?InputThe first line contains one integer TT (1≤T≤10001≤T≤1000) — the number of test cases.Then TT lines follow, each containing one string ss (1≤|s|≤2001≤|s|≤200) representing the test case. ss consists of lowercase Latin letters only. There are no two adjacent equal characters in ss.OutputFor each test case, do the following: if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); otherwise, print YES (in upper case), and then a string consisting of 2626 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. ExampleInputCopy5 ababa codedoca abcda zxzytyz abcdefghijklmnopqrstuvwxyza OutputCopyYES bacdefghijklmnopqrstuvwxyz YES edocabfghijklmnpqrstuvwxyz NO YES xzytabcdefghijklmnopqrsuvw NO
[ "dfs and similar", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; #define line cout << "\n"; const int N = 2e5 + 2; set<int> adj[N]; int vis[N]; void dfs(int u, int p) { char c = u + 'a'; vis[u] = 1; cout << c; for (auto v : adj[u]) { if (v == p) continue; if (vis[v] == 1) continue; dfs(v, u); } } void solve() { string s; cin >> s; for (int i = 1; i < s.size(); i++) { int x = s[i - 1] - 'a'; int y = s[i] - 'a'; adj[x].insert(y); adj[y].insert(x); } int root = 0; int cnt = 0; bool ok = false; for (int i = 0; i < 26; i++) { if (adj[i].size() == 1) { cnt++; ok = true; root = i; } if (adj[i].size() > 2) { ok = ok & false; } } if (cnt != 2) ok = false; if (s.size() == 1) ok = true; if (ok) { cout << "YES"; line; dfs(root, -1); for (int i = 0; i < 26; i++) { if (vis[i] == 0) { char c = i + 'a'; cout << c; } vis[i] = 0; adj[i].clear(); } line; } else { cout << "NO"; line; for (int i = 0; i < 26; i++) { vis[i] = 0; adj[i].clear(); } } } int main() { int t; cin >> t; while (t--) { solve(); } }
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 positive even integer yy (y>0y>0) and replace aa with a−ya−y. You can perform as many such operations as you want. You can choose the same numbers xx and yy in different moves.Your task is to find the minimum number of moves required to obtain bb from aa. It is guaranteed that you can always obtain bb from aa.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.Then tt test cases follow. Each test case is given as two space-separated integers aa and bb (1≤a,b≤1091≤a,b≤109).OutputFor each test case, print the answer — the minimum number of moves required to obtain bb from aa if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain bb from aa.ExampleInputCopy5 2 3 10 10 2 4 7 4 9 3 OutputCopy1 0 2 2 1 NoteIn the first test case; you can just add 11.In the second test case, you don't need to do anything.In the third test case, you can add 11 two times.In the fourth test case, you can subtract 44 and add 11.In the fifth test case, you can just subtract 66.
[ "greedy", "implementation", "math" ]
#include <bits/stdc++.h> using namespace std; int main(){ int t,a,b; cin >> t; while (t--){ cin >> a >> b; if (a==b)cout << 0; else if (a>b && (a-b)%2==0)cout << 1; else if (a>b && (a-b)%2==1)cout << 2; else if (a<b && (b-a)%2==0)cout << 2; else if (a<b && (b-a)%2==1)cout << 1; cout << "\n"; } }
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 John has mm cows, each with a favorite sweetness fifi and a hunger value hihi. He would like to pick two disjoint subsets of cows to line up on the left and right side of the grass row. There is no restriction on how many cows must be on either side. The cows will be treated in the following manner: The cows from the left and right side will take turns feeding in an order decided by Farmer John. When a cow feeds, it walks towards the other end without changing direction and eats grass of its favorite sweetness until it eats hihi units. The moment a cow eats hihi units, it will fall asleep there, preventing further cows from passing it from both directions. If it encounters another sleeping cow or reaches the end of the grass row, it will get upset. Farmer John absolutely does not want any cows to get upset. Note that grass does not grow back. Also, to prevent cows from getting upset, not every cow has to feed since FJ can choose a subset of them. Surprisingly, FJ has determined that sleeping cows are the most satisfied. If FJ orders optimally, what is the maximum number of sleeping cows that can result, and how many ways can FJ choose the subset of cows on the left and right side to achieve that maximum number of sleeping cows (modulo 109+7109+7)? The order in which FJ sends the cows does not matter as long as no cows get upset. InputThe first line contains two integers nn and mm (1≤n≤50001≤n≤5000, 1≤m≤50001≤m≤5000)  — the number of units of grass and the number of cows. The second line contains nn integers s1,s2,…,sns1,s2,…,sn (1≤si≤n1≤si≤n)  — the sweetness values of the grass.The ii-th of the following mm lines contains two integers fifi and hihi (1≤fi,hi≤n1≤fi,hi≤n)  — the favorite sweetness and hunger value of the ii-th cow. No two cows have the same hunger and favorite sweetness simultaneously.OutputOutput two integers  — the maximum number of sleeping cows that can result and the number of ways modulo 109+7109+7. ExamplesInputCopy5 2 1 1 1 1 1 1 2 1 3 OutputCopy2 2 InputCopy5 2 1 1 1 1 1 1 2 1 4 OutputCopy1 4 InputCopy3 2 2 3 2 3 1 2 1 OutputCopy2 4 InputCopy5 1 1 1 1 1 1 2 5 OutputCopy0 1 NoteIn the first example; FJ can line up the cows as follows to achieve 22 sleeping cows: Cow 11 is lined up on the left side and cow 22 is lined up on the right side. Cow 22 is lined up on the left side and cow 11 is lined up on the right side. In the second example, FJ can line up the cows as follows to achieve 11 sleeping cow: Cow 11 is lined up on the left side. Cow 22 is lined up on the left side. Cow 11 is lined up on the right side. Cow 22 is lined up on the right side. In the third example, FJ can line up the cows as follows to achieve 22 sleeping cows: Cow 11 and 22 are lined up on the left side. Cow 11 and 22 are lined up on the right side. Cow 11 is lined up on the left side and cow 22 is lined up on the right side. Cow 11 is lined up on the right side and cow 22 is lined up on the left side. In the fourth example, FJ cannot end up with any sleeping cows, so there will be no cows lined up on either side.
[ "binary search", "combinatorics", "dp", "greedy", "implementation", "math" ]
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> //#include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif // C++ #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <typeindex> #include <type_traits> #include <unordered_map> #include <unordered_set> #endif using namespace std; using i64 = long long; using i128 = __int128; #define MAXN 5005 #define MAXM 5005 #define M 1000000 #define K 17 #define MAXP 25 #define MAXK 55 #define MAXC 255 #define MAXERR 105 #define MAXLEN 105 #define MDIR 10 #define MAXR 705 #define BASE 102240 #define MAXA 28 #define MAXT 100005 #define LIMIT 86400 #define MAXV 305 #define LEQ 1 #define EQ 0 #define OP 0 #define CLO 1 #define DIG 1 #define C1 0 #define C2 1 #define PLUS 0 #define MINUS 1 #define MUL 2 #define CLO 1 #define VERT 1 //#define B 31 #define B2 1007 #define W 1 #define H 19 #define SPEC 1 #define MUL 2 #define CNT 3 #define ITER 1000 #define INF 1e9 #define EPS 1e9 #define MOD 1000000007 #define FACT 100000000000000 #define PI 3.14159265358979 #define SRC 0 typedef long long ll; typedef ll hash_t; typedef __int128_t lint; typedef long double ld; typedef pair<int,int> ii; typedef pair<double,int> ip; typedef pair<ll,ii> pl; typedef pair<int,ll> pll; typedef pair<ll,int> li; typedef pair<ll,ll> iv; typedef tuple<int,int,int> iii; typedef tuple<ll,ll,int> tll; typedef vector<vector<int>> vv; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ii> vii; typedef vector<vector<ll>> vll; typedef complex<ld> cd; #define pb push_back #define eb emplace_back #define ins insert #define er erase #define sc second #define fr first #define repl(i,x,y) for (int i = (x); i <= (y); ++i) #define rep(i,x,y) for (int i = (x); i < (y); ++i) #define rev(i,x,y) for (int i = (x); i >= (y); --i) #define LSOne(S) (S & (-S)) #define trav(i,v) for (auto &i : v) #define foreach(it,v) for (auto it = begin(v); it != end(v); ++it) #define bend(v) (v).begin(), (v).end() #define rbend(v) (v).rbegin(), (v).rend() #define sortarr(v) sort(bend(v)) #define rsortarr(v) sort(rbend(v)) #define sz(v) (int)((v).size()) #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()) #define combine(A,B) A.insert(end(A), bend(B)) template<class T> bool ckmin(T &a, T &b) { return a > b ? a = b, 1 : 0; }; template<class T> bool ckmax(T &a, T &b) { return a < b ? a = b, 1 : 0; }; template<class T> void amax(T &a, T b, T c) { a = max(b, c); }; template<class T> void amin(T &a, T b, T c) { a = min(b, c); }; template<class T> T getmax(vector<T> &v) { return *max_element(bend(v)); }; template<class T> T getmin(vector<T> &v) { return *min_element(bend(v)); }; template<class T> int compress(vector<T> &v, T &val) { return (int)(lower_bound(bend(v), val) - begin(v)); }; template<class T> auto vfind(vector<T> &v, T val) { return find(bend(v), val); } template<class T> auto verase(vector<T> &v, T val) { return v.er(vfind(v, val)); } int n,m; //candidate stores all possible cows with favourite jelly i vv cand, occ; int pos[MAXN]; //idea is to find an invariant(in this case rightmost point of the left queue and fix it) //now we want to count the number of cows in the left and right queue respectively //for all cows that likes the same jelly, 2 cows of the same type cannot both appear on the left of right queue but separately //calculate the number of ways to add the pair of cows to the left and right queue independently //only need to choose a subset so ordering of the cows do not matter int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; vector<int> a(n); cand.resize(n), occ.resize(n); rep(i,0,n) { int x; cin >> x; x--; a[i] = x; occ[x].pb(i); } rep(i,0,m) { int f,h; cin >> f >> h; f--; if (h <= sz(occ[f])) cand[f].pb(h), pos[occ[f][h - 1]] = true; } pll ans = {0, 1}; //consider the case where we leave the right queue empty rep(i,0,n) { if (sz(cand[i]) > 0) ans.fr++, ans.sc = (1LL * ans.sc * sz(cand[i])) % MOD; } //fix the valid starting positions rep(i,0,n) { if (pos[i]) { pll tmp = {1, 1}; //calculate all possible ending points for cows that like the same candy int canrgt = 0; trav(j, cand[a[i]]) { if (occ[a[i]][sz(occ[a[i]]) - j] > i) { if (occ[a[i]][j - 1] != i) canrgt++; } } if (canrgt) tmp.fr++, tmp.sc = (tmp.sc * canrgt) % MOD; rep(j,0,n) { if (a[i] == j) continue; int cntlft = 0, cntrgt = 0, cntdup = 0; trav(k, cand[j]) { if (occ[j][k - 1] < i) cntlft++; if (occ[j][sz(occ[j]) - k] > i) cntrgt++; if (occ[j][k - 1] < i && occ[j][sz(occ[j]) - k] > i) cntdup++; } int c2 = cntlft * cntrgt - cntdup; int c1 = cntlft + cntrgt; if (c2) { //add this pair of cows to the left and right queues respectively tmp.fr += 2; tmp.sc = (tmp.sc * c2) % MOD; } else if (c1) { tmp.fr++; tmp.sc = (tmp.sc * c1) % MOD; } } if (tmp.fr > ans.fr) { ans = tmp; } else if (tmp.fr == ans.fr) ans.sc = (ans.sc + tmp.sc) % MOD; } } cout << ans.fr << ' ' << ans.sc; }
cpp
1320
F
F. Blocks and Sensorstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputPolycarp plays a well-known computer game (we won't mention its name). Every object in this game consists of three-dimensional blocks — axis-aligned cubes of size 1×1×11×1×1. These blocks are unaffected by gravity, so they can float in the air without support. The blocks are placed in cells of size 1×1×11×1×1; each cell either contains exactly one block or is empty. Each cell is represented by its coordinates (x,y,z)(x,y,z) (the cell with these coordinates is a cube with opposite corners in (x,y,z)(x,y,z) and (x+1,y+1,z+1)(x+1,y+1,z+1)) and its contents ax,y,zax,y,z; if the cell is empty, then ax,y,z=0ax,y,z=0, otherwise ax,y,zax,y,z is equal to the type of the block placed in it (the types are integers from 11 to 2⋅1052⋅105).Polycarp has built a large structure consisting of blocks. This structure can be enclosed in an axis-aligned rectangular parallelepiped of size n×m×kn×m×k, containing all cells (x,y,z)(x,y,z) such that x∈[1,n]x∈[1,n], y∈[1,m]y∈[1,m], and z∈[1,k]z∈[1,k]. After that, Polycarp has installed 2nm+2nk+2mk2nm+2nk+2mk sensors around this parallelepiped. A sensor is a special block that sends a ray in some direction and shows the type of the first block that was hit by this ray (except for other sensors). The sensors installed by Polycarp are adjacent to the borders of the parallelepiped, and the rays sent by them are parallel to one of the coordinate axes and directed inside the parallelepiped. More formally, the sensors can be divided into 66 types: there are mkmk sensors of the first type; each such sensor is installed in (0,y,z)(0,y,z), where y∈[1,m]y∈[1,m] and z∈[1,k]z∈[1,k], and it sends a ray that is parallel to the OxOx axis and has the same direction; there are mkmk sensors of the second type; each such sensor is installed in (n+1,y,z)(n+1,y,z), where y∈[1,m]y∈[1,m] and z∈[1,k]z∈[1,k], and it sends a ray that is parallel to the OxOx axis and has the opposite direction; there are nknk sensors of the third type; each such sensor is installed in (x,0,z)(x,0,z), where x∈[1,n]x∈[1,n] and z∈[1,k]z∈[1,k], and it sends a ray that is parallel to the OyOy axis and has the same direction; there are nknk sensors of the fourth type; each such sensor is installed in (x,m+1,z)(x,m+1,z), where x∈[1,n]x∈[1,n] and z∈[1,k]z∈[1,k], and it sends a ray that is parallel to the OyOy axis and has the opposite direction; there are nmnm sensors of the fifth type; each such sensor is installed in (x,y,0)(x,y,0), where x∈[1,n]x∈[1,n] and y∈[1,m]y∈[1,m], and it sends a ray that is parallel to the OzOz axis and has the same direction; finally, there are nmnm sensors of the sixth type; each such sensor is installed in (x,y,k+1)(x,y,k+1), where x∈[1,n]x∈[1,n] and y∈[1,m]y∈[1,m], and it sends a ray that is parallel to the OzOz axis and has the opposite direction. Polycarp has invited his friend Monocarp to play with him. Of course, as soon as Monocarp saw a large parallelepiped bounded by sensor blocks, he began to wonder what was inside of it. Polycarp didn't want to tell Monocarp the exact shape of the figure, so he provided Monocarp with the data from all sensors and told him to try guessing the contents of the parallelepiped by himself.After some hours of thinking, Monocarp has no clue about what's inside the sensor-bounded space. But he does not want to give up, so he decided to ask for help. Can you write a program that will analyze the sensor data and construct any figure that is consistent with it?InputThe first line contains three integers nn, mm and kk (1≤n,m,k≤2⋅1051≤n,m,k≤2⋅105, nmk≤2⋅105nmk≤2⋅105) — the dimensions of the parallelepiped.Then the sensor data follows. For each sensor, its data is either 00, if the ray emitted from it reaches the opposite sensor (there are no blocks in between), or an integer from 11 to 2⋅1052⋅105 denoting the type of the first block hit by the ray. The data is divided into 66 sections (one for each type of sensors), each consecutive pair of sections is separated by a blank line, and the first section is separated by a blank line from the first line of the input.The first section consists of mm lines containing kk integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (0,i,j)(0,i,j).The second section consists of mm lines containing kk integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (n+1,i,j)(n+1,i,j).The third section consists of nn lines containing kk integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (i,0,j)(i,0,j).The fourth section consists of nn lines containing kk integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (i,m+1,j)(i,m+1,j).The fifth section consists of nn lines containing mm integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (i,j,0)(i,j,0).Finally, the sixth section consists of nn lines containing mm integers each. The jj-th integer in the ii-th line is the data from the sensor installed in (i,j,k+1)(i,j,k+1).OutputIf the information from the input is inconsistent, print one integer −1−1.Otherwise, print the figure inside the parallelepiped as follows. The output should consist of nmknmk integers: a1,1,1a1,1,1, a1,1,2a1,1,2, ..., a1,1,ka1,1,k, a1,2,1a1,2,1, ..., a1,2,ka1,2,k, ..., a1,m,ka1,m,k, a2,1,1a2,1,1, ..., an,m,kan,m,k, where ai,j,kai,j,k is the type of the block in (i,j,k)(i,j,k), or 00 if there is no block there. If there are multiple figures consistent with sensor data, describe any of them.For your convenience, the sample output is formatted as follows: there are nn separate sections for blocks having x=1x=1, x=2x=2, ..., x=nx=n; each section consists of mm lines containing kk integers each. Note that this type of output is acceptable, but you may print the integers with any other formatting instead (even all integers on the same line), only their order matters.ExamplesInputCopy4 3 2 1 4 3 2 6 5 1 4 3 2 6 7 1 4 1 4 0 0 0 7 6 5 6 5 0 0 0 7 1 3 6 1 3 6 0 0 0 0 0 7 4 3 5 4 2 5 0 0 0 0 0 7 OutputCopy1 4 3 0 6 5 1 4 3 2 6 5 0 0 0 0 0 0 0 0 0 0 0 7 InputCopy1 1 1 0 0 0 0 0 0 OutputCopy0 InputCopy1 1 1 0 0 1337 0 0 0 OutputCopy-1 InputCopy1 1 1 1337 1337 1337 1337 1337 1337 OutputCopy1337
[ "brute force" ]
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; const int dx[]={1,-1,0,0,0,0},dy[]={0,0,1,-1,0,0},dz[]={0,0,0,0,1,-1}; int n,m,k,col[N];vector<int>d[N]; inline int id(int x,int y,int z){return (x-1)*m*k+(y-1)*k+z;} void ins(int x,int y,int z,int c,int tp) { if(x<1||x>n||y<1||y>m||z<1||z>k) { if(c)puts("-1"),exit(0); return; } int now=id(x,y,z); if(col[now]==-1||col[now]==c) { col[now]=c;d[now].push_back(tp); if(!c)ins(x+dx[tp],y+dy[tp],z+dz[tp],c,tp); return; } if(col[now]>0) while(!d[now].empty()) { int o=d[now].back(); d[now].pop_back(); ins(x+dx[o],y+dy[o],z+dz[o],col[now],o); } col[now]=0; ins(x+dx[tp],y+dy[tp],z+dz[tp],c,tp); } int main() { memset(col,-1,sizeof col); scanf("%d%d%d",&n,&m,&k); for(int i=1,t;i<=m;++i)for(int j=1;j<=k;++j)scanf("%d",&t),ins(1,i,j,t,0); for(int i=1,t;i<=m;++i)for(int j=1;j<=k;++j)scanf("%d",&t),ins(n,i,j,t,1); for(int i=1,t;i<=n;++i)for(int j=1;j<=k;++j)scanf("%d",&t),ins(i,1,j,t,2); for(int i=1,t;i<=n;++i)for(int j=1;j<=k;++j)scanf("%d",&t),ins(i,m,j,t,3); for(int i=1,t;i<=n;++i)for(int j=1;j<=m;++j)scanf("%d",&t),ins(i,j,1,t,4); for(int i=1,t;i<=n;++i)for(int j=1;j<=m;++j)scanf("%d",&t),ins(i,j,k,t,5); for(int i=1;i<=n*m*k;++i)printf("%d ",col[i]<0?0:col[i]); return 0; }
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's task is to lead a Nekomimi girl from cell (1,1)(1,1) to the gate at (2,n)(2,n) and escape the maze. The girl can only move between cells sharing a common side.However, at some moments during the game, some cells may change their state: either from normal ground to lava (which forbids movement into that cell), or vice versa (which makes that cell passable again). Initially all cells are of the ground type.After hours of streaming, NEKO finally figured out there are only qq such moments: the ii-th moment toggles the state of cell (ri,ci)(ri,ci) (either from ground to lava or vice versa).Knowing this, NEKO wonders, after each of the qq moments, whether it is still possible to move from cell (1,1)(1,1) to cell (2,n)(2,n) without going through any lava cells.Although NEKO is a great streamer and gamer, she still can't get through quizzes and problems requiring large amount of Brain Power. Can you help her?InputThe first line contains integers nn, qq (2≤n≤1052≤n≤105, 1≤q≤1051≤q≤105).The ii-th of qq following lines contains two integers riri, cici (1≤ri≤21≤ri≤2, 1≤ci≤n1≤ci≤n), denoting the coordinates of the cell to be flipped at the ii-th moment.It is guaranteed that cells (1,1)(1,1) and (2,n)(2,n) never appear in the query list.OutputFor each moment, if it is possible to travel from cell (1,1)(1,1) to cell (2,n)(2,n), print "Yes", otherwise print "No". There should be exactly qq answers, one after every update.You can print the words in any case (either lowercase, uppercase or mixed).ExampleInputCopy5 5 2 3 1 4 2 4 2 3 1 4 OutputCopyYes No No No Yes NoteWe'll crack down the example test here: After the first query; the girl still able to reach the goal. One of the shortest path ways should be: (1,1)→(1,2)→(1,3)→(1,4)→(1,5)→(2,5)(1,1)→(1,2)→(1,3)→(1,4)→(1,5)→(2,5). After the second query, it's impossible to move to the goal, since the farthest cell she could reach is (1,3)(1,3). After the fourth query, the (2,3)(2,3) is not blocked, but now all the 44-th column is blocked, so she still can't reach the goal. After the fifth query, the column barrier has been lifted, thus she can go to the final goal again.
[ "data structures", "dsu", "implementation" ]
#include<bits/stdc++.h> #include<iostream> #include<iomanip> #include<vector> #include<string> #include<math.h> #include<map> #include<algorithm> #include<set> #include<unordered_map> #include <unordered_set> #define fast ios_base::sync_with_stdio(false),cin.tie(NULL) #define popcount __builtin_popcount using namespace std; #define ff first #define ss second #define pb push_back #define int long long int typedef long double lld; #define FOR(i,n) for(int i=0;i<n;i++) #define yes cout<<"YES"<<endl; #define no cout<<"NO"<<endl; #define p0(a) cout << a << " " #define p1(a) cout << a << endl #define p2(a, b) cout << a << " " << b << endl #define p3(a, b, c) cout << a << " " << b << " " << c << endl #define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl #define MOD 1000000007 #define vi vector<int> #define pii pair<int, int> #define vii vector<pii> #define fr front() #define bk back() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define max_heap priority_queue <int> #define min_heap priority_queue <int, vector <int> , greater <int> > typedef vector<char> vc; typedef vector<string> vs; typedef vector<pair<int, int> > vpii; typedef unordered_set<int> us; typedef map<int,int> mp; //debugging #ifndef ONLINE_JUDGE #define debug1(a) cerr <<#a<<" " <<a << endl #define debug2(a, b) cerr <<#a<<" "<< a << " "<<#b<<" " << b << endl #define debug3(a, b, c) cerr <<#a<<" "<<a << " "<<#b<<" " << b << " "<<#c<<" " << c << endl #define debug4(a, b, c, d) cerr << #a<<" "<<a << " " <<#b<<" "<< b << " "<<#c<<" " << c << " "<<#d<<" "<< d << endl #define debug(x) cerr << #x <<" "; _print(x); cerr << endl; #else #define debug(x) #define debug2(x,y) #define debug3(x,y,z) #define debug4(x,y,z,a) #endif void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(lld t) {cerr << t;} void _print(double t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} //Constants const long double pi= 3.141592653589793238; const int INF= 1e18; const int mod=1e9+7; //MATHEMATICAL FUNCTIONS int gcd(int a, int b){if (b == 0)return a;return gcd(b, a % b);} //gcd int lcm(int a, int b){return (a/gcd(a,b)*b);} //lcm //sieve vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;} //binary exponentation int expo(int a, int b, int mod) {int res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} //CHECK bool isprime(int n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} bool ispoweroftwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));} bool isperfectsquare(int x){if (x >= 0) {int sr = sqrt(x);return (sr * sr == x);}return false;} int ceils(int x, int y) {return x / y + (x % y > 0);} // Operator overloads template<typename T1, typename T2> // cin >> pair<T1, T2> istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); } template<typename T> // cin >> vector<T> istream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;} template<typename T1, typename T2> // cout << pair<T1, T2> ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); } template<typename T> // cout << vector<T> ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; } //USEFUL void printarr(vi arr, int n){FOR(i,n) cout << arr[i] << " ";cout << "\n";} int n,m; vector<vector<int>> arr; int is_block(int i,int j){ vector<pair<int,int>> points; i=1-i; if(j-1>=0){ points.pb({i,j-1}); } if(j+1<n){ points.pb({i,j+1}); } points.pb({i,j}); int f=0; for(auto val:points){ if(arr[val.first][val.second]==0){ f++; } } // p2(i,j); // cout<<points<<endl; points.clear(); return f; } void solve(){ cin>>n>>m; arr.resize(2,vi(n,1LL)); int c=0; while(m--){ int a,b; cin>>a>>b; a--,b--; // p2(a,b); // debug(arr); if(arr[a][b]==0){ arr[a][b]=1; c-=is_block(a,b); } else{ arr[a][b]=0; c+=is_block(a,b); } // debug(arr); // debug(c); if(c){ cout<<"No\n"; } else{ cout<<"Yes\n"; } } } int32_t main() { fast; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int t; t=1; //cin>>t; while(t--){ // cout<<t; solve(); } return 0; }
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 assume that Vova woke up exactly at the beginning of this story (the initial time is 00). Each time Vova sleeps exactly one day (in other words, hh hours).Vova thinks that the ii-th sleeping time is good if he starts to sleep between hours ll and rr inclusive.Vova can control himself and before the ii-th time can choose between two options: go to sleep after aiai hours or after ai−1ai−1 hours.Your task is to say the maximum number of good sleeping times Vova can obtain if he acts optimally.InputThe first line of the input contains four integers n,h,ln,h,l and rr (1≤n≤2000,3≤h≤2000,0≤l≤r<h1≤n≤2000,3≤h≤2000,0≤l≤r<h) — the number of times Vova goes to sleep, the number of hours in a day and the segment of the good sleeping time.The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai<h1≤ai<h), where aiai is the number of hours after which Vova goes to sleep the ii-th time.OutputPrint one integer — the maximum number of good sleeping times Vova can obtain if he acts optimally.ExampleInputCopy7 24 21 23 16 17 14 20 20 11 22 OutputCopy3 NoteThe maximum number of good times in the example is 33.The story starts from t=0t=0. Then Vova goes to sleep after a1−1a1−1 hours, now the time is 1515. This time is not good. Then Vova goes to sleep after a2−1a2−1 hours, now the time is 15+16=715+16=7. This time is also not good. Then Vova goes to sleep after a3a3 hours, now the time is 7+14=217+14=21. This time is good. Then Vova goes to sleep after a4−1a4−1 hours, now the time is 21+19=1621+19=16. This time is not good. Then Vova goes to sleep after a5a5 hours, now the time is 16+20=1216+20=12. This time is not good. Then Vova goes to sleep after a6a6 hours, now the time is 12+11=2312+11=23. This time is good. Then Vova goes to sleep after a7a7 hours, now the time is 23+22=2123+22=21. This time is also good.
[ "dp", "implementation" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; template <typename T> bool chkmax(T &x, T y) { if (y > x) {x = y; return true;} return false;} template <typename T> bool chkmin(T &x, T y) { if (y < x) {x = y; return true;} return false;} const ll INF = 0x3f3f3f3f3f3f3f3f; #ifdef ANSWERER #include "algo/debug.h" #else #define debug(...) 42 #endif void solve() { int n, k, l, r; cin >> n >> k >> l >> r; vector <vector<int>> f(n + 1, vector <int> (k + 1)); f[0][0] = 1; vector <int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { for (int j = 0; j < k; j++) { if (f[i - 1][(j - a[i] + k) % k]) f[i][j] = max(f[i][j], f[i - 1][(j - a[i] + k) % k] + (j >= l && j <= r)); if (f[i - 1][(j + 1 - a[i] + k) % k]) f[i][j] = max(f[i][j], f[i - 1][(j - a[i] + 1 + k) % k] + (j >= l && j <= r)); } } int ans = 0; for (int i = 0; i < k; i++) { chkmax(ans, f[n][i]); } cout << max(0, ans - 1) << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef ANSWERER freopen("../in.txt", "r", stdin); freopen("../out.txt", "w", stdout); clock_t start = clock(); #endif int t = 1; //cin >> t; while(t--) { solve(); } #ifdef ANSWERER clock_t ends = clock(); cout << "\n\nRunning Time : " << (double) (ends - start) / CLOCKS_PER_SEC * 1000 << "ms" << endl; #endif return EXIT_SUCCESS; } //#endif
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 "kkkkkk" are palindromes, while strings "moon", "tv", and "abab" are not. An empty string is also a palindrome.Gildong loves this concept so much, so he wants to play with it. He has nn distinct strings of equal length mm. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.InputThe first line contains two integers nn and mm (1≤n≤1001≤n≤100, 1≤m≤501≤m≤50) — the number of strings and the length of each string.Next nn lines contain a string of length mm each, consisting of lowercase Latin letters only. All strings are distinct.OutputIn the first line, print the length of the longest palindrome string you made.In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.ExamplesInputCopy3 3 tab one bat OutputCopy6 tabbat InputCopy4 2 oo ox xo xx OutputCopy6 oxxxxo InputCopy3 5 hello codef orces OutputCopy0 InputCopy9 4 abab baba abcd bcde cdef defg wxyz zyxw ijji OutputCopy20 ababwxyzijjizyxwbaba NoteIn the first example; "battab" is also a valid answer.In the second example; there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.In the third example; the empty string is the only valid palindrome string.
[ "brute force", "constructive algorithms", "greedy", "implementation", "strings" ]
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define all(v) ((v).begin()), ((v).end()) #define MOD ll(1e9+7) #define int long long typedef int ll; typedef vector<ll> vi; typedef tree< ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int N=2e5+10; void solve() { ll n, m; cin >> n >> m; vector<string> a(n); map<string, ll> mp; string fir = "", sec = "", mid = ""; for (int i = 0; i < n; ++i) { cin >> a[i]; mp[a[i]]++; } for (int i = 0; i < n; ++i) { string rno = a[i]; string no = a[i]; reverse(all(rno)); if (rno != no) { if (mp[no] && mp[rno]) { fir += no; sec = rno + sec; mp[no]--; mp[rno]--; } } else { if (mp[no] > 1) { if (mp[no] && mp[rno]) { fir += no; sec = rno + sec; mp[no]--; mp[rno]--; } } else { if (mid.size() < no.size())mid = no; } } } string y = fir + mid + sec; cout << y.size() << endl; cout << y << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t = 1; //cin >> t; while (t--) { solve(); } }
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 puzzle.In a 2-dimension plane, you have a set s={(x1,y1),(x2,y2),…,(xn,yn)}s={(x1,y1),(x2,y2),…,(xn,yn)} consisting of nn distinct points. In the set ss, no three distinct points lie on a single line. For a point p∈sp∈s, we can protect this point by building a castle. A castle is a simple quadrilateral (polygon with 44 vertices) that strictly encloses the point pp (i.e. the point pp is strictly inside a quadrilateral). Kiwon is interested in the number of 44-point subsets of ss that can be used to build a castle protecting pp. Note that, if a single subset can be connected in more than one way to enclose a point, it is counted only once. Let f(p)f(p) be the number of 44-point subsets that can enclose the point pp. Please compute the sum of f(p)f(p) for all points p∈sp∈s.InputThe first line contains a single integer nn (5≤n≤25005≤n≤2500).In the next nn lines, two integers xixi and yiyi (−109≤xi,yi≤109−109≤xi,yi≤109) denoting the position of points are given.It is guaranteed that all points are distinct, and there are no three collinear points.OutputPrint the sum of f(p)f(p) for all points p∈sp∈s.ExamplesInputCopy5 -1 0 1 0 -10 -1 10 -1 0 3 OutputCopy2InputCopy8 0 1 1 2 2 2 1 3 0 -1 -1 -2 -2 -2 -1 -3 OutputCopy40InputCopy10 588634631 265299215 -257682751 342279997 527377039 82412729 145077145 702473706 276067232 912883502 822614418 -514698233 280281434 -41461635 65985059 -827653144 188538640 592896147 -857422304 -529223472 OutputCopy213
[ "combinatorics", "geometry", "math", "sortings" ]
#ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno> #include <cfloat> #include <ciso646> #include <climits> #include <clocale> #include <cmath> #include <csetjmp> #include <csignal> #include <cstdarg> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #if __cplusplus >= 201103L #include <ccomplex> #include <cfenv> #include <cinttypes> //#include <cstdalign> #include <cstdbool> #include <cstdint> #include <ctgmath> #include <cwchar> #include <cwctype> #endif // C++ #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #if __cplusplus >= 201103L #include <array> #include <atomic> #include <chrono> #include <condition_variable> #include <forward_list> #include <future> #include <initializer_list> #include <mutex> #include <random> #include <ratio> #include <regex> #include <scoped_allocator> #include <system_error> #include <thread> #include <tuple> #include <typeindex> #include <type_traits> #include <unordered_map> #include <unordered_set> #endif using namespace std; using i64 = long long; using i128 = __int128; #define MAXN 100005 #define MAXM 100005 #define M 1000000 #define K 17 #define MAXP 25 #define MAXK 55 //#define MAXC 255 #define MAXERR 105 #define MAXLEN 105 #define MDIR 10 #define MAXR 705 #define BASE 102240 #define MAXA 28 #define MAXT 100005 #define LIMIT 86400 #define MAXV 305 #define LEQ 1 #define EQ 0 #define OP 0 #define CLO 1 #define DIG 1 #define C1 0 #define C2 1 #define PLUS 0 #define MINUS 1 #define MUL 2 #define CLO 1 #define VERT 1 //#define B 31 #define B2 1007 #define W 1 #define H 30 #define SPEC 1 #define MUL 2 #define CNT 3 #define ITER 1000 #define INF 1e9 #define EPS 1e-10 #define MOD 998244353 #define FACT 100000000000000 #define PI 3.1415926535897932384626433832795014 #define SRC 0 #define SCALE 1e16 typedef long long ll; typedef ll hash_t; typedef __int128_t lint; typedef long double ld; typedef pair<int,int> ii; typedef pair<double,int> ip; typedef pair<ll,ii> pl; typedef pair<ll, ll> pll; typedef pair<ll,int> ppll; typedef pair<ll,int> li; typedef pair<ll,ll> iv; typedef pair<double,int> ip; typedef tuple<int,int,int> iii; typedef tuple<int, int, int> tll; typedef tuple<ld, int, int> iit; typedef vector<vector<int>> vv; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ii> vii; typedef vector<vector<ll>> vll; typedef complex<ld> cd; #define pb push_back #define eb emplace_back #define ins insert #define er erase #define sc second #define fr first #define mp make_pair #define mt make_tuple #define repl(i,x,y) for (int i = (x); i <= (y); ++i) #define rep(i,x,y) for (int i = (x); i < (y); ++i) #define rev(i,x,y) for (int i = (x); i >= (y); --i) #define LSOne(S) (S & (-S)) #define trav(i,v) for (auto &i : v) #define foreach(it,v) for (auto it = begin(v); it != end(v); ++it) #define bend(v) (v).begin(), (v).end() #define rbend(v) (v).rbegin(), (v).rend() #define sortarr(v) sort(bend(v)) #define rsortarr(v) sort(rbend(v)) #define unique(v) v.er(unique(bend(v)), end(v)) #define extend(A,B) A.insert(end(A), bend(B)) #define sz(A) (int)(A.size()) #define fill(V) iota(bend(V), (0)) #define vfill(V, st) iota(bend(V), st) template<class T> bool ckmin(T &a, T b) { return a > b ? a = b, 1 : 0; }; template<class T> bool ckmax(T &a, T b) { return a < b ? a = b, 1 : 0; }; template<class T> void amax(T &a, T b, T c) { a = max(b, c); }; template<class T> void amin(T &a, T b, T c) { a = min(b, c); }; template<class T> T getmax(vector<T> &v) { return *max_element(bend(v)); }; template<class T> T getmin(vector<T> &v) { return *min_element(bend(v)); }; template<class T> int compress(vector<T> &v, T &val) { return (int)(lower_bound(bend(v), val) - begin(v)); }; template<class T> auto vfind(vector<T> &v, T val) { return find(bend(v), val); } template<class T> auto verase(vector<T> &v, T val) { return v.er(vfind(v, val)); } template<class T> void revarr(vector<T> &v) { reverse(bend(v)); }; struct pt { ll x, y; public : pt() {}; public : pt(ll _x, ll _y) : x(_x), y(_y) {}; }; struct vec { ll x,y; public : vec(pt a, pt b) { x = b.x - a.x, y = b.y - a.y; }; public : vec(ll _x, ll _y) : x(_x), y(_y) {}; bool operator<(const vec &p) { if (x == p.x) return y < p.y; return x < p.x; } }; vec toVec(pt &p, pt &q) { return vec(p, q); } ll dot (vec &p, vec &q) { return p.x * q.x + p.y * q.y; } ll sqrmag(vec &p) { return p.x * p.x + p.y * p.y; } pt translate(pt &p, vec q) { ll x = p.x + q.x, y = p.y + q.y; pt r(x,y); return r; } vec scale(ll u, vec p) { return vec(p.x * u, p.y * u); } ll cross(vec a, vec b) { return a.x * b.y - a.y * b.x; } bool onseg(pt &p, pt &q, pt &r) { return (min(p.x, r.x) <= q.x && max(r.x, p.x) >= q.x && q.y >= min(p.y, r.y) && max(r.y, p.y) >= q.y); } ll orien(pt &p, pt &q, pt &r) { ll res = cross(toVec(p,q), toVec(p,r)); if (!res) return 0; //Collinear return (res < 0) ? 1 : 2; //1 is CW, 2 is CCW } ll choose3(int n) { if (n < 3) return 0; ll ans = 1; repl(i,n-2,n) ans *= i; repl(i,2,3) ans /= i; return ans; } void fast_io() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } //xi denotes the number of subsets of 5 points here exactly i points form its convex hull //we want to find 2x3 + x4 //idea: if we cannot find the exact form properly, we can try to calculate other closed forms, do some addition/subtraction to get the exact form //in this case x3 + x4 + x5 = nC5 //5x3 + 5x4 + 5x5 - 3x3 - 4x4 - 5x5 = 2x3 + x4 which is what we desire int main() { fast_io(); int n; cin >> n; vector<pt> pts; rep(i,0,n) { int a,b; cin >> a >> b; pts.pb(pt(a,b)); } //5 * x3 + 5 * x4 + 5 * x5 = 5 * (nC5) ll ans = 1; repl(i,n-4,n) ans = (ans * i); repl(i,2,4) ans /= i; //this is equal to 3x3 + 4x4 + 5x5 ll sub = 0; //calculate the contribution of every edge to the total sum rep(i,0,n) { vector<vec> v; //get all vectors w.r.t point i rep(j,0,n) { if (j != i) v.pb(vec(pts[i], pts[j])); } sort(bend(v),[](vec &a, vec &b) { //check where the point lies on the left or right of point i w.r.t the x-axis bool ba = a < vec(0,0); bool bb = b < vec(0,0); //no duplicates so vector can never be (0,0) if (ba != bb) return ba > bb; //ccw walk from point i to point a to point b return cross(a,b) > 0; }); int ptr = 0; rep(j,0,n-1) { while (ptr < j + n - 1 && cross(v[j], v[ptr % (n - 1)]) >= 0)ptr++; sub += choose3(ptr - j - 1); } } cout << ans - sub; }
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 inclusive. All the written labels are distinct. The largest value among MEX(u,v)MEX(u,v) over all pairs of nodes (u,v)(u,v) is as small as possible. Here, MEX(u,v)MEX(u,v) denotes the smallest non-negative integer that isn't written on any edge on the unique simple path from node uu to node vv.InputThe first line contains the integer nn (2≤n≤1052≤n≤105) — the number of nodes in the tree.Each of the next n−1n−1 lines contains two space-separated integers uu and vv (1≤u,v≤n1≤u,v≤n) that mean there's an edge between nodes uu and vv. It's guaranteed that the given graph is a tree.OutputOutput n−1n−1 integers. The ithith of them will be the number written on the ithith edge (in the input order).ExamplesInputCopy3 1 2 1 3 OutputCopy0 1 InputCopy6 1 2 1 3 2 4 2 5 5 6 OutputCopy0 3 2 4 1NoteThe tree from the second sample:
[ "constructive algorithms", "dfs and similar", "greedy", "trees" ]
#include<bits/stdc++.h> #define endl '\n' #define iloveds std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); #define all(a) a.begin(),a.end() using namespace std; typedef long long ll ; const int N = 1e5 + 100; vector<int> g[N]; int now = 1; int ans[N]; int main(){ iloveds; int n; cin >> n; for(int i = 1 ; i < n ; i ++){ int u, v; cin >> u >> v; g[u].push_back(i); g[v].push_back(i); } for(int i = 1; i <= n ; i ++){ if(g[i].size() >= 3){ for(auto x : g[i]){ ans[x] = now ++; } break; } } for(int i = 1; i <= n ; i ++){ if(!ans[i]) ans[i] = now ++; } for(int i = 1; i < n ; i ++) cout << ans[i] - 1 << endl; }
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, where ⊕⊕ denotes the bitwise XOR operation.As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X).InputThe first line contains integer nn (1≤n≤1051≤n≤105).The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤230−10≤ai≤230−1).OutputPrint one integer — the minimum possible value of max1≤i≤n(ai⊕X)max1≤i≤n(ai⊕X).ExamplesInputCopy3 1 2 3 OutputCopy2 InputCopy2 1 5 OutputCopy4 NoteIn the first sample; we can choose X=3X=3.In the second sample, we can choose X=5X=5.
[ "bitmasks", "brute force", "dfs and similar", "divide and conquer", "dp", "greedy", "strings", "trees" ]
#pragma optimize("Bismillahirrahmanirrahim") //█▀█─█──█──█▀█─█─█ //█▄█─█──█──█▄█─█■█ //█─█─█▄─█▄─█─█─█─█ //Allahuekber //FatihSultanMehmedHan //YavuzSultanSelimHan //AbdulhamidHan //Sani buyuk Osman Pasa Plevneden cikmam diyor. //ahmet23 orz... #define author tolbi #include <bits/stdc++.h> #define int long long #define endl '\n' #define deci(x) int x;cin>>x; #define decstr(x) string x;cin>>x; #define cinarr(x) for (auto &it : x) cin>>it; #define coutarr(x) for (auto &it : x) cout<<it<<" ";cout<<endl; #define vint(x) vector<int> x #define sortarr(x) sort(x.begin(),x.end()) #define sortrarr(x) sort(x.rbegin(), x.rend()) #define tol(bi) (1ll<<((int)(bi))) #define revarr(x) reverse(x.begin(), x.end()) #define det(x) cout<<"NO\0YES"+(!!(x))*3<<endl; #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; const int MOD = 1e9+7; mt19937 ayahya(chrono::high_resolution_clock().now().time_since_epoch().count()); struct Trie{ struct Node{ int lnode, rnode; int kac; int dept; Node():lnode(-1),rnode(-1),kac(0){} }; vector<Node> trie; Trie(){trie.push_back(Node());} void insert(int val){ int node = 0ll; for (int bit = 31; bit >= 0; bit--){ if (val&tol(bit)){ if (trie[node].rnode==-1) { trie[node].rnode=trie.size(); trie.push_back(Node()); trie.back().dept=bit; } node=trie[node].rnode; } else { if (trie[node].lnode==-1){ trie[node].lnode=trie.size(); trie.push_back(Node()); trie.back().dept=bit; } node=trie[node].lnode; } trie[node].kac++; } } int query(int node = 0){ if (trie[node].lnode==-1 && trie[node].rnode==-1){ return 0ll; } if (trie[node].lnode!=-1 && trie[node].rnode!=-1){ return tol(trie[trie[node].lnode].dept)+min(query(trie[node].lnode),query(trie[node].rnode)); } else if (trie[node].lnode==-1){ return query(trie[node].rnode); } else { return query(trie[node].lnode); } } }; int32_t main(){ ios; int T = 1; if (!T) cin>>T; int tno = 0; while (T-(tno++)){ Trie trie; deci(n); for (int i = 0; i < n; ++i) { deci(x); trie.insert(x); } cout<<trie.query()<<endl; } }
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 repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are gg days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next bb days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again gg good days, bb bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days 1,2,…,g1,2,…,g are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the n=5n=5 then at least 33 units of the highway should have high quality; if n=4n=4 then at least 22 units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?InputThe first line contains a single integer TT (1≤T≤1041≤T≤104) — the number of test cases.Next TT lines contain test cases — one per line. Each line contains three integers nn, gg and bb (1≤n,g,b≤1091≤n,g,b≤109) — the length of the highway and the number of good and bad days respectively.OutputPrint TT integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.ExampleInputCopy3 5 1 1 8 10 10 1000000 1 1000000 OutputCopy5 8 499999500000 NoteIn the first test case; you can just lay new asphalt each day; since days 1,3,51,3,5 are good.In the second test case, you can also lay new asphalt each day, since days 11-88 are good.
[ "math" ]
/*********** MK-1311 ***********/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define nl '\n' #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif /* */ #define int long long void solve() { int n, g, b; cin >> n >> g >> b; int need = ceil(n / 2.0); int c = (need / g) * (g + b); debug(c); int left = need % g; if(left == 0) { c = max(c - b, n); } else { c = max(c + left, n); } cout << c << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; cin >> tt; for(int i = 0; i < tt; i++) { solve(); } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; return 0; }
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,4}.Let's define fk(a)fk(a), as applying ff to array aa kk times: fk(a)=f(fk−1(a)),f0(a)=afk(a)=f(fk−1(a)),f0(a)=a. E.g., f2({5,5,1,2,5,2,3,3,9,5})={1,2,2}f2({5,5,1,2,5,2,3,3,9,5})={1,2,2}.You are given integers n,kn,k and you are asked how many different values the function fk(a)fk(a) can have, where aa is arbitrary non-empty array with numbers of size no more than nn. Print the answer modulo 998244353998244353.InputThe first and only line of input consists of two integers n,kn,k (1≤n,k≤20201≤n,k≤2020).OutputPrint one number — the number of different values of function fk(a)fk(a) on all possible non-empty arrays with no more than nn elements modulo 998244353998244353.ExamplesInputCopy3 1 OutputCopy6 InputCopy5 6 OutputCopy1 InputCopy10 1 OutputCopy138 InputCopy10 2 OutputCopy33
[ "dp" ]
#include<bits/stdc++.h> #define LL long long #define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout); using namespace std; const int N=2e3+20,mod=998244353; int tot,n,k,ans; vector<int>a; inline void slove1() { static int f[N+5],g[N+5]; for(int i=1;;i++) { if(i*(3*i-1)/2<=N) g[++tot]=i*(3*i-1)/2; else break; if(i*(3*i+1)/2<=N) g[++tot]=i*(3*i+1)/2; else break; } f[0]=1; for(int i=1;i<=N;i++) { for(int j=1;j<=tot&&g[j]<=i;++j) if(((j+1)/2)&1) f[i]=(f[i]+f[i-g[j]])%mod; else f[i]=(f[i]-f[i-g[j]]+mod)%mod; } for(int i=2;i<=N;i++) f[i]=(f[i]+f[i-1])%mod; printf("%d\n",f[n]); } inline void slove2() { static int f[N+5];f[0]=1; for(int i=1;i*(i+1)/2<=N;i++) for(int j=i*(i+1)/2;j<=N;j++) f[j]=(f[j]+f[j-i*(i+1)/2])%mod; for(int i=2;i<=N;i++) f[i]=(f[i]+f[i-1])%mod; printf("%d\n",f[n]); } inline bool cmp(const int &x,const int &y){return x>y;} inline bool chk() { vector<int>b=a,c; for(int i=1,s;i<k;i++) { sort(b.begin(),b.end(),cmp);s=0; for(int j=0;j<b.size();j++) s+=b[j]*(j+1); if(s>n||(i+3<k&&s>23)) return 0; for(int j=0;j<b.size();j++) for(int K=1;K<=b[j];K++) c.push_back(j+1); b=c;c.clear(); } return 1; } bool dfs(int st) { if(!chk()) return 0;ans++; for(int i=st,t;;i++) { a.push_back(i);t=dfs(i);a.pop_back(); if(!t) return 1; }return 1; } int main() { // fr(function) scanf("%d%d",&n,&k); if(k==1) slove1(); else if(k==2) slove2(); else dfs(1),printf("%d",ans-1); return 0; }
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 sufficient. After almost 3 years; it's time for Ivy to awake her bonded sister, Vanessa.The system inside A.R.C.'s Library core can be considered as an undirected graph with infinite number of processing nodes, numbered with all positive integers (1,2,3,…1,2,3,…). The node with a number xx (x>1x>1), is directly connected with a node with number xf(x)xf(x), with f(x)f(x) being the lowest prime divisor of xx.Vanessa's mind is divided into nn fragments. Due to more than 500 years of coma, the fragments have been scattered: the ii-th fragment is now located at the node with a number ki!ki! (a factorial of kiki).To maximize the chance of successful awakening, Ivy decides to place the samples in a node PP, so that the total length of paths from each fragment to PP is smallest possible. If there are multiple fragments located at the same node, the path from that node to PP needs to be counted multiple times.In the world of zeros and ones, such a requirement is very simple for Ivy. Not longer than a second later, she has already figured out such a node.But for a mere human like you, is this still possible?For simplicity, please answer the minimal sum of paths' lengths from every fragment to the emotion samples' assembly node PP.InputThe first line contains an integer nn (1≤n≤1061≤n≤106) — number of fragments of Vanessa's mind.The second line contains nn integers: k1,k2,…,knk1,k2,…,kn (0≤ki≤50000≤ki≤5000), denoting the nodes where fragments of Vanessa's mind are located: the ii-th fragment is at the node with a number ki!ki!.OutputPrint a single integer, denoting the minimal sum of path from every fragment to the node with the emotion samples (a.k.a. node PP).As a reminder, if there are multiple fragments at the same node, the distance from that node to PP needs to be counted multiple times as well.ExamplesInputCopy3 2 1 4 OutputCopy5 InputCopy4 3 1 4 4 OutputCopy6 InputCopy4 3 1 4 1 OutputCopy6 InputCopy5 3 1 4 1 5 OutputCopy11 NoteConsidering the first 2424 nodes of the system; the node network will look as follows (the nodes 1!1!, 2!2!, 3!3!, 4!4! are drawn bold):For the first example, Ivy will place the emotion samples at the node 11. From here: The distance from Vanessa's first fragment to the node 11 is 11. The distance from Vanessa's second fragment to the node 11 is 00. The distance from Vanessa's third fragment to the node 11 is 44. The total length is 55.For the second example, the assembly node will be 66. From here: The distance from Vanessa's first fragment to the node 66 is 00. The distance from Vanessa's second fragment to the node 66 is 22. The distance from Vanessa's third fragment to the node 66 is 22. The distance from Vanessa's fourth fragment to the node 66 is again 22. The total path length is 66.
[ "dp", "graphs", "greedy", "math", "number theory", "trees" ]
#include <bits/stdc++.h> #define mod 998244353 #define int long long using namespace std; vector<int> p[5005]; int lst[5005],cnt[5005],w[5005]; vector<int> s[5005]; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i=2;i<=5000;i++) { if(p[i].empty()) { for(int j=i;j<=5000;j+=i) { int x=j; while(x%i==0) { p[j].push_back(i); x/=i; } } for(int j=i;j<=5000;j++) lst[j]=i; } } for(int i=2;i<=5000;i++) w[i]=p[i].size()+w[i-1]; int sum=0,L=0,R=0; long long ans=0; for(int i=1;i<=n;i++) { int x; cin >> x; ++cnt[x]; ans+=w[x]; } for(int i=2;i<=5000;i++) { if(lst[i]!=lst[i-1]) sum=0; sum+=cnt[i]; if(sum>n/2) { for(int j=1;j<=5000;j++) if(lst[j]==lst[i]){if(!L) L=j;R=j;} break; } } for(int i=L;i<=R;i++) { for(int j=1;j<=i;j++) for(auto t:p[j]) s[i].push_back(t); sort(s[i].begin(),s[i].end()); reverse(s[i].begin(),s[i].end()); } int nowl=L,nowr=R; for(int len=1;len<=20000;len++) { if(!nowl) break; int sum=0; for(int i=nowl;i<=nowr;i++) sum+=cnt[i]; ans-=sum,ans+=n-sum; map<int,int> mp; int pos=-1; for(int j=nowl;j<=nowr;j++) { if(s[j].size()<=len) continue; if((mp[s[j][len]]+=cnt[j])>n/2) { pos=s[j][len]; break; } } int L=0,R=0; for(int j=nowl;j<=nowr;j++) { if(s[j].size()<=len) continue; if(s[j][len]==pos) { if(!L) L=j; R=j; } } nowl=L,nowr=R; } cout << ans; return 0; }
cpp
1296
A
A. Array with Odd Sumtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn integers.In one move, you can choose two indices 1≤i,j≤n1≤i,j≤n such that i≠ji≠j and set ai:=ajai:=aj. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose ii and jj and replace aiai with ajaj).Your task is to say if it is possible to obtain an array with an odd (not divisible by 22) sum of elements.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤20001≤t≤2000) — the number of test cases.The next 2t2t lines describe test cases. The first line of the test case contains one integer nn (1≤n≤20001≤n≤2000) — the number of elements in aa. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤ai≤2000), where aiai is the ii-th element of aa.It is guaranteed that the sum of nn over all test cases does not exceed 20002000 (∑n≤2000∑n≤2000).OutputFor each test case, print the answer on it — "YES" (without quotes) if it is possible to obtain the array with an odd sum of elements, and "NO" otherwise.ExampleInputCopy5 2 2 3 4 2 2 8 8 3 3 3 3 4 5 5 5 5 4 1 1 1 1 OutputCopyYES NO YES NO NO
[ "math" ]
#include <bits/stdc++.h> using namespace std; #define print(a) for(auto x : a) {cout << x << " ";} cout << endl; typedef long long lli; typedef vector<int> vi; typedef vector<lli> vl; int main(){ int t; cin >> t; while (t --){ int n; cin >> n; int o = 0, e = 0; for (int i = 0; i < n; i++){ int k; cin >> k; if (k % 2 == 0){ e ++; } else { o ++; } } if (o % 2 != 0){ cout << "YES" << endl; } else if (o % 2 == 0 && e > 0 && o > 0){ cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
cpp
1288
D
D. Minimax Problemtime limit per test5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given nn arrays a1a1, a2a2, ..., anan; each array consists of exactly mm integers. We denote the yy-th element of the xx-th array as ax,yax,y.You have to choose two arrays aiai and ajaj (1≤i,j≤n1≤i,j≤n, it is possible that i=ji=j). After that, you will obtain a new array bb consisting of mm integers, such that for every k∈[1,m]k∈[1,m] bk=max(ai,k,aj,k)bk=max(ai,k,aj,k).Your goal is to choose ii and jj so that the value of mink=1mbkmink=1mbk is maximum possible.InputThe first line contains two integers nn and mm (1≤n≤3⋅1051≤n≤3⋅105, 1≤m≤81≤m≤8) — the number of arrays and the number of elements in each array, respectively.Then nn lines follow, the xx-th line contains the array axax represented by mm integers ax,1ax,1, ax,2ax,2, ..., ax,max,m (0≤ax,y≤1090≤ax,y≤109).OutputPrint two integers ii and jj (1≤i,j≤n1≤i,j≤n, it is possible that i=ji=j) — the indices of the two arrays you have to choose so that the value of mink=1mbkmink=1mbk is maximum possible. If there are multiple answers, print any of them.ExampleInputCopy6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 OutputCopy1 5
[ "binary search", "bitmasks", "dp" ]
#include<bits/stdc++.h> //#pragma GCC optimize(2) //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(l,r)(rng) using namespace std; typedef long long ll; typedef unsigned long long ull; #define endl '\n' #define INF 0x7FFFFFFF #define ch(a) (int(a-'a')+1) const int maxn=3*1e5+11; const int Maxx=1e6+11; const int mod=998244353; int t; int n,m; int a[maxn][10]; int vis[(1ll<<8)+2]; pair<int,int>Ans; bool check(int x) { for(int i=0;i<(1ll<<m);++i) vis[i]=0; vector<pair<int,int>>v; for(int i=1;i<=n;++i) { int res=0; for(int j=1;j<=m;++j) { if(a[i][j]>=x) res|=(1<<(j-1)); } if(vis[res]) continue; vis[res]=1; v.emplace_back(res,i); } int f=0; for(int i=0;i<v.size();++i) { for(int j=i;j<v.size();++j) { if((v[i].first|v[j].first)==(1<<m)-1) { Ans.first=v[i].second; Ans.second=v[j].second; f=1;break; } } } if(f) return true; return false; } void solve() { cin>>n>>m; int l=1e9+2,r=0; for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { cin>>a[i][j]; l=min(l,a[i][j]); r=max(r,a[i][j]); } } Ans.first=1;Ans.second=1; while(l<r) { int mid=(l+r+1)>>1; if(check(mid)) l=mid; else r=mid-1; } cout<<Ans.first<<" "<<Ans.second<<endl; } int main() { //scanf("%d",&t); ios::sync_with_stdio(false);cin.tie(0); //ios::sync_with_stdio(false);cin.tie(0);cin>>t;while(t--) solve(); 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 aa and bb. Similarly, LCM(a,b)LCM(a,b) is the smallest integer such that both aa and bb divide it.It's guaranteed that the solution always exists. If there are several such pairs (a,b)(a,b), you can output any of them.InputThe first line contains a single integer tt (1≤t≤100)(1≤t≤100)  — the number of testcases.Each testcase consists of one line containing a single integer, xx (2≤x≤109)(2≤x≤109).OutputFor each testcase, output a pair of positive integers aa and bb (1≤a,b≤109)1≤a,b≤109) such that GCD(a,b)+LCM(a,b)=xGCD(a,b)+LCM(a,b)=x. It's guaranteed that the solution always exists. If there are several such pairs (a,b)(a,b), you can output any of them.ExampleInputCopy2 2 14 OutputCopy1 1 6 4 NoteIn the first testcase of the sample; GCD(1,1)+LCM(1,1)=1+1=2GCD(1,1)+LCM(1,1)=1+1=2.In the second testcase of the sample, GCD(6,4)+LCM(6,4)=2+12=14GCD(6,4)+LCM(6,4)=2+12=14.
[ "constructive algorithms", "greedy", "number theory" ]
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll t; cin>>t; while(t--){ ll x; cin>>x; cout<<x - 1<<" "<<1<<endl; } return 0; }
cpp
1295
A
A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of 77 segments which can be turned on and off to compose different digits. The following picture describes how you can display all 1010 decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display 11, you have to turn on 22 segments of the screen, and if you want to display 88, all 77 segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than nn segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than nn segments.Your program should be able to process tt different test cases.InputThe first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases in the input.Then the test cases follow, each of them is represented by a separate line containing one integer nn (2≤n≤1052≤n≤105) — the maximum number of segments that can be turned on in the corresponding testcase.It is guaranteed that the sum of nn over all test cases in the input does not exceed 105105.OutputFor each test case, print the greatest integer that can be displayed by turning on no more than nn segments of the screen. Note that the answer may not fit in the standard 3232-bit or 6464-bit integral data type.ExampleInputCopy2 3 4 OutputCopy7 11
[ "greedy" ]
#include <iostream> using namespace std; int main() { int n,x; cin >> n; for (int i=0; i <n; i++){ cin >> x; if (x%2==0){ while (x>0){ x-=2; cout <<1; } cout << endl; } else{ while (x>0) { if (x%2!=0) { x -= 3; cout << 7; } else if (x%2==0) { cout << 1; x-=2; } } } cout << endl; } }
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[i:i+k−1] of ss. For example, if string ss is qwer and k=2k=2, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length 22) weqr (after reversing the second substring of length 22) werq (after reversing the last substring of length 22) Hence, the resulting string after modifying ss with k=2k=2 is werq. Vasya wants to choose a kk such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of kk. Among all such kk, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.A string aa is lexicographically smaller than a string bb if and only if one of the following holds: aa is a prefix of bb, but a≠ba≠b; in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb. InputEach test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50001≤t≤5000). The description of the test cases follows.The first line of each test case contains a single integer nn (1≤n≤50001≤n≤5000) — the length of the string ss.The second line of each test case contains the string ss of nn lowercase latin letters.It is guaranteed that the sum of nn over all test cases does not exceed 50005000.OutputFor each testcase output two lines:In the first line output the lexicographically smallest string s′s′ achievable after the above-mentioned modification. In the second line output the appropriate value of kk (1≤k≤n1≤k≤n) that you chose for performing the modification. If there are multiple values of kk that give the lexicographically smallest string, output the smallest value of kk among them.ExampleInputCopy6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p OutputCopyabab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 NoteIn the first testcase of the first sample; the string modification results for the sample abab are as follows : for k=1k=1 : abab for k=2k=2 : baba for k=3k=3 : abab for k=4k=4 : babaThe lexicographically smallest string achievable through modification is abab for k=1k=1 and 33. Smallest value of kk needed to achieve is hence 11.
[ "brute force", "constructive algorithms", "implementation", "sortings", "strings" ]
#include<bits/stdc++.h> #include<stdio.h> #define T(n) for(int i=0; i<n; i++) #define T2(k) for(int j=0; j<k; j++) #define fi first #define se second #define os ios_base::sync_with_stdio(false); #define INFI 0x3f3f3f3f using namespace std; typedef long long ll; int arr[129]; int arr2[2000006]; int main(){ int aan; cin >> aan; T(aan){ int n; string s; cin >> n >> s; map<char, int> mp; for(int i = 0; i < n; i++) { mp[s[i]]++; } auto mn = mp.begin(); int l = -1; string ans; char c = mn->fi; for(int i = 0; i < n; i++) { if(s[i] == c) { string add = s.substr(0, i); if((n - i) % 2 != 0) { reverse(add.begin(), add.end()); } string t = s.substr(i) + add; if(l == -1) { l = i; ans = t; } else if(ans > t) { ans = t; l = i; } } } cout << ans << "\n" << l+1 << "\n"; } return 0; }
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 share a side, and the elements in these cells are equal. Two cells of the matrix belong to the same connected component if there exists a sequence s1s1, s2s2, ..., sksk such that s1s1 is the first cell, sksk is the second cell, and for every i∈[1,k−1]i∈[1,k−1], sisi and si+1si+1 are connected.You are given qq queries of the form xixi yiyi cici (i∈[1,q]i∈[1,q]). For every such query, you have to do the following: replace the element ax,yax,y with cc; count the number of connected components in the matrix. There is one additional constraint: for every i∈[1,q−1]i∈[1,q−1], ci≤ci+1ci≤ci+1.InputThe first line contains three integers nn, mm and qq (1≤n,m≤3001≤n,m≤300, 1≤q≤2⋅1061≤q≤2⋅106) — the number of rows, the number of columns and the number of queries, respectively.Then qq lines follow, each representing a query. The ii-th line contains three integers xixi, yiyi and cici (1≤xi≤n1≤xi≤n, 1≤yi≤m1≤yi≤m, 1≤ci≤max(1000,⌈2⋅106nm⌉)1≤ci≤max(1000,⌈2⋅106nm⌉)). For every i∈[1,q−1]i∈[1,q−1], ci≤ci+1ci≤ci+1.OutputPrint qq integers, the ii-th of them should be equal to the number of components in the matrix after the first ii queries are performed.ExampleInputCopy3 2 10 2 1 1 1 2 1 2 2 1 1 1 2 3 1 2 1 2 2 2 2 2 2 1 2 3 2 4 2 1 5 OutputCopy2 4 3 3 4 4 4 2 2 4
[ "dsu", "implementation" ]
// LUOGU_RID: 96148485 #define FASTIO #include<bits/stdc++.h> //#include<ext/pb_ds/assoc_container.hpp> //#include<ext/pb_ds/hash_policy.hpp> //#include<ext/pb_ds/priority_queue.hpp> using namespace std; //using namespace __gnu_pbds; #define fi first #define se second #define fill0(a) memset(a,0,sizeof(a)) #define fill1(a) memset(a,-1,sizeof(a)) #define fillbig(a) memset(a,63,sizeof(a)) #define pb push_back #define ppb pop_back #define mp make_pair #define mt make_tuple #define SZ(v) ((int)v.size()) #ifdef LOCAL #define eprintf(...) fprintf(stderr,__VA_ARGS__) #else #define eprintf(...) 1064 #endif template<typename T1,typename T2>void chkmin(T1 &x,T2 y){if(x>y)x=y;} template<typename T1,typename T2>void chkmax(T1 &x,T2 y){if(x<y)x=y;} typedef pair<int,int> pii; typedef long long ll; typedef unsigned int u32; typedef unsigned long long u64; typedef long double ld; #ifdef FASTIO #define FILE_SIZE 1<<23 char rbuf[FILE_SIZE],*p1=rbuf,*p2=rbuf,wbuf[FILE_SIZE],*p3=wbuf; #ifdef LOCAL inline char getc(){return getchar();} inline void putc(char c){putchar(c);} #else inline char getc(){return p1==p2&&(p2=(p1=rbuf)+fread(rbuf,1,FILE_SIZE,stdin),p1==p2)?-1:*p1++;} inline void putc(char x){*p3++=x;} #endif template<typename T>void read(T &x){ x=0;char c=getc();T neg=0; while(!isdigit(c))neg|=(c=='-'),c=getc(); while(isdigit(c))x=x*10+(c-'0'),c=getc(); if(neg)x=-x; } template<typename T>void recursive_print(T x){if(!x)return;recursive_print(x/10);putc(x%10^48);} template<typename T>void print(T x){if(!x)putc('0');if(x<0)putc('-'),x=-x;recursive_print(x);} template<typename T>void print(T x,char c){print(x);putc(c);} void readstr(char *s){char c=getc();while(c<=32||c>=127)c=getc();while(c>32&&c<127)s[0]=c,s++,c=getc();(*s)=0;} void printstr(string s){for(int i=0;i<s.size();i++)putc(s[i]);} void printstr(char *s){int len=strlen(s);for(int i=0;i<len;i++)putc(s[i]);} void print_final(){fwrite(wbuf,1,p3-wbuf,stdout);} #endif const int MAXQ=2e6; const int MAXN=300; const int dx[]={1,0,-1,0}; const int dy[]={0,1,0,-1}; int n,m,qu,x[MAXQ+5],y[MAXQ+5],c[MAXQ+5],a[MAXN+5][MAXN+5],res[MAXQ+5]; int b[MAXN+5][MAXN+5],pre[MAXN+5][MAXN+5],f[MAXN*MAXN+5]; int getid(int x,int y){return (x-1)*m+y;} int find(int x){return (!f[x])?x:f[x]=find(f[x]);} bool merge(int x,int y){x=find(x);y=find(y);if(x!=y)return f[x]=y,1;return 0;} int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif read(n);read(m);read(qu); for(int i=1;i<=qu;i++)read(x[i]),read(y[i]),read(c[i]); for(int l=1,r;l<=qu;l=r){ r=l;while(r<=qu&&c[r]==c[l])++r; for(int j=1;j<=n;j++)for(int k=1;k<=m;k++)b[j][k]=-1; for(int j=1;j<=n;j++)for(int k=1;k<=m;k++)pre[j][k]=a[j][k]; static tuple<int,int,int,int>vec[MAXN*MAXN+5]; int C=0; for(int j=l;j<r;j++)if(pre[x[j]][y[j]]!=c[j]) vec[++C]=mt(x[j],y[j],pre[x[j]][y[j]],j-1),pre[x[j]][y[j]]=c[j]; for(int j=1;j<=n;j++)for(int k=1;k<=m;k++)if(pre[j][k]!=c[l]) vec[++C]=mt(j,k,pre[j][k],r); reverse(vec+1,vec+C+1); for(int j=1;j<=n*m;j++)f[j]=0;int cmp=0; for(int j=l;j<r;j++){ if(a[x[j]][y[j]]!=c[j]){ cmp++; for(int d=0;d<4;d++){ int xx=x[j]+dx[d],yy=y[j]+dy[d]; if(xx<1||xx>n||yy<1||yy>m||a[xx][yy]!=c[j])continue; cmp-=merge(getid(x[j],y[j]),getid(xx,yy)); }a[x[j]][y[j]]=c[j]; }res[j]+=cmp; } for(int j=1;j<=n*m;j++)f[j]=0;cmp=0; for(int j=r-1,k=1;j>=l;j--){ while(k<=C&&get<3>(vec[k])>=j){ int X=get<0>(vec[k]),Y=get<1>(vec[k]),V=get<2>(vec[k]);cmp++; for(int d=0;d<4;d++){ int xx=X+dx[d],yy=Y+dy[d]; if(xx<1||xx>n||yy<1||yy>m||b[xx][yy]!=V)continue; cmp-=merge(getid(X,Y),getid(xx,yy)); }b[X][Y]=V;++k; }res[j]+=cmp; } } for(int i=1;i<=qu;i++)print(res[i],'\n'); print_final(); return 0; }
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 means that if the frog is staying at the ii-th cell and the ii-th character is 'L', the frog can jump only to the left. If the frog is staying at the ii-th cell and the ii-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 00.Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the n+1n+1-th cell. The frog chooses some positive integer value dd before the first jump (and cannot change it later) and jumps by no more than dd cells at once. I.e. if the ii-th character is 'L' then the frog can jump to any cell in a range [max(0,i−d);i−1][max(0,i−d);i−1], and if the ii-th character is 'R' then the frog can jump to any cell in a range [i+1;min(n+1;i+d)][i+1;min(n+1;i+d)].The frog doesn't want to jump far, so your task is to find the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it can jump by no more than dd cells at once. It is guaranteed that it is always possible to reach n+1n+1 from 00.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.The next tt lines describe test cases. The ii-th test case is described as a string ss consisting of at least 11 and at most 2⋅1052⋅105 characters 'L' and 'R'.It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2⋅1052⋅105 (∑|s|≤2⋅105∑|s|≤2⋅105).OutputFor each test case, print the answer — the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it jumps by no more than dd at once.ExampleInputCopy6 LRLRRLL L LLR RRRR LLLLLL R OutputCopy3 2 3 1 7 1 NoteThe picture describing the first test case of the example and one of the possible answers:In the second test case of the example; the frog can only jump directly from 00 to n+1n+1.In the third test case of the example, the frog can choose d=3d=3, jump to the cell 33 from the cell 00 and then to the cell 44 from the cell 33.In the fourth test case of the example, the frog can choose d=1d=1 and jump 55 times to the right.In the fifth test case of the example, the frog can only jump directly from 00 to n+1n+1.In the sixth test case of the example, the frog can choose d=1d=1 and jump 22 times to the right.
[ "binary search", "data structures", "dfs and similar", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; #define ll long long #define fst first #define sec second #define mod (ll)(1e9+7) void solve(){ string s;cin >> s; ll ans=0; ll ct=0; for(ll i=0;i<s.size();i++){ if(s[i]=='L') ct++; else{ ans=max(ans,ct); ct=0; } } ans=max(ans,ct); cout << ans+1 << endl; } int main(){ ios::sync_with_stdio(0); cin.tie(0); ll t;cin >> t; while(t--){ solve(); } return 0; }
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 audience support, so she wants to select kk people as part of the audience.There are nn people in Byteland. Alice needs to select exactly pp players, one for each position, and exactly kk members of the audience from this pool of nn people. Her ultimate goal is to maximize the total strength of the club.The ii-th of the nn persons has an integer aiai associated with him — the strength he adds to the club if he is selected as a member of the audience.For each person ii and for each position jj, Alice knows si,jsi,j  — the strength added by the ii-th person to the club if he is selected to play in the jj-th position.Each person can be selected at most once as a player or a member of the audience. You have to choose exactly one player for each position.Since Alice is busy, she needs you to help her find the maximum possible strength of the club that can be achieved by an optimal choice of players and the audience.InputThe first line contains 33 integers n,p,kn,p,k (2≤n≤105,1≤p≤7,1≤k,p+k≤n2≤n≤105,1≤p≤7,1≤k,p+k≤n).The second line contains nn integers a1,a2,…,ana1,a2,…,an. (1≤ai≤1091≤ai≤109).The ii-th of the next nn lines contains pp integers si,1,si,2,…,si,psi,1,si,2,…,si,p. (1≤si,j≤1091≤si,j≤109)OutputPrint a single integer resres  — the maximum possible strength of the club.ExamplesInputCopy4 1 2 1 16 10 3 18 19 13 15 OutputCopy44 InputCopy6 2 3 78 93 9 17 13 78 80 97 30 52 26 17 56 68 60 36 84 55 OutputCopy377 InputCopy3 2 1 500 498 564 100002 3 422332 2 232323 1 OutputCopy422899 NoteIn the first sample; we can select person 11 to play in the 11-st position and persons 22 and 33 as audience members. Then the total strength of the club will be equal to a2+a3+s1,1a2+a3+s1,1.
[ "bitmasks", "dp", "greedy", "sortings" ]
#include<bits/stdc++.h> #include<chrono> #include<random> #include<assert.h> #include<stdexcept> #include<iostream> #include<cmath> #include<algorithm> #include<bitset> #include<iomanip> #include<map> #include<vector> #define num1bit(x) __builtin_popcount(x) #define C continue #define B break #define R return #define ll long long int #define ld long double #define ull unsigned long long #define nd second #define st first #define bn begin() #define ed end() #define wte long Tests ; cin >> Tests ; while(Tests--) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define b(myint,z) bitset<64>z(myint) ; cout<<z<<endl ; #define endl '\n' #define pb push_back #define SPR(a) cout<<fixed<<setprecision(a) using namespace std; const int oo = (int) 1e9 + 7; const ld eps = 1e-8; const ld PI = 3.14159265359; const ll lloo = (ll) 1e18; const int mod = 998244353; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll Pow(ll a, ll b, ll mod = oo){ ll res = 1;if(b < 0 || b >= mod)b = (b % (mod - 1) + mod - 1) % (mod - 1); for(;b;b >>= 1, a = 1ll * a * a % mod) if(b & 1)res = 1ll * res * a % mod; return res; } ld ldPow(ld a, ll b){ ld res = 1; for(;b;b >>= 1, a = 1.0 * a * a) if(b & 1)res = 1.0 * res * a; return res; } ll inverseMod(ll A, ll bB, ll mod = oo){ ll ans = Pow(bB, mod - 2, mod); ans %= mod; ans *= A; ans %= mod; return ans; } ll segma(ll x, ll y, ll mod = oo){ ll res = x + y; res *= (max(x, y) - min(y, x) + 1); R res / 2; } ll mid(ll l, ll r){ R(r - l) / 2 + l; } ll fac(ll a, ll mod){ if(a == 0) R 1; R(a * (fac(a - 1, mod) % mod)) % mod; } ll LCM(ll x, ll y){ R(x * y) / (ll) __gcd(x, y); } template<typename t> t intlog2(t x){ t res = 0; while(x > 0){ res++; x /= 2; } R res; } template<typename t> bool sec(pair<t, t> a, pair<t, t> b){ R a.st - a.nd > b.st - b.nd; }; template<typename t> bool secr(pair<t, t> a, pair<t, t> b){ if(a.st < b.st) R 1; if(a.st > b.st) R 0; if(a.nd < b.nd) R 0; if(a.nd >= b.nd) R 1; }; struct point{ ld x{}, y{}; point() = default; point(ld _x, ld _y) : x(_x), y(_y){} point operator+(const point& p) const{ return point(x + p.x, y + p.y); } point operator-(const point& p) const{ return point(x - p.x, y - p.y); } ld cross(const point& p) const{ return x * p.y - y * p.x; } ld dot(const point& p) const{ return x * p.x + y * p.y; } ld cross(const point& a, const point& b) const{ return (a - *this).cross(b - *this); } ld dot(const point& a, const point& b) const{ return (a - *this).dot(b - *this); } ld sqrLen() const{ return this->dot(*this); } void In(){ cin >> x >> y; } void Out() const{ cout << fixed << setprecision(7) << x << " " << y << endl; } }; ld L(point a, point b){ R(a.x - b.x)* (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } ll ccw(point a, point b, point c){ R(b.x - a.x)* (c.y - a.y) - (b.y - a.y) * (c.x - a.x); } //#define MPF #ifdef MPF const int N = 300300, M = 5050, NN = 10001005; int MinPrime[NN]; vector<int> Prime; void init(){ MinPrime[1] = 1; for(int i = 2;i < NN;i++){ if(MinPrime[i])C; Prime.pb(i); for(int j = i;j < NN;j += i){ if(MinPrime[j])C; MinPrime[j] = i; } } } #endif int n, p, k; const int NN=1e5+9; pair<pair<ll, vector<ll> >, int> a[NN]; ll dp[NN][(1<<8)]; int main(){ for(int i=0;i<NN;i++){ for(int j=0;j<(1<<8);j++) dp[i][j]=-oo; } cin >>n>>p>>k; for(int i=1;i<=n;i++){ cin >>a[i].st.st; a[i].nd=i; } for(int i=1;i<=n;i++){ for(int j=1;j<=p;j++){ int x; cin >>x; a[i].st.nd.pb(x); } } sort(a+1, a+n+1); dp[n+1][0]=0; // for(int i=1;i<=n;i++){ // cout<<a[i].st.st<<' '; // for(int j=0;j<p;j++) cout<<a[i].st.nd[j]<<' '; // cout<<endl ; // } for(int i=n;i>0;i--){ for(int mas=0;mas<=(1<<p)-1;mas++){ int cnt=num1bit(mas); if(dp[i+1][mas]!=-oo) dp[i][mas]=dp[i+1][mas]; if(n-i+1-cnt<=k) { if(dp[i+1][mas]!=-oo)dp[i][mas]+=a[i].st.st; } for(int pos=0;pos<p;pos++){ int l=1<<pos; if((l&mas)&&dp[i+1][(mas^l)]!=-oo) dp[i][mas]=max(dp[i][mas],dp[i+1][(mas^l)]+a[i].st.nd[pos]); } } } cout<<dp[1][(1<<p)-1] <<endl ; R 0; }
cpp
1325
D
D. Ehab the Xorcisttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven 2 integers uu and vv, find the shortest array such that bitwise-xor of its elements is uu, and the sum of its elements is vv.InputThe only line contains 2 integers uu and vv (0≤u,v≤1018)(0≤u,v≤1018).OutputIf there's no array that satisfies the condition, print "-1". Otherwise:The first line should contain one integer, nn, representing the length of the desired array. The next line should contain nn positive integers, the array itself. If there are multiple possible answers, print any.ExamplesInputCopy2 4 OutputCopy2 3 1InputCopy1 3 OutputCopy3 1 1 1InputCopy8 5 OutputCopy-1InputCopy0 0 OutputCopy0NoteIn the first sample; 3⊕1=23⊕1=2 and 3+1=43+1=4. There is no valid array of smaller length.Notice that in the fourth sample the array is empty.
[ "bitmasks", "constructive algorithms", "greedy", "number theory" ]
#include<iostream> #include<vector> #include<string> #include<cmath> #include<algorithm> #include<map> #include<unordered_map> #include<set> #include<stack> #include<queue> #include<iomanip> #include<bitset> #include<climits> #include<cstring> #include<list> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const long double pi = 3.141592653589793238462643383279; #define max(a,b) (a>b?a:b) #define min(a,b) (a<b?a:b) #define forb(i,a,b) for(ll i=a;i<b;i++) #define fo(i,a) for(ll i=0;i<a;i++) #define foa(i,a) for(auto i=a.begin();i!=a.end();i++) #define rep(i,a) for(ll i=a;i>-1;i--) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define vi vector<int> #define vll vector<ll> #define vvi vector<vector<int>> #define vvll vector<vector<ll>> #define vb vector<bool> #define vvb vector<vector<bool>> #define vc vector<char> #define vvc vector<vector<char>> #define pb push_back const ll mod=1e9+7; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif void printll(vector<ll> a){ ll n=a.size(); fo(i,n) cout<<a[i]<<' '; cout<<'\n'; } void printint(vector<int> a){ ll n=a.size(); fo(i,n) cout<<a[i]<<' '; cout<<'\n'; } bool prime(ll x){ if(x==1)return false; ll y=sqrt(x); forb(i,2,y+1){ if(x%i==0)return false; } return true; } ll gcd(ll a, ll b){ //b = a*q1 + r1; //a = r1*q2 + r2; //do until r2=0 if(a==0)return b; if(b==0)return a; if(a>b)swap(a,b); ll rem=b%a; while(rem){ b=a; a=rem; rem=b%a; } return a; } ll lcm(ll a, ll b){ return (a*b)/gcd(a,b); } vector<ll> computeLPS(string t){ vector<ll> lps(t.size()); ll len=0, i=1; while(i<ll(t.size())){ if(t[len]==t[i]){//characters match len++;//step 1 lps[i]=len;//step 2 i++;//step 3 } else{ if(len>0){//borderline case len=lps[len-1]; } else{//len==0 lps[i]=0; i++; } } } return lps; } bool KMPSearch(string s, string t){ //checks just the occurence of t in s //doesn't return the index of matching //but can be modified to do so vector<ll> lps=computeLPS(t); ll i=0, j=0; ll n=s.size(), m=t.size(); while(i<n){ if(s[i]==t[j]){ i++; j++; } if(j==m){ //if you want the first index of matching: return i-j; //if you wish to continue to find other matches: j=lps[j-1] return true; } else if(s[i]!=t[j]&&i<n){//characters don't match if(j!=0){//no need to match already matching characters j=lps[j-1]; } else{ i++; } } } return false; } string longestLPS(string s){ vector<ll> lps=computeLPS(s); ll mx=*max_element(all(lps)); return s.substr(0,mx); } bool bipart(vector<vector<int>> adjL, int n){ vector<int> col(n,-1); vector<int> visited(n,0); queue<int> q; fo(i,n){ if(visited[i]==0){ visited[i]=1; q.push(i); col[i]=0; } while(!q.empty()){ int v=q.front(); q.pop(); for(int c: adjL[v]){ if(visited[c]==0){ visited[c]=1; q.push(c); col[c]=col[v]?0:1; } if(col[c]==col[v])return false; } } } return true; } void bfs(int root, vector<vector<int>> vec, vector<int> &level, vector<int> &visited){ queue<int> q; q.push(root); level[root]=0; visited[root]=1; while(!q.empty()){ int v=q.front(); q.pop(); for(int c: vec[v]){ if(!visited[c]){ visited[c]=1; q.push(c); level[c]=level[v]+1; } } } } string min_to_time(int min){ //when 0<=HH<=23, 0<=MM<=59 //ex. //input: 1439, 1445 //output: 23:59, 00:05 if(min>1440) min%=1440; int hours=min/60; int seconds=min%60; string h=to_string(hours); if(h.size()==1)h="0"+h; if(h=="24")h="00"; // cout<<h<<' '; string s=to_string(seconds); if(s.size()==1)s="0"+s; // cout<<s<<' '; string final=h+":"+s; return final; } int s_to_time(string s){ //returns time in minutes for input string as HH:MM //e.x. //input: 12:21, 23:59 //output: 741, 1439 int time=0; string hours=s.substr(0,2); string min=s.substr(3,2); time=stoi(hours)*60+stoi(min); return time; } template<typename T> T kadane(vector<T> a){ //used to find the max. sum subarray in O(n) //doesn't work if ALL elements are negative //returns 0 if all elements are negative int n=a.size(); T cur=0, best=0; fo(i,n){ cur=max(a[i],a[i]+cur); best=max(cur,best); } return best; } class segTree{ public: int size; //no. of leaves in the segTree vll sums; void init(int n){ size=1; while(size<n)size*=2; sums.assign(2*size,0LL); } void set(int i, ll v, int x, int lx, int rx){ if(rx-lx==1){//element at index 'x' is a leaf sums[x]=v; return; } int m=(lx+rx)/2; if(i<m){ set(i,v,2*x+1,lx,m); }else{ set(i,v,2*x+2,m,rx); } sums[x]=sums[2*x+1]+sums[2*x+2]; } void set(int i, ll v){ set(i,v,0,0,size); } ll sum(int l, int r, int x, int lx, int rx){ if(l>=rx || r<=lx) return 0; if(lx>=l && rx<=r) return sums[x]; int m=(lx+rx)/2; ll val1=sum(l,r,2*x+1,lx,m); ll val2=sum(l,r,2*x+2,m,rx); return val1+val2; } ll sum(int l, int r){ return sum(l,r,0,0,size); } }; ll binExp(ll a, ll b){ //returns (a^b) in O(log(b)) //to understand the implementation, //consider the bitwise representation of 'b' ll ans=1; while(b){ if(b&1)ans=(ans*a); a=(a*a); b>>=1; } return ans; } ll sieve(ll n){ //returns no. of primes in the range [1,n] vb prime(n+1,1); for(ll i=2;i*i<=n;i++){ if(prime[i]){//if 'i' is prime(prime <==> '1' in vb prime) //marking all multiples of the prime number 'i' //from i*i upto n for(ll j=i*i;j<=n;j+=i)prime[j]=0; } } ll cnt=0; forb(i,1,n+1)cnt+=prime[i]; return cnt; } class DSU{ private: vi parent, rank; public: DSU(int n){ parent.resize(n+1); rank.resize(n+1); fo(i,n+1)parent[i]=i, rank[i]=0; } int findPar(int a){ if(a==parent[a])return a; return parent[a]=findPar(parent[a]); } void Union(int a, int b){ a=findPar(a); b=findPar(b); if(a==b)return; if(rank[a]==rank[b]){ parent[b]=a; rank[a]++; }else if(rank[a]<rank[b]){ parent[a]=b; }else parent[b]=a; } }; class trieNode{ public: vector<trieNode*> children=vector<trieNode*>(26); bool isLeaf;//whether there's a word ending at this node int v;//how many prefixes exist upto this trie node, for e.x. "abcd","abc", for 'b', v=2. trieNode(bool val){ v=0; isLeaf=val; fo(i,26){ children[i]=nullptr; } } }; class trie{ public: trieNode* root; trie(){ root=new trieNode(0); } void insert(string word){ trieNode* cur=root; for(char c: word){ if(cur->children[c-'a']==nullptr){ cur->children[c-'a']=new trieNode(0); } cur->children[c-'a']->v++; cur=cur->children[c-'a']; } cur->isLeaf=1; } bool search(string word){ trieNode* cur=root; for(auto c: word){ if(cur->children[c-'a']==nullptr)return 0; cur=cur->children[c-'a']; } return (cur->isLeaf); } bool startsWith(string pref){ //has been modified so that it works on multiple copies as well. //for e.x. when you want the longest common prefix of two strings in a set, this variant works. //for e.x. if the trie has only "abc", function returns 0 //if it has "abc" && "abcd", function returns 1 if(pref.empty())return 1; trieNode* cur=root; trieNode* prev=root; fo(i,pref.size()){ if(cur->children[pref[i]-'a']==nullptr)return 0; prev=cur; cur=cur->children[pref[i]-'a']; } return (cur!=nullptr && ((prev->children[pref.back()-'a']->v)>1)); } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll u,v; cin>>u>>v; if(u>v || u%2!=v%2){ cout<<"-1\n"; return 0; }else if(u==v && !u){ cout<<0<<'\n'; return 0; }else if(u==v){ cout<<1<<'\n'<<u<<'\n'; return 0; } bitset<63> a,b; bitset<63> andVal((v-u)/2); bool cant=0; fo(i,63){ if(andVal[i]){ if((u>>i)&1){ cant=1; }else{ a[i]=b[i]=1; } }else{ if((u>>i)&1){ a[i]=0,b[i]=1; }else{ a[i]=b[i]=0; } } } if(cant){ cout<<3<<'\n'; cout<<u<<' '<<(v-u)/2<<' '<<(v-u)/2<<'\n'; }else{ cout<<2<<'\n'; ll v1=a.to_ullong(), v2=b.to_ullong(); cout<<v1<<' '<<v2<<'\n'; } return 0; }
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 placed arbitrarily  — be inside each other, coincide and so on. Segments can degenerate into points, that is li=rili=ri is possible.Union of the set of segments is such a set of segments which covers exactly the same set of points as the original set. For example: if n=3n=3 and there are segments [3,6][3,6], [100,100][100,100], [5,8][5,8] then their union is 22 segments: [3,8][3,8] and [100,100][100,100]; if n=5n=5 and there are segments [1,2][1,2], [2,3][2,3], [4,5][4,5], [4,6][4,6], [6,6][6,6] then their union is 22 segments: [1,3][1,3] and [4,6][4,6]. Obviously, a union is a set of pairwise non-intersecting segments.You are asked to erase exactly one segment of the given nn so that the number of segments in the union of the rest n−1n−1 segments is maximum possible.For example, if n=4n=4 and there are segments [1,4][1,4], [2,3][2,3], [3,6][3,6], [5,7][5,7], then: erasing the first segment will lead to [2,3][2,3], [3,6][3,6], [5,7][5,7] remaining, which have 11 segment in their union; erasing the second segment will lead to [1,4][1,4], [3,6][3,6], [5,7][5,7] remaining, which have 11 segment in their union; erasing the third segment will lead to [1,4][1,4], [2,3][2,3], [5,7][5,7] remaining, which have 22 segments in their union; erasing the fourth segment will lead to [1,4][1,4], [2,3][2,3], [3,6][3,6] remaining, which have 11 segment in their union. Thus, you are required to erase the third segment to get answer 22.Write a program that will find the maximum number of segments in the union of n−1n−1 segments if you erase any of the given nn segments.Note that if there are multiple equal segments in the given set, then you can erase only one of them anyway. So the set after erasing will have exactly n−1n−1 segments.InputThe first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test. Then the descriptions of tt test cases follow.The first of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of segments in the given set. Then nn lines follow, each contains a description of a segment — a pair of integers lili, riri (−109≤li≤ri≤109−109≤li≤ri≤109), where lili and riri are the coordinates of the left and right borders of the ii-th segment, respectively.The segments are given in an arbitrary order.It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.OutputPrint tt integers — the answers to the tt given test cases in the order of input. The answer is the maximum number of segments in the union of n−1n−1 segments if you erase any of the given nn segments.ExampleInputCopy3 4 1 4 2 3 3 6 5 7 3 5 5 5 5 5 5 6 3 3 1 1 5 5 1 5 2 2 4 4 OutputCopy2 1 5
[ "brute force", "constructive algorithms", "data structures", "dp", "graphs", "sortings", "trees", "two pointers" ]
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") /* ╭╮╱╱╱╱╱╱╱╱╭━╮ ┃┃╱╱╱╱╱╱╱╱┃╭╯ ┃╰━┳━━┳━━┳╯╰╮ ┃╭╮┃╭╮┃╭╮┣╮╭╯ ┃┃┃┃╰╯┃╰╯┃┃┃ ╰╯╰┻━╮┣━╮┃╰╯ ╱╱╱╭━╯┣━╯┃ ╱╱╱╰━━┻━━╯ */ #include<bits/stdc++.h> using namespace std; #define itn int #define ll long long #define endl "\n" #define connect_yesno() string yesno(bool o,string yes="YES",string no="NO"){if(o){return yes;}return no;} #define connect_math() vector<bool> resheto_eratosfena(int n){vector<bool> prime(n+1,true);prime[0]=prime[1]=false;for(int i=2;i<=n;i++){if(prime[i]){if(prime[i]){if(i*1LL*i<=n){for(int j=i*i;j<=n;j+=i){prime[j]=false;}}}}}return prime;};bool primer(long long k){if(k==0 or k==1 or k<0 or !(k&1)){return false;}for(long long i=3LL;i*i<=k;i++){if(k%i==0){return false;}}return true;}int phunkcia_ailera(int n){int result=n;for(int i=2;i*i<=n;i++){if (n % i == 0) {while (n % i == 0){n /= i;}result -= result / i;}}if (n > 1){result -= result / n;}return result;}long long pow(long long a,long long n){int res = 1;while (n){if (n & 1) {res *= a;--n;}else {a *= a;n >>= 1;}}return res;}long long gcd (long long a, long long b) { while (b) { a %= b;swap (a, b);}return a;}long long findD(long long x1,long long y1,long long x2,long long y2){long long a=abs(x1-x2),b=abs(y1-y2);return sqrtl(a*a+b*b);}long long findS(long long a,long long b,long long c){long long p=(a+b+c)/2;return sqrtl(p*(p-a)*(p-b)*(p-c));} #define connect_inout() long long inll(){long long x;cin>>x;return x;} #define pb push_back #define mp make_pair #define pbmp(a,b) pb(mp(a,b)) #define F first #define S second connect_yesno(); connect_inout(); ll d[200][200]; void gogo(){ int n,mx=-1,t=0; cin>>n; itn cnt2[n+1]; fill(cnt2+1,cnt2+n+1,0); pair<int,int>q[2*n+1]; set<int>st; st.clear(); mx=-1; int cnt=0; for(int i=1;i<=n;i++){ int l,r; cin>>l>>r; cnt++; q[cnt]={l,-i}; cnt++; q[cnt]={r,i}; } sort(q+1,q+cnt+1); for(int i=1;i<=cnt;i++){ int j=q[i].second; if(j>0){ if(st.size()==1){ cnt2[j]--; } st.erase(j); }else{ if(st.empty()){ cnt2[-j]--; } st.insert(-j); } if(st.size()==1){ cnt2[*st.begin()]++; }else if(st.empty()){ t++; } } for(int i=1;i<=n;i++){ mx=max(mx,cnt2[i]); } cout<<mx+t<<endl; } main(){ int t; cin>>t; while(t--){ gogo(); } }
cpp
1295
A
A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of 77 segments which can be turned on and off to compose different digits. The following picture describes how you can display all 1010 decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display 11, you have to turn on 22 segments of the screen, and if you want to display 88, all 77 segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than nn segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than nn segments.Your program should be able to process tt different test cases.InputThe first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases in the input.Then the test cases follow, each of them is represented by a separate line containing one integer nn (2≤n≤1052≤n≤105) — the maximum number of segments that can be turned on in the corresponding testcase.It is guaranteed that the sum of nn over all test cases in the input does not exceed 105105.OutputFor each test case, print the greatest integer that can be displayed by turning on no more than nn segments of the screen. Note that the answer may not fit in the standard 3232-bit or 6464-bit integral data type.ExampleInputCopy2 3 4 OutputCopy7 11
[ "greedy" ]
#include<bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef pair<int, int> pi; #define ll long long #define pb push_back #define lb(s,x) lower_bound(s.begin(),s.end(),x) #define ub(s,x) upper_bound(s.begin(),s.end(),x) #define asort(p) sort(p.begin(),p.end()) #define dsort(p) sort(p.begin(),p.end(),greater<int>()) #define count(s,x) count(s.begin(),s.end(),x) //ceil function returns least greater or equal value //floor fucntion returns smaller or equal value to the number //upper case aplhabet-(65-90) //lower case aplhabet-(97-122) //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@___**___@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ void killer() { ll int n; cin>>n; if(n%2==0) { for(ll int i=0;i<n/2;i++) cout<<"1"; } else {cout<<"7"; for(ll int i=0;i<(n/2)-1;i++) { cout<<"1"; } } cout<<endl; } int main() { ll int t; cin>>t; while(t--) { killer(); } }
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 pipe with speed vivi. It flies to the right with probability pipi and flies to the left with probability (1−pi)(1−pi). The duration of the experiment is determined as the time of the first collision of any two protons. In case there is no collision, the duration of the experiment is considered to be zero.Find the expected value of the duration of the experiment.Illustration for the first exampleInputThe first line of input contains one integer nn — the number of pipes (1≤n≤1051≤n≤105). Each of the following nn lines contains three integers xixi, vivi, pipi — the coordinate of the ii-th pipe, the speed of the ii-th proton and the probability that the ii-th proton flies to the right in percentage points (−109≤xi≤109,1≤v≤106,0≤pi≤100−109≤xi≤109,1≤v≤106,0≤pi≤100). It is guaranteed that all xixi are distinct and sorted in increasing order.OutputIt's possible to prove that the answer can always be represented as a fraction P/QP/Q, where PP is an integer and QQ is a natural number not divisible by 998244353998244353. In this case, print P⋅Q−1P⋅Q−1 modulo 998244353998244353.ExamplesInputCopy2 1 1 100 3 1 0 OutputCopy1 InputCopy3 7 10 0 9 4 86 14 5 100 OutputCopy0 InputCopy4 6 4 50 11 25 50 13 16 50 15 8 50 OutputCopy150902884
[ "data structures", "math", "matrices", "probabilities" ]
// LUOGU_RID: 101728632 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 100001 #define yu (998244353) inline ll ksm(ll x,ll y){ ll an=1; for(;y;y>>=1){ if(y&1)an=an*x%yu; x=x*x%yu; }return an; }inline ll ni(ll x){return ksm(x,yu-2);} ll n; ll x[N],v[N],p[N]; ll xu[N]; struct juz{ ll a[3][3],n,m; juz(){ memset(a,0,sizeof(a));return; } inline juz operator*(const juz &x){ juz t;t.n=n;t.m=x.m; for(int i=1;i<=n;i++){ for(int j=1;j<=t.m;j++){ for(int k=1;k<=m;k++){ t.a[i][j]=(t.a[i][j]+a[i][k]*x.a[k][j])%yu; } } }return t; } }; juz sum[N<<2],a[N]; inline void pushup(ll o){ sum[o]=sum[o<<1]*sum[o<<1|1]; return ; } inline void build(ll o,ll l,ll r){ if(l==r){sum[o]=a[l];return ;} ll mid=(l+r)>>1; build(o<<1,l,mid);build(o<<1|1,mid+1,r); pushup(o); return ; } inline void update(ll o,ll l,ll r,ll x){ if(l==r){ sum[o]=a[l];return ; }ll mid=(l+r)>>1; if(mid>=x)update(o<<1,l,mid,x); else update(o<<1|1,mid+1,r,x); pushup(o); return ; } inline juz ask(ll o,ll l,ll r,ll x,ll y){ if(x<=l&&r<=y)return sum[o]; ll mid=(l+r)>>1; if(mid>=x&&mid>=y)return ask(o<<1,l,mid,x,y); if(mid<x&&mid<y)return ask(o<<1|1,mid+1,r,x,y); return ask(o<<1,l,mid,x,y)*ask(o<<1|1,mid+1,r,x,y); } inline void add(ll &x,ll y){x+=y;if(x>=yu)x-=yu;return ;} #define T pair<double,ll> #define mk make_pair #define fi first #define se second T t[N<<1]; ll cn=0; int main() { // freopen("test1.in","r",stdin); //freopen(".in","r",stdin); //freopen("test1.out","w",stdout); ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n;for(int i=1;i<n;i++)xu[i]=i;ll xs=ni(100); for(int i=1;i<=n;i++)cin>>x[i]>>v[i]>>p[i],p[i]=p[i]*xs%yu; for(int i=1;i<n;i++)t[++cn]=mk((double)(x[i+1]-x[i])/(double)(v[i]+v[i+1]),i); for(int i=1;i<n;i++)if(v[i]!=v[i+1])t[++cn]=mk((double)(x[i+1]-x[i])/(double)(max(v[i],v[i+1])-min(v[i],v[i+1])),i+n); sort(t+1,t+cn+1); for(int i=1;i<=n;i++){ a[i].n=a[i].m=2; a[i].a[1][1]=a[i].a[2][1]=yu+1-p[i];a[i].a[1][2]=a[i].a[2][2]=p[i]; }build(1,1,n); ll ans=0; for(int i=1;i<=cn;i++){ ll q=t[i].se,o=q; if(q>n)o-=n; juz an;an.n=1;an.m=2;an.a[1][1]=1;an.a[1][2]=0; juz z=an,y=an; juz tem=ask(1,1,n,1,o); z=z*tem; if(q<n||v[o]<v[o+1])y.a[1][1]=yu+1-p[o+1],y.a[1][2]=0; else y.a[1][1]=0,y.a[1][2]=p[o+1]; if(o+2<=n){ y=y*ask(1,1,n,o+2,n); }ll x1,x2=(y.a[1][1]+y.a[1][2]); if(q<n||v[o]>v[o+1])x1=z.a[1][2]; else x1=z.a[1][1]; ll ji=x1*x2%yu; ll val; if(q<n)val=(x[o+1]-x[o])*ni(v[o]+v[o+1])%yu; else val=(x[o+1]-x[o])*ni(max(v[o],v[o+1])-min(v[o],v[o+1]))%yu; add(ans,ji*val%yu); if(q<n)a[o+1].a[2][1]=0; else{ if(v[o]>v[o+1])a[o+1].a[2][2]=0; else a[o+1].a[1][1]=0; } update(1,1,n,o+1); }cout<<ans; return 0; }
cpp
1294
A
A. Collecting Coinstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp has three sisters: Alice; Barbara, and Cerene. They're collecting coins. Currently, Alice has aa coins, Barbara has bb coins and Cerene has cc coins. Recently Polycarp has returned from the trip around the world and brought nn coins.He wants to distribute all these nn coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives AA coins to Alice, BB coins to Barbara and CC coins to Cerene (A+B+C=nA+B+C=n), then a+A=b+B=c+Ca+A=b+B=c+C.Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0.Your task is to find out if it is possible to distribute all nn coins between sisters in a way described above.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.The next tt lines describe test cases. Each test case is given on a new line and consists of four space-separated integers a,b,ca,b,c and nn (1≤a,b,c,n≤1081≤a,b,c,n≤108) — the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has.OutputFor each test case, print "YES" if Polycarp can distribute all nn coins between his sisters and "NO" otherwise.ExampleInputCopy5 5 3 2 8 100 101 102 105 3 2 1 100000000 10 20 15 14 101 101 101 3 OutputCopyYES YES NO NO YES
[ "math" ]
#include <bits/stdc++.h> using namespace std; int main (){ int t;cin >> t; while(t--){ int a,b,c,n;cin >> a>>b>>c>>n; int ans = (a+b+c+n)/3; if((ans < a || ans < b || ans < c) || ((a+b+c+n) % 3 !=0)) cout << "NO"<<endl; else cout << "YES" << endl; } system("pause"); return 0; }
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 repairing.Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are gg days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next bb days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again gg good days, bb bad days and so on.You can be sure that you start repairing at the start of a good season, in other words, days 1,2,…,g1,2,…,g are good.You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the n=5n=5 then at least 33 units of the highway should have high quality; if n=4n=4 then at least 22 units should have high quality.What is the minimum number of days is needed to finish the repair of the whole highway?InputThe first line contains a single integer TT (1≤T≤1041≤T≤104) — the number of test cases.Next TT lines contain test cases — one per line. Each line contains three integers nn, gg and bb (1≤n,g,b≤1091≤n,g,b≤109) — the length of the highway and the number of good and bad days respectively.OutputPrint TT integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.ExampleInputCopy3 5 1 1 8 10 10 1000000 1 1000000 OutputCopy5 8 499999500000 NoteIn the first test case; you can just lay new asphalt each day; since days 1,3,51,3,5 are good.In the second test case, you can also lay new asphalt each day, since days 11-88 are good.
[ "math" ]
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int t; cin >> t; while (t--) { ll n, g, b; cin >> n >> g >> b; auto h = (n >> 1LL) + (n & 1LL); auto t = g + b; cout << max(n, ((h / g) * t) + (h % g == 0LL ? -b : h % g)) << '\n'; } }
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 to fill the bag completely.For example, if n=10n=10 and a=[1,1,32]a=[1,1,32] then you have to divide the box of size 3232 into two parts of size 1616, and then divide the box of size 1616. So you can fill the bag with boxes of size 11, 11 and 88.Calculate the minimum number of divisions required to fill the bag of size nn.InputThe first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.The first line of each test case contains two integers nn and mm (1≤n≤1018,1≤m≤1051≤n≤1018,1≤m≤105) — the size of bag and the number of boxes, respectively.The second line of each test case contains mm integers a1,a2,…,ama1,a2,…,am (1≤ai≤1091≤ai≤109) — the sizes of boxes. It is guaranteed that each aiai is a power of two.It is also guaranteed that sum of all mm over all test cases does not exceed 105105.OutputFor each test case print one integer — the minimum number of divisions required to fill the bag of size nn (or −1−1, if it is impossible).ExampleInputCopy3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 OutputCopy2 -1 0
[ "bitmasks", "greedy" ]
#include<bits/stdc++.h> #define endl '\n' #define int long long using namespace std; const int N=1e5+3; int a[N]; void solve() { int n,m; cin>>n>>m; int sum=0; for(int i=0;i<=100;i++) a[i]=0; for(int i=1; i<=m; i++) { int x,cnt=0; cin>>x; sum+=x; while(x)cnt++,x/=2; a[cnt-1]++; } if(sum<n) cout<<"-1"<<endl; else { int ans=0; for(int i=0;i<64; i++) { int x=(n>>i)&1; a[i]-=x; if(a[i]>=2)a[i+1]+=a[i]/2; if(a[i]<0)ans++,a[i+1]--; } cout<<ans<<endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) solve(); return 0; }
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 making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its nn favorite numbers: a1,a2,…,ana1,a2,…,an. What is the minimum number of hops Rabbit needs to get from (0,0)(0,0) to (x,0)(x,0)? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.Recall that the Euclidean distance between points (xi,yi)(xi,yi) and (xj,yj)(xj,yj) is (xi−xj)2+(yi−yj)2−−−−−−−−−−−−−−−−−−√(xi−xj)2+(yi−yj)2.For example, if Rabbit has favorite numbers 11 and 33 he could hop from (0,0)(0,0) to (4,0)(4,0) in two hops as shown below. Note that there also exists other valid ways to hop to (4,0)(4,0) in 22 hops (e.g. (0,0)(0,0) →→ (2,−5–√)(2,−5) →→ (4,0)(4,0)). Here is a graphic for the first example. Both hops have distance 33, one of Rabbit's favorite numbers. In other words, each time Rabbit chooses some number aiai and hops with distance equal to aiai in any direction he wants. The same number can be used multiple times.InputThe input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001≤t≤1000)  — the number of test cases. Next 2t2t lines contain test cases — two lines per test case.The first line of each test case contains two integers nn and xx (1≤n≤1051≤n≤105, 1≤x≤1091≤x≤109)  — the number of favorite numbers and the distance Rabbit wants to travel, respectively.The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109)  — Rabbit's favorite numbers. It is guaranteed that the favorite numbers are distinct.It is guaranteed that the sum of nn over all the test cases will not exceed 105105.OutputFor each test case, print a single integer — the minimum number of hops needed.ExampleInputCopy42 41 33 123 4 51 552 1015 4OutputCopy2 3 1 2 NoteThe first test case of the sample is shown in the picture above. Rabbit can hop to (2,5–√)(2,5), then to (4,0)(4,0) for a total of two hops. Each hop has a distance of 33, which is one of his favorite numbers.In the second test case of the sample, one way for Rabbit to hop 33 times is: (0,0)(0,0) →→ (4,0)(4,0) →→ (8,0)(8,0) →→ (12,0)(12,0).In the third test case of the sample, Rabbit can hop from (0,0)(0,0) to (5,0)(5,0).In the fourth test case of the sample, Rabbit can hop: (0,0)(0,0) →→ (5,102–√)(5,102) →→ (10,0)(10,0).
[ "geometry", "greedy", "math" ]
#include<bits/stdc++.h> using namespace std; int main() { int T; cin>>T; while(T--) { long long n,x,arr[100005],best=INT_MAX; cin>>n>>x; for(int i=0;i<n;i++) { cin>>arr[i]; best=min(best,x/arr[i]+(x%arr[i]?1:0)+(arr[i]>x?1:0)); } cout<<best<<endl; } }
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 to fill the bag completely.For example, if n=10n=10 and a=[1,1,32]a=[1,1,32] then you have to divide the box of size 3232 into two parts of size 1616, and then divide the box of size 1616. So you can fill the bag with boxes of size 11, 11 and 88.Calculate the minimum number of divisions required to fill the bag of size nn.InputThe first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.The first line of each test case contains two integers nn and mm (1≤n≤1018,1≤m≤1051≤n≤1018,1≤m≤105) — the size of bag and the number of boxes, respectively.The second line of each test case contains mm integers a1,a2,…,ama1,a2,…,am (1≤ai≤1091≤ai≤109) — the sizes of boxes. It is guaranteed that each aiai is a power of two.It is also guaranteed that sum of all mm over all test cases does not exceed 105105.OutputFor each test case print one integer — the minimum number of divisions required to fill the bag of size nn (or −1−1, if it is impossible).ExampleInputCopy3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 OutputCopy2 -1 0
[ "bitmasks", "greedy" ]
// LUOGU_RID: 94264985 #include<bits/stdc++.h> #define int long long using namespace std; main(){ ios::sync_with_stdio(false); int t; cin>>t; while (t--){ int n,m,s=0,c=0; cin>>n>>m; multiset<int,greater<int>> f; while(m--){ int x; cin>>x; s+=x; f.insert(x); } if(s<n){cout<<"-1\n"; continue;} while(n){ m=*f.begin(); f.erase(f.begin()); if(m<=n)n-=m,s-=m; else if(s-m<n){ c++; f.insert(m>>1); f.insert(m>>1); } else s-=m; } cout<<c<<endl; } return 0; }
cpp
1311
C
C. Perform the Combotime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou want to perform the combo on your opponent in one popular fighting game. The combo is the string ss consisting of nn lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in ss. I.e. if s=s="abca" then you have to press 'a', then 'b', 'c' and 'a' again.You know that you will spend mm wrong tries to perform the combo and during the ii-th try you will make a mistake right after pipi-th button (1≤pi<n1≤pi<n) (i.e. you will press first pipi buttons right and start performing the combo from the beginning). It is guaranteed that during the m+1m+1-th try you press all buttons right and finally perform the combo.I.e. if s=s="abca", m=2m=2 and p=[1,3]p=[1,3] then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'.Your task is to calculate for each button (letter) the number of times you'll press it.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.Then tt test cases follow.The first line of each test case contains two integers nn and mm (2≤n≤2⋅1052≤n≤2⋅105, 1≤m≤2⋅1051≤m≤2⋅105) — the length of ss and the number of tries correspondingly.The second line of each test case contains the string ss consisting of nn lowercase Latin letters.The third line of each test case contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pi<n1≤pi<n) — the number of characters pressed right during the ii-th try.It is guaranteed that the sum of nn and the sum of mm both does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105, ∑m≤2⋅105∑m≤2⋅105).It is guaranteed that the answer for each letter does not exceed 2⋅1092⋅109.OutputFor each test case, print the answer — 2626 integers: the number of times you press the button 'a', the number of times you press the button 'b', ……, the number of times you press the button 'z'.ExampleInputCopy3 4 2 abca 1 3 10 5 codeforces 2 8 3 2 9 26 10 qwertyuioplkjhgfdsazxcvbnm 20 10 1 2 3 5 10 5 9 4 OutputCopy4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2 NoteThe first test case is described in the problem statement. Wrong tries are "a"; "abc" and the final try is "abca". The number of times you press 'a' is 44, 'b' is 22 and 'c' is 22.In the second test case, there are five wrong tries: "co", "codeforc", "cod", "co", "codeforce" and the final try is "codeforces". The number of times you press 'c' is 99, 'd' is 44, 'e' is 55, 'f' is 33, 'o' is 99, 'r' is 33 and 's' is 11.
[ "brute force" ]
#include <bits/stdc++.h> #define ll long long #define pb push_back #define lp(loop,a,b) for(loop=a;loop<b;loop++) #define vi(a) vector<int>a; #define vl(a) vector<ll>a; #define nl cout<<"\n"; #define cc(a) cout<<(a?"YES":"NO")<<endl; #define cs(a) cout<<(a?"Yes":"No")<<endl; #define t int test_case;cin>>test_case;while(test_case--) #define f first #define s second using namespace std; int main() { t { ll a,b,c,p,m; string f; ll w[30]; memset(w,0,sizeof(w)); cin>>a>>b>>f; vl(v); lp(p,0,b) { cin>>c; v.pb(c); }v.pb(a); sort(v.begin(),v.end()); lp(p,0,a) { m=v.end()-upper_bound(v.begin(),v.end(),p); w[f[p]-'a']+=m; } lp(p,0,26) { cout<<w[p]<<" "; } nl } return 0; }
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[i:i+k−1] of ss. For example, if string ss is qwer and k=2k=2, below is the series of transformations the string goes through: qwer (original string) wqer (after reversing the first substring of length 22) weqr (after reversing the second substring of length 22) werq (after reversing the last substring of length 22) Hence, the resulting string after modifying ss with k=2k=2 is werq. Vasya wants to choose a kk such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of kk. Among all such kk, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.A string aa is lexicographically smaller than a string bb if and only if one of the following holds: aa is a prefix of bb, but a≠ba≠b; in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb. InputEach test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50001≤t≤5000). The description of the test cases follows.The first line of each test case contains a single integer nn (1≤n≤50001≤n≤5000) — the length of the string ss.The second line of each test case contains the string ss of nn lowercase latin letters.It is guaranteed that the sum of nn over all test cases does not exceed 50005000.OutputFor each testcase output two lines:In the first line output the lexicographically smallest string s′s′ achievable after the above-mentioned modification. In the second line output the appropriate value of kk (1≤k≤n1≤k≤n) that you chose for performing the modification. If there are multiple values of kk that give the lexicographically smallest string, output the smallest value of kk among them.ExampleInputCopy6 4 abab 6 qwerty 5 aaaaa 6 alaska 9 lfpbavjsm 1 p OutputCopyabab 1 ertyqw 3 aaaaa 1 aksala 6 avjsmbpfl 5 p 1 NoteIn the first testcase of the first sample; the string modification results for the sample abab are as follows : for k=1k=1 : abab for k=2k=2 : baba for k=3k=3 : abab for k=4k=4 : babaThe lexicographically smallest string achievable through modification is abab for k=1k=1 and 33. Smallest value of kk needed to achieve is hence 11.
[ "brute force", "constructive algorithms", "implementation", "sortings", "strings" ]
#include<bits/stdc++.h> #include<iostream> #include<iomanip> #include<vector> #include<string> #include<math.h> #include<map> #include<algorithm> #include<set> #include<unordered_map> #include <unordered_set> #define fast ios_base::sync_with_stdio(false),cin.tie(NULL) #define popcount __builtin_popcount using namespace std; #define ff first #define ss second #define pb push_back #define int long long int typedef long double lld; #define FOR(i,n) for(int i=0;i<n;i++) #define yes cout<<"YES"<<endl; #define no cout<<"NO"<<endl; #define p0(a) cout << a << " " #define p1(a) cout << a << endl #define p2(a, b) cout << a << " " << b << endl #define p3(a, b, c) cout << a << " " << b << " " << c << endl #define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl #define MOD 1000000007 #define vi vector<int> #define pii pair<int, int> #define vii vector<pii> #define fr front() #define bk back() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define max_heap priority_queue <int> #define min_heap priority_queue <int, vector <int> , greater <int> > typedef vector<char> vc; typedef vector<string> vs; typedef vector<pair<int, int> > vpii; typedef unordered_set<int> us; typedef map<int,int> mp; //debugging #ifndef ONLINE_JUDGE #define debug1(a) cerr <<#a<<" " <<a << endl #define debug2(a, b) cerr <<#a<<" "<< a << " "<<#b<<" " << b << endl #define debug3(a, b, c) cerr <<#a<<" "<<a << " "<<#b<<" " << b << " "<<#c<<" " << c << endl #define debug4(a, b, c, d) cerr << #a<<" "<<a << " " <<#b<<" "<< b << " "<<#c<<" " << c << " "<<#d<<" "<< d << endl #define debug(x) cerr << #x <<" "; _print(x); cerr << endl; #else #define debug(x) #define debug2(x,y) #define debug3(x,y,z) #define debug4(x,y,z,a) #endif void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(lld t) {cerr << t;} void _print(double t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} //Constants const long double pi= 3.141592653589793238; const int INF= 1e18; const int mod=1e9+7; //MATHEMATICAL FUNCTIONS int gcd(int a, int b){if (b == 0)return a;return gcd(b, a % b);} //gcd int lcm(int a, int b){return (a/gcd(a,b)*b);} //lcm //sieve vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;} //binary exponentation int expo(int a, int b, int mod) {int res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} //CHECK bool isprime(int n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} bool ispoweroftwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));} bool isperfectsquare(int x){if (x >= 0) {int sr = sqrt(x);return (sr * sr == x);}return false;} int ceils(int x, int y) {return x / y + (x % y > 0);} // Operator overloads template<typename T1, typename T2> // cin >> pair<T1, T2> istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); } template<typename T> // cin >> vector<T> istream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;} template<typename T1, typename T2> // cout << pair<T1, T2> ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); } template<typename T> // cout << vector<T> ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; } //USEFUL void printarr(vi arr, int n){FOR(i,n) cout << arr[i] << " ";cout << "\n";} void solve(){ int n,m,i,j; cin>>n; string s; cin>>s; char ch=*min_element(s.begin(), s.end()); vector<pair<string,int>> vals; for(int i=0;i<n;i++){ if(s[i]==ch){ int len1=i+1; int len2=n-(i+1); string g=s.substr(0,len1-1); string t=s.substr(len1,len2); string val=""; val.push_back(s[i]); if(len2%2==0){ reverse(g.begin(), g.end()); } val+=t; val+=g; vals.push_back({val,i+1}); } } sort(vals.begin(), vals.end()); cout<<vals[0].first<<endl; cout<<vals[0].second<<endl; } int32_t main() { fast; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int t; t=1; cin>>t; while(t--){ // cout<<t; solve(); } return 0; }
cpp
1312
B
B. Bogosorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a1,a2,…,ana1,a2,…,an. Array is good if for each pair of indexes i<ji<j the condition j−aj≠i−aij−aj≠i−ai holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).For example, if a=[1,1,3,5]a=[1,1,3,5], then shuffled arrays [1,3,5,1][1,3,5,1], [3,5,1,1][3,5,1,1] and [5,3,1,1][5,3,1,1] are good, but shuffled arrays [3,1,5,1][3,1,5,1], [1,1,3,5][1,1,3,5] and [1,1,5,3][1,1,5,3] aren't.It's guaranteed that it's always possible to shuffle an array to meet this condition.InputThe first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.The first line of each test case contains one integer nn (1≤n≤1001≤n≤100) — the length of array aa.The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100).OutputFor each test case print the shuffled version of the array aa which is good.ExampleInputCopy3 1 7 4 1 1 3 5 6 3 2 1 5 6 4 OutputCopy7 1 5 1 3 2 4 6 1 3 5
[ "constructive algorithms", "sortings" ]
/* ======Buddha*Dhormo*Songho====== _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' */ /* ┌───┬───┬───┬───┐ =======│ M │ O │ N │ G │======= └───┴───┴───┴───┘ */ #include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define ll long long int #define endl "\n" #define INF 0x3f3f3f3f #define map map<int,int> #define pi pair<int, int> #define vi vector<int> #define vii vector <pair<int,int>> #define viii priority_queue<int,vector<int>,greater<int>> #define viiii priority_queue<int> #define vpi vecotr<pi> #define set set<ll> #define mp make_pair #define pb push_back #define endl '\n' #define soja(i,a,n) for(int i=a;i<n;i++) #define soja1(i,a,n) for(int i=a;i<=n;i++) #define ulta(i,n,a) for(int i=n; i>a; i--) #define ulta1(i,n,a) for(int i=n-1; i>=a; i--) #define IMA INT_MAX #define IMI INT_MIN #define PIE 3.1415926536 #define inrange(a, b, c, d) (a>=0 && a<c && b>=0 && b<d) #define all(n) n.begin(),n.end() #define fi first #define se second #define no cout<<"NO"<<endl; #define yes cout<<"YES"<<endl; #define MAXN 500005 #define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) /******************************************MONG*********************************************************************/ /******************************************MONG*************************01010101010010101010101010101010101010101010/ /******************************************MONG*********************************************************************/ /***************010101001010100101001010***MONG******101010010100101101010010101010101010101010101010010101010101010/ /******************************************MONG*********************************************************************/ /* Mistakes can only make you close to perfect.*/ /******Since Perfection doesn't exist.:) *********************/ void mong_10() { fast; ll n; cin>>n; //vi a(n); ll a[n]; soja(i,0,n) { cin>>a[i]; } sort(a,a+n); reverse(a,a+n); soja(i,0,n) cout<<a[i]<<" "; cout<<endl; return; } //===============We become what we think about most of the time!All Opinion are my own==================================// signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); ios_base::sync_with_stdio(false); cin.tie(0); cout << setprecision(12) << fixed; ll test_case; test_case=1; cin>>test_case; while(test_case-->0) { mong_10(); } return 0; }
cpp
1310
D
D. Tourismtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMasha lives in a country with nn cities numbered from 11 to nn. She lives in the city number 11. There is a direct train route between each pair of distinct cities ii and jj, where i≠ji≠j. In total there are n(n−1)n(n−1) distinct routes. Every route has a cost, cost for route from ii to jj may be different from the cost of route from jj to ii.Masha wants to start her journey in city 11, take exactly kk routes from one city to another and as a result return to the city 11. Masha is really careful with money, so she wants the journey to be as cheap as possible. To do so Masha doesn't mind visiting a city multiple times or even taking the same route multiple times.Masha doesn't want her journey to have odd cycles. Formally, if you can select visited by Masha city vv, take odd number of routes used by Masha in her journey and return to the city vv, such journey is considered unsuccessful.Help Masha to find the cheapest (with minimal total cost of all taken routes) successful journey.InputFirst line of input had two integer numbers n,kn,k (2≤n≤80;2≤k≤102≤n≤80;2≤k≤10): number of cities in the country and number of routes in Masha's journey. It is guaranteed that kk is even.Next nn lines hold route descriptions: jj-th number in ii-th line represents the cost of route from ii to jj if i≠ji≠j, and is 0 otherwise (there are no routes i→ii→i). All route costs are integers from 00 to 108108.OutputOutput a single integer — total cost of the cheapest Masha's successful journey.ExamplesInputCopy5 8 0 1 2 2 0 0 0 1 1 2 0 1 0 0 0 2 1 1 0 0 2 0 1 2 0 OutputCopy2 InputCopy3 2 0 1 1 2 0 1 2 2 0 OutputCopy3
[ "dp", "graphs", "probabilities" ]
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector d(n, vector<int>(n)); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { int x; cin >> x; d[i][j] = x; } mt19937 rng(chrono::steady_clock().now().time_since_epoch().count()); int ans = 1e9; for (int it = 0; it < 10000; it++) { vector<int> L, R; for (int i = 0; i < n; i++) { if (rng() % 2 || i == 0) L.push_back(i); else R.push_back(i); } vector dp(k + 1, vector<int>(n, 1e9)); for (int v : R) dp[1][v] = d[0][v]; for (int i = 1; i < k; i++) { if (i % 2) for (int rv : R) for (int lv : L) dp[i + 1][lv] = min(dp[i + 1][lv], dp[i][rv] + d[rv][lv]); else for (int lv : L) for (int rv : R) dp[i + 1][rv] = min(dp[i + 1][rv], dp[i][lv] + d[lv][rv]); } ans = min(ans, dp[k][0]); } cout << ans << "\n"; }
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 to bb hp.You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 00.The fight with a monster happens in turns. You hit the monster by aa hp. If it is dead after your hit, you gain one point and you both proceed to the next monster. Your opponent hits the monster by bb hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster. You have some secret technique to force your opponent to skip his turn. You can use this technique at most kk times in total (for example, if there are two monsters and k=4k=4, then you can use the technique 22 times on the first monster and 11 time on the second monster, but not 22 times on the first monster and 33 times on the second monster).Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.InputThe first line of the input contains four integers n,a,bn,a,b and kk (1≤n≤2⋅105,1≤a,b,k≤1091≤n≤2⋅105,1≤a,b,k≤109) — the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.The second line of the input contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109), where hihi is the health points of the ii-th monster.OutputPrint one integer — the maximum number of points you can gain if you use the secret technique optimally.ExamplesInputCopy6 2 3 3 7 10 50 12 1 8 OutputCopy5 InputCopy1 1 100 99 100 OutputCopy1 InputCopy7 4 2 1 1 3 5 4 2 7 6 OutputCopy6
[ "greedy", "sortings" ]
#include<iostream> #include<cstring> #include<vector> #include<map> #include<queue> #include<unordered_map> #include<cmath> #include<cstdio> #include<algorithm> #include<set> #include<cstdlib> #include<stack> #include<ctime> #define forin(i,a,n) for(int i=a;i<=n;i++) #define forni(i,n,a) for(int i=n;i>=a;i--) #define fi first #define se second using namespace std; typedef long long ll; typedef double db; typedef pair<int,int> PII; const double eps=1e-7; const int N=2e5+7 ,M=2*N , INF=0x3f3f3f3f,mod=1e9+7; inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();} while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;} void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);} template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);} template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);} int T; int n,m,k; int w[N]; void solve() { n=read(); int a=read(),b=read(); k=read(); for(int i=1;i<=n;i++) w[i]=read(); vector<int> res; int ans=0; int p=a+b; for(int i=1;i<=n;i++) { if(w[i]%p==0) { int s=b/a+(b%a!=0); res.push_back(s); }else if(w[i]%p-a<=0) ans++; else { int s=w[i]%p-a; int t=s/a+(s%a!=0); res.push_back(t); } } sort(res.begin(),res.end()); for(int i=0;i<res.size();i++) { if(k>=res[i]) { k-=res[i]; ans++; }else break; } printf("%d\n",ans); } int main() { // init(); // stin(); // scanf("%d",&T); T=1; while(T--) solve(); return 0; }
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 straight line and numbered in order of 11 to nn. The height of the ii-th mountain top is aiai. "Median mountain range" is famous for the so called alignment of mountain peaks happening to it every day. At the moment of alignment simultaneously for each mountain from 22 to n−1n−1 its height becomes equal to the median height among it and two neighboring mountains. Formally, if before the alignment the heights were equal bibi, then after the alignment new heights aiai are as follows: a1=b1a1=b1, an=bnan=bn and for all ii from 22 to n−1n−1 ai=median(bi−1,bi,bi+1)ai=median(bi−1,bi,bi+1). The median of three integers is the second largest number among them. For example, median(5,1,2)=2median(5,1,2)=2, and median(4,2,4)=4median(4,2,4)=4.Recently, Berland scientists have proved that whatever are the current heights of the mountains, the alignment process will stabilize sooner or later, i.e. at some point the altitude of the mountains won't changing after the alignment any more. The government of Berland wants to understand how soon it will happen, i.e. to find the value of cc — how many alignments will occur, which will change the height of at least one mountain. Also, the government of Berland needs to determine the heights of the mountains after cc alignments, that is, find out what heights of the mountains stay forever. Help scientists solve this important problem!InputThe first line contains integers nn (1≤n≤5000001≤n≤500000) — the number of mountains.The second line contains integers a1,a2,a3,…,ana1,a2,a3,…,an (1≤ai≤1091≤ai≤109) — current heights of the mountains.OutputIn the first line print cc — the number of alignments, which change the height of at least one mountain.In the second line print nn integers — the final heights of the mountains after cc alignments.ExamplesInputCopy5 1 2 1 2 1 OutputCopy2 1 1 1 1 1 InputCopy6 1 3 2 5 4 6 OutputCopy1 1 2 3 4 5 6 InputCopy6 1 1 2 2 1 1 OutputCopy0 1 1 2 2 1 1 NoteIn the first example; the heights of the mountains at index 11 and 55 never change. Since the median of 11, 22, 11 is 11, the second and the fourth mountains will have height 1 after the first alignment, and since the median of 22, 11, 22 is 22, the third mountain will have height 2 after the first alignment. This way, after one alignment the heights are 11, 11, 22, 11, 11. After the second alignment the heights change into 11, 11, 11, 11, 11 and never change from now on, so there are only 22 alignments changing the mountain heights.In the third examples the alignment doesn't change any mountain height, so the number of alignments changing any height is 00.
[ "data structures" ]
#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")) assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout)); #else using namespace chrono; auto start = steady_clock::now(); #endif cin.tie(0), cout.tie(0) -> sync_with_stdio(0); int testcases = 1; // cin >> testcases; for (int test = 1; test <= testcases; test++){ // cout << "Case #" << test << ": "; Hollwo_Pelw(); } #ifdef hollwo_pelw_local auto end = steady_clock::now(); cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl; #endif } const int N = 5e5 + 5; set<int> not_in_res, alter_seg; int n, m, a[N], b[N], res[N]; vector<int> pos[N]; void update(int l, int r, int v) { auto it = not_in_res.lower_bound(l); while (it != not_in_res.end() && *it <= r) { res[*it] = v; not_in_res.erase(it); it = not_in_res.lower_bound(l); } } void Hollwo_Pelw(){ cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; vector<int> vals(a + 1, a + n + 1); sort(vals.begin(), vals.end()); vals.resize(m = (unique(vals.begin(), vals.end()) - vals.begin())); for (int i = 1; i <= n; i++) pos[a[i] = lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin()].push_back(i); int longest_segment_size = 0; for (int i = 0; i <= n; i++) alter_seg.insert(i); for (int i = 1; i <= n; i++) not_in_res.insert(i); for (int i = 0; i < m; i++) { for (int p : pos[i]) { // b[p] : 0 -> 1 if (p > 1 && !b[p - 1]) alter_seg.erase(p - 1); else alter_seg.insert(p - 1); if (p < n && !b[p + 1]) alter_seg.erase(p); else alter_seg.insert(p); b[p] = 1; } for (int p : pos[i]) { for (int x : {p - 1, p, p + 1}) if (x >= 1 && x <= n) { auto it = alter_seg.lower_bound(x); int r = *it, l = *(-- it) + 1, m = (l + r) / 2; if (b[l]) update(l, m + 0, vals[i]); if (b[r]) update(m + 1, r, vals[i]); longest_segment_size = max(longest_segment_size, r - l); } } } cout << longest_segment_size / 2 << '\n'; for (int i = 1; i <= n; i++) cout << res[i] << " \n"[i == n]; }
cpp
1295
A
A. Display The Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a large electronic screen which can display up to 998244353998244353 decimal digits. The digits are displayed in the same way as on different electronic alarm clocks: each place for a digit consists of 77 segments which can be turned on and off to compose different digits. The following picture describes how you can display all 1010 decimal digits:As you can see, different digits may require different number of segments to be turned on. For example, if you want to display 11, you have to turn on 22 segments of the screen, and if you want to display 88, all 77 segments of some place to display a digit should be turned on.You want to display a really large integer on the screen. Unfortunately, the screen is bugged: no more than nn segments can be turned on simultaneously. So now you wonder what is the greatest integer that can be displayed by turning on no more than nn segments.Your program should be able to process tt different test cases.InputThe first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases in the input.Then the test cases follow, each of them is represented by a separate line containing one integer nn (2≤n≤1052≤n≤105) — the maximum number of segments that can be turned on in the corresponding testcase.It is guaranteed that the sum of nn over all test cases in the input does not exceed 105105.OutputFor each test case, print the greatest integer that can be displayed by turning on no more than nn segments of the screen. Note that the answer may not fit in the standard 3232-bit or 6464-bit integral data type.ExampleInputCopy2 3 4 OutputCopy7 11
[ "greedy" ]
#include<bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t>0) { int n; cin >> n; if(n % 2 == 1){ cout << 7; n -= 3; } while (n > 1) { cout << 1; n -= 2; } cout <<"\n"; t--; } }
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 different weapons and exactly one of mm different armor sets. Weapon ii has attack modifier aiai and is worth caicai coins, and armor set jj has defense modifier bjbj and is worth cbjcbj coins.After choosing his equipment Roma can proceed to defeat some monsters. There are pp monsters he can try to defeat. Monster kk has defense xkxk, attack ykyk and possesses zkzk coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster kk can be defeated with a weapon ii and an armor set jj if ai>xkai>xk and bj>ykbj>yk. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.Help Roma find the maximum profit of the grind.InputThe first line contains three integers nn, mm, and pp (1≤n,m,p≤2⋅1051≤n,m,p≤2⋅105) — the number of available weapons, armor sets and monsters respectively.The following nn lines describe available weapons. The ii-th of these lines contains two integers aiai and caicai (1≤ai≤1061≤ai≤106, 1≤cai≤1091≤cai≤109) — the attack modifier and the cost of the weapon ii.The following mm lines describe available armor sets. The jj-th of these lines contains two integers bjbj and cbjcbj (1≤bj≤1061≤bj≤106, 1≤cbj≤1091≤cbj≤109) — the defense modifier and the cost of the armor set jj.The following pp lines describe monsters. The kk-th of these lines contains three integers xk,yk,zkxk,yk,zk (1≤xk,yk≤1061≤xk,yk≤106, 1≤zk≤1031≤zk≤103) — defense, attack and the number of coins of the monster kk.OutputPrint a single integer — the maximum profit of the grind.ExampleInputCopy2 3 3 2 3 4 7 2 4 3 2 5 11 1 2 4 2 1 6 3 4 6 OutputCopy1
[ "brute force", "data structures", "sortings" ]
/* ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡞⠉⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⣀⡠⠖⠒⠓⣦⢀⡞⠀⢰⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⣴⣪⠟⢙⣶⣴⣿⣿⣿⠟⠀⢲⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⢻⣏⣠⣿⣿⣿⢿⣿⠋⠀⢀⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠋⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⢹⣹⣿⣿⡷⢖⢿⠭⠄⡾⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣄⢀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⢸⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⡞⠀⣏⡏⢀⣈⣄⢀⡾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣾⣶⣾⣿⣯⣷⣶⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣠⠌⠓⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⢧⠀⡉⣄⣠⣝⠙⡾⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡢⣄⠀⠀⠀⠀⠀⢀⣼⢁⣠⡾⠈⠳⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⢸⠻⣖⢨⣻⡿⢱⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠉⠀⠀⠀⠉⠙⠿⣿⣿⣿⣿⣞⣦⡀⠀⠀⢠⠞⣶⡾⣿⣅⣴⠇⢈⡱⡄⠀⠀⠀⠀⠀⠀⠀ ⠀⢸⣶⠸⣆⣯⣥⣼⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⣿⣥⠀⢀⣀⡀⠀⠀⠀⠀⡨⢻⣿⣿⣿⣿⣷⡀⠀⢻⣴⣿⣴⣿⣿⣿⣶⣿⣷⣾⡀⠀⠀⠀⠀⠀⠀ ⠀⡼⣹⣴⣿⡦⣟⣹⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡷⠿⠛⣤⣴⣿⣿⡷⣄⠀⠀⣴⣿⣿⣿⣿⣿⣿⣷⠀⠘⣆⣹⡋⠛⢹⣿⡷⠏⠀⢸⠁⠀⠀⠀⠀⠀⠀ ⠀⡇⣿⢹⡌⣧⣽⢺⡁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣞⣁⠀⠀⠙⢿⠛⠿⠟⠈⠰⠀⢹⣿⣿⣿⣿⣿⣿⣿⡇⠀⢹⡋⢀⢰⡟⠁⠀⠀⠀⡄⠀⠀⠀⠀⠀⠀⠀ ⠀⣟⣿⠟⠛⣯⡀⡌⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣨⣿⣿⣶⠄⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⣿⣿⠁⠀⠀⠳⣿⣾⣅⣀⣀⠀⠸⡇⠀⠀⠀⠀⠀⠀⠀ ⠀⣏⣼⣦⣔⣚⣹⣇⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⢿⣿⣽⣿⣦⡀⠀⠀⠀⠀⠀⣀⣤⡘⣛⢩⣯⣿⣿⣿⠏⠀⠀⠀⠀⠙⣷⠿⡉⠿⠿⠀⠹⡄⠀⠀⠀⠀⠀⠀ ⠀⣿⡀⢻⣿⣿⡋⢸⡌⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⡌⣾⣭⠉⢻⡷⠀⢠⡄⠀⠨⣿⣿⣤⣿⡟⠆⣏⣿⠋⠀⠀⠢⠀⠀⠀⠈⢣⠙⠳⠄⠀⠀⢳⠀⠀⠀⠀⠀⠀ ⠀⣧⣷⣿⣿⣿⣿⣿⣟⣈⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⣳⡏⣁⣐⣿⣿⣆⣨⣿⣹⣶⣫⣿⣝⣗⣲⣾⠟⠁⠀⠀⠀⠨⠀⠀⠀⠀⠀⢧⠀⠀⠀⠀⠈⢧⠀⠀⠀⠀⠀ ⠀⢹⠉⣽⣯⣄⣀⣽⢻⣿⠞⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠋⢱⣿⠋⠀⠀⠀⠀⠀⠀⠄⠀⠀⠀⠀⠸⡌⠄⠀⠀⠀⠈⢧⠀⠀⠀⠀ ⠀⠈⣿⣿⣿⣫⣿⣽⡿⠛⣶⢿⡟⠓⠢⠤⢤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⠤⣀⠀⠀⠀⠙⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⠀⠀⢸⠃⠀⠀⠀⠀⠀⠁⠀⡀⠀⠀⠀⠀⠀⢧⠀⠀⠀⣀⠀⠈⢣⡀⠀⠀ ⠀⠀⢸⡿⣿⣷⣽⣿⣿⣶⠾⠦⠆⠀⠀⠂⠀⠈⠉⠉⠓⠒⠺⠉⠉⠉⠉⠉⠉⠀⠀⡌⢳⣀⡀⠀⣷⢿⣿⡿⣿⠻⠟⠛⠈⢤⠈⠀⣻⢤⣀⡀⠀⣀⣀⣀⠀⠂⠀⠀⠀⠀⠀⢸⠀⠁⢀⠀⠀⠀⠀⢣⠀⠀ ⠀⠀⠸⣿⣿⣿⣿⣿⣿⡏⠁⠀⡇⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⠀⠹⡄⡀⠨⣽⡋⠁⢬⡿⠆⠁⠀⠀⠀⣁⠴⠊⠁⠀⢀⡊⠉⠁⠀⠈⠙⡟⠓⠒⠢⣤⣀⡈⡆⠀⠀⠀⣦⠀⠀⠈⢧⠀ ⠀⠀⠀⠈⠻⢿⣿⣿⣿⣷⡄⠰⣷⠀⢀⣁⣀⠀⠀⠀⠀⠀⠂⠀⠀⠀⠀⣠⢶⣄⠀⠀⢱⡙⣄⠈⠧⠤⣀⣤⠤⠤⠖⠚⠉⠀⠀⢀⠴⠚⢿⡇⠀⠀⠀⠀⢀⠅⢀⣴⣶⣿⣿⠿⠿⡆⢀⡘⠹⣆⠀⠀⠈⣆ ⠀⠀⠀⠀⠀⠀⠈⠙⠛⠿⣿⣆⣌⣇⠀⢿⠠⢭⡙⠒⠒⠤⠄⠀⡀⢀⡼⠁⠈⢙⣆⠀⠀⠉⠁⠀⠀⠀⢀⣀⣀⡄⠀⠀⠀⢠⡶⠉⠀⣰⢺⠃⠀⠀⠀⠠⠁⢠⣾⣿⣿⡿⠁⠀⠀⢳⡏⠀⣠⣿⡄⠀⠀⢸ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⡆⠈⢷⣄⠓⣦⠀⡀⢰⢲⠇⢸⠉⢳⡴⠋⠈⢧⠀⠀⠀⠀⣀⠈⢿⣃⢰⠃⢀⡤⠤⠽⢒⡞⠉⠁⢹⠀⠀⢸⠺⡤⢰⣿⣿⣿⣿⡇⠀⠀⠀⠘⣷⡾⠿⣿⣷⡄⠀⢸ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢯⡉⣀⣨⣿⣷⣬⣦⠸⡜⣿⠀⠘⢳⡾⠿⣿⣿⡏⠀⠀⠀⡞⢠⠞⠛⠛⠋⠀⣼⣿⣷⣷⢸⣽⠀⠀⢸⠀⠀⠈⣷⢷⣸⣿⣿⣿⣿⣿⣀⡀⠀⠀⠙⠦⠤⣠⣤⣷⣤⡞ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠁⠀⠀⠙⠿⣝⢶⣧⡇⢀⠇⢰⠋⣳⡀⠉⠀⠀⣴⠿⡵⣄⣀⠀⠀⠀⠀⢷⣿⠿⠿⢓⣾⣤⣶⣾⠄⠀⠀⣻⢸⣿⣹⣿⣿⣿⣿⣷⣤⣴⣶⣾⣿⣿⣿⠟⠋⠁⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠳⣌⠀⢸⠒⠋⠘⢩⠃⠀⠀⣠⣿⠶⢣⣶⣶⣦⣄⠀⠀⠘⢛⣿⡿⠿⡷⠟⠋⡝⠀⠀⠀⡿⣼⣿⡿⣿⡏⢻⣿⣿⣿⡿⠿⠿⠛⠉⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡇⢸⠀⠀⠀⠈⠀⠀⠀⢉⣿⢀⣿⡏⠹⠿⣿⠄⠀⠀⠀⠙⠚⣰⠀⠁⠈⡇⠀⠀⢰⡇⣿⣟⢠⣯⡀⣿⠞⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⢸⠀⠀⠀⠀⠀⠀⠀⣸⡿⢸⣿⠀⠀⢸⣿⠀⠀⠀⢀⠇⠀⡏⠁⠀⢸⠁⠀⠁⢸⡇⢿⣿⣿⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⢸⠀⠀⡀⠀⠀⠀⢀⣿⠇⣿⡇⠀⠀⣿⡇⠀⠀⠀⣸⠀⠀⢱⣀⠀⣸⠀⠀⠀⢸⠇⣳⢟⣭⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⢀⡇⠀⠀⠀⣸⡿⠠⣿⣷⣤⣾⡿⠀⠀⠀⠀⣿⢧⡀⠀⠀⠳⡇⠀⠠⠀⢸⣀⡟⢣⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⢸⠃⠀⠀⠀⢸⣇⠀⠈⣙⡟⠁⠀⠀⠀⠀⠀⠻⠦⠭⠷⠆⣼⠁⠀⠀⠀⢸⢹⡇⡎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⣸⠙⢢⠊⠁⠀⠀⠀⠀⠀⠀⠀⠀⠰⠀⠀⠀⢹⠀⠀⠀⠀⢸⣿⢳⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠚⠃⠀⠀⠀⠀⠀⠀⠀⣠⣥⠀⠀⠀⠀⠀⠀⠀⠀⢸⣇⡞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⡇⠈⠳⢤⡀⠀⠀⠀⠀⠀⢸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⠀⠀⠀⠀⠀⢠⡄⠀⠀⠀⣹⠀⠀⠀⠀⠀⠀⠀⢧⣂⡢⠄⢹⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠇⠀⠀⠀⠀⠀⠀⠀⠀⢰⠊⢉⡇⠀⠀⠀⠀⠀⠀⠀⠀⠠⠼⢤⣯⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠓⠄⠀⠀⠀⠀⢀⠄⠀⠈⠧⣼⠁⠀⠀⠀⠀⠀⠀⠀⠀⡃⠀⣠⡾⠀⠀⠀⠀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⢸⣄⠀⠀⠀⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⣻⣴⠏⡇⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠲⠀⠀⠀⠀⠀⠈⠛⠳⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡞⠍⠁⠀⠒⡇⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠛⠑⠀⠀⠀⢸⠇⠀⠀⠀⠀⢀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ */ #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/rope> #define int ll #define mp make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define sz(a) (int)a.size() #define eq(a, b) (fabs(a - b) < EPS) #define md(a, b) ((a) % b + b) % b #define mod(a) md(a, MOD) #define _max(a, b) ((a) > (b) ? (a) : (b)) #define srt(a) sort(all(a)) #define mem(a, h) memset(a, (h), sizeof(a)) #define f first #define s second #define forn(i, n) for(int i = 0; i < n; i++) #define fore(i, b, e) for(int i = b; i < e; i++) #define forg(i, b, e, m) for(int i = b; i < e; i+=m) #define index int mid = (b + e) / 2, l = node * 2 + 1, r = l + 1; #define DBG(x) cerr<<#x<<" = "<<(x)<<endl #define RAYA cerr<<"=============================="<<endl // int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;} using namespace std; // using namespace __gnu_pbds; // using namespace __gnu_cxx; // #pragma GCC target ("avx2") // #pragma GCC optimization ("O3") // #pragma GCC optimization ("unroll-loops") typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int, int> ii; typedef pair<pair<int, int>, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<ll> vll; // typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; // find_by_order kth largest order_of_key < mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // rng const int tam = 1000010; const int MOD = 1000000007; const int MOD1 = 998244353; const double DINF=1e100; const double EPS = 1e-9; const double PI = acos(-1); int ar[tam], t[4 * tam], l[4 * tam]; void push(int b, int e, int node) { if(l[node]) { t[node] += l[node]; if(b < e) l[node * 2 + 1] += l[node], l[node * 2 + 2] += l[node]; l[node] = 0; } } void init(int b, int e, int node) { if(b == e) { t[node] = ar[b]; return; } index; init(b, mid, l); init(mid + 1, e, r); t[node] = max(t[l], t[r]); } int query(int b, int e, int node, int i, int j) { push(b, e, node); if(b >= i && e <= j) return t[node]; index; if(mid >= j) return query(b, mid, l, i, j); if(mid < i) return query(mid + 1, e, r, i, j); return max(query(b, mid, l, i, j), query(mid + 1, e, r, i, j)); } void update(int b, int e, int node, int i, int j, int val) { if(b > e) return; push(b, e, node); if(e < i || b > j) return; if(b >= i && e <= j) { l[node] += val; push(b, e, node); return; } index; update(b, mid, l, i, j, val); update(mid + 1, e, r, i, j, val); t[node] = max(t[l], t[r]); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("asd.txt", "r", stdin); // freopen("qwe.txt", "w", stdout); fore(i, 0, tam) ar[i] = -1ll * MOD * MOD; int n, m, p; cin>>n>>m>>p; vector<pair<ii, int>> sweep; fore(i, 0, n) { int a, b; cin>>a>>b; sweep.pb({{a, -MOD}, b}); } fore(i, 0, m) { int a, b; cin>>a>>b; ar[a] = max(ar[a], -b); } init(0, tam - 1, 0); fore(i, 0, p) { int a, b, c; cin>>a>>b>>c; sweep.pb({{a, b}, c}); } sort(all(sweep)); int res = -1ll * MOD * MOD; // cout<<t[0]<<'\n'; // fore(i, 0, pos) // cout<<query(0, pos - 1, 0, i, i)<<' '; // cout<<'\n'; for(auto cat : sweep) { if(cat.f.s == -MOD) { res = max(res, t[0] - cat.s); // cout<<"$$$ "<<t[0]<<' '<<cat.f.f<<' '<<cat.s<<'\n'; } else { update(0, tam - 1, 0, cat.f.s + 1, tam - 1, cat.s); // cout<<"%%%%%%\n"; // cout<<cat.f.f<<' '<<cat.f.s<<' '<<cat.s<<'\n'; // cout<<t[0]<<'\n'; // fore(i, 0, pos) // cout<<ar[i]<<' '<<query(0, pos - 1, 0, i, i)<<'\n'; } } cout<<res<<'\n'; return 0; } // Se vuelve más fácil, // cada día es un poco más fácil, pero tienes que hacerlo cada día, // es la parte difícil, pero se vuelve más fácil. // Crecer duele. // La única manera de pasar esa barrera es pasandola. // efe no más. // Si no vá por todo, andá pa' allá bobo. // No sirve de nada hacer sacrificios si no tienes disciplina. // Cae 7 veces, levántate 8. // Ale perdóname por favor :,v
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 (as always). Having a lot of free time, he decides to put on his legendary hacker "X" instinct and fight against the gangs of the cyber world.His target is a network of nn small gangs. This network contains exactly n−1n−1 direct links, each of them connecting two gangs together. The links are placed in such a way that every pair of gangs is connected through a sequence of direct links.By mining data, Xenon figured out that the gangs used a form of cross-encryption to avoid being busted: every link was assigned an integer from 00 to n−2n−2 such that all assigned integers are distinct and every integer was assigned to some link. If an intruder tries to access the encrypted data, they will have to surpass SS password layers, with SS being defined by the following formula:S=∑1≤u<v≤nmex(u,v)S=∑1≤u<v≤nmex(u,v)Here, mex(u,v)mex(u,v) denotes the smallest non-negative integer that does not appear on any link on the unique simple path from gang uu to gang vv.Xenon doesn't know the way the integers are assigned, but it's not a problem. He decides to let his AI's instances try all the passwords on his behalf, but before that, he needs to know the maximum possible value of SS, so that the AIs can be deployed efficiently.Now, Xenon is out to write the AI scripts, and he is expected to finish them in two hours. Can you find the maximum possible SS before he returns?InputThe first line contains an integer nn (2≤n≤30002≤n≤3000), the number of gangs in the network.Each of the next n−1n−1 lines contains integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n; ui≠viui≠vi), indicating there's a direct link between gangs uiui and vivi.It's guaranteed that links are placed in such a way that each pair of gangs will be connected by exactly one simple path.OutputPrint the maximum possible value of SS — the number of password layers in the gangs' network.ExamplesInputCopy3 1 2 2 3 OutputCopy3 InputCopy5 1 2 1 3 1 4 3 5 OutputCopy10 NoteIn the first example; one can achieve the maximum SS with the following assignment: With this assignment, mex(1,2)=0mex(1,2)=0, mex(1,3)=2mex(1,3)=2 and mex(2,3)=1mex(2,3)=1. Therefore, S=0+2+1=3S=0+2+1=3.In the second example, one can achieve the maximum SS with the following assignment: With this assignment, all non-zero mex value are listed below: mex(1,3)=1mex(1,3)=1 mex(1,5)=2mex(1,5)=2 mex(2,3)=1mex(2,3)=1 mex(2,5)=2mex(2,5)=2 mex(3,4)=1mex(3,4)=1 mex(4,5)=3mex(4,5)=3 Therefore, S=1+2+1+2+1+3=10S=1+2+1+2+1+3=10.
[ "combinatorics", "dfs and similar", "dp", "greedy", "trees" ]
#include <bits/stdc++.h> using namespace std; typedef long long int ll; const ll MOD=998244353; const ll INF=1e18; #define F first #define S second typedef long double ld; typedef complex<long double> pt; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif vector<ll> al[3001]; vector<vector<ll>> dp(3001, vector<ll> (3001,-1)); vector<vector<ll>> ss(3001, vector<ll> (3001)); vector<vector<ll>> pa(3001, vector<ll> (3001)); void dfs(ll node, ll parent, ll root){ pa[node][root]=parent; ss[node][root]=1; for (auto u: al[node]){ if (u!=parent){ dfs(u,node,root); ss[node][root]+=ss[u][root]; } } } ll f(ll u, ll v){ if (dp[u][v]!=-1) return dp[u][v]; if (u==v) dp[u][v]=0; else dp[u][v]=(ss[u][v]*ss[v][u])+max(f(u,pa[v][u]),f(pa[u][v],v)); return dp[u][v]; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // for getting input frm input.txt freopen("output.txt", "w", stdout); // for getting output frm output.tx #else #endif ll n;cin>>n; for (ll i=1; i<n; i++){ ll a, b; cin>>a>>b; al[a].push_back(b); al[b].push_back(a); } for (ll i=1; i<=n; i++){ dfs(i,i,i); } for (ll i=1; i<=n; i++){ for (ll j=i; j<=n; j++) f(i,j); } ll fa=0; for (ll i=1; i<=n; i++){ for (ll j=i; j<=n; j++){ if (al[i].size()==1 && al[j].size()==1){ fa=max(fa,dp[i][j]); } } } cout<<fa; return 0; }
cpp
1301
F
F. Super Jabertime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJaber is a superhero in a large country that can be described as a grid with nn rows and mm columns, where every cell in that grid contains a different city.Jaber gave every city in that country a specific color between 11 and kk. In one second he can go from the current city to any of the cities adjacent by the side or to any city with the same color as the current city color.Jaber has to do qq missions. In every mission he will be in the city at row r1r1 and column c1c1, and he should help someone in the city at row r2r2 and column c2c2.Jaber wants your help to tell him the minimum possible time to go from the starting city to the finishing city for every mission.InputThe first line contains three integers nn, mm and kk (1≤n,m≤10001≤n,m≤1000, 1≤k≤min(40,n⋅m)1≤k≤min(40,n⋅m)) — the number of rows, columns and colors.Each of the next nn lines contains mm integers. In the ii-th line, the jj-th integer is aijaij (1≤aij≤k1≤aij≤k), which is the color assigned to the city in the ii-th row and jj-th column.The next line contains one integer qq (1≤q≤1051≤q≤105)  — the number of missions.For the next qq lines, every line contains four integers r1r1, c1c1, r2r2, c2c2 (1≤r1,r2≤n1≤r1,r2≤n, 1≤c1,c2≤m1≤c1,c2≤m)  — the coordinates of the starting and the finishing cities of the corresponding mission.It is guaranteed that for every color between 11 and kk there is at least one city of that color.OutputFor every mission print the minimum possible time to reach city at the cell (r2,c2)(r2,c2) starting from city at the cell (r1,c1)(r1,c1).ExamplesInputCopy3 4 5 1 2 1 3 4 4 5 5 1 2 1 3 2 1 1 3 4 2 2 2 2 OutputCopy2 0 InputCopy4 4 8 1 2 2 8 1 3 4 7 5 1 7 6 2 3 8 8 4 1 1 2 2 1 1 3 4 1 1 2 4 1 1 4 4 OutputCopy2 3 3 4 NoteIn the first example: mission 11: Jaber should go from the cell (1,1)(1,1) to the cell (3,3)(3,3) because they have the same colors, then from the cell (3,3)(3,3) to the cell (3,4)(3,4) because they are adjacent by side (two moves in total); mission 22: Jaber already starts in the finishing cell. In the second example: mission 11: (1,1)(1,1) →→ (1,2)(1,2) →→ (2,2)(2,2); mission 22: (1,1)(1,1) →→ (3,2)(3,2) →→ (3,3)(3,3) →→ (3,4)(3,4); mission 33: (1,1)(1,1) →→ (3,2)(3,2) →→ (3,3)(3,3) →→ (2,4)(2,4); mission 44: (1,1)(1,1) →→ (1,2)(1,2) →→ (1,3)(1,3) →→ (1,4)(1,4) →→ (4,4)(4,4).
[ "dfs and similar", "graphs", "implementation", "shortest paths" ]
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <random> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define AboTaha_on_da_code ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define X first #define Y second const int dx[8]={0, 0, 1, -1, 1, -1, -1, 1}, dy[8]={1, -1, 0, 0, 1, -1, 1, -1}; const int M = 1e9+7, M2 = 998244353; const double EPS = 1e-9; void burn(int tc) { int n, m, k; cin >> n >> m >> k; vector <vector <int>> grid(n, vector <int> (m)); vector <vector <pair<int, int>>> col(k); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> grid[i][j], grid[i][j]--; col[grid[i][j]].emplace_back(i, j); } } vector <vector <vector <int>>> dist(k, vector <vector <int>> (n, vector <int> (m, -1))); for (int c = 0; c < k; c++) { queue <pair<int, int>> q; for (auto [x, y] : col[c]) { q.push({x, y}); dist[c][x][y] = 0; } vector <bool> vis(k, false); vis[c] = true; while(!q.empty()) { auto [x, y] = q.front(); q.pop(); if (!vis[grid[x][y]]) { vis[grid[x][y]] = true; for (auto[nx, ny]: col[grid[x][y]]) { if (!~dist[c][nx][ny]) { dist[c][nx][ny] = dist[c][x][y] + 1; q.push({nx, ny}); } } } for (int i = 0; i < 4; i++) { int nx = x+dx[i], ny = y+dy[i]; if (~nx && ~ny && nx < n && ny < m && !~dist[c][nx][ny]) { dist[c][nx][ny] = dist[c][x][y]+1; q.push({nx, ny}); } } } } int q; cin >> q; while(q--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; int dis = abs(x1-x2)+abs(y1-y2); for (int i = 0; i < k; i++) { dis = min(dis, 1+dist[i][x1][y1]+dist[i][x2][y2]); } cout << dis << '\n'; } } int main() { AboTaha_on_da_code // freopen("Ain.txt", "r", stdin); // freopen("Aout.txt", "w", stdout); int T = 1; // cin >> T; for (int i = 1; i <= T; i++) { // cout << "Case #" << i << ": "; burn(i); // cout << '\n'; } return 0; }
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 tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex vv is the last different from vv vertex on the path from the root to the vertex vv. The depth of the vertex vv is the length of the path from the root to the vertex vv. Children of vertex vv are all vertices for which vv is the parent. The binary tree is such a tree that no vertex has more than 22 children.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.The only line of each test case contains two integers nn and dd (2≤n,d≤50002≤n,d≤5000) — the number of vertices in the tree and the required sum of depths of all vertices.It is guaranteed that the sum of nn and the sum of dd both does not exceed 50005000 (∑n≤5000,∑d≤5000∑n≤5000,∑d≤5000).OutputFor each test case, print the answer.If it is impossible to construct such a tree, print "NO" (without quotes) in the first line. Otherwise, print "{YES}" in the first line. Then print n−1n−1 integers p2,p3,…,pnp2,p3,…,pn in the second line, where pipi is the parent of the vertex ii. Note that the sequence of parents you print should describe some binary tree.ExampleInputCopy3 5 7 10 19 10 18 OutputCopyYES 1 2 1 3 YES 1 2 3 3 9 9 2 1 6 NO NotePictures corresponding to the first and the second test cases of the example:
[ "brute force", "constructive algorithms", "trees" ]
#include <bits/stdc++.h> using namespace std; struct Node{ set<int> child; int par = -1; int depth = 0; }; vector<int> smallest; int check(int n, int d, int n1){ int ln1 = n1*(n1-1); ln1 /= 2; int l1 = d-(n-1)-ln1; int r1 = d-(n-1)-smallest[n1]; int l2 = smallest[n-1-n1]; int r2 = (n-1-n1)*(n-2-n1); r2 /= 2; // cout << "called: " << n << " " << d << " " << n1 << " " << l1 << " " << r1 << " " << l2 << " " << r2 << "\n"; if(max(l1,l2) <= min(r1,r2)){ return max(l1,l2); } return -1; } int go(int n, int d, vector<Node>& arr,int& curr){ // cout<<n<<" "<<d<<" "<<curr<<endl; int root = curr++; for(int i=0;i<n;i++){ int d2 = check(n,d,i); if(d2 != -1){ if(i!=n-1){ int lchild = go(n-i-1,d2,arr,curr); arr[root].child.insert(lchild); arr[lchild].par = root; // cout<<"lchild: "<<lchild<<" "<<root<<endl; } if(i!=0){ int rchild = go(i,d+1-n-d2,arr,curr); arr[root].child.insert(rchild); arr[rchild].par = root; // cout<<"rchild: "<<rchild<<" "<<root<<endl; } break; } } return root; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin>>t; while(t--){ int n,d; cin>>n>>d; int sm = 0; int add = 0; int currp = 0; int cnt = 0; smallest.resize(n+1); for(int i=1;i<=n;i++){ cnt++; sm += add; smallest[i] = sm; if(cnt == (1<<currp)){ cnt = 0; currp++; add++; } // cout << smallest[i] << " "; } // cout << "\n"; // cout << check(5,7,3); vector<Node> arr(n); int curr = 0; bool done = 0; int root = curr++; for(int n1=0;n1<n;n1++){ int d2 = check(n,d,n1); if(d2 != -1){ done = 1; if(n1!=n-1){ int lchild = go(n-n1-1,d2,arr,curr); arr[root].child.insert(lchild); arr[lchild].par = root; } if(n1!=0){ int rchild = go(n1,d+1-n-d2,arr,curr); arr[root].child.insert(rchild); arr[rchild].par = root; } break; } } if(!done){ cout<<"NO\n"; } else{ cout<<"YES\n"; for(int i=1;i<n;i++){ cout<<arr[i].par+1<<" "; } cout<<"\n"; } } return 0; }
cpp
1322
C
C. Instant Noodlestime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWu got hungry after an intense training session; and came to a nearby store to buy his favourite instant noodles. After Wu paid for his purchase, the cashier gave him an interesting task.You are given a bipartite graph with positive integers in all vertices of the right half. For a subset SS of vertices of the left half we define N(S)N(S) as the set of all vertices of the right half adjacent to at least one vertex in SS, and f(S)f(S) as the sum of all numbers in vertices of N(S)N(S). Find the greatest common divisor of f(S)f(S) for all possible non-empty subsets SS (assume that GCD of empty set is 00).Wu is too tired after his training to solve this problem. Help him!InputThe first line contains a single integer tt (1≤t≤5000001≤t≤500000) — the number of test cases in the given test set. Test case descriptions follow.The first line of each case description contains two integers nn and mm (1 ≤ n, m ≤ 5000001 ≤ n, m ≤ 500000) — the number of vertices in either half of the graph, and the number of edges respectively.The second line contains nn integers cici (1≤ci≤10121≤ci≤1012). The ii-th number describes the integer in the vertex ii of the right half of the graph.Each of the following mm lines contains a pair of integers uiui and vivi (1≤ui,vi≤n1≤ui,vi≤n), describing an edge between the vertex uiui of the left half and the vertex vivi of the right half. It is guaranteed that the graph does not contain multiple edges.Test case descriptions are separated with empty lines. The total value of nn across all test cases does not exceed 500000500000, and the total value of mm across all test cases does not exceed 500000500000 as well.OutputFor each test case print a single integer — the required greatest common divisor.ExampleInputCopy3 2 4 1 1 1 1 1 2 2 1 2 2 3 4 1 1 1 1 1 1 2 2 2 2 3 4 7 36 31 96 29 1 2 1 3 1 4 2 2 2 4 3 1 4 3 OutputCopy2 1 12 NoteThe greatest common divisor of a set of integers is the largest integer gg such that all elements of the set are divisible by gg.In the first sample case vertices of the left half and vertices of the right half are pairwise connected, and f(S)f(S) for any non-empty subset is 22, thus the greatest common divisor of these values if also equal to 22.In the second sample case the subset {1}{1} in the left half is connected to vertices {1,2}{1,2} of the right half, with the sum of numbers equal to 22, and the subset {1,2}{1,2} in the left half is connected to vertices {1,2,3}{1,2,3} of the right half, with the sum of numbers equal to 33. Thus, f({1})=2f({1})=2, f({1,2})=3f({1,2})=3, which means that the greatest common divisor of all values of f(S)f(S) is 11.
[ "graphs", "hashing", "math", "number theory" ]
#include <bits/stdc++.h> #define ll long long #define ld long double #define pii pair<int, int> #define pll pair<ll int, ll int> #define ff first #define ss second #define pb push_back #pragma GCC optimize("O2") using namespace std; // debug template #ifdef POTATO #include "debug.h" #define debug(...) cerr << "[" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__) #else #define debug(...) #endif // convenient functions inline void yes() { cout << "YES" << endl; return; } inline void no() { cout << "NO" << endl; return; } template <class T> inline void out(T temp) { cout << temp << endl; return; } // globals #define int long long mt19937_64 rng((int)chrono::duration_cast<chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count()); long long rnd(long long l, long long r) { // returns a random number in the range [l, r] return uniform_int_distribution<long long>(l, r)(rng); } const int mxN = 5e5 + 1; int hashval[mxN]; void init() { // initialize for (int i = 1; i < mxN; i++) { hashval[i] = rnd(1e18, 4e18); } } int gcd(int a, int b) { while (b) b ^= a ^= b ^= a %= b; return a; } void solve(int &case_no) { // solve int n, m; cin >> n >> m; int c[n + 1]; for (int i = 1; i <= n; i++) cin >> c[i]; int val[n + 1]; for (int i = 1; i <= n; i++) val[i] = 0; while (m--) { int u, v; cin >> u >> v; val[v] ^= hashval[u]; } vector<pii> v; for (int i = 1; i <= n; i++) { if (!val[i]) continue; v.pb({val[i], c[i]}); } sort(v.begin(), v.end()); int ans = -1; int sum = 0; int prev = v[0].ff; for (pii &cur : v) { if (prev != cur.ff) { if (ans == -1) ans = sum; else ans = gcd(ans, sum); sum = 0; prev = cur.ff; } sum += cur.ss; } if (ans == -1) ans = sum; else ans = gcd(ans, sum); cout << ans << endl; } signed main() { #ifdef POTATO assert(freopen("input.txt", "r", stdin)); // assert(freopen("output.txt", "w", stdout)); #endif ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); srand(time(NULL)); init(); int t = 1; cin >> t; for (int i = 1; i <= t; i++) solve(i); } /* */
cpp
1287
B
B. Hypersettime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes; shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature — color, number, shape, and shading — the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look.Polina came up with a new game called "Hyperset". In her game, there are nn cards with kk features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with k=4k=4.Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set.Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table.InputThe first line of each test contains two integers nn and kk (1≤n≤15001≤n≤1500, 1≤k≤301≤k≤30) — number of cards and number of features.Each of the following nn lines contains a card description: a string consisting of kk letters "S", "E", "T". The ii-th character of this string decribes the ii-th feature of that card. All cards are distinct.OutputOutput a single integer — the number of ways to choose three cards that form a set.ExamplesInputCopy3 3 SET ETS TSE OutputCopy1InputCopy3 4 SETE ETSE TSES OutputCopy0InputCopy5 4 SETT TEST EEET ESTE STES OutputCopy2NoteIn the third example test; these two triples of cards are sets: "SETT"; "TEST"; "EEET" "TEST"; "ESTE", "STES"
[ "brute force", "data structures", "implementation" ]
#include<bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; vector<string> vec(n); unordered_map<string, int> mpp; for (int i = 0; i < n; i++) { cin >> vec[i]; mpp[vec[i]]++; } int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { string s = ""; for (int z = 0; z < k; z++) { if (vec[i][z] == vec[j][z]) s += vec[i][z]; else { if (vec[i][z] == 'S' && vec[j][z] == 'E' || vec[i][z] == 'E' && vec[j][z] == 'S') s += 'T'; if (vec[i][z] == 'S' && vec[j][z] == 'T' || vec[i][z] == 'T' && vec[j][z] == 'S') s += 'E'; if (vec[i][z] == 'T' && vec[j][z] == 'E' || vec[i][z] == 'E' && vec[j][z] == 'T') s += 'S'; } } /* debug(s); */ if (mpp[s]) { count++; } mpp[vec[j]]--; } for (int t = i + 1; t < n; t++) mpp[vec[t]]++; mpp[vec[i]]--; } cout << count; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; /* cin >> t; */ while (t--) { solve(); } return 0; }
cpp
1284
B
B. New Year and Ascent Sequencetime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputA sequence a=[a1,a2,…,al]a=[a1,a2,…,al] of length ll has an ascent if there exists a pair of indices (i,j)(i,j) such that 1≤i<j≤l1≤i<j≤l and ai<ajai<aj. For example, the sequence [0,2,0,2,0][0,2,0,2,0] has an ascent because of the pair (1,4)(1,4), but the sequence [4,3,3,3,1][4,3,3,3,1] doesn't have an ascent.Let's call a concatenation of sequences pp and qq the sequence that is obtained by writing down sequences pp and qq one right after another without changing the order. For example, the concatenation of the [0,2,0,2,0][0,2,0,2,0] and [4,3,3,3,1][4,3,3,3,1] is the sequence [0,2,0,2,0,4,3,3,3,1][0,2,0,2,0,4,3,3,3,1]. The concatenation of sequences pp and qq is denoted as p+qp+q.Gyeonggeun thinks that sequences with ascents bring luck. Therefore, he wants to make many such sequences for the new year. Gyeonggeun has nn sequences s1,s2,…,sns1,s2,…,sn which may have different lengths. Gyeonggeun will consider all n2n2 pairs of sequences sxsx and sysy (1≤x,y≤n1≤x,y≤n), and will check if its concatenation sx+sysx+sy has an ascent. Note that he may select the same sequence twice, and the order of selection matters.Please count the number of pairs (x,yx,y) of sequences s1,s2,…,sns1,s2,…,sn whose concatenation sx+sysx+sy contains an ascent.InputThe first line contains the number nn (1≤n≤1000001≤n≤100000) denoting the number of sequences.The next nn lines contain the number lili (1≤li1≤li) denoting the length of sisi, followed by lili integers si,1,si,2,…,si,lisi,1,si,2,…,si,li (0≤si,j≤1060≤si,j≤106) denoting the sequence sisi. It is guaranteed that the sum of all lili does not exceed 100000100000.OutputPrint a single integer, the number of pairs of sequences whose concatenation has an ascent.ExamplesInputCopy5 1 1 1 1 1 2 1 4 1 3 OutputCopy9 InputCopy3 4 2 0 2 0 6 9 9 8 8 7 7 1 6 OutputCopy7 InputCopy10 3 62 24 39 1 17 1 99 1 60 1 64 1 30 2 79 29 2 20 73 2 85 37 1 100 OutputCopy72 NoteFor the first example; the following 99 arrays have an ascent: [1,2],[1,2],[1,3],[1,3],[1,4],[1,4],[2,3],[2,4],[3,4][1,2],[1,2],[1,3],[1,3],[1,4],[1,4],[2,3],[2,4],[3,4]. Arrays with the same contents are counted as their occurences.
[ "binary search", "combinatorics", "data structures", "dp", "implementation", "sortings" ]
//Made By Phuong Nam PROPTIT <3// #pragma GCC Optimize("O3") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> #define f(i,a,b) for(int i=a;i<=b;i++) #define f1(i,n) for(int i=1;i<=n;i++) #define f0(i,n) for(int i=0;i<n;i++) #define ff(i,b,a) for(int i=b;i>=a;i--) #define el cout<<'\n' #define fi first #define se second #define pb push_back #define pk pop_back #define vi vector<int> #define vl vector<ll> #define pii pair<int,int> #define pll pair<ll,ll> #define all(s) s.begin(),s.end() #define oset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace __gnu_cxx; using namespace std; typedef long long ll; const int N=1e6+3; const int MOD=1e9+7; int n,f[N]; set<int>a[N]; vector<pii>b; ll dem=0,ans=0,cnt=0; void xuly() { cin>>n; f1(i,n) { int m; cin>>m; f1(j,m) { int x; cin>>x; if(a[i].empty()) { a[i].insert(x); continue; } auto it=a[i].lower_bound(x); if(it!=a[i].begin()) it--; if((*it)<x) f[i]=1; a[i].insert(x); } if(f[i]) dem++; else { auto it=a[i].begin(); b.pb({(*it)+1,0}); it=a[i].end(); it--; b.pb({(*it),1}); } } sort(all(b)); for(auto i:b) { if(i.se) ans+=cnt; else cnt++; } ans+=dem*dem+2*dem*cnt; cout<<ans; } int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int t=1; //cin>>t; while(t--) xuly(); } //-----YEU CODE HON CRUSH-----//
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 means that if the frog is staying at the ii-th cell and the ii-th character is 'L', the frog can jump only to the left. If the frog is staying at the ii-th cell and the ii-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 00.Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the n+1n+1-th cell. The frog chooses some positive integer value dd before the first jump (and cannot change it later) and jumps by no more than dd cells at once. I.e. if the ii-th character is 'L' then the frog can jump to any cell in a range [max(0,i−d);i−1][max(0,i−d);i−1], and if the ii-th character is 'R' then the frog can jump to any cell in a range [i+1;min(n+1;i+d)][i+1;min(n+1;i+d)].The frog doesn't want to jump far, so your task is to find the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it can jump by no more than dd cells at once. It is guaranteed that it is always possible to reach n+1n+1 from 00.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.The next tt lines describe test cases. The ii-th test case is described as a string ss consisting of at least 11 and at most 2⋅1052⋅105 characters 'L' and 'R'.It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2⋅1052⋅105 (∑|s|≤2⋅105∑|s|≤2⋅105).OutputFor each test case, print the answer — the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it jumps by no more than dd at once.ExampleInputCopy6 LRLRRLL L LLR RRRR LLLLLL R OutputCopy3 2 3 1 7 1 NoteThe picture describing the first test case of the example and one of the possible answers:In the second test case of the example; the frog can only jump directly from 00 to n+1n+1.In the third test case of the example, the frog can choose d=3d=3, jump to the cell 33 from the cell 00 and then to the cell 44 from the cell 33.In the fourth test case of the example, the frog can choose d=1d=1 and jump 55 times to the right.In the fifth test case of the example, the frog can only jump directly from 00 to n+1n+1.In the sixth test case of the example, the frog can choose d=1d=1 and jump 22 times to the right.
[ "binary search", "data structures", "dfs and similar", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; while (t--){ string map = "R", str; cin >> str; map += str + "R"; vector<int>arr; for(int i=0; i<map.size(); i++)if(map[i] == 'R')arr.push_back(i); int res = 0; for(int i=1; i<arr.size(); i++)res = max(res, arr[i] - arr[i-1]); cout << res << "\n"; } return 0; }
cpp
1286
A
A. Garlandtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVadim loves decorating the Christmas tree; so he got a beautiful garland as a present. It consists of nn light bulbs in a single row. Each bulb has a number from 11 to nn (in arbitrary order), such that all the numbers are distinct. While Vadim was solving problems, his home Carp removed some light bulbs from the garland. Now Vadim wants to put them back on.Vadim wants to put all bulb back on the garland. Vadim defines complexity of a garland to be the number of pairs of adjacent bulbs with numbers with different parity (remainder of the division by 22). For example, the complexity of 1 4 2 3 5 is 22 and the complexity of 1 3 5 7 6 4 2 is 11.No one likes complexity, so Vadim wants to minimize the number of such pairs. Find the way to put all bulbs back on the garland, such that the complexity is as small as possible.InputThe first line contains a single integer nn (1≤n≤1001≤n≤100) — the number of light bulbs on the garland.The second line contains nn integers p1, p2, …, pnp1, p2, …, pn (0≤pi≤n0≤pi≤n) — the number on the ii-th bulb, or 00 if it was removed.OutputOutput a single number — the minimum complexity of the garland.ExamplesInputCopy5 0 5 0 2 3 OutputCopy2 InputCopy7 1 0 0 5 0 0 2 OutputCopy1 NoteIn the first example; one should place light bulbs as 1 5 4 2 3. In that case; the complexity would be equal to 2; because only (5,4)(5,4) and (2,3)(2,3) are the pairs of adjacent bulbs that have different parity.In the second case, one of the correct answers is 1 7 3 5 6 4 2.
[ "dp", "greedy", "sortings" ]
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define dbg(...) #define setIO(...) #endif using ll = long long; #define pb push_back #define all(x) (x).begin(), (x).end() int MOD = (int)1e9 + 7; ll INF = 1e18 + 7; int dp[101][2][101][101]; int res = MOD; int dfs(vector<int>&a, int idx, int n, bool isEven, int odd, int even, int diff){ if ((even > (n / 2)) || (odd > ((n+1) / 2))) return MOD; if (idx == n){ res = min(diff, res); return diff; } if (dp[idx][isEven][odd][even] != MOD && dp[idx][isEven][odd][even] <= diff) return dp[idx][isEven][odd][even]; int ans = 0; if (a[idx] != 0) { bool currEven = a[idx]%2 == 0; if (currEven){ ans += dfs(a, idx+1, n, true, odd, even+1, diff + !isEven); } else { ans += dfs(a, idx+1, n, false, odd+1, even, diff + isEven); } } else { ans += dfs(a, idx+1, n, true, odd, even+1, diff + !isEven); ans += dfs(a, idx+1, n, false, odd+1, even, diff + isEven); } return dp[idx][isEven][odd][even] = min(diff, ans); } void reset(){ for (int i = 0; i < 101; i++){ for (int j = 0; j < 2; j++){ for (int k = 0;k < 101; k++){ for (int l = 0; l < 101; l++){ dp[i][j][k][l] = MOD; } } } } } void solve() { int n; cin >> n; vector<int> v(n); for (auto &a : v){ cin >> a; } reset(); dfs(v, 0, n, false, 0, 0, 0); reset(); dfs(v, 0, n, true, 0, 0, 0); cout << res << endl; } int main(){ cin.tie(0)->sync_with_stdio(0); setIO(); int tc = 1; // cin >> tc; for (int i = 1; i <= tc; i++){ //cout << "Case #" << i << ": "; solve(); } return 0; }
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 that meeting room walls are strictly between letters on the building. Obviously, meeting rooms should not be of size 0, but can be as small as one letter wide. Each meeting room will be named after the substring of ss written on its side.For each possible arrangement of mm meeting rooms we ordered a test meeting room label for the meeting room with lexicographically minimal name. When delivered, those labels got sorted backward lexicographically.What is printed on kkth label of the delivery?InputIn the first line, you are given three integer numbers n,m,kn,m,k — length of string ss, number of planned meeting rooms to split ss into and number of the interesting label (2≤n≤1000;1≤m≤1000;1≤k≤10182≤n≤1000;1≤m≤1000;1≤k≤1018).Second input line has string ss, consisting of nn lowercase english letters.For given n,m,kn,m,k there are at least kk ways to split ss into mm substrings.OutputOutput single string – name of meeting room printed on kk-th label of the delivery.ExamplesInputCopy4 2 1 abac OutputCopyaba InputCopy19 5 1821 aupontrougevkoffice OutputCopyau NoteIn the first example; delivery consists of the labels "aba"; "ab"; "a".In the second example; delivery consists of 30603060 labels. The first label is "aupontrougevkof" and the last one is "a".
[ "binary search", "dp", "strings" ]
// LUOGU_RID: 90343523 #include<cstdio> #include<cstdlib> #include<utility> #include<algorithm> int n,m,lcp[1005][1005]; long long dp[1005][1005],sf[1005][1005],cnt,k; std::pair<int,int> s[1000005]; char st[1005]; long long adjust(long long x){return std::min(x,2000000000000000000ll);} int cmp(std::pair<int,int> x,std::pair<int,int> y) { int l=lcp[x.first][y.first],ml=std::min(x.second,y.second); if (l<ml) return st[x.first+l]<st[y.first+l]; else return x.second<y.second; } int ask(int k,int i) { int l=lcp[s[k].first][i]; if (l>s[k].second) l=s[k].second; if (l==s[k].second) return i+l-1; if (i+l-1==n) return 0; if (st[s[k].first+l]<st[i+l]) return i+l; return 0; } long long solve(int k) { for (int i=0;i<=n+1;i++) for (int j=0;j<=m+1;j++) dp[i][j]=sf[i][j]=0; sf[n+1][0]=dp[n+1][0]=1; for (int i=n;i>=1;i--) { int ret=ask(k,i); if (ret) for (int s=1;s<=m;s++) dp[i][s]=sf[ret+1][s-1]; for (int s=0;s<=m;s++) sf[i][s]=adjust(sf[i+1][s]+dp[i][s]); } return dp[1][m]; } int bs(int l,int r) { if (l==r) return l; int mid=(l+r+1)/2; if (solve(mid)<k) return bs(l,mid-1); return bs(mid,r); } int main() { scanf("%d%d%lld",&n,&m,&k); scanf("%s",st+1); for (int i=n;i>=1;i--) for (int j=n;j>=1;j--) lcp[i][j]=(st[i]==st[j])*(lcp[i+1][j+1]+1); for (int i=1;i<=n;i++) for (int j=i;j<=n;j++) s[++cnt]=std::make_pair(i,j-i+1); std::sort(s+1,s+cnt+1,cmp); int k=bs(1,cnt); for (int i=s[k].first;i<=s[k].first+s[k].second-1;i++) printf("%c",st[i]); puts(""); return 0; }
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 the results.Fortunately, Adilbek can optimize the program. If he spends xx (xx is a non-negative integer) days optimizing the program, he will make the program run in ⌈dx+1⌉⌈dx+1⌉ days (⌈a⌉⌈a⌉ is the ceiling function: ⌈2.4⌉=3⌈2.4⌉=3, ⌈2⌉=2⌈2⌉=2). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to x+⌈dx+1⌉x+⌈dx+1⌉.Will Adilbek be able to provide the generated results in no more than nn days?InputThe first line contains a single integer TT (1≤T≤501≤T≤50) — the number of test cases.The next TT lines contain test cases – one per line. Each line contains two integers nn and dd (1≤n≤1091≤n≤109, 1≤d≤1091≤d≤109) — the number of days before the deadline and the number of days the program runs.OutputPrint TT answers — one per test case. For each test case print YES (case insensitive) if Adilbek can fit in nn days or NO (case insensitive) otherwise.ExampleInputCopy3 1 1 4 5 5 11 OutputCopyYES YES NO NoteIn the first test case; Adilbek decides not to optimize the program at all; since d≤nd≤n.In the second test case, Adilbek can spend 11 day optimizing the program and it will run ⌈52⌉=3⌈52⌉=3 days. In total, he will spend 44 days and will fit in the limit.In the third test case, it's impossible to fit in the limit. For example, if Adilbek will optimize the program 22 days, it'll still work ⌈112+1⌉=4⌈112+1⌉=4 days.
[ "binary search", "brute force", "math", "ternary search" ]
#include <bits/stdc++.h> using namespace std; typedef long long int ll; # define mod 998244353 int main() { int t; cin>>t; while(t--) { ll n,d; cin>>n>>d; if((n-1)*(n-1)+4ll*(n-d)>=0) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } } return 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. Treeland is preparing for a seasonal virus epidemic, and currently, they are trying to evaluate different infection scenarios.In each scenario, several cities are initially infected with different virus species. Suppose that there are kiki virus species in the ii-th scenario. Let us denote vjvj the initial city for the virus jj, and sjsj the propagation speed of the virus jj. The spread of the viruses happens in turns: first virus 11 spreads, followed by virus 22, and so on. After virus kiki spreads, the process starts again from virus 11.A spread turn of virus jj proceeds as follows. For each city xx not infected with any virus at the start of the turn, at the end of the turn it becomes infected with virus jj if and only if there is such a city yy that: city yy was infected with virus jj at the start of the turn; the path between cities xx and yy contains at most sjsj edges; all cities on the path between cities xx and yy (excluding yy) were uninfected with any virus at the start of the turn.Once a city is infected with a virus, it stays infected indefinitely and can not be infected with any other virus. The spread stops once all cities are infected.You need to process qq independent scenarios. Each scenario is described by kiki virus species and mimi important cities. For each important city determine which the virus it will be infected by in the end.InputThe first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of cities in Treeland.The following n−1n−1 lines describe the roads. The ii-th of these lines contains two integers xixi and yiyi (1≤xi,yi≤n1≤xi,yi≤n) — indices of cities connecting by the ii-th road. It is guaranteed that the given graph of cities and roads is a tree.The next line contains a single integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of infection scenarios. qq scenario descriptions follow.The description of the ii-th scenario starts with a line containing two integers kiki and mimi (1≤ki,mi≤n1≤ki,mi≤n) — the number of virus species and the number of important cities in this scenario respectively. It is guaranteed that ∑qi=1ki∑i=1qki and ∑qi=1mi∑i=1qmi do not exceed 2⋅1052⋅105.The following kiki lines describe the virus species. The jj-th of these lines contains two integers vjvj and sjsj (1≤vj≤n1≤vj≤n, 1≤sj≤1061≤sj≤106) – the initial city and the propagation speed of the virus species jj. It is guaranteed that the initial cities of all virus species within a scenario are distinct.The following line contains mimi distinct integers u1,…,umiu1,…,umi (1≤uj≤n1≤uj≤n) — indices of important cities.OutputPrint qq lines. The ii-th line should contain mimi integers — indices of virus species that cities u1,…,umiu1,…,umi are infected with at the end of the ii-th scenario.ExampleInputCopy7 1 2 1 3 2 4 2 5 3 6 3 7 3 2 2 4 1 7 1 1 3 2 2 4 3 7 1 1 3 3 3 1 1 4 100 7 100 1 2 3 OutputCopy1 2 1 1 1 1 1
[ "data structures", "dfs and similar", "dp", "shortest paths", "trees" ]
// LUOGU_RID: 97737920 #include<bits/stdc++.h> using namespace std; typedef pair<int,int> Pair; const int N=2e5+500,INF=1e9; #define F first #define S second Pair f[N],g[N],pre[N],suf[N]; int dfn[N],ST[N<<1][19],Log[N<<1],dep[N],col[N],s[N],p[N],vis[N],vet[N],stk[N],que[N]; int n,m,k,q,u,v,L,top; vector<int> G[N],Tr[N]; bool cmp(int i, int j){ return dfn[i]<dfn[j]; } void dfs(int u, int f){ ST[dfn[u]=++L][0]=u; dep[u]=dep[f]+1; for (int v:G[u]) if (v!=f) dfs(v,u),ST[++L][0]=u; } inline int LCA(int u, int v){ int l=dfn[u],r=dfn[v]; if (l>r) swap(l,r); int k=Log[r-l+1],x=ST[l][k],y=ST[r-(1<<k)+1][k]; return dep[x]<dep[y]?x:y; } inline int dist(int u, int v){ return dep[u]+dep[v]-dep[LCA(u,v)]*2; } void dfs1(int u){ for (int v:Tr[u]){ dfs1(v); pre[v]=f[u]; int c=f[v].S; if (c) f[u]=min(f[u],make_pair((dist(p[c],u)+s[c]-1)/s[c],c)); } Pair P={INF,0}; for (int i=Tr[u].size()-1; ~i; i--){ suf[Tr[u][i]]=P; int c=f[Tr[u][i]].S; if (c) P=min(P,make_pair((dist(p[c],u)+s[c]-1)/s[c],c)); } if (col[u]) f[u]={0,col[u]}; } void dfs2(int u){ int c=g[u].S; if (c) f[u]=min(f[u],make_pair((dist(p[c],u)+s[c]-1)/s[c],c)); for (int v:Tr[u]){ if (col[u]) g[v]={0,col[u]}; else { g[v]=min(pre[v],suf[v]); int c=g[u].S; if (c) g[v]=min(g[v],make_pair((dist(p[c],u)+s[c]-1)/s[c],c)); } dfs2(v); } } void Clear(int u){ f[u]=g[u]=pre[u]=suf[u]={INF,0}; col[u]=vis[u]=0; for (int v:Tr[u]) Clear(v); Tr[u].clear(); } inline void Add(int u, int v){ Tr[u].push_back(v); } char buf[1<<24],*S=buf,obuf[1<<23],*Os=obuf; #define gc() (*S++) inline int read(){ char ch=gc(); int num=0; while (!isdigit(ch)) ch=gc(); while (isdigit(ch)) num=num*10+ch-'0',ch=gc(); return num; } inline void Pc(char ch){ *Os++=ch; } void P(int x){ if (x>9) P(x/10); Pc(x%10+'0'); } int main(){ fread(buf,1,1<<24,stdin); n=read(); Log[0]=-1; for (int i=1; i<n; i++){ u=read(),v=read(); G[u].push_back(v),G[v].push_back(u); } dfs(1,0); for (int i=1; i<=L; i++) Log[i]=Log[i>>1]+1; for (int i=L; i>=1; i--) for (int j=1; i+(1<<j)-1<=L; j++){ int x=ST[i][j-1],y=ST[i+(1<<(j-1))][j-1]; ST[i][j]=dep[x]<dep[y]?x:y; } for (int i=1; i<=n; i++) f[i]=g[i]=pre[i]=suf[i]={INF,0}; for (q=read(); q; q--){ m=read(),k=read(); int len=0; for (int i=1; i<=m; i++){ p[i]=read(),s[i]=read(); col[p[i]]=i; if (!vis[p[i]]) vis[p[i]]=1,vet[++len]=p[i]; } for (int i=1; i<=k; i++){ que[i]=read(); if (!vis[que[i]]) vis[que[i]]=1,vet[++len]=que[i]; } sort(vet+1,vet+1+len,cmp); stk[top=1]=1; for (int i=1; i<=len; i++){ if (vet[i]==1) continue; while (LCA(stk[top],vet[i])!=stk[top]) if (LCA(stk[top-1],vet[i])==stk[top-1]){ int u=LCA(stk[top],vet[i]); Add(u,stk[top--]); if (u!=stk[top]) stk[++top]=u; } else Add(stk[top-1],stk[top]),top--; stk[++top]=vet[i]; } for (int i=1; i<top; i++) Add(stk[i],stk[i+1]); dfs1(1); dfs2(1); for (int i=1; i<=k; i++) P(f[que[i]].S),Pc(' '); Clear(1); Pc('\n'); } fwrite(obuf,1,Os-obuf,stdout); }
cpp
1313
A
A. Fast Food Restauranttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTired of boring office work; Denis decided to open a fast food restaurant.On the first day he made aa portions of dumplings, bb portions of cranberry juice and cc pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules: every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes); each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk; all visitors should receive different sets of dishes. What is the maximum number of visitors Denis can feed?InputThe first line contains an integer tt (1≤t≤5001≤t≤500) — the number of test cases to solve.Each of the remaining tt lines contains integers aa, bb and cc (0≤a,b,c≤100≤a,b,c≤10) — the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.OutputFor each test case print a single integer — the maximum number of visitors Denis can feed.ExampleInputCopy71 2 10 0 09 1 72 2 32 3 23 2 24 4 4OutputCopy3045557NoteIn the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
[ "brute force", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a[3]; cin >> a[0] >> a[1] >> a[2]; int ans = 0; for (int mask = 0; mask < (1 << 7); mask++) { int b[3] = {a[0], a[1], a[2]}; for (int i = 1; i <= 7; i++) if ((mask >> (i - 1)) & 1) for (int j = 0; j < 3; j++) if ((i >> j) & 1) b[j]--; if (b[0] >= 0 && b[1] >= 0 && b[2] >= 0) ans = max(ans, __builtin_popcount(mask)); } cout << ans << "\n"; } }
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 vector and add the segment of the vector to the current point. For example, if your current point is at (x,y)(x,y) and you choose the vector (u,v)(u,v), draw a segment from your current point to the point at (x+u,y+v)(x+u,y+v) and set your current point to (x+u,y+v)(x+u,y+v). Repeat step 2 until you reach the origin again.You can reuse a vector as many times as you want.Count the number of different, non-degenerate (with an area greater than 00) and convex shapes made from applying the steps, such that the shape can be contained within a m×mm×m square, and the vectors building the shape are in counter-clockwise fashion. Since this number can be too large, you should calculate it by modulo 998244353998244353.Two shapes are considered the same if there exists some parallel translation of the first shape to another.A shape can be contained within a m×mm×m square if there exists some parallel translation of this shape so that every point (u,v)(u,v) inside or on the border of the shape satisfies 0≤u,v≤m0≤u,v≤m.InputThe first line contains two integers nn and mm  — the number of vectors and the size of the square (1≤n≤51≤n≤5, 1≤m≤1091≤m≤109).Each of the next nn lines contains two integers xixi and yiyi  — the xx-coordinate and yy-coordinate of the ii-th vector (|xi|,|yi|≤4|xi|,|yi|≤4, (xi,yi)≠(0,0)(xi,yi)≠(0,0)).It is guaranteed, that no two vectors are parallel, so for any two indices ii and jj such that 1≤i<j≤n1≤i<j≤n, there is no real value kk such that xi⋅k=xjxi⋅k=xj and yi⋅k=yjyi⋅k=yj.OutputOutput a single integer  — the number of satisfiable shapes by modulo 998244353998244353.ExamplesInputCopy3 3 -1 0 1 1 0 -1 OutputCopy3 InputCopy3 3 -1 0 2 2 0 -1 OutputCopy1 InputCopy3 1776966 -1 0 3 3 0 -2 OutputCopy296161 InputCopy4 15 -4 -4 -1 1 -1 -4 4 3 OutputCopy1 InputCopy5 10 3 -4 4 -3 1 -3 2 -3 -3 -4 OutputCopy0 InputCopy5 1000000000 -2 4 2 -3 0 -4 2 4 -1 -3 OutputCopy9248783 NoteThe shapes for the first sample are: The only shape for the second sample is: The only shape for the fourth sample is:
[ "dp" ]
// LUOGU_RID: 102614426 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 11 #define yu (998244353) ll n,m; ll ans=0; inline void add(ll &x,ll y){x+=y;if(x>=yu)x-=yu;return ;} ll xi[6],yi[6]; ll f[31][22][22][22][22][2][2]; inline ll dfs(ll pos,ll x,ll fx,ll y,ll fy,ll x1,ll x2){ if(pos==31){ return ((x==0)&(fx==0)&(y==0)&(fy==0)&(x1==0)&(x2==0)); }if(f[pos][x][fx][y][fy][x1][x2]!=-1)return f[pos][x][fx][y][fy][x1][x2]; ll an=0; for(int i=0;i<(1<<n);i++){ ll jix=0,jifx=0,jiy=0,jify=0; for(int j=1;j<=n;j++){ if(i&(1<<(j-1))){ if(xi[j]>0)jix+=xi[j]; else jifx-=xi[j]; if(yi[j]>0)jiy+=yi[j]; else jify-=yi[j]; } } jix+=x;jifx+=fx;jiy+=y;jify+=fy; if(((jix&1)!=(jifx&1))||((jiy&1)!=(jify&1)))continue; ll o=(m>>pos)&1; add(an,dfs(pos+1,jix>>1,jifx>>1,jiy>>1,jify>>1,((o==(jix&1))?x1:(o<(jix&1))),((o==(jiy&1))?x2:(o<(jiy&1))))); }return f[pos][x][fx][y][fy][x1][x2]=an; } int main() { // freopen("test1.in","r",stdin); //freopen(".in","r",stdin); //freopen("test1.out","w",stdout); ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n>>m; for(int i=1;i<=n;i++){ cin>>xi[i]>>yi[i]; }memset(f,-1,sizeof(f)); cout<<(dfs(0,0,0,0,0,0,0)-1+yu)%yu; return 0; }
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 sufficient. After almost 3 years; it's time for Ivy to awake her bonded sister, Vanessa.The system inside A.R.C.'s Library core can be considered as an undirected graph with infinite number of processing nodes, numbered with all positive integers (1,2,3,…1,2,3,…). The node with a number xx (x>1x>1), is directly connected with a node with number xf(x)xf(x), with f(x)f(x) being the lowest prime divisor of xx.Vanessa's mind is divided into nn fragments. Due to more than 500 years of coma, the fragments have been scattered: the ii-th fragment is now located at the node with a number ki!ki! (a factorial of kiki).To maximize the chance of successful awakening, Ivy decides to place the samples in a node PP, so that the total length of paths from each fragment to PP is smallest possible. If there are multiple fragments located at the same node, the path from that node to PP needs to be counted multiple times.In the world of zeros and ones, such a requirement is very simple for Ivy. Not longer than a second later, she has already figured out such a node.But for a mere human like you, is this still possible?For simplicity, please answer the minimal sum of paths' lengths from every fragment to the emotion samples' assembly node PP.InputThe first line contains an integer nn (1≤n≤1061≤n≤106) — number of fragments of Vanessa's mind.The second line contains nn integers: k1,k2,…,knk1,k2,…,kn (0≤ki≤50000≤ki≤5000), denoting the nodes where fragments of Vanessa's mind are located: the ii-th fragment is at the node with a number ki!ki!.OutputPrint a single integer, denoting the minimal sum of path from every fragment to the node with the emotion samples (a.k.a. node PP).As a reminder, if there are multiple fragments at the same node, the distance from that node to PP needs to be counted multiple times as well.ExamplesInputCopy3 2 1 4 OutputCopy5 InputCopy4 3 1 4 4 OutputCopy6 InputCopy4 3 1 4 1 OutputCopy6 InputCopy5 3 1 4 1 5 OutputCopy11 NoteConsidering the first 2424 nodes of the system; the node network will look as follows (the nodes 1!1!, 2!2!, 3!3!, 4!4! are drawn bold):For the first example, Ivy will place the emotion samples at the node 11. From here: The distance from Vanessa's first fragment to the node 11 is 11. The distance from Vanessa's second fragment to the node 11 is 00. The distance from Vanessa's third fragment to the node 11 is 44. The total length is 55.For the second example, the assembly node will be 66. From here: The distance from Vanessa's first fragment to the node 66 is 00. The distance from Vanessa's second fragment to the node 66 is 22. The distance from Vanessa's third fragment to the node 66 is 22. The distance from Vanessa's fourth fragment to the node 66 is again 22. The total path length is 66.
[ "dp", "graphs", "greedy", "math", "number theory", "trees" ]
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt") #include <bits/stdc++.h> #define f first #define s second #define mp make_pair #define pb push_back #define all(x) (x).begin(), (x).end() #define sz(x) ((int) (x).size()) using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vp = vector <pii>; using vl = vector<ll>; const int inf = INT_MAX; const ll linf = LLONG_MAX; const int PI = 669; const int MX = 5000; int main() { int n; scanf("%d", &n); array <int, PI> primes; vi spf(MX+1), w(MX+1); vector <array<int, PI>> info(MX+1); int ptr = 0; for(int i = 2; i <= MX; i++) { if(!spf[i]) { primes[ptr++] = i; for(int j = i; j <= MX; j += i) spf[j] = i; } } vi rel, a(n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); if(a[i] == 0) a[i] = 1; if(!w[a[i]]) rel.pb(a[i]); w[a[i]]++; } reverse(all(primes)); for(int v : rel) { ptr = 0; for(int p : primes) { int x = v; int r = 0; while(x) { r += x / p; x /= p; } info[v][ptr++] = r; } } vector <array<int, PI>> ancestor_info; sort(all(rel), [&](int u, int v) { return info[u] < info[v]; }); for(int v : rel) { ancestor_info.pb(info[v]); } auto lca = [&](array <int, PI> &u, array <int, PI> &v) -> array<int, PI> { array <int, PI> ret; for(int j = 0; j < PI; j++) ret[j] = 0; for(int i = 0; i < PI; i++) { if(u[i] == v[i]) { ret[i] = u[i]; } else { ret[i] = min(u[i], v[i]); break; } } return ret; }; for(int i = 1; i < sz(rel); i++) { auto lca_info = lca(info[rel[i - 1]], info[rel[i]]); ancestor_info.pb(lca_info); } sort(all(ancestor_info)); ancestor_info.resize(unique(all(ancestor_info)) - begin(ancestor_info)); vi sub(sz(ancestor_info)); vector <vp> adj(sz(ancestor_info)); auto lb = [&](array <int, PI> &v_info) -> int { return lower_bound(all(ancestor_info), v_info) - begin(ancestor_info); }; for(int v : rel) { sub[lb(info[v])] += w[v]; } for(int i = 1; i < sz(ancestor_info); i++) { auto lca_info = lca(ancestor_info[i - 1], ancestor_info[i]); int w = 0; for(int j = 0; j < PI; j++) { w += ancestor_info[i][j] - lca_info[j]; } int idx = lb(lca_info); adj[idx].pb({i, w}); } vl below(sz(ancestor_info)); ll ans = linf; auto dfs1 = [&](auto &&self, int u) -> void { for(auto [v, w] : adj[u]) { self(self, v); sub[u] += sub[v]; below[u] += below[v] + 1ll * sub[v] * 1ll * w; } }; dfs1(dfs1, 0); auto dfs2 = [&](auto &&self, int u, ll above) -> void { for(auto [v, w] : adj[u]) { self(self, v, above + below[u] - (below[v] + 1ll * sub[v] * 1ll * w) + 1ll * (n - sub[v]) * 1ll * w); } ans = min(ans, below[u] + above); }; dfs2(dfs2, 0, 0); printf("%lld\n", ans); return 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), where ai=0ai=0 if Polycarp works during the ii-th hour of the day and ai=1ai=1 if Polycarp rests during the ii-th hour of the day.Days go one after another endlessly and Polycarp uses the same schedule for each day.What is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day.InputThe first line contains nn (1≤n≤2⋅1051≤n≤2⋅105) — number of hours per day.The second line contains nn integer numbers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1), where ai=0ai=0 if the ii-th hour in a day is working and ai=1ai=1 if the ii-th hour is resting. It is guaranteed that ai=0ai=0 for at least one ii.OutputPrint the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day.ExamplesInputCopy5 1 0 1 0 1 OutputCopy2 InputCopy6 0 1 0 1 1 0 OutputCopy2 InputCopy7 1 0 1 1 1 0 1 OutputCopy3 InputCopy3 0 0 0 OutputCopy0 NoteIn the first example; the maximal rest starts in last hour and goes to the first hour of the next day.In the second example; Polycarp has maximal rest from the 44-th to the 55-th hour.In the third example, Polycarp has maximal rest from the 33-rd to the 55-th hour.In the fourth example, Polycarp has no rest at all.
[ "implementation" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } #define endl '\n'; void solve() { int n; cin>>n; vector<int> v(n); for (auto &it : v) { cin >>it; } int maxi = 0; int result = 0; for (int i = 0; i < 2*n; i++) { if (v[i%n] == 1) { maxi++; result = max(result,maxi); } else { maxi = 0; } } cout<<result; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
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 decided to choose a substring of the string aa, then choose a substring of the string bb and concatenate them. Formally, he chooses a segment [l1,r1][l1,r1] (1≤l1≤r1≤n1≤l1≤r1≤n) and a segment [l2,r2][l2,r2] (1≤l2≤r2≤n1≤l2≤r2≤n), and after concatenation he obtains a string a[l1,r1]+b[l2,r2]=al1al1+1…ar1bl2bl2+1…br2a[l1,r1]+b[l2,r2]=al1al1+1…ar1bl2bl2+1…br2.Now, Vasya is interested in counting number of ways to choose those segments adhering to the following conditions: segments [l1,r1][l1,r1] and [l2,r2][l2,r2] have non-empty intersection, i.e. there exists at least one integer xx, such that l1≤x≤r1l1≤x≤r1 and l2≤x≤r2l2≤x≤r2; the string a[l1,r1]+b[l2,r2]a[l1,r1]+b[l2,r2] is equal to the string ss. InputThe first line contains integers nn and mm (1≤n≤500000,2≤m≤2⋅n1≤n≤500000,2≤m≤2⋅n) — the length of strings aa and bb and the length of the string ss.The next three lines contain strings aa, bb and ss, respectively. The length of the strings aa and bb is nn, while the length of the string ss is mm.All strings consist of lowercase English letters.OutputPrint one integer — the number of ways to choose a pair of segments, which satisfy Vasya's conditions.ExamplesInputCopy6 5aabbaabaaaabaaaaaOutputCopy4InputCopy5 4azazazazazazazOutputCopy11InputCopy9 12abcabcabcxyzxyzxyzabcabcayzxyzOutputCopy2NoteLet's list all the pairs of segments that Vasya could choose in the first example: [2,2][2,2] and [2,5][2,5]; [1,2][1,2] and [2,4][2,4]; [5,5][5,5] and [2,5][2,5]; [5,6][5,6] and [3,5][3,5];
[ "data structures", "hashing", "strings", "two pointers" ]
#include<bits/stdc++.h> using namespace std; typedef long long ll; //#include<bits/extc++.h> //__gnu_pbds #define int ll void zalgo(int *z,string s){ fill(z,z+s.size(),0); for(int i = 1,l = 0, r = 0;i<s.size();i++){ if(i<=r) z[i] = min(z[i-l],r-i+1); while(i+z[i]<s.size() && s[i+z[i]]==s[z[i]]) z[i]++; if(i+z[i]-1>r) l = i,r = i+z[i]-1; } } struct sg{ struct node{ int v = 0; int tag = 0; int len = 1; void apply(int u){ v+=len*u; tag+=u; } }; vector<node> arr; int n; void build(){ for(int i = n-1;i>=1;i--){ arr[i].len = arr[2*i].len+arr[2*i^1].len; } } void init(int s){ n = s; arr.resize(2*s); build(); } void pull(int ind){ if(ind<=1) return; pull(ind>>1); ind>>=1; arr[2*ind].apply(arr[ind].tag); arr[2*ind^1].apply(arr[ind].tag); arr[ind].tag = 0; } void push(int ind){ ind>>=1; while(ind){ arr[ind].v = arr[2*ind].v+arr[2*ind^1].v; int k = arr[ind].tag; arr[ind].apply(k); arr[ind].tag = k; ind>>=1; } } void add(int l,int r,int v){ l+=n;r+=n; pull(l); pull(r-1); int tl = l; int tr = r; while(l<r){ if(l&1) arr[l++].apply(v); if(r&1) arr[--r].apply(v); l>>=1;r>>=1; } push(tl); push(tr-1); } int query(int l,int r){ l+=n;r+=n; pull(l); pull(r-1); int ans = 0; while(l<r){ if(l&1) ans+=arr[l++].v; if(r&1) ans+=arr[--r].v; l>>=1;r>>=1; } return ans; } void print(){ for(int i = 0;i<n;i++){ cout<<query(i,i+1)<<"\n"; } } } seg; signed main(){ ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n,m;cin>>n>>m; string a,b;cin>>a>>b; reverse(b.begin(),b.end()); string s;cin>>s; int z[n+m+1]; zalgo(z,s+"#"+a); vector<int> aarr(n); for(int i=0;i<n;i++) aarr[i] = (z[i+m+1]==m)?(m-1):(z[i+m+1]); //for(int i=0;i<n;i++) cerr<<aarr[i]<<" "; //cerr<<"\n"; reverse(s.begin(),s.end()); zalgo(z,s+"#"+b); vector<int> barr(n); for(int i=0;i<n;i++) barr[n-i-1] = (z[i+m+1]==m)?(m-1):(z[i+m+1]); //for(int i=0;i<n;i++) cerr<<barr[i]<<" "; //cerr<<"\n"; seg.init(max(n,m)+5); ll ans = 0; for(int i = 0;i<min(m-1,n);i++){ seg.add(0,barr[i]+1,1); } for(int i = 0;i<n;i++){ ans+=seg.query(m-aarr[i],max(m,n)); seg.add(0,barr[i]+1,-1); if((i+m-1)<n) seg.add(0,barr[i+m-1]+1,1); } cout<<ans<<"\n"; return 0; }
cpp
1295
E
E. Permutation Separationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a permutation p1,p2,…,pnp1,p2,…,pn (an array where each integer from 11 to nn appears exactly once). The weight of the ii-th element of this permutation is aiai.At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p1,p2,…,pkp1,p2,…,pk, the second — pk+1,pk+2,…,pnpk+1,pk+2,…,pn, where 1≤k<n1≤k<n.After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay aiai dollars to move the element pipi.Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met.For example, if p=[3,1,2]p=[3,1,2] and a=[7,1,4]a=[7,1,4], then the optimal strategy is: separate pp into two parts [3,1][3,1] and [2][2] and then move the 22-element into first set (it costs 44). And if p=[3,5,1,6,2,4]p=[3,5,1,6,2,4], a=[9,1,9,9,1,9]a=[9,1,9,9,1,9], then the optimal strategy is: separate pp into two parts [3,5,1][3,5,1] and [6,2,4][6,2,4], and then move the 22-element into first set (it costs 11), and 55-element into second set (it also costs 11).Calculate the minimum number of dollars you have to spend.InputThe first line contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of permutation.The second line contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). It's guaranteed that this sequence contains each element from 11 to nn exactly once.The third line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).OutputPrint one integer — the minimum number of dollars you have to spend.ExamplesInputCopy3 3 1 2 7 1 4 OutputCopy4 InputCopy4 2 4 1 3 5 9 8 3 OutputCopy3 InputCopy6 3 5 1 6 2 4 9 1 9 9 1 9 OutputCopy2
[ "data structures", "divide and conquer" ]
#include <bits/stdc++.h> #define int long long using namespace std; int n, i, ans = 1e18, a[1200001], p[1200001], pr[1200001], pos[1200001], t[1200001], d[1200001]; void push(int v) { if (!d[v]) return; t[v + v] += d[v]; t[v + v + 1] += d[v]; d[v + v] += d[v]; d[v + v + 1] += d[v]; d[v] = 0; } void build(int v, int tl, int tr) { if (tl == tr) { t[v] = pr[tl]; return; } int tm = (tl + tr) / 2; build(v + v, tl, tm); build(v + v + 1, tm + 1, tr); t[v] = min(t[v + v], t[v + v + 1]); } void update(int v, int tl, int tr, int l, int r, int x) { if (l > r) return; if (l == tl && tr == r) { t[v] += x; d[v] += x; push(v); return; } push(v); int tm = (tl + tr) / 2; update(v + v, tl, tm, l, min(r, tm), x); update(v + v + 1, tm + 1, tr, max(l, tm + 1), r, x); t[v] = min(t[v + v], t[v + v + 1]); } int get(int v, int tl, int tr, int l, int r) { if (l > r) return 1e18; push(v); if (l == tl && tr == r) return t[v]; int tm = (tl + tr) / 2; return min(get(v + v, tl, tm, l, min(r, tm)), get(v + v + 1, tm + 1, tr, max(l, tm + 1), r)); } signed main() { cin.tie(0)->sync_with_stdio(0); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif cin >> n; for (i = 1; i <= n; i++) cin >> p[i], pos[p[i]] = i; for (i = 1; i <= n; i++) { cin >> a[i]; pr[i] = pr[i - 1] + a[i]; } build(1, 1, n); ans = min(ans, get(1, 1, n, 1, n - 1)); for (i = 1; i <= n; i++) { update(1, 1, n, pos[i], n, -a[pos[i]]); update(1, 1, n, 1, pos[i] - 1, a[pos[i]]); ans = min(ans, get(1, 1, n, 1, n - 1)); //cout << ans << " "; } cout << ans; }
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 any three subsets is empty. In other words, for all 1≤i1<i2<i3≤k1≤i1<i2<i3≤k, Ai1∩Ai2∩Ai3=∅Ai1∩Ai2∩Ai3=∅.In one operation, you can choose one of these kk subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.Let mimi be the minimum number of operations you have to do in order to make the ii first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1i+1 and nn), they can be either off or on.You have to compute mimi for all 1≤i≤n1≤i≤n.InputThe first line contains two integers nn and kk (1≤n,k≤3⋅1051≤n,k≤3⋅105).The second line contains a binary string of length nn, representing the initial state of each lamp (the lamp ii is off if si=0si=0, on if si=1si=1).The description of each one of the kk subsets follows, in the following format:The first line of the description contains a single integer cc (1≤c≤n1≤c≤n)  — the number of elements in the subset.The second line of the description contains cc distinct integers x1,…,xcx1,…,xc (1≤xi≤n1≤xi≤n)  — the elements of the subset.It is guaranteed that: The intersection of any three subsets is empty; It's possible to make all lamps be simultaneously on using some operations. OutputYou must output nn lines. The ii-th line should contain a single integer mimi  — the minimum number of operations required to make the lamps 11 to ii be simultaneously on.ExamplesInputCopy7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 OutputCopy1 2 3 3 3 3 3 InputCopy8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 OutputCopy1 1 1 1 1 1 4 4 InputCopy5 3 00011 3 1 2 3 1 4 3 3 4 5 OutputCopy1 1 1 1 1 InputCopy19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 OutputCopy0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 NoteIn the first example: For i=1i=1; we can just apply one operation on A1A1, the final states will be 10101101010110; For i=2i=2, we can apply operations on A1A1 and A3A3, the final states will be 11001101100110; For i≥3i≥3, we can apply operations on A1A1, A2A2 and A3A3, the final states will be 11111111111111. In the second example: For i≤6i≤6, we can just apply one operation on A2A2, the final states will be 1111110111111101; For i≥7i≥7, we can apply operations on A1,A3,A4,A6A1,A3,A4,A6, the final states will be 1111111111111111.
[ "dfs and similar", "dsu", "graphs" ]
/************************************* * author: marvinthang * * created: 10.01.2023 21:53:11 * *************************************/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define FULL(i) (MASK(i) - 1) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } // end of template const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0); const int dir[] = {1, 0, -1, 0, 1, 1, -1, -1, 1}; // {2, 1, -2, -1, -2, 1, 2, -1, 2}; void process(void) { int n, k; string s; cin >> n >> k >> s; vector <vector <int>> in_subset(n); REP(i, k) { int c; cin >> c; while (c--) { int x; cin >> x; in_subset[x - 1].push_back(i); } } vector <int> lab(2 * k, -1), cnt(2 * k); REP(i, k) cnt[i * 2 + 1] = 1; vector <bool> active(2 * k, true); function <int(int)> find = [&] (int u) { return lab[u] < 0 ? u : lab[u] = find(lab[u]); }; function <bool(int, int)> join = [&] (int u, int v) { if ((u = find(u)) == (v = find(v))) return false; if (lab[u] > lab[v]) swap(u, v); lab[u] += lab[v]; lab[v] = u; cnt[u] += cnt[v]; if (!active[v]) active[u] = false; return true; }; int cur = 0; auto get_min = [&] (int u) { int x = find(u * 2), y = find(u * 2 + 1); if (x == y) return 0; int mi = k; if (active[x] && mi > cnt[x]) mi = cnt[x]; if (active[y] && mi > cnt[y]) mi = cnt[y]; return mi; }; REP(i, n) { int c = s[i] - '0'; if (in_subset[i].size() == 1) { int u = in_subset[i][0]; int x = find(u * 2 + c); int y = find(u * 2 + !c); if (cnt[x] < cnt[y] && active[x]) cur += cnt[y] - cnt[x]; active[x] = false; } else if (in_subset[i].size() == 2) { int u = in_subset[i][0], v = in_subset[i][1]; if (!c) { if (find(u * 2) != find(v * 2 + 1)) { cur -= get_min(u) + get_min(v); join(u * 2, v * 2 + 1); join(u * 2 + 1, v * 2); cur += get_min(u); } } else { if (find(u * 2) != find(v * 2)) { cur -= get_min(u) + get_min(v); join(u * 2, v * 2); join(u * 2 + 1, v * 2 + 1); cur += get_min(u); } } } cout << cur << '\n'; } } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr); file("test"); process(); cerr << "Time elapsed: " << TIME << " s.\n"; return (0^0); }
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 distribution for the round.The round consists of nn problems, numbered from 11 to nn. The problems are ordered in increasing order of difficulty, no two problems have the same difficulty. A score distribution for the round can be denoted by an array a1,a2,…,ana1,a2,…,an, where aiai is the score of ii-th problem. Kuroni thinks that the score distribution should satisfy the following requirements: The score of each problem should be a positive integer not exceeding 109109. A harder problem should grant a strictly higher score than an easier problem. In other words, 1≤a1<a2<⋯<an≤1091≤a1<a2<⋯<an≤109. The balance of the score distribution, defined as the number of triples (i,j,k)(i,j,k) such that 1≤i<j<k≤n1≤i<j<k≤n and ai+aj=akai+aj=ak, should be exactly mm. Help the team find a score distribution that satisfies Kuroni's requirement. In case such a score distribution does not exist, output −1−1.InputThe first and single line contains two integers nn and mm (1≤n≤50001≤n≤5000, 0≤m≤1090≤m≤109) — the number of problems and the required balance.OutputIf there is no solution, print a single integer −1−1.Otherwise, print a line containing nn integers a1,a2,…,ana1,a2,…,an, representing a score distribution that satisfies all the requirements. If there are multiple answers, print any of them.ExamplesInputCopy5 3 OutputCopy4 5 9 13 18InputCopy8 0 OutputCopy10 11 12 13 14 15 16 17 InputCopy4 10 OutputCopy-1 NoteIn the first example; there are 33 triples (i,j,k)(i,j,k) that contribute to the balance of the score distribution. (1,2,3)(1,2,3) (1,3,4)(1,3,4) (2,4,5)(2,4,5)
[ "constructive algorithms", "greedy", "implementation", "math" ]
// LUOGU_RID: 98099727 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <random> #include <set> using namespace std; #define fi first #define sc second #define mkp make_pair #define pii pair<int,int> typedef long long ll; const int N=1e5+5,oo=1e8,mod=1e9+9; inline int read() { int x=0,flag=0;char ch=getchar(); while(ch<'0'||ch>'9') {flag|=(ch=='-');ch=getchar();} while('0'<=ch&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return flag?-x:x; } inline int mx(int x,int y) {return x>y?x:y;} inline int mn(int x,int y) {return x<y?x:y;} inline void swp(int &x,int &y) {x^=y^=x^=y;} inline int as(int x) {return x>0?x:-x;} inline int ok(int x) {return x>0?1:-1;} int n,m,a[N],t[N]; int main() { #ifndef ONLINE_JUDGE freopen("1.in","r",stdin); freopen("1.out","w",stdout); #endif n=read();m=read(); int lim=0; for(int i=1;i<=n;++i) lim+=(i-1)/2; if(lim<m) { printf("-1\n"); return 0; } int p=1; for(p=1;p<=n;++p) if(m-(p-1)/2>=0) { m-=(p-1)/2; a[p]=p; } else break; for(int i=1;i<p;++i) for(int j=i+1;j<p;++j) ++t[i+j]; for(int i=2*p;i>=1;--i) if(t[i]==m) { a[p]=i; break; } for(int i=p+1;i<=n;++i) a[i]=oo-(n-i)*10000; for(int i=1;i<=n;++i) printf("%d ",a[i]); return 0; } //yqmpo62327 //Ro5dPTkt
cpp
1286
E
E. Fedya the Potter Strikes Backtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFedya has a string SS, initially empty, and an array WW, also initially empty.There are nn queries to process, one at a time. Query ii consists of a lowercase English letter cici and a nonnegative integer wiwi. First, cici must be appended to SS, and wiwi must be appended to WW. The answer to the query is the sum of suspiciousnesses for all subsegments of WW [L, R][L, R], (1≤L≤R≤i)(1≤L≤R≤i).We define the suspiciousness of a subsegment as follows: if the substring of SS corresponding to this subsegment (that is, a string of consecutive characters from LL-th to RR-th, inclusive) matches the prefix of SS of the same length (that is, a substring corresponding to the subsegment [1, R−L+1][1, R−L+1]), then its suspiciousness is equal to the minimum in the array WW on the [L, R][L, R] subsegment. Otherwise, in case the substring does not match the corresponding prefix, the suspiciousness is 00.Help Fedya answer all the queries before the orderlies come for him!InputThe first line contains an integer nn (1≤n≤600000)(1≤n≤600000) — the number of queries.The ii-th of the following nn lines contains the query ii: a lowercase letter of the Latin alphabet cici and an integer wiwi (0≤wi≤230−1)(0≤wi≤230−1).All queries are given in an encrypted form. Let ansans be the answer to the previous query (for the first query we set this value equal to 00). Then, in order to get the real query, you need to do the following: perform a cyclic shift of cici in the alphabet forward by ansans, and set wiwi equal to wi⊕(ans & MASK)wi⊕(ans & MASK), where ⊕⊕ is the bitwise exclusive "or", && is the bitwise "and", and MASK=230−1MASK=230−1.OutputPrint nn lines, ii-th line should contain a single integer — the answer to the ii-th query.ExamplesInputCopy7 a 1 a 0 y 3 y 5 v 4 u 6 r 8 OutputCopy1 2 4 5 7 9 12 InputCopy4 a 2 y 2 z 0 y 2 OutputCopy2 2 2 2 InputCopy5 a 7 u 5 t 3 s 10 s 11 OutputCopy7 9 11 12 13 NoteFor convenience; we will call "suspicious" those subsegments for which the corresponding lines are prefixes of SS, that is, those whose suspiciousness may not be zero.As a result of decryption in the first example, after all requests, the string SS is equal to "abacaba", and all wi=1wi=1, that is, the suspiciousness of all suspicious sub-segments is simply equal to 11. Let's see how the answer is obtained after each request:1. SS = "a", the array WW has a single subsegment — [1, 1][1, 1], and the corresponding substring is "a", that is, the entire string SS, thus it is a prefix of SS, and the suspiciousness of the subsegment is 11.2. SS = "ab", suspicious subsegments: [1, 1][1, 1] and [1, 2][1, 2], total 22.3. SS = "aba", suspicious subsegments: [1, 1][1, 1], [1, 2][1, 2], [1, 3][1, 3] and [3, 3][3, 3], total 44.4. SS = "abac", suspicious subsegments: [1, 1][1, 1], [1, 2][1, 2], [1, 3][1, 3], [1, 4][1, 4] and [3, 3][3, 3], total 55.5. SS = "abaca", suspicious subsegments: [1, 1][1, 1], [1, 2][1, 2], [1, 3][1, 3], [1, 4][1, 4] , [1, 5][1, 5], [3, 3][3, 3] and [5, 5][5, 5], total 77.6. SS = "abacab", suspicious subsegments: [1, 1][1, 1], [1, 2][1, 2], [1, 3][1, 3], [1, 4][1, 4] , [1, 5][1, 5], [1, 6][1, 6], [3, 3][3, 3], [5, 5][5, 5] and [5, 6][5, 6], total 99.7. SS = "abacaba", suspicious subsegments: [1, 1][1, 1], [1, 2][1, 2], [1, 3][1, 3], [1, 4][1, 4] , [1, 5][1, 5], [1, 6][1, 6], [1, 7][1, 7], [3, 3][3, 3], [5, 5][5, 5], [5, 6][5, 6], [5, 7][5, 7] and [7, 7][7, 7], total 1212.In the second example, after all requests SS = "aaba", W=[2,0,2,0]W=[2,0,2,0].1. SS = "a", suspicious subsegments: [1, 1][1, 1] (suspiciousness 22), totaling 22.2. SS = "aa", suspicious subsegments: [1, 1][1, 1] (22), [1, 2][1, 2] (00), [2, 2][2, 2] ( 00), totaling 22.3. SS = "aab", suspicious subsegments: [1, 1][1, 1] (22), [1, 2][1, 2] (00), [1, 3][1, 3] ( 00), [2, 2][2, 2] (00), totaling 22.4. SS = "aaba", suspicious subsegments: [1, 1][1, 1] (22), [1, 2][1, 2] (00), [1, 3][1, 3] ( 00), [1, 4][1, 4] (00), [2, 2][2, 2] (00), [4, 4][4, 4] (00), totaling 22.In the third example, from the condition after all requests SS = "abcde", W=[7,2,10,1,7]W=[7,2,10,1,7].1. SS = "a", suspicious subsegments: [1, 1][1, 1] (77), totaling 77.2. SS = "ab", suspicious subsegments: [1, 1][1, 1] (77), [1, 2][1, 2] (22), totaling 99.3. SS = "abc", suspicious subsegments: [1, 1][1, 1] (77), [1, 2][1, 2] (22), [1, 3][1, 3] ( 22), totaling 1111.4. SS = "abcd", suspicious subsegments: [1, 1][1, 1] (77), [1, 2][1, 2] (22), [1, 3][1, 3] ( 22), [1, 4][1, 4] (11), totaling 1212.5. SS = "abcde", suspicious subsegments: [1, 1][1, 1] (77), [1, 2][1, 2] (22), [1, 3][1, 3] ( 22), [1, 4][1, 4] (11), [1, 5][1, 5] (11), totaling 1313.
[ "data structures", "strings" ]
// LUOGU_RID: 102578485 #include<bits/stdc++.h> using namespace std; #define ll long long #define lll __int128 #define inf 1e18 #define N 1000010 ll n; ll ji[N]; ll nxt[N],fl[N],nn[N]; const ll mask=(1ll<<30)-1; lll ans=0; struct node{ ll val[N<<2]; inline void pushup(ll x){ val[x]=min(val[x<<1],val[x<<1|1]); return ; } inline void update(ll o,ll l,ll r,ll x,ll y){ if(l==r){ val[o]=y;return ; }ll mid=(l+r)>>1; if(mid>=x)update(o<<1,l,mid,x,y); else update(o<<1|1,mid+1,r,x,y); pushup(o);return ; } inline ll ask(ll o,ll l,ll r,ll x,ll y){ if(x<=l&&r<=y)return val[o]; ll mid=(l+r)>>1,an=inf; if(mid>=x)an=min(an,ask(o<<1,l,mid,x,y)); if(mid<y)an=min(an,ask(o<<1|1,mid+1,r,x,y)); return an; } }g; struct T{ ll fi; mutable ll se; bool operator <(const T y)const{ if(y.fi==fi)return se<y.se; return fi<y.fi; } }; set< T >s; inline void del(ll x){ auto o=s.lower_bound((T){x,0}); (*o).se--;ans-=x; return ; } inline void push(ll x){ auto o=s.lower_bound((T){x,0}); if(o!=s.end()&&(*o).fi==x)(*o).se++; else{ s.insert((T){x,1}); } ans+=x; return ; } inline void qm(ll x){ auto o=s.lower_bound((T){x+1,0}),p=o; ll cnt=0; while(o!=s.end()){ ans-=(*o).fi*(*o).se;cnt+=(*o).se; o++; }s.erase(p,s.end());ans+=cnt*x; p=s.lower_bound((T){x,0}); if(p!=s.end()&&(*p).fi==x){ (*p).se+=cnt; }else{ s.insert((T){x,cnt}); } return ; } ll u[31],cnn=0; inline void out(lll x){ if(x==0){ putchar('0');putchar(10); return ; }cnn=0;while(x){ u[++cnn]=x%10;x/=10; }for(int i=cnn;i>=1;i--)putchar(u[i]+'0'); putchar(10); return ; } int main() { // freopen("test1.in","r",stdin); //freopen(".in","r",stdin); //freopen("test1.out","w",stdout); ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n;char x;ll y;lll las=0; memset(g.val,0x3f,sizeof(g.val)); for(int i=1;i<=n;i++){ cin>>x>>y;ll o=x-'a'; o=(o+las)%26;y=y^(las&mask); g.update(1,1,n,i,y);ji[i]=o; if(i>1){ nn[i-1]=o; if(nn[nxt[i-1]]!=o)fl[i-1]=nxt[i-1]; else fl[i-1]=fl[nxt[i-1]]; ll nw=i-1; while(nw){ if(nn[nw]!=ji[i]){ del(g.ask(1,1,n,i-nw,i-1)); nw=nxt[nw]; }else nw=fl[nw]; } } if(ji[1]==o){ push(y); }qm(y); out(las=las+ans); ll p=nxt[i-1]; while(p>0&&ji[p+1]!=ji[i])p=nxt[p]; if(i!=1&&ji[p+1]==ji[i])nxt[i]=p+1; } return 0; }
cpp
1294
D
D. MEX maximizingtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array [0,0,1,0,2][0,0,1,0,2] MEX equals to 33 because numbers 0,10,1 and 22 are presented in the array and 33 is the minimum non-negative integer not presented in the array; for the array [1,2,3,4][1,2,3,4] MEX equals to 00 because 00 is the minimum non-negative integer not presented in the array; for the array [0,1,4,3][0,1,4,3] MEX equals to 22 because 22 is the minimum non-negative integer not presented in the array. You are given an empty array a=[]a=[] (in other words, a zero-length array). You are also given a positive integer xx.You are also given qq queries. The jj-th query consists of one integer yjyj and means that you have to append one element yjyj to the array. The array length increases by 11 after a query.In one move, you can choose any index ii and set ai:=ai+xai:=ai+x or ai:=ai−xai:=ai−x (i.e. increase or decrease any element of the array by xx). The only restriction is that aiai cannot become negative. Since initially the array is empty, you can perform moves only after the first query.You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).You have to find the answer after each of qq queries (i.e. the jj-th answer corresponds to the array of length jj).Operations are discarded before each query. I.e. the array aa after the jj-th query equals to [y1,y2,…,yj][y1,y2,…,yj].InputThe first line of the input contains two integers q,xq,x (1≤q,x≤4⋅1051≤q,x≤4⋅105) — the number of queries and the value of xx.The next qq lines describe queries. The jj-th query consists of one integer yjyj (0≤yj≤1090≤yj≤109) and means that you have to append one element yjyj to the array.OutputPrint the answer to the initial problem after each query — for the query jj print the maximum value of MEX after first jj queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.ExamplesInputCopy7 3 0 1 2 2 0 0 10 OutputCopy1 2 3 3 4 4 7 InputCopy4 3 1 2 1 2 OutputCopy0 0 0 0 NoteIn the first example: After the first query; the array is a=[0]a=[0]: you don't need to perform any operations, maximum possible MEX is 11. After the second query, the array is a=[0,1]a=[0,1]: you don't need to perform any operations, maximum possible MEX is 22. After the third query, the array is a=[0,1,2]a=[0,1,2]: you don't need to perform any operations, maximum possible MEX is 33. After the fourth query, the array is a=[0,1,2,2]a=[0,1,2,2]: you don't need to perform any operations, maximum possible MEX is 33 (you can't make it greater with operations). After the fifth query, the array is a=[0,1,2,2,0]a=[0,1,2,2,0]: you can perform a[4]:=a[4]+3=3a[4]:=a[4]+3=3. The array changes to be a=[0,1,2,2,3]a=[0,1,2,2,3]. Now MEX is maximum possible and equals to 44. After the sixth query, the array is a=[0,1,2,2,0,0]a=[0,1,2,2,0,0]: you can perform a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3. The array changes to be a=[0,1,2,2,3,0]a=[0,1,2,2,3,0]. Now MEX is maximum possible and equals to 44. After the seventh query, the array is a=[0,1,2,2,0,0,10]a=[0,1,2,2,0,0,10]. You can perform the following operations: a[3]:=a[3]+3=2+3=5a[3]:=a[3]+3=2+3=5, a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3, a[5]:=a[5]+3=0+3=3a[5]:=a[5]+3=0+3=3, a[5]:=a[5]+3=3+3=6a[5]:=a[5]+3=3+3=6, a[6]:=a[6]−3=10−3=7a[6]:=a[6]−3=10−3=7, a[6]:=a[6]−3=7−3=4a[6]:=a[6]−3=7−3=4. The resulting array will be a=[0,1,2,5,3,6,4]a=[0,1,2,5,3,6,4]. Now MEX is maximum possible and equals to 77.
[ "data structures", "greedy", "implementation", "math" ]
#include<bits/stdc++.h> using namespace std; //#define int long long typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define pb emplace_back #define mp make_pair #define f first #define endl '\n' #define s second #define all(c) (c).begin(), (c).end() #define MOD 1000000007 long long mod(long long x) { return ((x%MOD+MOD)%MOD); } long long add(long long a,long long b) { return mod(mod(a)+mod(b)); } long long mult(long long a,long long b) { return mod(mod(a)*mod(b)); } //RADHE RADHE //Toh chaliye shuru karte hai bina kis bkc ke// int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int q,x; cin>>q>>x; int mex=0; multiset<int>ms; while(q--) { int t; cin>>t; ms.insert(t%x); bool f=1; while(f) { int need=mex%x; auto it=ms.find(need); if(it!=ms.end()) { mex++; ms.erase(it); } else{ f=0; } } cout<<mex<<endl; } return 0; //////////////////////////////////////HaStA La ViStA RoWdY's///////////////////// }
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 string. We denote the substring of string ss starting from the ll-th character and ending with the rr-th character as s[l…r]s[l…r]. The characters of each string are numbered from 11.We can perform several operations on the strings we consider. Each operation is to choose a substring of our string and replace it with another string. There are two possible types of operations: replace 011 with 110, or replace 110 with 011. For example, if we apply exactly one operation to the string 110011110, it can be transformed into 011011110, 110110110, or 110011011.Binary string aa is considered reachable from binary string bb if there exists a sequence s1s1, s2s2, ..., sksk such that s1=as1=a, sk=bsk=b, and for every i∈[1,k−1]i∈[1,k−1], sisi can be transformed into si+1si+1 using exactly one operation. Note that kk can be equal to 11, i. e., every string is reachable from itself.You are given a string tt and qq queries to it. Each query consists of three integers l1l1, l2l2 and lenlen. To answer each query, you have to determine whether t[l1…l1+len−1]t[l1…l1+len−1] is reachable from t[l2…l2+len−1]t[l2…l2+len−1].InputThe first line contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of string tt.The second line contains one string tt (|t|=n|t|=n). Each character of tt is either 0 or 1.The third line contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries.Then qq lines follow, each line represents a query. The ii-th line contains three integers l1l1, l2l2 and lenlen (1≤l1,l2≤|t|1≤l1,l2≤|t|, 1≤len≤|t|−max(l1,l2)+11≤len≤|t|−max(l1,l2)+1) for the ii-th query.OutputFor each query, print either YES if t[l1…l1+len−1]t[l1…l1+len−1] is reachable from t[l2…l2+len−1]t[l2…l2+len−1], or NO otherwise. You may print each letter in any register.ExampleInputCopy5 11011 3 1 3 3 1 4 2 1 2 3 OutputCopyYes Yes No
[ "data structures", "hashing", "strings" ]
#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 reduction // Reference: https://en.wikipedia.org/wiki/Barrett_reduction // NOTE: reconsider after Ice Lake struct barrett { unsigned int _m; unsigned long long im; // @param m `1 <= m < 2^31` barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {} // @return m unsigned int umod() const { return _m; } // @param a `0 <= a < m` // @param b `0 <= b < m` // @return `a * b % m` unsigned int mul(unsigned int a, unsigned int b) const { // [1] m = 1 // a = b = im = 0, so okay // [2] m >= 2 // im = ceil(2^64 / m) // -> im * m = 2^64 + r (0 <= r < m) // let z = a*b = c*m + d (0 <= c, d < m) // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2 // ((ab * im) >> 64) == c or c + 1 unsigned long long z = a; z *= b; #ifdef _MSC_VER unsigned long long x; _umul128(z, im, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(z)*im) >> 64); #endif unsigned int v = (unsigned int)(z - x * _m); if (_m <= v) v += _m; return v; } }; // @param n `0 <= n` // @param m `1 <= m` // @return `(x ** n) % m` constexpr long long pow_mod_constexpr(long long x, long long n, int m) { if (m == 1) return 0; unsigned int _m = (unsigned int)(m); unsigned long long r = 1; unsigned long long y = safe_mod(x, m); while (n) { if (n & 1) r = (r * y) % _m; y = (y * y) % _m; n >>= 1; } return r; } // Reference: // M. Forisek and J. Jancina, // Fast Primality Testing for Integers That Fit into a Machine Word // @param n `0 <= n` constexpr bool is_prime_constexpr(int n) { if (n <= 1) return false; if (n == 2 || n == 7 || n == 61) return true; if (n % 2 == 0) return false; long long d = n - 1; while (d % 2 == 0) d /= 2; constexpr long long bases[3] = {2, 7, 61}; for (long long a : bases) { long long t = d; long long y = pow_mod_constexpr(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = y * y % n; t <<= 1; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } template <int n> constexpr bool is_prime = is_prime_constexpr(n); // @param b `1 <= b` // @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) { a = safe_mod(a, b); if (a == 0) return {b, 0}; // Contracts: // [1] s - m0 * a = 0 (mod b) // [2] t - m1 * a = 0 (mod b) // [3] s * |m1| + t * |m0| <= b long long s = b, t = a; long long m0 = 0, m1 = 1; while (t) { long long u = s / t; s -= t * u; m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b // [3]: // (s - t * u) * |m1| + t * |m0 - m1 * u| // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u) // = s * |m1| + t * |m0| <= b auto tmp = s; s = t; t = tmp; tmp = m0; m0 = m1; m1 = tmp; } // by [3]: |m0| <= b/g // by g != b: |m0| < b/g if (m0 < 0) m0 += b / s; return {s, m0}; } // Compile time primitive root // @param m must be prime // @return primitive root (and minimum in now) constexpr int primitive_root_constexpr(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; int x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (int i = 3; (long long)(i)*i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) { x /= i; } } } if (x > 1) { divs[cnt++] = x; } for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } template <int m> constexpr int primitive_root = primitive_root_constexpr(m); } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder #include <cassert> #include <numeric> #include <type_traits> #ifdef _MSC_VER #include <intrin.h> #endif namespace atcoder { namespace internal { struct modint_base {}; struct static_modint_base : modint_base {}; template <class T> using is_modint = std::is_base_of<modint_base, T>; template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>; } // namespace internal template <int m, std::enable_if_t<(1 <= m)>* = nullptr> struct static_modint : internal::static_modint_base { using mint = static_modint; public: static constexpr int mod() { return m; } static mint raw(int v) { mint x; x._v = v; return x; } static_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> static_modint(T v) { long long x = (long long)(v % (long long)(umod())); if (x < 0) x += umod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> static_modint(T v) { _v = (unsigned int)(v % umod()); } static_modint(bool v) { _v = ((unsigned int)(v) % umod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v -= rhs._v; if (_v >= umod()) _v += umod(); return *this; } mint& operator*=(const mint& rhs) { unsigned long long z = _v; z *= rhs._v; _v = z % m; return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { if (prime) { assert(_v); return pow(umod() - 2); } else { auto eg = internal::inv_gcd(_v, m); assert(eg.first == 1); return eg.second; } } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static constexpr unsigned int umod() { return m; } static constexpr bool prime = internal::is_prime<m>; }; template <int id> struct dynamic_modint : internal::modint_base { using mint = dynamic_modint; public: static int mod() { return (int)(bt.umod()); } static void set_mod(int m) { assert(1 <= m); bt = internal::barrett(m); } static mint raw(int v) { mint x; x._v = v; return x; } dynamic_modint() : _v(0) {} template <class T, internal::is_signed_int_t<T>* = nullptr> dynamic_modint(T v) { long long x = (long long)(v % (long long)(mod())); if (x < 0) x += mod(); _v = (unsigned int)(x); } template <class T, internal::is_unsigned_int_t<T>* = nullptr> dynamic_modint(T v) { _v = (unsigned int)(v % mod()); } dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); } unsigned int val() const { return _v; } mint& operator++() { _v++; if (_v == umod()) _v = 0; return *this; } mint& operator--() { if (_v == 0) _v = umod(); _v--; return *this; } mint operator++(int) { mint result = *this; ++*this; return result; } mint operator--(int) { mint result = *this; --*this; return result; } mint& operator+=(const mint& rhs) { _v += rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator-=(const mint& rhs) { _v += mod() - rhs._v; if (_v >= umod()) _v -= umod(); return *this; } mint& operator*=(const mint& rhs) { _v = bt.mul(_v, rhs._v); return *this; } mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); } mint operator+() const { return *this; } mint operator-() const { return mint() - *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { auto eg = internal::inv_gcd(_v, mod()); assert(eg.first == 1); return eg.second; } friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; } friend bool operator==(const mint& lhs, const mint& rhs) { return lhs._v == rhs._v; } friend bool operator!=(const mint& lhs, const mint& rhs) { return lhs._v != rhs._v; } private: unsigned int _v; static internal::barrett bt; static unsigned int umod() { return bt.umod(); } }; template <int id> internal::barrett dynamic_modint<id>::bt = 998244353; using modint998244353 = static_modint<998244353>; using modint1000000007 = static_modint<1000000007>; using modint = dynamic_modint<-1>; namespace internal { template <class T> using is_static_modint = std::is_base_of<internal::static_modint_base, T>; template <class T> using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>; template <class> struct is_dynamic_modint : public std::false_type {}; template <int id> struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {}; template <class T> using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>; } // namespace internal } // namespace atcoder using namespace std; typedef atcoder::static_modint<754974721> mint; constexpr int N = 2e5 + 9, p = 469762049; string s; int n, m, cnt[N]; mint pw[N], hsh[2][N]; mint ghsh(int l, int len) { return hsh[~l & 1][l + len] - hsh[~l & 1][l] * pw[cnt[l + len] - cnt[l]]; } signed main() { cin.tie(nullptr)->sync_with_stdio(false); cin >> n >> s, pw[0] = 1; for (int i = 1; i <= n; ++i) { cnt[i] = cnt[i - 1], pw[i] = pw[i - 1] * p; hsh[0][i] = hsh[0][i - 1], hsh[1][i] = hsh[1][i - 1]; if (~s[i - 1] & 1) { ++cnt[i]; (hsh[0][i] *= p) += (i & 1) + 1; (hsh[1][i] *= p) += (~i & 1) + 1; } } cin >> m; for (int l1, l2, len; m; --m) { cin >> l1 >> l2 >> len, --l1, --l2; cout << (ghsh(l1, len) == ghsh(l2, len) ? "Yes" : "No") << '\n'; } return cout << flush, 0; }
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 person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.You are standing in the mm-th position in the line. Before the process starts, you may choose up to kk different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.Suppose that you're doing your choices optimally. What is the greatest integer xx such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to xx?Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.InputThe input consists of multiple test cases. The first line contains a single integer tt (1≤t≤10001≤t≤1000)  — the number of test cases. The description of the test cases follows.The first line of each test case contains three space-separated integers nn, mm and kk (1≤m≤n≤35001≤m≤n≤3500, 0≤k≤n−10≤k≤n−1)  — the number of elements in the array, your position in line and the number of people whose choices you can fix.The second line of each test case contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109)  — elements of the array.It is guaranteed that the sum of nn over all test cases does not exceed 35003500.OutputFor each test case, print the largest integer xx such that you can guarantee to obtain at least xx.ExampleInputCopy4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 OutputCopy8 4 1 1 NoteIn the first test case; an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element (55) because he or she was forced by you to take the last element. After this turn the remaining array will be [2,9,2,3,8][2,9,2,3,8]; the second person will take the first element (22) because he or she was forced by you to take the first element. After this turn the remaining array will be [9,2,3,8][9,2,3,8]; if the third person will choose to take the first element (99), at your turn the remaining array will be [2,3,8][2,3,8] and you will take 88 (the last element); if the third person will choose to take the last element (88), at your turn the remaining array will be [9,2,3][9,2,3] and you will take 99 (the first element). Thus, this strategy guarantees to end up with at least 88. We can prove that there is no strategy that guarantees to end up with at least 99. Hence, the answer is 88.In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 44.
[ "brute force", "data structures", "implementation" ]
/* ....###....########..##....##....###....##....##............##....#######. ...##.##...##.....##..##..##....##.##...###...##..........####...##.....## ..##...##..##.....##...####....##...##..####..##............##...##.....## .##.....##.########.....##....##.....##.##.##.##............##....######## .#########.##...##......##....#########.##..####............##..........## .##.....##.##....##.....##....##.....##.##...###............##...##.....## .##.....##.##.....##....##....##.....##.##....##.#######..######..#######. */ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define FIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define endl "\n" #define vll vector <ll> #define pb push_back #define pf push_front #define mp make_pair #define f(i,j,k) for(ll i=j;i<k;i++) #define fr(i,j,k) for(ll i=j;i>=k;i--) #define MOD 1000000007 #define MOD1 998244353 #define pi 3.141592653589 #define eq enqueue #define dq dequeue #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) #define max4(a,b,c,d) max(a,max3(b,c,d)) #define min4(a,b,c,d) min(a,min3(b,c,d)) #define lcm(a,b) (a*(b/gcd(a,b))) #define INF 2e18 #define ff first #define ss second #ifndef ONLINE_JUDGE #define debug(val) cerr << (#val) << ": " << val << endl #else #define debug(val) #endif /////////////////////////////////////////////////////////////////////////////////// //power long long power(long long x, unsigned long long y) { long long temp; if( y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } // modular exponentiation-iterative approach. long long modpow(long long x, unsigned int y, long long p) { ll res = 1; x = x % p; if(y==0) return 1; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } // modular exponentiation-recursive approach. long long exponentMod(long long A, long long B, long long C) { if (B == 0) return 1; if (A == 0) return 0; long long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (long long)((y + C) % C); } // GCD of two numbers (Time Complexity: O(Log min(a, b)) ) long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } // Function for extended Euclidean Algorithm int gcdExtended(int a, int b, int *x, int *y) { if (a == 0) { *x = 0; *y = 1; return b; } int x1, y1; int gcd = gcdExtended(b%a, a, &x1, &y1); *x = y1 - (b/a) * x1; *y = x1; return gcd; } // Function to find modulo inverse of a void modInverse(int a, int m) { int x, y; int g = gcdExtended(a, m, &x, &y); if (g != 1) cout << "Inverse doesn't exist"; else { int res = (x % m + m) % m; cout << "Modular multiplicative inverse is " << res; } } // sieve of eratosthenes void SieveOfEratosthenes(int n) { bool sieve[n + 1];ll cnt=0; memset(sieve, 0, sizeof(sieve)); for (int p = 2; p * p <= n; p++) { if (!sieve[p]) { for (int i = 2 * p; i <= n; i += p) sieve[i] = p; } } for (int p = 2; p <= n; p++) { if (sieve[p]) cnt++;} cout<<cnt; } //euler's totient-number of coprime numbers to n between 1 and n.(excluding n) int phi(unsigned int n) { float result = n; for(int p = 2; p * p <= n; ++p) { if (n % p == 0) { while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (float)p)); } } if (n > 1) result *= (1.0 - (1.0 / (float)n)); return (int)result; } // //finding square root in O(logN). // ll floorSqrt(ll x){ // if (x == 0 || x == 1) // return x; // unsigned long long start = 1, end = x, ans; // while (start <= end) // { // unsigned long long mid = start+(end-start)/2; // if (mid*mid == x) // return mid; // if (mid*mid < x) // { // start = mid + 1; // ans = mid; // } // else end = mid-1; // } // return ans; // } /******************************************START OF PROGRAM LOGIC***************************************************/ int32_t main() { FIO; ll t; cin>>t; while(t--) { ll n,m,k; cin>>n>>m>>k; ll a[n+1]; f(i,0,n) { cin>>a[i]; } // vector<vector<vector<ll>>>dp(n+1,vector<vector<ll>>(m+1, vector<ll>(k+1,-1))); // cout<<solve() k=min(m-1,k); ll ans=0; f(i,0,k+1) { ll mini=LLONG_MAX; f(j,0,(m-k)) { mini=min(mini, max(a[i+j], a[i+j+(n-m)])); } ans=max(ans,mini); } cout<<ans<<endl; } }
cpp
1312
G
G. Autocompletiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a set of strings SS. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions: if the current string is tt, choose some lowercase Latin letter cc and append it to the back of tt, so the current string becomes t+ct+c. This action takes 11 second; use autocompletion. When you try to autocomplete the current string tt, a list of all strings s∈Ss∈S such that tt is a prefix of ss is shown to you. This list includes tt itself, if tt is a string from SS, and the strings are ordered lexicographically. You can transform tt into the ii-th string from this list in ii seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type. What is the minimum number of seconds that you have to spend to type each string from SS?Note that the strings from SS are given in an unusual way.InputThe first line contains one integer nn (1≤n≤1061≤n≤106).Then nn lines follow, the ii-th line contains one integer pipi (0≤pi<i0≤pi<i) and one lowercase Latin character cici. These lines form some set of strings such that SS is its subset as follows: there are n+1n+1 strings, numbered from 00 to nn; the 00-th string is an empty string, and the ii-th string (i≥1i≥1) is the result of appending the character cici to the string pipi. It is guaranteed that all these strings are distinct.The next line contains one integer kk (1≤k≤n1≤k≤n) — the number of strings in SS.The last line contains kk integers a1a1, a2a2, ..., akak (1≤ai≤n1≤ai≤n, all aiai are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set SS — formally, if we denote the ii-th generated string as sisi, then S=sa1,sa2,…,sakS=sa1,sa2,…,sak.OutputPrint kk integers, the ii-th of them should be equal to the minimum number of seconds required to type the string saisai.ExamplesInputCopy10 0 i 1 q 2 g 0 k 1 e 5 r 4 m 5 h 3 p 3 e 5 8 9 1 10 6 OutputCopy2 4 1 3 3 InputCopy8 0 a 1 b 2 a 2 b 4 a 4 b 5 c 6 d 5 2 3 4 7 8 OutputCopy1 2 2 4 4 NoteIn the first example; SS consists of the following strings: ieh, iqgp, i, iqge, ier.
[ "data structures", "dfs and similar", "dp" ]
#include <bits/stdc++.h> using namespace std; const int N=1e6+10; int st[N]; int rs[N],dp[N]; int e[N][27]; vector<pair<int,int>> stk; int Rank; void dfs(int u) { if(stk.size()&&st[u]) dp[u]=min(dp[u],stk.back().second+Rank+1); if(stk.empty()||stk.back().second>dp[u]-Rank) stk.push_back({u,dp[u]-Rank}); Rank+=st[u]; //cout<<stk.size()<<endl; for(int i=0;i<26;++i) { if(!e[u][i])continue; int v=e[u][i]; dp[v]=dp[u]+1; dfs(v); } if(stk.size()&&stk.back().first==u)stk.pop_back(); } int main() { int n,m;scanf("%d",&n); char op[3]; for(int i=1,fa;i<=n;++i) { scanf("%d%s",&fa,op);e[fa][op[0]-'a']=i; } scanf("%d",&m); for(int i=1;i<=m;++i)scanf("%d",&rs[i]),st[rs[i]]=1; dfs(0); for(int i=1;i<=m;++i)printf("%d%c",dp[rs[i]]," \n"[i==m]); // cout<<stk.size(); }
cpp
1312
G
G. Autocompletiontime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a set of strings SS. Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions: if the current string is tt, choose some lowercase Latin letter cc and append it to the back of tt, so the current string becomes t+ct+c. This action takes 11 second; use autocompletion. When you try to autocomplete the current string tt, a list of all strings s∈Ss∈S such that tt is a prefix of ss is shown to you. This list includes tt itself, if tt is a string from SS, and the strings are ordered lexicographically. You can transform tt into the ii-th string from this list in ii seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type. What is the minimum number of seconds that you have to spend to type each string from SS?Note that the strings from SS are given in an unusual way.InputThe first line contains one integer nn (1≤n≤1061≤n≤106).Then nn lines follow, the ii-th line contains one integer pipi (0≤pi<i0≤pi<i) and one lowercase Latin character cici. These lines form some set of strings such that SS is its subset as follows: there are n+1n+1 strings, numbered from 00 to nn; the 00-th string is an empty string, and the ii-th string (i≥1i≥1) is the result of appending the character cici to the string pipi. It is guaranteed that all these strings are distinct.The next line contains one integer kk (1≤k≤n1≤k≤n) — the number of strings in SS.The last line contains kk integers a1a1, a2a2, ..., akak (1≤ai≤n1≤ai≤n, all aiai are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set SS — formally, if we denote the ii-th generated string as sisi, then S=sa1,sa2,…,sakS=sa1,sa2,…,sak.OutputPrint kk integers, the ii-th of them should be equal to the minimum number of seconds required to type the string saisai.ExamplesInputCopy10 0 i 1 q 2 g 0 k 1 e 5 r 4 m 5 h 3 p 3 e 5 8 9 1 10 6 OutputCopy2 4 1 3 3 InputCopy8 0 a 1 b 2 a 2 b 4 a 4 b 5 c 6 d 5 2 3 4 7 8 OutputCopy1 2 2 4 4 NoteIn the first example; SS consists of the following strings: ieh, iqgp, i, iqge, ier.
[ "data structures", "dfs and similar", "dp" ]
#include<bits/stdc++.h> using namespace std; int const M=1000100;char ch; int i,n,k,x,a[M],f[M],g[M],s[M]; vector<pair<char,int> >G[M]; void dfs(int x,int p){ f[x]=f[p]+1;g[x]=min(f[x],g[p]+s[p]); if (s[x]) f[x]=min(f[x],g[x]+1); sort(G[x].begin(),G[x].end()); for (auto v:G[x]) dfs(v.second,x); s[p]+=s[x]; } int main(){ scanf("%d",&n); for (i=1;i<=n;i++){ scanf("%d %c",&x,&ch); G[x].push_back(make_pair(ch,i)); } scanf("%d",&k); for (i=1;i<=k;i++) scanf("%d",&a[i]),s[a[i]]=1; f[0]=-1;dfs(0,0); for (i=1;i<=k;i++) printf("%d ",f[a[i]]); return 0; }
cpp
1313
D
D. Happy New Yeartime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBeing Santa Claus is very difficult. Sometimes you have to deal with difficult situations.Today Santa Claus came to the holiday and there were mm children lined up in front of him. Let's number them from 11 to mm. Grandfather Frost knows nn spells. The ii-th spell gives a candy to every child whose place is in the [Li,Ri][Li,Ri] range. Each spell can be used at most once. It is also known that if all spells are used, each child will receive at most kk candies.It is not good for children to eat a lot of sweets, so each child can eat no more than one candy, while the remaining candies will be equally divided between his (or her) Mom and Dad. So it turns out that if a child would be given an even amount of candies (possibly zero), then he (or she) will be unable to eat any candies and will go sad. However, the rest of the children (who received an odd number of candies) will be happy.Help Santa Claus to know the maximum number of children he can make happy by casting some of his spells.InputThe first line contains three integers of nn, mm, and kk (1≤n≤100000,1≤m≤109,1≤k≤81≤n≤100000,1≤m≤109,1≤k≤8) — the number of spells, the number of children and the upper limit on the number of candy a child can get if all spells are used, respectively.This is followed by nn lines, each containing integers LiLi and RiRi (1≤Li≤Ri≤m1≤Li≤Ri≤m) — the parameters of the ii spell.OutputPrint a single integer — the maximum number of children that Santa can make happy.ExampleInputCopy3 5 31 32 43 5OutputCopy4NoteIn the first example, Santa should apply the first and third spell. In this case all children will be happy except the third.
[ "bitmasks", "dp", "implementation" ]
#include<iostream> #include<cstring> #include<algorithm> using namespace std; using LL = long long; const int maxn = 2e5 + 5; pair<int, int> op[maxn]; int f[maxn][1 << 8]; int a[8]; int main(){ #ifdef LOCAL freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; int cnt = 0; for(int i = 1; i <= n; i++){ int l, r; cin >> l >> r; op[++cnt] = {l, i}; op[++cnt] = {r + 1, -i}; } sort(op + 1, op + cnt + 1); memset(f, -0x3f, sizeof f); f[0][0] = 0; for(int i = 1; i <= cnt; i++){ auto [x, id] = op[i]; int len = (i + 1 <= cnt ? op[i + 1].first - op[i].first : 0); int t; if (id > 0){ for(int j = 0; j < k; j++) if (!a[j]){ a[j] = id; t = j; break; } for(int j = 0; j < 1 << k; j++){ int par = __builtin_parity(j); if (j >> t & 1) f[i][j] = f[i - 1][j ^ (1 << t)] + par * len; else f[i][j] = f[i - 1][j] + par * len; } } else{ for(int j = 0; j < k; j++) if (a[j] == -id){ t = j; a[j] = 0; break; } for(int j = 0; j < 1 << k; j++){ if (j >> t & 1) continue; int par = __builtin_parity(j); f[i][j] = max(f[i - 1][j], f[i - 1][j | (1 << t)]) + par * len; } } } cout << f[cnt][0] << '\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)LCM(a,b) is the smallest positive integer that is divisible by both aa and bb. For example, LCM(6,8)=24LCM(6,8)=24, LCM(4,12)=12LCM(4,12)=12, LCM(2,3)=6LCM(2,3)=6.Of course, Fadi immediately knew the answer. Can you be just like Fadi and find any such pair?InputThe first and only line contains an integer XX (1≤X≤10121≤X≤1012).OutputPrint two positive integers, aa and bb, such that the value of max(a,b)max(a,b) is minimum possible and LCM(a,b)LCM(a,b) equals XX. If there are several possible such pairs, you can print any.ExamplesInputCopy2 OutputCopy1 2 InputCopy6 OutputCopy2 3 InputCopy4 OutputCopy1 4 InputCopy1 OutputCopy1 1
[ "brute force", "math", "number theory" ]
#include<bits/stdc++.h> #define ll long long #define el '\n' using namespace std; void solve(){ ll x; cin >> x; vector<ll>a; int cnt = 0; ll mn = 1000000000000; bool ok = false; for (ll i = 2; i * i <= x; i++){ if (x % i == 0){ ll y = x / i; if (__gcd(i, y) == 1){ ok = true; mn = min(y, mn); } } } if (ok)cout << mn << ' ' << x / mn; else cout << x << ' ' << x; } int main () { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; //cin >> t; while(t--){ solve(); } }
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 board. Rows and columns of this board are numbered from 11 to nn. The cell on the intersection of the rr-th row and cc-th column is denoted by (r,c)(r,c).Some cells on the board are called blocked zones. On each cell of the board, there is written one of the following 55 characters  — UU, DD, LL, RR or XX  — instructions for the player. Suppose that the current cell is (r,c)(r,c). If the character is RR, the player should move to the right cell (r,c+1)(r,c+1), for LL the player should move to the left cell (r,c−1)(r,c−1), for UU the player should move to the top cell (r−1,c)(r−1,c), for DD the player should move to the bottom cell (r+1,c)(r+1,c). Finally, if the character in the cell is XX, then this cell is the blocked zone. The player should remain in this cell (the game for him isn't very interesting from now on).It is guaranteed that the characters are written in a way that the player will never have to step outside of the board, no matter at which cell he starts.As a player starts from a cell, he moves according to the character in the current cell. The player keeps moving until he lands in a blocked zone. It is also possible that the player will keep moving infinitely long.For every of the n2n2 cells of the board Alice, your friend, wants to know, how will the game go, if the player starts in this cell. For each starting cell of the board, she writes down the cell that the player stops at, or that the player never stops at all. She gives you the information she has written: for each cell (r,c)(r,c) she wrote: a pair (xx,yy), meaning if a player had started at (r,c)(r,c), he would end up at cell (xx,yy). or a pair (−1−1,−1−1), meaning if a player had started at (r,c)(r,c), he would keep moving infinitely long and would never enter the blocked zone. It might be possible that Alice is trying to fool you and there's no possible grid that satisfies all the constraints Alice gave you. For the given information Alice provided you, you are required to decipher a possible board, or to determine that such a board doesn't exist. If there exist several different boards that satisfy the provided information, you can find any of them.InputThe first line of the input contains a single integer nn (1≤n≤1031≤n≤103)  — the side of the board.The ii-th of the next nn lines of the input contains 2n2n integers x1,y1,x2,y2,…,xn,ynx1,y1,x2,y2,…,xn,yn, where (xj,yj)(xj,yj) (1≤xj≤n,1≤yj≤n1≤xj≤n,1≤yj≤n, or (xj,yj)=(−1,−1)(xj,yj)=(−1,−1)) is the pair written by Alice for the cell (i,j)(i,j). OutputIf there doesn't exist a board satisfying the information that Alice gave you, print a single line containing INVALID. Otherwise, in the first line print VALID. In the ii-th of the next nn lines, print the string of nn characters, corresponding to the characters in the ii-th row of the suitable board you found. Each character of a string can either be UU, DD, LL, RR or XX. If there exist several different boards that satisfy the provided information, you can find any of them.ExamplesInputCopy2 1 1 1 1 2 2 2 2 OutputCopyVALID XL RX InputCopy3 -1 -1 -1 -1 -1 -1 -1 -1 2 2 -1 -1 -1 -1 -1 -1 -1 -1 OutputCopyVALID RRD UXD ULLNoteFor the sample test 1 :The given grid in output is a valid one. If the player starts at (1,1)(1,1), he doesn't move any further following XX and stops there. If the player starts at (1,2)(1,2), he moves to left following LL and stops at (1,1)(1,1). If the player starts at (2,1)(2,1), he moves to right following RR and stops at (2,2)(2,2). If the player starts at (2,2)(2,2), he doesn't move any further following XX and stops there. The simulation can be seen below : For the sample test 2 : The given grid in output is a valid one, as a player starting at any cell other than the one at center (2,2)(2,2), keeps moving in an infinitely long cycle and never stops. Had he started at (2,2)(2,2), he wouldn't have moved further following instruction XX .The simulation can be seen below :
[ "constructive algorithms", "dfs and similar", "graphs", "implementation" ]
#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> #include <ext/pb_ds/priority_queue.hpp> using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; template <class T> using Tree = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>; using Trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>; // template <class T> using heapq = __gnu_pbds::priority_queue<T, greater<T>, pairing_heap_tag>; template <class T> using heapq = std::priority_queue<T, vector<T>, greater<T>>; #define ll long long #define i128 __int128 #define ld long double #define ui unsigned int #define ull unsigned long long #define pii pair<int, int> #define pll pair<ll, ll> #define pdd pair<ld, ld> #define vi vector<int> #define vvi vector<vector<int>> #define vll vector<ll> #define vvll vector<vector<ll>> #define vpii vector<pii> #define vpll vector<pll> #define lb lower_bound #define ub upper_bound #define pb push_back #define pf push_front #define eb emplace_back #define fi first #define se second #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 each(x, v) for(auto& x: v) #define len(x) (int)x.size() #define elif else if #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define mst(x, a) memset(x, a, sizeof(x)) #define lowbit(x) (x & (-x)) #define bitcnt(x) (__builtin_popcountll(x)) #define endl "\n" mt19937 rng( chrono::steady_clock::now().time_since_epoch().count() ); #define Ran(a, b) rng() % ( (b) - (a) + 1 ) + (a) struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<uint64_t,uint64_t> x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x.first + FIXED_RANDOM) ^ (splitmix64(x.second + FIXED_RANDOM) >> 1); } }; const i128 ONE = 1; istream &operator>>(istream &in, i128 &x) { string s; in >> s; bool minus = false; if (s[0] == '-') { minus = true; s.erase(s.begin()); } x = 0; for (auto i : s) { x *= 10; x += i - '0'; } if (minus) x = -x; return in; } ostream &operator<<(ostream &out, i128 x) { string s; bool minus = false; if (x < 0) { minus = true; x = -x; } while (x) { s.push_back(x % 10 + '0'); x /= 10; } if (s.empty()) s = "0"; if (minus) out << '-'; reverse(s.begin(), s.end()); out << s; return out; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for(auto it = begin(v); it != end(v); ++it) { if(it == begin(v)) os << *it; else os << " " << *it; } return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { for(auto it = begin(v); it != end(v); ++it) { if(it == begin(v)) os << *it; else os << " " << *it; } return os; } template <class T> ostream &operator<<(ostream &os, const multiset<T> &v) { for(auto it = begin(v); it != end(v); ++it) { if(it == begin(v)) os << *it; else os << " " << *it; } return os; } template <class T> ostream &operator<<(ostream &os, const Tree<T> &v) { for(auto it = begin(v); it != end(v); ++it) { if(it == begin(v)) os << *it; else os << " " << *it; } return os; } template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) { os << p.first << " " << p.second; return os; } ll gcd(ll x,ll y) { if(!x) return y; if(!y) return x; int t = __builtin_ctzll(x | y); x >>= __builtin_ctzll(x); do { y >>= __builtin_ctzll(y); if(x > y) swap(x, y); y -= x; } while(y); return x<<t; } ll lcm(ll x, ll y) { return x * y / gcd(x, y); } ll exgcd(ll a, ll b, ll &x, ll &y) { if(!b) return x = 1, y = 0, a; ll d = exgcd(b, a % b, x, y); ll t = x; x = y; y = t - a / b * x; return d; } ll max(ll x, ll y) { return x > y ? x : y; } ll min(ll x, ll y) { return x < y ? x : y; } ll Mod(ll x, int mod) { return (x % mod + mod) % mod; } ll pow(ll x, ll y, ll mod){ ll res = 1, cur = x; while (y) { if (y & 1) res = res * cur % mod; cur = ONE * cur * cur % mod; y >>= 1; } return res % mod; } ll probabilityMod(ll x, ll y, ll mod) { return x * pow(y, mod-2, mod) % mod; } vvi getGraph(int n, int m, bool directed = false) { vvi res(n); rep(_, 0, m) { int u, v; cin >> u >> v; u--, v--; res[u].emplace_back(v); if(!directed) res[v].emplace_back(u); } return res; } vector<vpii> getWeightedGraph(int n, int m, bool directed = false) { vector<vpii> res(n); rep(_, 0, m) { int u, v, w; cin >> u >> v >> w; u--, v--; res[u].emplace_back(v, w); if(!directed) res[v].emplace_back(u, w); } return res; } const ll LINF = 0x1fffffffffffffff; const ll MINF = 0x7fffffffffff; const int INF = 0x3fffffff; const int MOD = 1000000007; const int MODD = 998244353; const int N = 1e6 + 10; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; string S = "DLUR"; void solve() { int n; cin >> n; vector<vpii> a(n, vpii(n)); vector<vector<string>> ans(n, vector<string>(n, " ")); rep(i, 0, n) rep(j, 0, n) { cin >> a[i][j].fi >> a[i][j].se; if (a[i][j].fi != -1) { a[i][j].fi--, a[i][j].se--; } } auto dfs = [&] (auto dfs, int x, int y, int sx, int sy) -> void { rep(f, 0, 4) { int nx = x + dx[f], ny = y + dy[f]; if (0 <= nx && nx < n && 0 <= ny && ny < n && a[x][y] == a[nx][ny] && ans[nx][ny] == " ") { ans[nx][ny] = S[f]; dfs(dfs, nx, ny, sx, sy); } } }; rep(i, 0, n) rep(j, 0, n) if (a[i][j] == pii{i, j}) ans[i][j] = "X", dfs(dfs, i, j, i, j); rep(i, 0, n) rep(j, 0, n) if (a[i][j].fi == -1 && ans[i][j] == " ") { if (i + 1 < n && a[i+1][j].fi == -1) { ans[i][j] == "D", ans[i+1][j] == "U"; dfs(dfs, i, j, -1, -1); dfs(dfs, i+1, j, -1, -1); } if (j + 1 < n && a[i][j+1].fi == -1) { ans[i][j] = "R", ans[i][j+1] == "L"; dfs(dfs, i, j, -1, -1); dfs(dfs, i, j+1, -1, -1); } } rep(i, 0, n) rep(j, 0, n) if (ans[i][j] == " ") { cout << "INVALID" << endl; return; } cout << "VALID" << endl; rep(i, 0, n) { rep(j, 0, n) cout << ans[i][j]; cout << endl; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
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×1 (i.e. the height of this figure is 22 blocks and the width of this figure is 11 block). Note that you cannot rotate these figures.Your task is to say if you can clear the whole field by placing such figures.More formally, the problem can be described like this:The following process occurs while at least one aiai is greater than 00: You place one figure 2×12×1 (choose some ii from 11 to nn and replace aiai with ai+2ai+2); then, while all aiai are greater than zero, replace each aiai with ai−1ai−1. And your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.The next 2t2t lines describe test cases. The first line of the test case contains one integer nn (1≤n≤1001≤n≤100) — the number of columns in the Tetris field. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100), where aiai is the initial height of the ii-th column of the Tetris field.OutputFor each test case, print the answer — "YES" (without quotes) if you can clear the whole Tetris field and "NO" otherwise.ExampleInputCopy4 3 1 1 3 4 1 1 2 1 2 11 11 1 100 OutputCopyYES NO YES YES NoteThe first test case of the example field is shown below:Gray lines are bounds of the Tetris field. Note that the field has no upper bound.One of the correct answers is to first place the figure in the first column. Then after the second step of the process; the field becomes [2,0,2][2,0,2]. Then place the figure in the second column and after the second step of the process, the field becomes [0,0,0][0,0,0].And the second test case of the example field is shown below:It can be shown that you cannot do anything to end the process.In the third test case of the example, you first place the figure in the second column after the second step of the process, the field becomes [0,2][0,2]. Then place the figure in the first column and after the second step of the process, the field becomes [0,0][0,0].In the fourth test case of the example, place the figure in the first column, then the field becomes [102][102] after the first step of the process, and then the field becomes [0][0] after the second step of the process.
[ "implementation", "number theory" ]
#include<bits/stdc++.h> #define deq deque<int #define vec vector<int #define pb push_back #define pf push_front #define sonic ios::sync_with_stdio(false); cin.tie(0); using namespace std; using ll = long long; using ull = unsigned long long; int main() { sonic; int t; cin >> t; while (t --) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i ++) { cin >> a[i]; } int ch = 0, nch = 0; for (int i = 0; i < n; i ++) { if (a[i] % 2 == 0) { ch += 1; } else { nch += 1; } } if (ch == n || nch == n) { cout << "YES\n"; } else { cout << "NO\n"; } } }
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.InputThe first line contains the single integer TT (1≤T≤501≤T≤50) — the number of test cases.Next TT lines contain test cases — one per line. Each line contains two integers aa and mm (1≤a<m≤10101≤a<m≤1010).OutputPrint TT integers — one per test case. For each test case print the number of appropriate xx-s.ExampleInputCopy3 4 9 5 10 42 9999999967 OutputCopy6 1 9999999966 NoteIn the first test case appropriate xx-s are [0,1,3,4,6,7][0,1,3,4,6,7].In the second test case the only appropriate xx is 00.
[ "math", "number theory" ]
#include<iostream> using namespace std; #include <iostream> #include <cmath> #include <algorithm> #include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sst string #define pb push_back #define maxco 100000+5 #define lld long double #define cha ios_base::sync_with_stdio(false); #define ffl cout.flush(); #define phi acos(-1) #define mod 1000000007 ll mem[1000069]; ll fak (ll h){ if(h<=1){ return 1; } else if(mem[h]!=0){ return mem[h]; } else{ return mem[h]=h%mod*fak(h-1)%mod; } } ll powmod(ll x,ll y){ if(y==0){ return 1; } else{ ll tmp=powmod(x,y/2); tmp=(tmp*tmp)%mod; if(y%2==1){ return (tmp*x)%mod; } else{ return tmp; } } } ll com(ll n,ll m){ if(n < 0 || m < 0 || n < m) return 0; ll ans=fak(n)%mod; ll ans2=fak(m)%mod; ll ans3=(fak(n-m))%mod; ans2=powmod(ans2,mod-2); ans3=powmod(ans3,mod-2); ans2*=ans3%mod; ans*=ans2%mod; ans%=mod; return ans; } int main(){ cha ll tc; cin>>tc; while(tc--){ ll a,b; cin>>a>>b; ll val=b/gcd(a,b); ll nih=val; for(ll i=2;i*i<=val;i++){ if(val%i==0){ // cout<<i<<" "<<val<<endl; nih*=(i-1); nih/=i; while(val%i==0)val/=i; } } if(val>1 && val<b/gcd(a,b)){ nih*=(val-1); nih/=val; } if(nih==val)cout<<nih-1<<endl; else cout<<nih<<endl; } }
cpp
1312
E
E. Array Shrinkingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a1,a2,…,ana1,a2,…,an. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements ai=ai+1ai=ai+1 (if there is at least one such pair). Replace them by one element with value ai+1ai+1. After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array aa you can get?InputThe first line contains the single integer nn (1≤n≤5001≤n≤500) — the initial length of the array aa.The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — the initial array aa.OutputPrint the only integer — the minimum possible length you can get after performing the operation described above any number of times.ExamplesInputCopy5 4 3 2 2 3 OutputCopy2 InputCopy7 3 3 4 4 4 3 3 OutputCopy2 InputCopy3 1 3 5 OutputCopy3 InputCopy1 1000 OutputCopy1 NoteIn the first test; this is one of the optimal sequences of operations: 44 33 22 22 33 →→ 44 33 33 33 →→ 44 44 33 →→ 55 33.In the second test, this is one of the optimal sequences of operations: 33 33 44 44 44 33 33 →→ 44 44 44 44 33 33 →→ 44 44 44 44 44 →→ 55 44 44 44 →→ 55 55 44 →→ 66 44.In the third and fourth tests, you can't perform the operation at all.
[ "dp", "greedy" ]
#include<bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; const ll base = 37;//进制 const ll mod = 998244353;//质数 const int inf = 1e9+5; const int maxn = 2e5+5; const ll linf = 1e18+5ll; int main() { cin.tie(NULL); cout.tie(NULL); ios::sync_with_stdio(false); int n; cin>>n; vector<int>v(n+1); for(int i = 1;i<=n;i++) { cin>>v[i]; } vector<vector<int>>dp(n+1,vector<int>(n+1,inf)); vector val = dp; for(int i = 1;i<=n;i++) { dp[i][i] = 1; val[i][i] = v[i]; } for(int i = 1;i<=n;i++) { for(int s = 1;i+s<=n+1;s++) { int t = i+s-1; for(int k = s+1;k<=t;k++) { if(dp[s][k-1] == 1 && dp[k][t] == 1 && val[s][k-1]==val[k][t]) { dp[s][t] = 1; val[s][t] = val[s][k-1] + 1; } else { dp[s][t] = min (dp[s][t] , dp[s][k-1]+dp[k][t]); } } } } cout<<dp[1][n]<<endl; }
cpp
1313
A
A. Fast Food Restauranttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTired of boring office work; Denis decided to open a fast food restaurant.On the first day he made aa portions of dumplings, bb portions of cranberry juice and cc pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules: every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes); each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk; all visitors should receive different sets of dishes. What is the maximum number of visitors Denis can feed?InputThe first line contains an integer tt (1≤t≤5001≤t≤500) — the number of test cases to solve.Each of the remaining tt lines contains integers aa, bb and cc (0≤a,b,c≤100≤a,b,c≤10) — the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.OutputFor each test case print a single integer — the maximum number of visitors Denis can feed.ExampleInputCopy71 2 10 0 09 1 72 2 32 3 23 2 24 4 4OutputCopy3045557NoteIn the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
[ "brute force", "greedy", "implementation" ]
#include<bits/stdc++.h> using namespace std; #define str(t) ll t; cin>>t;while(t--) #define ll long long int #define vll vector<ll> #define g(n) ll n;cin>>n #define f(i,a,b) for(ll i=a;i<b;i++) #define fin(i,a,b) for(int i=a;i<b;i++) #define vsort(a) sort(a.begin(),a.end()) #define vrsort(a) sort(a.begin(),a.end(),greater< ll >()) #define mll map<ll,ll> #define vin vector <int> #define sll set <ll> #define pb push_back #define all(a) a.begin(),a.end() #define get(a,n) vll a(n); f(i,0,n) cin>>a[i] #define put(a) f(i,0,a.size()) cout<<a[i]<<' '; cout<<endl int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); str(t) { vector<ll> a(3); for (auto& it : a) { cin >> it; } sort(a.begin(), a.end()); if (a[0] == 0 && a[1] == 0 && a[2] == 0) cout << 0 << endl; else if (a[0] == 0 && a[1] == 0 && a[2] > 0) cout << 1 << endl; else if (a[0] == 0 && a[1] == 1 && a[2] > 0) cout << 2 << endl; else if (a[0] == 0 && a[1] >= 2 && a[2] >= 2) cout << 3 << endl; else if (a[0] == 1 && a[1] == 1 && a[2] >= 1) cout << 3 << endl; else if (a[0] == 1 && a[1] >= 2 && a[2] >= 2) cout << 4 << endl; else if (a[0] == 2 && a[1] == 2 && a[2] == 2) cout << 4 << endl; else if (a[0] == 2 && a[1] >= 2 && a[2] >= 2) cout << 5 << endl; else if (a[0] == 3 && a[1] >= 3 && a[2] >= 3) cout << 6 << endl; else cout << 7 << endl; } return 0; }
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 any three subsets is empty. In other words, for all 1≤i1<i2<i3≤k1≤i1<i2<i3≤k, Ai1∩Ai2∩Ai3=∅Ai1∩Ai2∩Ai3=∅.In one operation, you can choose one of these kk subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.Let mimi be the minimum number of operations you have to do in order to make the ii first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1i+1 and nn), they can be either off or on.You have to compute mimi for all 1≤i≤n1≤i≤n.InputThe first line contains two integers nn and kk (1≤n,k≤3⋅1051≤n,k≤3⋅105).The second line contains a binary string of length nn, representing the initial state of each lamp (the lamp ii is off if si=0si=0, on if si=1si=1).The description of each one of the kk subsets follows, in the following format:The first line of the description contains a single integer cc (1≤c≤n1≤c≤n)  — the number of elements in the subset.The second line of the description contains cc distinct integers x1,…,xcx1,…,xc (1≤xi≤n1≤xi≤n)  — the elements of the subset.It is guaranteed that: The intersection of any three subsets is empty; It's possible to make all lamps be simultaneously on using some operations. OutputYou must output nn lines. The ii-th line should contain a single integer mimi  — the minimum number of operations required to make the lamps 11 to ii be simultaneously on.ExamplesInputCopy7 3 0011100 3 1 4 6 3 3 4 7 2 2 3 OutputCopy1 2 3 3 3 3 3 InputCopy8 6 00110011 3 1 3 8 5 1 2 5 6 7 2 6 8 2 3 5 2 4 7 1 2 OutputCopy1 1 1 1 1 1 4 4 InputCopy5 3 00011 3 1 2 3 1 4 3 3 4 5 OutputCopy1 1 1 1 1 InputCopy19 5 1001001001100000110 2 2 3 2 5 6 2 8 9 5 12 13 14 15 16 1 19 OutputCopy0 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 4 4 5 NoteIn the first example: For i=1i=1; we can just apply one operation on A1A1, the final states will be 10101101010110; For i=2i=2, we can apply operations on A1A1 and A3A3, the final states will be 11001101100110; For i≥3i≥3, we can apply operations on A1A1, A2A2 and A3A3, the final states will be 11111111111111. In the second example: For i≤6i≤6, we can just apply one operation on A2A2, the final states will be 1111110111111101; For i≥7i≥7, we can apply operations on A1,A3,A4,A6A1,A3,A4,A6, the final states will be 1111111111111111.
[ "dfs and similar", "dsu", "graphs" ]
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define x first #define y second #define pb push_back #define all(a) a.begin(),a.end() #define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) typedef long long ll;typedef pair<int,int> pii;typedef pair<ll,ll> pll; template<class T>inline void read(T &x){ x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); if(f) x=-x; } template<class T,class... V> inline void read(T &a,V&... b){read(a); read(b...);} const int maxn = 600100, maxm = 1000100, inf = 0x3f3f3f3f, mod = 1e9 + 7; const ll INF = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-8; template <typename T> inline void chkmax(T &x,const T &y){x=x>y?x:y;} template <typename T> inline void chkmin(T &x,const T &y){x=x<y?x:y;} int n, k, c, t; string s; vector<int> v[maxn]; int p[maxn], sum[maxn], w[maxn]; int find(int x) { return p[x] == x? x: p[x] = find(p[x]); } void unite(int u, int v) { int pu = find(u), pv = find(v); if(pu != pv) p[pu] = pv, w[pv] += w[pu]; } int get(int x) { return min(w[find(x)], w[find(x + k)]); } int main() { ios; cin >> n >> k >> s; s = "?" + s; t = 2 * k + 1; w[t] = (1 << 29); p[t] = t; for(int i = 1; i <= k; i++) { cin >> c; for(int j = 1; j <= c; j++) { int x; cin >> x; v[x].pb(i); } } for(int i = 1; i <= 2 * k; i++) p[i] = i; for(int i = 1; i <= k; i++) w[i] = 1; int ans = 0; for(int i = 1; i <= n; i++) { if(v[i].size() == 1) { ans -= get(v[i][0]); if(s[i] == '1') unite(v[i][0], t); else unite(v[i][0] + k, t); ans += get(v[i][0]); } else if(v[i].size() == 2) { int x = v[i][0], y = v[i][1]; if(s[i] == '0') { if(find(x) != find(y + k) && find(x + k) != find(y)) { ans -= get(x) + get(y); unite(x, y + k); unite(x + k, y); ans += get(x); } } else { if(find(x) != find(y) && find(x + k) != find(y + k)) { ans -= get(x) + get(y); unite(x, y); unite(x + k, y + k); ans += get(x); } } } cout << ans << endl; } }
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 arranged in some order.Polycarp uses the same password ss on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in ss, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in ss, so, for example, the password cannot be password (two characters s are adjacent).Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?InputThe first line contains one integer TT (1≤T≤10001≤T≤1000) — the number of test cases.Then TT lines follow, each containing one string ss (1≤|s|≤2001≤|s|≤200) representing the test case. ss consists of lowercase Latin letters only. There are no two adjacent equal characters in ss.OutputFor each test case, do the following: if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); otherwise, print YES (in upper case), and then a string consisting of 2626 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. ExampleInputCopy5 ababa codedoca abcda zxzytyz abcdefghijklmnopqrstuvwxyza OutputCopyYES bacdefghijklmnopqrstuvwxyz YES edocabfghijklmnpqrstuvwxyz NO YES xzytabcdefghijklmnopqrsuvw NO
[ "dfs and similar", "greedy", "implementation" ]
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define F first #define S second #define enter cout<<'\n'; #define INF 99999999999999999 #define MOD 1000000007 int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--) { string s; cin>>s; ll n; n=s.size(); vector<set<int>>g(26); int start=-1; if(n==1) { cout<<"YES"<<'\n'<<"abcdefghijklmnopqrstuvwxyz"<<'\n'; continue; } for(int i=1;i<n;i++) { g[s[i]-'a'].insert(s[i-1]-'a'); g[s[i-1]-'a'].insert(s[i]-'a'); } bool tf=true; for(int i=0;i<26;i++) { if(g[i].size()==1 && start==-1) start=i; if(g[i].size()>2) { tf=false; break; } if(g[i].size()==2) start=i; } if(!tf) { cout<<"NO"<<'\n'; continue; } queue<pair<int,int>>q; deque<int>dq; dq.push_front(start); bool vis[26]={ }; q.push({0,start}); vis[start]=1; while(!q.empty()) { ll curr=q.front().S; ll dir=q.front().F; q.pop(); for(auto x:g[curr]) { if(g[curr].size()>=2) { if(g[curr].size()==2 && curr==start){} else { tf=false; break; } } if(!dir) { vis[x]=true; dq.push_front(x); q.push({1,x}); g[x].erase(curr); dir=2; continue; } if(vis[x]==true) { tf=false; break; } vis[x]=true; q.push({dir,x}); g[x].erase(curr); if(dir-1) dq.push_back(x); else dq.push_front(x); } if(!tf) break; } if(!tf) { cout<<"NO"<<'\n'; continue; } cout<<"YES"<<'\n'; while(!dq.empty()) { cout<<(char)(dq.front()+'a'); dq.pop_front(); } for(char i='a';i<='z';i++) if(!vis[i-'a']) cout<<i; enter } }
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 arranged in some order.Polycarp uses the same password ss on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in ss, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in ss, so, for example, the password cannot be password (two characters s are adjacent).Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?InputThe first line contains one integer TT (1≤T≤10001≤T≤1000) — the number of test cases.Then TT lines follow, each containing one string ss (1≤|s|≤2001≤|s|≤200) representing the test case. ss consists of lowercase Latin letters only. There are no two adjacent equal characters in ss.OutputFor each test case, do the following: if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem); otherwise, print YES (in upper case), and then a string consisting of 2626 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. ExampleInputCopy5 ababa codedoca abcda zxzytyz abcdefghijklmnopqrstuvwxyza OutputCopyYES bacdefghijklmnopqrstuvwxyz YES edocabfghijklmnpqrstuvwxyz NO YES xzytabcdefghijklmnopqrsuvw NO
[ "dfs and similar", "greedy", "implementation" ]
//ANY TIME YOU SEE OPTIMIZATION PROBLEM -> BINARY SEARCH THE ANSWER!!! #include <bits/stdc++.h> using namespace std; #define ll long long #define No cout<<"No"<<endl; #define NO cout<<"NO"<<endl; #define Yes cout<<"Yes"<<endl; #define YES cout<<"YES"<<endl; #define MOD 1000000007 #define endl '\n' void solve() { ll n, i, j, flag =0; string s; cin>>s; map<char, ll> mp; deque<char> dq; deque<char>::iterator it; dq.push_back(s[0]); it = dq.begin(); mp[s[0]]++; for(i=1;i<s.size();i++) { if(mp[s[i]]==0) { if(it == dq.begin()) { dq.push_front(s[i]); it = dq.begin(); } else if(it+1 == dq.end()) { dq.push_back(s[i]); it = dq.end()-1; } else { //cout<<*it<<endl; NO flag=1; break; } mp[s[i]]++; } else { if(it==dq.begin()) { if(*(it+1)==s[i]) { it = it +1; } else { //cout<<s[i]<<endl; NO flag=1; break; } } else if(it == dq.end()-1) { if(*(it-1)==s[i]) { it = it - 1; } else { NO flag=1; break; } } else { auto it1 = it + 1; auto it2 = it-1; if(*(it1)==s[i]) { it = it1; } else if(*(it2)==s[i]) { it = it2; } else { NO flag = 1; break; } } } } if(flag==0) { YES for(auto it: dq) { cout<<it; } for(char i='a';i<='z';i++) { if(mp[i]==0) { cout<<i; } } cout<<""<<endl; } else { } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) solve(); }
cpp
1324
B
B. Yet Another Palindrome Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array aa consisting of nn integers.Your task is to determine if aa has some subsequence of length at least 33 that is a palindrome.Recall that an array bb is called a subsequence of the array aa if bb can be obtained by removing some (possibly, zero) elements from aa (not necessarily consecutive) without changing the order of remaining elements. For example, [2][2], [1,2,1,3][1,2,1,3] and [2,3][2,3] are subsequences of [1,2,1,3][1,2,1,3], but [1,1,2][1,1,2] and [4][4] are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array aa of length nn is the palindrome if ai=an−i−1ai=an−i−1 for all ii from 11 to nn. For example, arrays [1234][1234], [1,2,1][1,2,1], [1,3,2,2,3,1][1,3,2,2,3,1] and [10,100,10][10,100,10] are palindromes, but arrays [1,2][1,2] and [1,2,3,1][1,2,3,1] are not.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.Next 2t2t lines describe test cases. The first line of the test case contains one integer nn (3≤n≤50003≤n≤5000) — the length of aa. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n), where aiai is the ii-th element of aa.It is guaranteed that the sum of nn over all test cases does not exceed 50005000 (∑n≤5000∑n≤5000).OutputFor each test case, print the answer — "YES" (without quotes) if aa has some subsequence of length at least 33 that is a palindrome and "NO" otherwise.ExampleInputCopy5 3 1 2 1 5 1 2 2 3 2 3 1 1 2 4 1 2 2 1 10 1 1 2 2 3 3 4 4 5 5 OutputCopyYES YES NO YES NO NoteIn the first test case of the example; the array aa has a subsequence [1,2,1][1,2,1] which is a palindrome.In the second test case of the example, the array aa has two subsequences of length 33 which are palindromes: [2,3,2][2,3,2] and [2,2,2][2,2,2].In the third test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.In the fourth test case of the example, the array aa has one subsequence of length 44 which is a palindrome: [1,2,2,1][1,2,2,1] (and has two subsequences of length 33 which are palindromes: both are [1,2,1][1,2,1]).In the fifth test case of the example, the array aa has no subsequences of length at least 33 which are palindromes.
[ "brute force", "strings" ]
#include<bits/stdc++.h> using namespace std; #define ll long long #define fst first #define sec second #define mod (ll)(1e9+7) void solve(){ ll n;cin >> n; map<ll,vector<ll> > mp; for(ll i=0;i<n;i++) { ll data; cin >> data; mp[data].push_back(i); } for(auto v:mp){ vector<ll> tmp=v.second; if(tmp.size()>1){ if(tmp[tmp.size()-1]-tmp[0]>1){ cout << "YES" << endl; return; } } } cout << "NO" << endl; } int main(){ ios::sync_with_stdio(0); cin.tie(0); ll t=1;cin >> t; while(t--){ solve(); } return 0; }
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, you can print any.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.The next nn lines describe test cases. The ii-th test case is given on a new line as one integer nn (2≤n≤1092≤n≤109).OutputFor each test case, print the answer on it. Print "NO" if it is impossible to represent nn as a⋅b⋅ca⋅b⋅c for some distinct integers a,b,ca,b,c such that 2≤a,b,c2≤a,b,c.Otherwise, print "YES" and any possible such representation.ExampleInputCopy5 64 32 97 2 12345 OutputCopyYES 2 4 8 NO NO NO YES 3 5 823
[ "greedy", "math", "number theory" ]
/*Pash_Mak*/ #include <bits/stdc++.h> using namespace std; [[maybe_unused]] const bool Tests = true; class Pash { public: int t; static void C() { int n , a = 2 , b = 3 , c = 4; cin >> n; if (n < 24){cout << "NO\n";return;} while(a * b * c <= n){ while(a * b * c <= n) { c = n / (a * b); if(a * b * c == n && c != a && c != b){cout << "Yes\n" << a << ' ' << b << ' ' << c << '\n';return;} b ++ , c = b + 1; } a ++ , b = a + 1 , c = b + 1; } cout << "No\n"; } }; int main() { ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); Pash Mak; if (Tests) cin >> Mak.t; else Mak.t = 1; while(Mak.t --) Pash::C(); return 0; }/*. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡄⠀⠀⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣤⣤⣄⠀⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⣤⣶⣷⣿⣿⣿⣻⠿⣦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡾⢛⣯⣽⣿⣿⣷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣮⡻⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠜⢣⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣮⣳⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠌⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡷⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀ ⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣡⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡰⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⢦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠅⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡜⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡌⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢱⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢐⠀⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡜⢀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⢱⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠁⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡆⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠉⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡄⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠁⢸⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠐⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⣸⡏⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⣿⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⣿⣷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⣀⣠⣤⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠷⠾⠛⠛⠛⠉⠉⠁⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣇⠙⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣿⣿⣿⡆⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⠤⠽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⣿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡸⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣿⣿⡄⠈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠟⠛⠙⠁⣀⣠⣤⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢹⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠒⠲⠶⢦⣬⣿⣿⣄⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠀⢀⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣤⣶⣶⣶⣶⣶⣶⣶⣶⣶⣦⣤⣀⣀⠈⠙⠳⠍⠂⠀⠈⠻⣿⣧⡙⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⠛⠻⢿⣿⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⡀⠀⠀⠀⠀⠀⠈⠓⢄⠀⠈⠙⠿⢿⣿⣿⠻⠿⠿⠿⠟⠿⠀⠀⠀⢠⣾⣿⡿⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠈⠂⠀⠀⠀⠈⠉⠑⠀⠀⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢸⣿⣿⣿⡿⠟⠛⠿⠿⡿⣿⣿⣿⣿⠀⠀⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⡟⠀⠀⠀⠀⠀⠀⢘⣿⣿⠃⠀⠀⠠⠾⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠉⠀⢙⣿⣿⣿⣿⡟⠉⠉⠛⠛⠿⢿⣿⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠉⠉⠉⠉⠙⠛⠛⠛⠛⠛⠒⠂⠁⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠉⠙⠿⣧⡀⠀⠀⠀⠻⣿⣿⡟⠁⠀⢀⡀⠤⠶⠾⠛⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠲⠀⠀⠀⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠀⠈⠁⠀⠀⢀⣀⣭⠿⠗⠚⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠄⡀⢀⡠⢠⠂⣠⠀⢀⠀⠀⠀⠀⠄⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠄⠐⢁⠁⣣⢣⢢⠃⠀⢊⡾⢠⡦⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⣠⠀⠀⠠⢀⢆⢄⡜⣸⢠⡔⡴⠠⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠁⠠⠃⠀⠀⠠⠁⠅⣿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⡆⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠈⠀⠀⠠⣡⣯⠎⡸⣿⢳⠇⠏⢀⠂⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠠⠡⠁⠐⠁⠏⠎⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠞⠁⢸⣿⣿⣿⣿⣿⣿⠟⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⡀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠔⠃⠀⠀⠀⠀⣿⣿⣿⡿⠟⠁⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢻⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠙⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣟⢢⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⢀⣿⠋⣽⡇⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠀⠛⢿⣿⣿⣿⣿⣿⣿⠀⠈⠁⠀⠁⠒⠂⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⡿⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⡇⢰⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢱⠀⠀⠀⠈⠙⠿⠿⠿⢿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡿⠀⣸⠇⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⢸⠀⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⣇⠀⠀⠀⠀⠀⠀⠀⠀⢻⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⡴⠖⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⠁⠀⡿⠀⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣿⣿⢿⣿⣿⣿⠀⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⢸⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢤⣤⣤⠤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠁⢀⠔⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡟⠀⠀⣸⠃⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⡿⣸⣿⣿⣿⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠘⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⡎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⢨⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⢳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢷⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠓⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⠋⠀⠀⢰⡟⠀⠀⠀⠀⠀⢰⣿⣿⣿⣿⣿⣿⣿⠇⣿⣿⣿⣿⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⢰⠁⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⢸⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⢳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠞⠁⠀⠀⢀⡞⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⢸⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⢸⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠈⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠳⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠁⠀⠀⠀⠀⡞⠁⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⠇⢠⣿⣿⣿⡟⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠇⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⢸⠀⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠘⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠓⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡠⠊⠀⠀⠀⠀⠀⢀⠞⠀⠀⠀⠀⠀⠀⠀⣸⢿⣿⣿⣿⣿⣿⡿⠀⣼⣿⣿⣿⡇⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⢿⡇⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⡘⠀⢸⣿⡟⢻⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⢸⠀⢸⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿⣿⣿⡄⠀⠀⠈⢻⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠣⢄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠔⠉⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⢰⡿⣼⣿⣿⣿⣿⣿⠁⢀⣿⣿⣿⣿⣧⠀⠀⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⡅⠀⣾⣿⠇⢸⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⡾⠀⢸⣿⣿⣿⣿⡟⣿⣿⣿⣿⣿⣿⣿⡷⠀⠀⠀⠀⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠓⠤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣶⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡾⢱⣿⣿⣿⣿⣿⠇⠀⣼⡇⣿⣿⣿⣿⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⢰⠀⣀⣿⣿⠀⢸⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⡇⠀⣿⣿⣿⣿⣿⣷⢻⣿⣿⣿⣿⣿⣿⣷⡁⠀⠀⠀⠀⠀⠉⠂⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣾⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠃⣾⣿⣿⣿⣿⡏⠀⢸⡿⠀⣿⣿⣿⣿⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠸⡇⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠇⢀⣿⣿⡿⠀⢸⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⢀⠃⢀⣿⣿⣿⣿⣿⣿⠀⢿⣿⣿⣿⣿⣿⣿⣯⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣦⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⣾⣿⣿⣿⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡾⠃⣸⣿⣿⣿⣿⡏⠀⣰⡿⠁⠀⣿⣿⣿⣿⠀⠀⠀⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⣇⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⡼⠀⠸⣿⣿⠇⠀⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⢸⠀⣼⣿⣿⣿⣿⣿⣿⣇⠈⣿⣿⣿⣿⣷⢿⣿⣖⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣷⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣶⡤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠞⠀⣰⣿⣿⣿⣿⠏⠀⣴⠟⠀⠀⠀⣿⣿⣿⣿⡀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⢹⡄⠀⠀⠀⠀⠀⠀ ⠀⠀⢠⠃⠀⣶⣿⣿⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⠀⡏⢰⣿⣿⣿⣿⠁⢻⣿⣿⡄⠘⢿⣿⣿⣿⡇⠻⣿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣶⣤⣀⣀⣤⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⣿⣟⣿⣿⠋⢀⠌⠃⠀⠀⠀⠀⣿⣿⣿⣿⡇⠀⠀⠀⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠈⡇⠀⠀⠀⠀⠀⠀ ⠀⠀⡏⠀⡸⣾⣿⡟⠀⠀⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀⠀⢸⢱⣿⣿⣿⣿⡏⠀⠈⣿⣿⣷⡀⠈⢿⣿⣿⣿⡄⠙⢿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⡿⢫⣾⠟⠁⠀⠁⠀⠀⠀⠀⠀⠀⢻⣿⢸⣿⣿⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⢹⡄⠀⠀⠀⠀⠀ ⠀⡜⠀⠀⣧⣿⣿⠃⠀⠀⡇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⠀⠀⠀⣴⣿⣿⣿⣿⣿⠇⠀⠀⣿⠈⢿⣧⠀⠀⠹⣿⣿⣷⡀⠀⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡾⠋⣠⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⡟⣿⣿⡄⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠸⣇⠀⠀⠀⠀⠀ ⠘⠁⠀⣸⣾⣿⡟⠀⠀⢰⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⣼⣿⣿⣿⣿⣿⡟⠀⠀⢀⣿⠀⠈⢻⣧⠀⠀⠈⢿⣿⣷⡀⠀⠀⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠤⠔⠛⠉⡠⠞⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⣇⠸⣿⣷⠀⠀⠀⠈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⢻⡆⠀⠀⠀⠀ ⠁⠀⠀⣿⣿⣿⡇⠀⠀⣸⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀⠀⣼⣿⣿⣿⣿⣿⣿⠃⠀⠀⢸⣿⠀⠀⠀⠙⠷⣄⠀⠀⠙⢿⣷⡄⠀⠀⠀⠀⠉⠒⠀⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣿⡄⠹⣿⣆⠀⠀⠀⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠘⣿⡀⠀⠀⠀ ⠀⠀⣸⣿⣿⣿⠀⠀⠀⠇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⢀⣾⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⣿⡏⠀⠀⠀⠀⠀⠈⠑⠄⠀⠀⠉⠳⢤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣧⠀⠹⣿⣆⠀⠀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠸⣿⣿⣿⣿⣇⠘⣷⠀⠀⠀ ⠀⢀⣿⣿⣿⡇⠀⠀⢀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣤⣤⣤⣼⣿⣄⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠠⠀⠀⠀⠀⣀⣀⣀⣀⣀⣤⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣧⠀⠙⣿⣧⡀⠀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⠀⢻⣆⠀⠀ ⢀⣾⣿⣿⣿⠁⠀⠀⡌⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⣠⣾⣿⣿⣿⡿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣼⣿⣶⣶⡶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣆⠀⠙⢿⣿⣦⡀⠀⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠸⣿⣿⣿⣿⣇⠀⢿⡂⠀ ⣼⣿⣹⣿⡇⠀⠀⠰⠁⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⣾⣿⣿⣿⣿⣿⣿⣷⣤⣀⠙⠛⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⣤⣴⣶⣶⣶⣶⣶⣶⣶⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣦⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⣿⣿⣿⣿⣿⡀⠈⣷⡀ ⣿⣿⣿⡿⠀⠀⠀⡘⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠋⠉⠀⠈⠈⠙⠿⢿⣿⣿⣿⣷⣴⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠘⣿⣿⣿⣿⣧⠀⠘⣧ ⣿⣾⣿⠃⠀⠀⢰⠁⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⠀⢿⣿⣿⣿⣿⡄⠀⠸ ⣿⣿⡿⠀⠀⠀⡀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠻⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⠋⠉⠀⠀⠀⠀⠀⠉⠙⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠘⣿⣿⣿⣿⣿⡀⠀ ⣿⣿⠁⠀⠀⠰⢁⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⢻⣿⣿⣿⣿⣧⠀ ⣿⡟⠀⠀⢠⠇⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠙⠻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣏⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠧⠹⣿⣿⣿⣿⣿⡄⠀⠀⢸⡿⣿⣿⣿⣿⡇ ⡟⠀⠀⢀⡞⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠙⢫⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡸⣿⣿⣿⡿⠟⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣿⣿⣧⠀⠀⠈⣿⠸⣿⣿⣿⣿ ⠃⠀⢀⡾⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⡀⠀⠀⢹⡆⠹⣿⣿⣿ ⠀⢀⣞⢡⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣿⣿⣿⣿⣇⠀⠀⢸⣧⠀⠻⣿⣿ ⢀⢼⢃⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣟⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⣿⣿⣿⣿⡄⠀⠈⣿⠀⠀⠻⣿ ⢮⢏⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢹⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣇⠀⠀⢹⡆⠀⠀⠹ ⢊⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⣸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⡄⠀⠀⣷⠀⠀⠀ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣧⠀⠀⢻⡄⠀⠀ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣇⠀⠘⣧⠀⠀ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⡄⠀⢿⡂⠀ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⢸⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢢⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣷⠀⠘⣧⠀ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣹⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⡇⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⢠⣞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣇⠀⢻⡆ ⣿⣿⣿⣿⣿⣿⣿⣿⠏⣿⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢷⡀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⢠⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⡀⠘⣷ ⣿⣿⣿⣿⣿⣿⣿⣿⢠⡇⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⢠⣿⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⡀⢹ ⣿⣿⣿⣿⣿⣿⣿⡏⢸⠁⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⡆⠀⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⢺⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⢸*/
cpp