Skip to content

Commit 7422e62

Browse files
committed
solutions added
1 parent a41c50b commit 7422e62

22 files changed

+132
-0
lines changed

src/1086 - Writing Numbers.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Writing Numbers
3+
Problem Link: https://cses.fi/problemset/task/1086
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'bool check(int n, int k) { int ans = 0; for (int i = 1; i <= n; i*=10) { int d = i*10; ans += n/d*i + min(max(n%d-i+1,0LL), i); } if (ans <= k) return 1; return 0;} signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int k; cin>>k; int l = 1, h = 6e17; int ans = 1; while(l <= h ) { int m = (l+h)/2; if (check(m, k)) { ans = m; l = m + 1; } else h = m - 1; } cout<<ans;}

src/1087 - Shortest Subsequence.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Shortest Subsequence
3+
Problem Link: https://cses.fi/problemset/task/1087
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;// #define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif string s; cin>>s; int l = 0, r = 0; bool a = 0, b = 0, c = 0, d = 0; int n = s.size(); string ans; while(l < n) { a = 0, b = 0, c = 0, d = 0; a += (s[l] == 'A'); b += (s[l] == 'C'); c += (s[l] == 'G'); d += (s[l] == 'T'); r = l+1; while(r < n && a+b+c+d != 4) { a += (s[r] == 'A'); b += (s[r] == 'C'); c += (s[r] == 'G'); d += (s[r] == 'T'); r++; } if (a+b+c+d == 4) ans += s[r-1]; l = r; } cerr<<a<<' '<<b<<' '<<c<<' '<<d; if (a+b+c+d == 4) ans += 'A'; else if (!a) ans += 'A'; else if (!b) ans += 'C'; else if (!c) ans += 'G'; else if (!d) ans += 'T'; cout<<ans;}

src/1134 - Prüfer Code.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Prüfer Code
3+
Problem Link: https://cses.fi/problemset/task/1134
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n; cin>>n; vector<int> v(n-2); set<int> s; for (int i = 1; i <= n; i++) s.insert(i); int a[n+1] = {0}; for (auto &i: v) { cin>>i; a[i]++; if (s.count(i)) s.erase(i); } for (int i = 0; i < n-2; i++) { int x = *s.begin(); s.erase(x); cout<<x<<' '<<v[i]<<endl; a[v[i]]--; if (a[v[i]] == 0) s.insert(v[i]); } cout<<*s.begin()<<' '<<*s.rbegin();}

src/1142 - Advertisement.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Advertisement
3+
Problem Link: https://cses.fi/problemset/task/1142
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n; cin>>n; int pre[n] = {0}, suf[n+1] = {0}, a[n]; int ans = 0; stack<pair<int, int>> q; for (int i = 0; i < n; i++) { cin>>a[i]; while(!q.empty() && q.top().first >= a[i]) q.pop(); int x = -1; if (!q.empty()) { x = q.top().second; } pre[i] = 1 + i-x-1; q.push({a[i], i}); } while(!q.empty()) q.pop(); for (int i = n-1; i >= 0; i--) { while(!q.empty() && q.top().first >= a[i]) q.pop(); int x = n; if (!q.empty()) { x = q.top().second; } suf[i] = 1 + x-i-1; q.push({a[i], i}); ans = max(ans, (pre[i]+suf[i]-1)*a[i]); } cout<<ans;}

src/1146 - Counting Bits.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Counting Bits
3+
Problem Link: https://cses.fi/problemset/task/1146
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n; cin>>n; int dp[50] = {0}; dp[0] = 1; for (int i = 1; i < 50; i++) dp[i] = 2*dp[i-1] + ((1LL<<(i-1)) - 1); int ans = 0; while(n>0) { int b = log2(n); ans += dp[b]; b = 1LL<<b; ans += n - b; n = n - b; } cout<<ans;}

src/1149 - String Removals.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: String Removals
3+
Problem Link: https://cses.fi/problemset/task/1149
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'const int mxN = 500005;const int md = 1e9+7;int dp[mxN];int ind[26];signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif string s; cin>>s; int n = s.size(); memset(ind, -1, sizeof ind); dp[0] = 1; for (int i = 1; i <= n; i++) { dp[i] = 2*dp[i-1]; if (ind[s[i-1]-'a'] >= 0) dp[i] -= dp[ind[s[i-1]-'a']]; dp[i] = (dp[i] + md)%md; ind[s[i-1]-'a'] = i-1; } cout<<(dp[n]-1+md)%md;}

src/1157 - Number Grid.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Number Grid
3+
Problem Link: https://cses.fi/problemset/task/1157
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int x,y; cin>>x>>y; cout<<((x-1)^(y-1));}

src/1159 - Book Shop II.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Book Shop II
3+
Problem Link: https://cses.fi/problemset/task/1159
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'int dp[100005];signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n, x; cin>>n>>x; vector<int> cost; vector<int> val; int a[n], b[n], c[n]; for (int i = 0; i < n; i++) cin>>a[i]; for (int i = 0; i < n; i++) cin>>c[i]; // converstion to 0-1 knapsack for (int i = 0; i < n; i++) { cin>>b[i]; int x = 1; while(b[i] - x >=0) { cost.push_back(x*a[i]); val.push_back(x*c[i]); b[i] -= x; x*=2; } if (b[i]) { cost.push_back(b[i]*a[i]); val.push_back(b[i]*c[i]); } } n = cost.size(); for (int i = 0; i < n; i++) { for (int j = x; j >0; j--) { if (j >= cost[i]) dp[j] = max(dp[j], val[i] + dp[j-cost[i]]); } } cout<<dp[x];}

src/1161 - Stick Divisions.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Stick Divisions
3+
Problem Link: https://cses.fi/problemset/task/1161
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int k,n; cin>>k>>n; int ans = 0; multiset<int> s; for (int i = 0; i < n; i++) { int x; cin>>x; s.insert(x); } while(s.size() > 1) { auto x = s.begin(); s.erase(x); auto y = s.begin(); s.erase(y); ans += *x+*y; s.insert(*x+*y); } cout<<ans;}

src/1188 - Bit Inversions.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Bit Inversions
3+
Problem Link: https://cses.fi/problemset/task/1188
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif string s; cin>>s; set<int> up, lo; int n = s.size(); int l = 0, r = 0; up.insert(0); lo.insert(0); multiset<int> ans; int mx = 0; while (l < n) { r = l + 1; while(r < n && s[r] == s[l]) r++; up.insert(r); lo.insert(-r); ans.insert(r-l); l = r; } int q; cin>>q; while(q--) { int x; cin>>x; int r = n, l = 0; if (x < n) r = *up.upper_bound(x); if (x > 1) l = -*lo.upper_bound(1-x); if (up.count(x) && up.count(x-1)) { if (r-x) ans.erase(ans.find(r-x)); if (x-1-l) ans.erase(ans.find(x-1-l)); ans.erase(ans.find(1)); if (x != n) up.erase(x), lo.erase(-x); if (x-1) up.erase(x-1), lo.erase(1-x); ans.insert(r-l); } else if (up.count(x-1)) { if (r-x+1) ans.erase(ans.find(r-x+1)); if (x-1-l) ans.erase(ans.find(x-1-l)); if (x-1) up.erase(x-1), lo.erase(1-x); if (x-l) ans.insert(x-l); if (r-x) ans.insert(r-x); up.insert(x); lo.insert(-x); } else if (up.count(x)) { if (r-x) ans.erase(ans.find(r-x)); if (x-l) ans.erase(ans.find(x-l)); if (x != n) up.erase(x), lo.erase(-x); if (x-1-l) ans.insert(x-1-l); if (r-x+1) ans.insert(r-x+1); up.insert(x-1); lo.insert(1-x); } else { if (r-l) ans.erase(ans.find(r-l)); up.insert(x); lo.insert(-x); up.insert(x-1); lo.insert(1-x); if (x-1-l) ans.insert(x-1-l); if (r-x) ans.insert(r-x); ans.insert(1); } cout<<*ans.rbegin()<<' '; }}

