Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FLIPKART
- copy
- #include<bits//stdc++.h>
- using namespace std;
- #define INF 1e8
- bool check(char a, char b)
- {
- if(a==b)
- return 1;
- if(a=='a'&&b=='o')
- return 1;
- if(a=='o'&&b=='a')
- return 1;
- if(a=='t'&&b=='l')
- return 1;
- if(a=='l'&&b=='t')
- return 1;
- return 0;
- }
- int main()
- {
- int n;
- cin>>n;
- string draw_string;
- cin>>draw_string;
- string ticket[n];
- for(int i=0;i<n;i++)
- {
- cin>>ticket[i];
- }
- int k;
- cin>>k;
- int ans=0;
- for(int i=0;i<n;i++)
- {
- int dp[201][201][2][2];
- int sz2=draw_string.size(),sz1=ticket[i].size();
- for(int j=0;j<sz1;j++)
- {
- dp[j][sz2][0][0]=INF;
- dp[j][sz2][1][0]=INF;
- dp[j][sz2][0][1]=INF;
- dp[j][sz2][1][1]=INF;
- }
- dp[sz1-1][sz2][0][1]=0;
- dp[sz1-1][sz2][1][1]=0;
- for(int j=0;j<=sz2;j++)
- {
- dp[sz1][j][0][0]=0;
- dp[sz1][j][1][0]=0;
- dp[sz1][j][0][1]=0;
- dp[sz1][j][1][1]=0;
- }
- for(int j=sz1-1;j>=0;j--)
- {
- for(int k=sz2-1;k>=0;k--)
- {
- if(check(ticket[i][j],draw_string[k]))
- {
- dp[j][k][0][0]=min(dp[j+1][k+1][1][0],dp[j][k+1][0][0]);
- dp[j][k][1][0]=dp[j+1][k+1][1][0];
- }
- else
- {
- dp[j][k][0][0]=dp[j][k+1][0][0];
- dp[j][k][1][0]=1+dp[j][k+1][1][0];
- }
- if(check(ticket[i][j],draw_string[k]))
- {
- dp[j][k][0][1]=min(dp[j+1][k+1][1][1],dp[j][k+1][0][1]);
- dp[j][k][1][1]=dp[j+1][k+1][1][1];
- }
- else
- {
- dp[j][k][0][1]=min(dp[j][k+1][0][1],dp[j+1][k+1][0][0]);
- dp[j][k][1][1]=min(1+dp[j][k+1][1][1],dp[j+1][k+1][1][0]);
- }
- }
- }
- if(dp[0][0][0][1]<=k)
- ans++;
- }
- cout<<ans<<endl;
- return 0;
- }
- ///////////////////////////////////////////
- bool cmp(vector<int>& a, vector<int>& b) {return a[1] < b[1];}
- class Solution {
- public:
- int findMinArrowShots(vector<vector<int>>& segments) {
- sort(segments.begin(), segments.end(), cmp);
- int ans = 0, arrow = 0;
- for (int i = 0; i < segments.size(); i ++) {
- if (ans == 0 || segments[i][0] > arrow) {
- ans ++;
- arrow = segments[i][1];
- }
- }
- return ans;
- }
- };
- /////////////////////////////
- #include<bits/stdc++.h>
- using namespace std;
- void solve() {
- int n; cin >> n;
- vector<int> v(n);
- unordered_map<int, int> um;
- for (int i = 0; i < n; i++) {
- cin >> v[i];
- um[v[i]]++;
- }
- int pos_sum = 0, neg_sum = 0;
- for (auto &x : um) {
- if (x.second == 1) {
- if (x.first > 0) pos_sum += x.first;
- else neg_sum += x.first;
- }
- }
- cout << pos_sum - neg_sum << "\n";
- }
- int main(){
- solve();
- return 0;
- }
- ////////////////////////////
- #include<bits/stdc++.h>
- using namespace std;
- vector<vector<int>>dp;
- int solve(vector<vector<int>>&city, int n) {
- for(int i=1;i<=n;i++) {
- dp[i][0] = city[i-1][0]+ *max_element(dp[i-1].begin(),dp[i-1].end());
- dp[i][1] = city[i-1][1] + dp[i-1][2];
- dp[i][2] = *max_element(dp[i-1].begin(),dp[i-1].end()); //max salary till i-1th
- }
- return *max_element(dp[n].begin(),dp[n].end());
- }
- int main() {
- int n,x,y;
- cin>>n;
- vector<vector<int>>city(n,vector<int>(2));
- dp.resize(n+1,vector<int>(3));
- for(int i=0;i<n;i++) {
- cin>>city[i][0]>>city[i][1];
- }
- cout<<solve(city, n)<<endl;
- }
- ////////////////////////
- #include <bits/stdc++.h>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <cstring>
- #include <chrono>
- #include <complex>
- #define endl "\n"
- #define ll long long int
- #define vi vector<int>
- #define vll vector<ll>
- #define vvi vector < vi >
- #define pii pair<int,int>
- #define pll pair<long long, long long>
- #define mod 1000000007
- #define inf 1000000000000000001;
- #define all(c) c.begin(),c.end()
- #define mp(x,y) make_pair(x,y)
- #define mem(a,val) memset(a,val,sizeof(a))
- #define eb emplace_back
- #define f first
- #define s second
- using namespace std;
- void dfs(int i, int par, vector<bool>&visited, vector<vector<int>>&adj)
- {
- if(visited[i] == true)
- {
- visited[par] = true;
- cout<<i<<par<<" ";
- return;
- }
- for(int j : adj[i])
- {
- dfs(j,i,visited,adj);
- }
- }
- void solve(){
- int n, p, projects, temp;
- cin >> n >> p;
- vector<vector<int>> adj(n+1);
- for(int i = 1; i <= n; i++) {
- cin >> temp;
- adj[i].push_back(temp);
- }
- vector<bool>visited(n+1,false);
- visited[0] = true;
- for(int i=0; i<p; i++)
- {
- cin >> projects;
- dfs(projects,projects,visited,adj);
- }
- for(int i=1; i<=n; i++)
- {
- if(visited[i] == false);
- // cout << i << " ";
- }
- }
- int main()
- {
- std::ios::sync_with_stdio(false);
- int T=1;
- // cin >> T;
- while(T--){
- solve();
- }
- return 0;
- }
- /////////////////////
- #include<bits/stdc++.h>
- #define int long long
- using namespace std ;
- signed main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL) ;
- int n , k ;
- cin >> n >> k ;
- vector<int> a(n) ;
- for(int i = 0; i < n ; ++i)
- cin >> a[i] ;
- int lo = 1, hi = 1e10 ;
- while(lo < hi)
- {
- int mid = (lo + hi)/2 , r = 0 ;
- for(int i = 0 ; i < n ; ++i)
- {
- r += ceil((double)a[i]/mid) ;
- }
- if(r <= k)
- {
- hi= mid ;
- }
- else
- lo = mid +1 ;
- }
- cout << hi ;
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- // GRAPH ALGOS
- // DIJKSTRA
- #include<bits/stdc++.h>
- using namespace std;
- vector<int> dijkstra(int start, int n, vector<vector<pair<int,int>>> adj){
- priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
- vector<int> dist(n,INT_MAX);
- pq.push(make_pair(0,start));
- dist[start] = 0;
- while(!pq.empty()) {
- int dst = pq.top().first;
- int prev = pq.top().second;
- pq.pop();
- for(auto it: adj[prev]){
- int next = it.first;
- int nextDist = it.second;
- if(dist[next] > dist[prev] + nextDist){
- dist[next] = dist[prev] + nextDist;
- pq.push(make_pair(nextDist,next));
- }
- }
- }
- return dist;
- }
- int main(){
- int n;
- cin>>n;
- vector<vector<pair<int,int>>> adj(n, vector<pair<int,int>>());
- for(int i=0; i<n-1; i++){
- int l,r,w;
- cin>>l>>r>>w;
- adj[l-1].push_back({r-1,w});
- adj[r-1].push_back({l-1,w});
- }
- int x,y,z;
- cin>>x>>y>>z;
- vector<int> xDis;
- xDis=dijkstra(x-1, n, adj);
- for(auto a:xDis) cout<<a<<" ";
- return 0;
- }
- // FLOYD WARSHALL
- //Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of vertices in a weighted graph. This algorithm works for both the directed and undirected weighted graphs. But, it does not work for the graphs with negative cycles
- void floydWarshall(int graph[][V])
- {
- for (i = 0; i < V; i++)
- for (j = 0; j < V; j++)
- dist[i][j] = graph[i][j];
- for (k = 0; k < V; k++) {
- // Pick all vertices as source one by one
- for (i = 0; i < V; i++) {
- // Pick all vertices as destination for the
- // above picked source
- for (j = 0; j < V; j++) {
- // If vertex k is on the shortest path from
- // i to j, then update the value of
- // dist[i][j]
- if (dist[i][j] > (dist[i][k] + dist[k][j])
- && (dist[k][j] != INF
- && dist[i][k] != INF))
- dist[i][j] = dist[i][k] + dist[k][j];
- }
- }
- }
- // Print the shortest distance matrix
- printSolution(dist);
- }
- // KRUSKAL
- bool sortbythird(vector<int> &A, vector<int> &B){
- return A[2] < B[2];
- }
- int findParent ( int A, vector<int> &parent ) {
- if( parent[A] == A ) return A;
- else return findParent( parent[A], parent );
- }
- int solve(int A, vector<vector<int> > &B) {
- sort( B.begin(), B.end(), sortbythird );
- vector<int> parent;
- for(int i = 0; i < A; i++){
- parent.push_back(i);
- }
- int i = 0, count = 0;
- int ans = 0;
- while ( i < B.size() ) {
- if( findParent( B[i][0] - 1, parent ) != findParent( B[i][1] - 1, parent ) ) {
- ans += B[i][2];
- parent[findParent( B[i][0] - 1, parent )] = parent[findParent( B[i][1] - 1, parent )];
- count++;
- }
- if( count == A-1 ) break;
- i++;
- }
- return ans;
- }
- // TOPOLOGICAL SORTING
- void Graph::topologicalSortUtil(int v, bool visited[],
- stack<int>& Stack)
- {
- // Mark the current node as visited.
- visited[v] = true;
- // Recur for all the vertices
- // adjacent to this vertex
- list<int>::iterator i;
- for (i = adj[v].begin(); i != adj[v].end(); ++i)
- if (!visited[*i])
- topologicalSortUtil(*i, visited, Stack);
- // Push current vertex to stack
- // which stores result
- Stack.push(v);
- }
- // The function to do Topological Sort.
- // It uses recursive topologicalSortUtil()
- class Solution {
- public:
- vector<int> findOrder(int N, vector<vector<int>>& P) {
- vector<vector<int>> G(N); // {prerequisite-course : [list of next courses]}
- vector<int> ans, indegree(N); // indegree[i] denotes number of prerequisite courses for ith course
- for(auto& pre : P)
- G[pre[1]].push_back(pre[0]), // forming adjacency list graph
- indegree[pre[0]]++;
- queue<int> q;
- for(int i = 0; i < N; i++)
- if(indegree[i] == 0) q.push(i); // we can start with courses having no prequisites
- while(size(q)) {
- auto cur = q.front(); q.pop();
- ans.push_back(cur); // cur has no remaining pre courses, so we can take it now
- for(auto nextCourse : G[cur])
- if(--indegree[nextCourse] == 0) // if there's a next course having 0 prequisite remaining,
- q.push(nextCourse); // then we can take it
- }
- if(size(ans) == N) return ans; // ordering exists where all courses can be finished
- return {};
- }
- };
- // Level Order Traversal
- vector <vector <int>> tree(n);
- int k = 0;
- for(int i = 0; i < nums.size(); i++){
- int m = pow(2, k);
- for(int j = i; j < i + m; j++) tree[k].push_back(nums[j]);
- k++;
- i += m-1;
- }
- // SIEVE
- #include<bits/stdc++.h>
- using namespace std;
- vector<int> sieve(int n){
- vector<bool> is_prime(n+1, true);
- is_prime[0] = is_prime[1] = false;
- for (int i = 2; i <= n; i++) {
- if (is_prime[i] && (long long)i * i <= n) {
- for (int j = i * i; j <= n; j += i)
- is_prime[j] = false;
- }
- }
- vector<int> ans;
- for(int i=0; i<=n; i++){
- if(is_prime[i]) ans.push_back(i);
- }
- return ans;
- }
- // LIS
- // Input --> h
- vector<int> ans;
- for(int i=0; i<h.size(); i++){
- int idx=lower_bound(ans.begin(), ans.end(), h[i])-ans.begin();
- if(idx==ans.size())ans.push_back(h[i]);
- else ans[idx]=h[i];
- }
- return ans.size();
- // WIERD ELEVATORS
- #include<bits/stdc++.h>
- using namespace std;
- int eGcd(int a, int b){
- if(a==0) return b;
- else return eGcd(b%a, a);
- }
- int main(){
- int x,y,m;
- cin>>x>>y>>m;
- int hcf=eGcd(x, y);
- int floorX=x/hcf;
- int floorY=y/hcf;
- int ans=0;
- if(2>=m) cout<<-1<<'\n';
- else{
- while (floorX%2 == 0)
- {
- ans++;
- floorX /=2;
- }
- for (int i=3; i*i<=floorX; i = i+2)
- {
- if(i>=m) cout<<-1<<'\n';
- else{
- while (floorX%i ==0)
- {
- ans++;
- floorX /=i;
- }
- }
- }
- if(floorX>2) ans++;
- while (floorY%2 == 0)
- {
- ans++;
- floorY /=2;
- }
- for (int i=3; i*i<=floorY; i = i+2)
- {
- if(i>=m) cout<<-1<<'\n';
- else{
- while (floorY%i ==0)
- {
- ans++;
- floorY /=i;
- }
- }
- }
- if(floorY>2) ans++;
- cout<<ans<<" "<<eGcd(x,y);
- }
- return 0;
- }
- // FRONTEND AND BACKEND
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n, m;
- vector <int> f = {};
- vector <int> b = {};
- int k = f.size();
- vector <int> fd;
- vector <int> bd;
- int s1 = 0, s2 = 0;
- for(int i = 0; i < k; i++){
- if (f[i] <= b[i]){
- fd.push_back(b[i]-f[i]);
- s1 += f[i];
- }
- else{
- bd.push_back(f[i]-b[i]);
- s2 += b[i];
- }
- }
- if ( (fd.size() == n) && (bd.size() == m)) return (s1 + s2);
- if ( fd.size() > n){
- sort(fd.begin(), fd.end());
- for(int i =0; i < fd.size() -n; i++) s1 += fd[i];
- return s1 + s2;
- }
- if ( bd.size() > m){
- sort(bd.begin(), bd.end());
- for(int i =0; i < bd.size() -m; i++) s1 += bd[i];
- return s1 + s2;
- }
- }
- // RUSSIAN DOLL ENVELOPE
- #include<bits/stdc++.h>
- using namespace std;
- bool comp(vector<int> &a, vector<int> &b){
- if(a[0]!=b[0]) return a[0]<b[0];
- return a[1]>b[1];
- }
- int solve(vector<vector<int>> v){
- sort(v.begin(), v.end(), comp);
- vector<int> h;
- for(auto x:v){
- h.push_back(x[1]);
- }
- vector<int> ans;
- for(int i=0; i<h.size(); i++){
- int idx=lower_bound(ans.begin(), ans.end(), h[i])-ans.begin();
- if(idx==ans.size())ans.push_back(h[i]);
- else ans[idx]=h[i];
- }
- return ans.size();
- }
- int main() {
- int n;
- cin>>n;
- vector<vector<int>> v(n, vector<int>(2));
- for(int i=0; i<n; i++) cin>>v[i][0]>>v[i][1];
- cout<<solve(v);
- return 0;
- }
- // BITS SEGMENT TREE
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- class Segment_tree{
- public:
- int arr[1000]={0};
- int seg[4*1000]={0};
- Segment_tree(){
- }
- void build(int idx, int low,int high){
- if(low==high){
- seg[idx]=arr[low];
- return;
- }
- int mid=(low+high)/2;
- build(2*idx+1,low,mid);
- build(2*idx+2,mid+1,high);
- seg[idx]=max(seg[2*idx+1],seg[2*idx+2]);
- }
- int query(int idx,int low,int high,int l,int r){
- if(high < l || r< low)return 0;
- else if(l<= low && high <=r) return seg[idx];
- int mid=(low+high)/2;
- int a=query(2*idx+1,low,mid,l,r);
- int b=query(2*idx+2,mid+1,high,l,r);
- return max(a,b);
- }
- void update(int idx,int low,int high,int i,int val){
- if(low==high){
- seg[idx]=val;return;
- }
- int mid=(low+high)/2;
- if(i<=mid){
- update(2*idx+1,low,mid,i,val);
- }else{
- update(2*idx+2,mid+1,high,i,val);
- }
- seg[idx]=max(seg[2*idx+1],seg[2*idx+2]);
- }
- };
- int main()
- {
- ll n,q;
- cin>>n>>q;
- vector<int> v(n);
- for(int i=0;i<n;i++)cin>>v[i];
- vector<Segment_tree> res(32);
- for(int i=0;i<n;i++){
- int x=__builtin_popcount(v[i]);
- res[x].update(0,0,n-1,i,v[i]);
- }
- while(q--){
- ll l,r,x;
- cin>>l>>r>>x;
- l--;r--;
- int ans=res[x].query(0,0,n-1,l,r);
- cout<<ans<<'\n';
- }
- return 0;
- }
- // MULTIPLYING STACKS
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- stack<int> s;
- int q;
- cin>>q;
- int sum=0, ss=0;
- for(int i=0; i<q; i++){
- int n, val;
- cin>>n>>val;
- if(n==0){
- s.push(val);
- sum+=val;
- ss+=pow(val,2);
- }
- else {
- int v=s.top();
- s.pop();
- sum-=v;
- ss-=pow(v,2);
- }
- cout<<sum*ss<<'\n';
- }
- return 0;
- }
- // MAXIMIZE FRIENDSHIP
- #include<bits/stdc++.h>
- using namespace std;
- int dfs(int i, vector<vector<int>> v, vector<int> &vis){
- vis[i]=1;
- int cnt=1;
- for(auto x:v[i]){
- if(!vis[x]) cnt+=dfs(x, v, vis);
- }
- return cnt;
- }
- int solve(int n, vector<vector<int>> v){
- int ans=0;
- vector<int> vis(n, 0);
- for(int i=0; i<n; i++){
- if(!vis[i]) {
- ans=max(ans, dfs(i, v, vis));
- }
- }
- return ans;
- }
- int main(){
- int n;
- cin>>n;
- int m;
- cin>>m;
- vector<vector<int>> v(n);
- vector<int> a(n);
- for(int i=0; i<m; i++){
- int l,r;
- cin>>l>>r;
- l--;
- r--;
- v[l].push_back(r);
- v[r].push_back(l);
- }
- for(int i=0; i<n; i++)cin>>a[i];
- int maxSize=solve(n, v);
- sort(a.rbegin(),a.rend());
- int p=0;
- for(int i=0; i<maxSize; i++) {
- p+=a[i];
- }
- cout<<(p*(p-1))/2;
- return 0;
- }
- // TEACHERS AND STUDENTS
- #include<bits/stdc++.h>
- using namespace std;
- int main() {
- int n;
- cin>>n;
- vector< pair< int ,int > >T(n),S(n);
- for(int i=0;i<n;i++){
- cin>>S[i].first>>S[i].second;
- }
- for(int i=0;i<n;i++){
- cin>>T[i].first>>T[i].second;
- }
- sort(S.begin(),S.end());
- sort(T.begin(),T.end());
- set< pair< pair<int ,int > ,int>>st;
- for(int i=0;i<n;i++){
- st.insert({{T[i].second,T[i].first},i});
- }
- int i=0,j=0;
- int ans=0;
- for(i=0;i<n;i++){
- while(T[j].first<=S[i].first){
- if(T[j].first == -1){j++;continue;}
- st.erase(st.find({{T[j].second,T[j].first},j}));
- j++;
- }
- auto it = st.upper_bound({{S[i].second,S[i].first},0});
- if(it!=st.end()){
- ans++;
- // cout<<S[i].first<<S[i].second<<" "<<T[(*it).second].first<<T[(*it).second].second<<'\n';
- T[(*it).second].first=-1;
- st.erase(it);
- }
- }
- cout<<ans<<endl;
- }
- // ODD EVEN JUMPS
- #include<bits/stdc++.h>
- using namespace std;
- vector<int> solve(vector<int>& arr){
- size_t n = arr.size();
- vector<int> smallest_geq(n, -1);
- vector<int> largest_leq(n, -1);
- map<int,int> mp;
- for(int i = n - 1; i >= 0; i --)
- {
- auto it = mp.lower_bound(arr[i]);
- if(it != mp.end())
- {
- smallest_geq[i] = it->second;
- if(it->first == arr[i])
- {
- largest_leq[i] = it->second;
- }
- }
- if(largest_leq[i] == -1 && it != mp.begin())
- {
- it--;
- largest_leq[i] = it->second;
- }
- mp[arr[i]] = i;
- }
- vector<vector<char>> dp(n, vector<char>(2, 0));
- dp[n-1] = {1, 1};
- vector<int> ans;
- ans.push_back(n-1);
- for(int i = n - 2; i >= 0; i --)
- {
- //odd_numbered
- {
- int j = smallest_geq[i];
- dp[i][0] = j == -1 ? 0 : dp[j][1];
- }
- //even_numbered
- {
- int j = largest_leq[i];
- dp[i][1] = j == -1 ? 0 : dp[j][0];
- }
- if(dp[i][0])ans.push_back(i);
- }
- return ans;
- }
- int main(){
- int n;
- cin>>n;
- vector<int> v(n);
- for(int i=0; i<n; i++) cin>>v[i];
- vector<int> ans=solve(v);
- reverse(ans.begin(),ans.end());
- for(auto x:ans) cout<<x<<" ";
- return 0;
- }
- // MATRIX KNAPSACK
- #include<bits/stdc++.h>
- using namespace std;
- int dp[101][101][101];
- int helper(vector<vector<int>>& reward, vector<vector<int>>& prob, int i, int j, int currProb, int thresh){
- if(currProb>=thresh) return -1;
- if(i>=reward.size() || j>=reward[0].size()) return -1;
- if(i==reward.size()-1 and j==reward[0].size()-1){
- return reward[i][j];
- }
- if(dp[i][j][currProb]!=-1) return dp[i][j][currProb];
- int ma = max(helper(reward, prob,i+1,j,currProb+prob[i][j],thresh),helper(reward,prob,i,j+1,currProb+prob[i][j],thresh));
- if(ma==-1) return dp[i][j][currProb]=-1;
- return dp[i][j][currProb] = reward[i][j]+ma;
- }
- int maxReward(vector<vector<int>>& reward, vector<vector<int>>& prob, int threshold){
- memset(dp,-1,sizeof(dp));
- return helper(reward,prob,0,0,0,threshold);
- }
- int main(){
- int n;
- cin>>n;
- vector<vector<int>> reward (n,vector<int>(n));
- vector<vector<int>> prob (n,vector<int>(n));
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- cin>>reward[i][j];
- }
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- cin>>prob[i][j];
- }
- }
- int th;
- cin>>th;
- cout<<maxReward(reward,prob,th);
- for(int i=0;i<=n;i++){
- for(int j=0;j<=n;j++){
- for(int k=0;k<=th;k++){
- cout<<dp[i][j][k]<<" ";
- }
- cout<<'\n';
- }
- cout<<'\n';
- }
- return 0;
- }
- /////////////////////////////////////////////////////// ATLASSIAN /////////////////////////////////////////////////
- // super stack
- #include <bits/stdc++.h>
- using namespace std;
- void superStack(vector<string> ops) {
- int n = ops.size();
- vector<int> a(n), b(n);
- int k = 0;
- for (string op : ops) {
- stringstream strin(op);
- string optype;
- strin >> optype;
- if (optype == "push") {
- int x;
- strin >> x;
- a[k++] = x;
- }
- if (optype == "pop") {
- if (--k) b[k - 1] += b[k];
- b[k] = 0;
- }
- if (optype == "inc") {
- int x, y;
- strin >> x >> y;
- b[x - 1] += y;
- }
- if (not k) {
- cout << "EMPTY";
- }
- else {
- cout << a[k - 1] + b[k - 1];
- }
- cout << " ";
- }
- }
- int main() {
- vector<string> operations = {
- "push 4", "pop", "push 3", "push 5", "inc 2 1",
- "pop", "push 5", "push -1", "inc 1 5", "pop"
- };
- superStack(operations);
- return 0;
- }
- // MINIMISE EFFORTS
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main() {
- ll n;
- cin>>n;
- ll a[n+1];
- ll i = 1 ;
- while(i<=n){
- cin>>a[i];
- i++;
- }
- ll k;
- cin>>k;
- sort(a+1,a+n+1);
- ll pref[n+1]={0};
- i = 1 ;
- while(i<=n){
- pref[i] = pref[i-1] + a[i];
- i++;
- }
- ll my = 1e18 ;
- i = 1 ;
- while(i<=n-k+1){
- ll start = i ;
- ll d = k + i - 1 ;
- if(k%2!=0){
- ll md = (start+d)/2;
- //start-->md-1
- ll l1 = abs(md-start);
- ll sum1 = pref[md-1]-pref[start-1]; //pref[y]-pref[x-1]
- ll ans1 = abs(l1*a[md] - sum1);
- //md+1-->d
- ll l2 = abs(d-md);
- ll sum2 = pref[d]-pref[md]; //pref[y]-pref[x-1]
- ll ans2 = abs(l2*a[md] - sum2);
- ll final_ans = ans1 + ans2 ;
- my = min(my,final_ans);
- } else {
- ll md1 = (start+d)/2;
- ll md2 = md1+1;
- //start-->md-1
- ll l1 = abs(md1-start);
- ll sum1 = pref[md1-1]-pref[start-1]; //pref[y]-pref[x-1]
- ll ans1 = abs(l1*a[md1] - sum1);
- //md+1-->d
- ll l2 = abs(d-md1);
- ll sum2 = pref[d]-pref[md1]; //pref[y]-pref[x-1]
- ll ans2 = abs(l2*a[md1] - sum2);
- ll final_ans = ans1 + ans2 ;
- my = min(my,final_ans);
- //cout<<i<<" "<<final_ans;
- //cout<<"\n";
- //start-->md-1
- l1 = abs(md2-start);
- sum1 = pref[md2-1]-pref[start-1]; //pref[y]-pref[x-1]
- ans1 = abs(l1*a[md2] - sum1);
- //md+1-->d
- l2 = abs(d-md2);
- sum2 = pref[d]-pref[md2]; //pref[y]-pref[x-1]
- ans2 = abs(l2*a[md2] - sum2);
- final_ans = ans1 + ans2 ;
- my = min(my,final_ans);
- // cout<<i<<" "<<final_ans;
- //cout<<"\n";
- }
- i++;
- }
- cout<<my
- return 0 ;
- }
- // MAXIMUM VALUE COUNT
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main(){
- int n;
- cin>>n;
- int q;
- cin>>q;
- vector<int> nums(n);
- vector<int> queries(q);
- vector<int> ans(n);
- for(int i=0; i<n; i++){
- cin>>nums[i];
- }
- for(int i=0; i<q; i++){
- cin>>queries[i];
- }
- int ctr=0;
- int maxVal=0;
- for(int i=n-1; i>=0; i--){
- if(nums[i]>maxVal){
- maxVal=nums[i];
- ctr=0;
- ctr++;
- }
- else if(nums[i]==maxVal) {
- ctr++;
- }
- ans[i]=ctr;
- }
- for(int i=0; i<q; i++){
- cout<<ans[queries[i]-1]<<" ";
- }
- }
- // EVALUATE EXPRESSION
- #include <bits/stdc++.h>
- using namespace std;
- // Function to evaluate the logical expression
- char logicalExpressionEvaluation(string str)
- {
- stack<char> arr;
- // traversing string from the end.
- for (int i = str.length() - 1; i >= 0; i--)
- {
- if (str[i] == '[')
- {
- vector<char> s;
- while (arr.top() != ']')
- {
- s.push_back(arr.top());
- arr.pop();
- }
- arr.pop();
- // for NOT operation
- if (s.size() == 3)
- {
- s[2] == '1' ? arr.push('0') : arr.push('1');
- }
- // for AND and OR operation
- else if (s.size() == 5)
- {
- int a = s[2] - '0', b = s[4] - '0', c;
- s[0] == '&' ? c = a && b : c = a || b;
- arr.push((char)c + '0');
- }
- }
- else
- {
- arr.push(str[i]);
- }
- }
- return arr.top();
- }
- // Driver code
- int main()
- {
- int n;
- cin>>n;
- while(n--){
- string s;
- cin>>s;
- cout<<s;
- cout<<logicalExpressionEvaluation(s) << endl;
- }
- return 0;
- }
- // DOMINOES TILLING
- #include<bits/stdc++.h>
- using namespace std;
- long long mod=1e9+7;
- long long find(int n) {
- vector<long long> v(n+1,0);
- v[1]=2;
- v[2]=9;
- for(int i=3;i<=n;i++) {
- v[i]= (4%mod *v[i-1]%mod)%mod - v[i-2] + 2*pow(-1,i);
- v[i]=(v[i]+mod)%mod;
- }
- return v[n];
- }
- int main() {
- for(int i=1;i<=20;i++) {
- cout<<find(i)<<" ";
- }
- }
- // CUTTING METAL SURPLUS
- #include <bits/stdc++.h>
- using namespace std;
- int get_profit(vector<int> &rods, int sz, int cpc, int sl) {
- int profit = 0;
- for(int r : rods) {
- int rodProf = 0;
- if(r%sz == 0) {
- rodProf += ((r/sz) * sz * sl) - (r/sz - 1) * cpc;
- } else {
- rodProf += ((r/sz) * sz * sl) - (r/sz) * cpc;
- }
- if(rodProf > 0)
- profit += rodProf;
- }
- if(profit == 1913) {
- cout << sz << endl;
- }
- return profit;
- }
- int main() {
- int n;
- int cpc , sl;
- cin >> cpc >> sl;
- cin >> n;
- vector<int> v(n);
- int maxlen = 0;
- for(int i=0; i<n; i++) {
- cin >> v[i];
- maxlen = max(maxlen, v[i]);
- }
- int ret = 0;
- for(int sz=1; sz<=maxlen; sz++) {
- int prof = get_profit(v, sz, cpc, sl);
- ret = max(prof, ret);
- }
- cout <<ret << endl;
- return 0;
- }
- // SEGMENT TREE MAX VAL WITH COUNT
- #include<bits/stdc++.h>
- using namespace std;
- vector<pair<int, int> > t;
- pair<int, int> combine(pair<int, int> a, pair<int, int> b) {
- if (a.first > b.first)
- return a;
- if (b.first > a.first)
- return b;
- return make_pair(a.first, a.second + b.second);
- }
- void build(vector<int> &a, int v, int tl, int tr){
- if(tl == tr) t[v]=make_pair(a[tl], 1);
- else {
- int tm=(tl+tr)/2;
- build(a, v*2+1, tl, tm);
- build(a, v*2+1+1, tm+1, tr);
- t[v]=combine(t[v*2+1], t[v*2+1+1]);
- }
- }
- pair<int, int> get_max( int v, int tl, int tr, int l, int r){
- if(l > r) return {INT_MIN,INT_MIN};
- if(l==tl && r==tr) return t[v];
- int tm=(tl+tr)/2;
- return combine(get_max(v*2+1, tl, tm, l, min(r, tm)), get_max(v*2+1+1, tm+1, tr, max(l, tm+1), r));
- }
- void update(int v, int tl, int tr, int pos, int new_val){
- if(tl==tr) t[v]=make_pair(new_val, 1);
- else{
- int tm=(tl+tr)/2;
- if(pos<=tm) update( v*2+1, tl, tm, pos, new_val);
- else update(v*2+2, tm+1, tr, pos, new_val);
- t[v]=combine(t[v*2+1], t[v*2+1+1]);
- }
- }
- int main() {
- int num, q;
- cin >> num>>q; //Reading input from STDIN
- vector<int> a(num);
- vector<pair<int,int>> temp(4*num, {0,0});
- t=temp;
- for(int i=0; i<num; i++) cin>>a[i];
- build(a, 0, 0, num-1);
- for(int i=0; i<q; i++){
- char c;
- cin>>c;
- int l, r;
- cin>>l>>r;
- if(c=='q') cout<<get_max(0, 0, num-1, l-1, r-1).first<<get_max(0, 0, num-1, l-1, r-1).second<<'\n';
- else update(0, 0, num-1, l-1, r);
- }
- return 0;
- }
- // SEGMENT TREE SUM
- #include <bits/stdc++.h>
- using namespace std;
- vector<int> t;
- void build(vector<int> &a, int v, int tl, int tr){
- if(tl == tr) t[v]=a[tl];
- else {
- int tm=(tl+tr)/2;
- build(a, v*2+1, tl, tm);
- build(a, v*2+2, tm+1, tr);
- t[v]=t[v*2+1]+t[v*2+2];
- }
- }
- int sum(int v, int tl, int tr, int l, int r){
- if(l > r) return 0;
- if(l==tl && r==tr) return t[v];
- int tm=(tl+tr)/2;
- return sum(v*2+1, tl, tm, l, min(r, tm)) + sum(v*2+2, tm+1, tr, max(l, tm+1), r);
- }
- void update(int v, int tl, int tr, int pos, int new_val){
- if(tl==tr) t[v]=new_val;
- else{
- int tm=(tl+tr)/2;
- if(pos<=tm) update(v*2+1, tl, tm, pos, new_val);
- else update(v*2+2, tm+1, tr, pos, new_val);
- t[v]=t[v*2+1]+t[v*2+2];
- }
- }
- int main(){
- int n;
- cin>>n;
- vector<int> a(n);
- vector<int> arr(4*n,0);
- t=arr;
- for(int i=0; i<n; i++){
- cin>>a[i];
- }
- build(a, 0, 0, n-1);
- for(auto x:t) cout<<x<<" ";
- cout<< sum(0, 0, n-1, 2, 4);
- update (0, 0, n-1, 3, 7);
- cout<< sum(0, 0, n-1, 2, 4);
- //for(auto x:t) cout<<x<<" ";
- return 0;
- }
- // SEGMENT TREE LCM
- #include <bits/stdc++.h>
- using namespace std;
- vector<int> t;
- int gcd(int a, int b){
- if(a==0) return b;
- else return gcd(b%a, a);
- }
- int lcm(int a, int b){
- return (a*b)/gcd(a,b);
- }
- void build(vector<int> &a, int v, int tl, int tr){
- if(tl == tr) t[v]=a[tl];
- else {
- int tm=(tl+tr)/2;
- build(a, v*2+1, tl, tm);
- build(a, v*2+2, tm+1, tr);
- t[v]=lcm(t[v*2+1], t[v*2+2]);
- }
- }
- int get_lcm(int v, int tl, int tr, int l, int r){
- if(l > r) return 1;
- if(l<=tl && r>=tr) return t[v];
- int tm=(tl+tr)/2;
- return lcm(get_lcm(v*2+1, tl, tm, l, min(r, tm)), get_lcm(v*2+2, tm+1, tr, max(l, tm+1), r));
- }
- int main() {
- int num, q;
- cin >> num>>q; //Reading input from STDIN
- vector<int> a(num);
- vector<int> temp(4*num, 0);
- t=temp;
- for(int i=0; i<num; i++) cin>>a[i];
- build(a, 0, 0, num-1);
- for(int i=0; i<q; i++){
- int l, r;
- cin>>l>>r;
- cout<<get_lcm(0, 0, num-1, l-1, r-1)<<'\n';
- }
- return 0;
- }
- // BELLMAN FORD
- vector<int> bellmanFord(vector<vector<int>>,int src, int V){
- // Step 1 - Creating a V sized array/vector,
- // and initializing it with a very big value.
- vector<int> dis(V,INT_MAX); // Creating a vector dis of size V with values as INT_MAX.
- dis[src] = 0; // Since we are already on source vertex, we can reach it within no time.
- // Step 2 - For V-1 times, traversing over,
- // all the edges and checking if a shorter
- // path between any edge u to v is possible.
- int u,v,wt;
- for(int i=0;i<V-1;i++) // Iterating V-1 times
- {
- for(int j=0;j<edges.size();j++) // Iterating over all the edges.
- {
- u = edges[j][0]; // Source vertex.
- v = edges[j][1]; // Destination vertex.
- wt = edges[j][2];// Weight of the edge.
- // If we can reach v from u in less time it
- // is currently required to reach v then update
- // the value.
- if(dis[u]!=INT_MAX && dis[v] > dis[u] + wt)
- dis[v] = dis[u] + wt;
- }
- }
- // Step 3 - Checking for negative edge weight cycle,
- // by checking if the underliying condition satifies.
- for(int j=0;j<edges.size();j++)
- {
- u = edges[j][0];
- v = edges[j][1];
- wt = edges[j][2];
- // If the below condition satisfies, it means negative
- // edge weight cycle exists. Because traversing again
- // is reducing the cost and in order to minimize the
- // cost we can traverse till infinity and hence a proper
- // answer can't be calculated.
- if(dis[u]!=INT_MAX && dis[v] > dis[u] + wt)
- return {};
- }
- return dis; // returning our answer vector/array.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement