1523C - Compression and Expansion - CodeForces Solution


brute force data structures greedy implementation trees *1600

Please click on ads to support us..

C++ Code:

//      BISMILLAH-IR-RAHMAN-IR-RAHIM
 
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<float, float> pff;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
#define el << '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds ;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
#define pb push_back
#define PI 3.14159265358979323
#define file() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
#define fastIO	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) ;
ll bin_expo(ll base, ll pow, ll m) {
        if (pow == 0) return 1;
        ll ret = bin_expo(base, pow / 2, m) % m;
        if (pow & 1) return (ret % m * ret % m * base % m) % m;
        else return (ret % m * ret % m) % m;
}
ll mod_add(ll a, ll b, ll m) { return ((a % m) + (b % m)) % m; }
ll mod_sub(ll a, ll b, ll m) { return ((a % m) - (b % m)) % m; }
ll mod_mul(ll a, ll b, ll m) { return ((a % m) * (b % m)) % m; }
ll mod_div(ll a, ll b, ll m) { return ((a % m) * bin_expo(b, m - 2, m)) % m; }
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
int dx[] = {0, 0, +1, -1, +1, -1, -1, +1};
int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};
const ll infLL = 4e18 + 123;
const ll inf = 1e17 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 1e9 + 9;
const int mx = 2e5 + 123;
const double eps = 1e-7;
 
 
// forever-striver 
 
 
void solution() { 
 
        ll n ; 
        cin >> n  ; 
        stack<ll>stk ; 
        for(ll i=0;i<n;i++) {
                ll x ; 
                cin >> x ; 
                stack<ll>tmp; 
                tmp = stk ; 
                vl v ; 
                if(x==1) {
                        stk.push(1) ; 
                        tmp.push(1) ; 
                        while(!tmp.empty()) {
                                v.push_back(tmp.top()) ; 
                                tmp.pop() ; 
                        } 
                }
                else {
                        while(!tmp.empty() and tmp.top()!=(x-1)) {
                                //v.push_back(tmp.top()) ; 
                                tmp.pop() ; 
                        }
                        v.push_back(x) ; 
                        if(!tmp.empty()) {
                                tmp.pop() ; 
                                while(!tmp.empty()) {
                                        v.push_back(tmp.top()) ; 
                                        tmp.pop() ; 
                                }
                        }
                }
                reverse(v.begin(),v.end()) ; 
                for(int j=0;j<v.size();j++) {
                        if(j<v.size()-1) {
                                cout << v[j] << "." ; 
                        }
                        else cout << v[j] el ; 
                }
                reverse(v.begin(),v.end()) ;
                stack<ll>newstk ; 
                while(v.size()) {
                        newstk.push(v.back()) ; 
                        v.pop_back() ; 
                }
                stk = newstk ; 
        }
 
}
 
 
int main() {
        fastIO;
        // file() ;
        ll _ = 1; cin >> _;
        while (_--) solution();
        return 0 ;
}


Comments

Submit
0 Comments
More Questions

215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game