src/1628 - Meet in the Middle.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Meet in the Middle
3+
Problem Link: https://cses.fi/problemset/task/1628
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'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); }};unordered_map<int,int, custom_hash> mp;signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n, k; cin>>n>>k; vector<int> v1, v2; for (int i = 0; i < n/2; i++) { int x; cin>>x; v1.push_back(x); } for (int i = n/2; i < n; i++) { int x; cin>>x; v2.push_back(x); } int n1 = n/2; int n2 = n - n/2; for (int i = 0; i < (1<<n1); i++) { int x = 0, y = 0, st = i; while(st > 0) { if (st&1) x += v1[y]; y++; st>>=1; } mp[x]++; } int ans = 0; for (int i = 0; i < (1<<n2); i++) { int x = 0, y = 0, st = i; while(st > 0) { if (st&1) x+= v2[y]; y++; st>>=1; } if (mp.count(k-x)) ans += mp[k-x]; } cout<<ans;}

src/1653 - Elevator Rides.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Elevator Rides
3+
Problem Link: https://cses.fi/problemset/task/1653
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n, k; cin>>n>>k; int a[n]; for (int i = 0; i < n; i++) cin>>a[i]; pair<int, int> dp[1<<n]; dp[0] = {0, k+1}; for (int s = 1; s < (1<<n); s++) { dp[s] = {25, 0}; for (int i = 0; i < n; i++) { if (s>>i&1){ auto [c, w] = dp[s^(1<<i)]; if (w + a[i] > k) { c++; w = min(a[i], w); } else w += a[i]; dp[s] = min(dp[s], {c, w}); } } // cerr<<s<<' '<<dp[s].first<<' '<<dp[s].second<<endl; } cout<<dp[(1<<n)-1].first;}

src/1655 - Maximum Xor Subarray.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Maximum Xor Subarray
3+
Problem Link: https://cses.fi/problemset/task/1655
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'const int N = 30;int trie[200005*N][2], ct = 0;void insert(int x) { int node = 0; for (int i = N; i >= 0; i--) { int y = x>>i&1; if (!trie[node][y]) trie[node][y] = ++ct; node = trie[node][y]; }}int search(int x) { int ans = 0, node = 0; for (int i = N; i>=0; i--) { int y = x>>i&1; if (trie[node][y^1]) { node = trie[node][y^1]; ans += (1<<i); } else node = trie[node][y]; } return ans;}signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n; cin>>n; int xr = 0, mx = 0; insert(xr); for (int i = 0; i < n; i++) { int x; cin>>x; xr ^= x; insert(xr); mx = max(search(xr), mx); } cout<<mx;}

src/1670 - Swap Game.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Swap Game
3+
Problem Link: https://cses.fi/problemset/task/1670
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;// #define int long long#define endl '\n'unordered_set<int> vis;int go(int x1,int x2, int x3,int x4,int x5,int x6,int x7,int x8,int x9) { return x1*100000000 + x2*10000000 + x3*1000000 + x4*100000 + x5*10000 + x6*1000 + x7*100 + x8*10 + x9;}signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int x = 0; for (int i = 0; i < 9; i++) { int a; cin>>a; x = x*10+a; } queue<pair<int, int>> q; q.push({123456789, 0}); vis.insert(123456789); while(!q.empty()) { auto [s, c] = q.front(); q.pop(); if (s == x) return cout<<c, 0; int x9 = s%10; s/=10; int x8 = s%10; s/=10; int x7 = s%10; s/=10; int x6 = s%10; s/=10; int x5 = s%10; s/=10; int x4 = s%10; s/=10; int x3 = s%10; s/=10; int x2 = s%10; s/=10; int x1 = s%10; s/=10; int n1 = go(x2, x1, x3, x4, x5, x6, x7, x8, x9); int n2 = go(x1, x3, x2, x4, x5, x6, x7, x8, x9); int n3 = go(x1, x2, x3, x5, x4, x6, x7, x8, x9); int n4 = go(x1, x2, x3, x4, x6, x5, x7, x8, x9); int n5 = go(x1, x2, x3, x4, x5, x6, x8, x7, x9); int n6 = go(x1, x2, x3, x4, x5, x6, x7, x9, x8); int n7 = go(x4, x2, x3, x1, x5, x6, x7, x8, x9); int n8 = go(x1, x2, x3, x7, x5, x6, x4, x8, x9); int n9 = go(x1, x5, x3, x4, x2, x6, x7, x8, x9); int n10 = go(x1, x2, x3, x4, x8, x6, x7, x5, x9); int n11 = go(x1, x2, x6, x4, x5, x3, x7, x8, x9); int n12 = go(x1, x2, x3, x4, x5, x9, x7, x8, x6); if (!vis.count(n1)) { q.push({n1, c+1}); vis.insert(n1); } if (!vis.count(n2)) { q.push({n2, c+1}); vis.insert(n2); } if (!vis.count(n3)) { q.push({n3, c+1}); vis.insert(n3); } if (!vis.count(n4)) { q.push({n4, c+1}); vis.insert(n4); } if (!vis.count(n5)) { q.push({n5, c+1}); vis.insert(n5); } if (!vis.count(n6)) { q.push({n6, c+1}); vis.insert(n6); } if (!vis.count(n7)) { q.push({n7, c+1}); vis.insert(n7); } if (!vis.count(n8)){ q.push({n8, c+1}); vis.insert(n8); } if (!vis.count(n9)){ q.push({n9, c+1}); vis.insert(n9); } if (!vis.count(n10)){ q.push({n10, c+1}); vis.insert(n10); } if (!vis.count(n11)){ q.push({n11, c+1}); vis.insert(n11); } if (!vis.count(n12)){ q.push({n12, c+1}); vis.insert(n12) ; } } }

src/1697 - Chess Tournament.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Chess Tournament
3+
Problem Link: https://cses.fi/problemset/task/1697
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n; cin>>n; int a[n+1]; set<pair<int,int>> st; int sm = 0; for (int i = 1; i <= n; i++) { cin>>a[i]; sm += a[i]; if (a[i]) st.insert({a[i], i}); } sm; vector<pair<int,int>> ans; while(st.size()>=2) { auto [x1, i1] = *st.begin(); st.erase({x1, i1}); vector<pair<int,int>> s; while(x1>0 && st.size()) { auto [x2, i2] = *st.rbegin(); st.erase({x2, i2}); ans.push_back({i1, i2}); x1--, x2--; s.push_back({x2, i2}); } for (auto [i, j]: s) { if (i>0) st.insert({i, j}); } } if (2*ans.size() == sm) { cout<<ans.size()<<endl; for (auto [i, j]: ans) { cout<<i<<' '<<j<<endl; } } else cout<<"IMPOSSIBLE";}

src/1702 - Tree Traversals.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
Problem Name: Tree Traversals
3+
Problem Link: https://cses.fi/problemset/task/1702
4+
Author: Sachin Srivastava (mrsac7)
5+
*/
6+
#include<bits/stdc++.h>using namespace std;#define int long long#define endl '\n'vector<int> pre, in;int a[100005];void post(int l1, int h1, int l2, int h2) { if (l1 > h1) return; int i = a[pre[l1]]; post(l1+1, l1+1+i-l2-1, l2, i); post(l1+1+i-l2, h1, i+1, h2); cout<<in[i]<<' ';}signed main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif int n; cin>>n; for (int i = 0; i < n; i++) { int x; cin>>x; pre.push_back(x); } for (int i = 0; i < n; i++) { int x; cin>>x; in.push_back(x); a[x] = i; } post(0,n-1,0,n-1);}

0 commit comments

Comments
 (0)