1886C - Decreasing String - CodeForces Solution


strings strings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define FOD(i,b,a) for (int i = (b), _a = (a); i >= _a; i--)
#define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define CNTBIT(x) __builtin_popcount(x)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define left ___left
#define right ___right
#define pii pair<int, int>
#define DEBUG(n, a) FOR (i, 1, n) cout << a[i] << ' '; cout << endl;1234567890
#define lob lower_bound // >=
#define upb upper_bound // >
#define endl "\n"

using namespace std;

template<class X, class Y> bool maximize(X &x, Y y){ if (x < y){ x = y; return true; } return false; }
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y){ x = y; return true; } return false; }

#define  gogobruhbruh  ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define  file(name)  if (fopen (name".inp", "r")) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }

const int MOD = 123456789;

void add(int &a, int b){ if ((a += b) >= MOD) a -= MOD; }
int sub(int a, int b){ if ((a -= b) < 0) a += MOD; return a; }
int muti(int a, int b){ return (1LL * a * b) % MOD; }
int Pow(int x, int y){ int res = 1; for (; y; y >>= 1){ if (y & 1) res = muti(res, x); x = muti(x, x); } return res; }

const int MAX = 1e6 + 5;
const int INF = 1e9 + 7;

string s;
ll pos;
int len, L[MAX], R[MAX];
priority_queue<int> heap;
bool ok[MAX];

int Find(int u, bool t){
    if (t)
        return ok[L[u]] ? L[u] = Find(L[u], t) : L[u];
    return ok[R[u]] ? R[u] = Find(R[u], t) : R[u];
}

void solve(){
    ll sum = 0;
    int x = 0;
    len = s.size();
    s = " " + s;
    for (int i = len; sum + i < pos; sum += i, i--)
        x++;
    s[0] = 'a' - 1;
    s[len + 1] = 'z' + 1;
    FOR (i, 1, len){
        L[i] = i - 1; R[i] = i + 1;
        if (s[i + 1] < s[i])
            heap.push(-i);
    }
    int cnt = 0;
//    cout << x << endl;
    while (heap.size()){
        int i = heap.top(); heap.pop();
        i *= -1;
//        cout << i << endl;
        if (cnt == x)
            continue;
        cnt++;
        ok[i] = 1;
        R[Find(i, 1)] = Find(i, 0);
        if (s[Find(i, 0)] < s[Find(i, 1)])
            heap.push(-Find(i, 1));
    }
    vector<pii> V;
    if (cnt < x){
        FOR (i, 1, len)
            if (!ok[i])
                V.pb({s[i], i});
        sort(ALL(V), greater<pii>());
        FOR (i, 0, x - cnt - 1)
            ok[V[i].se] = 1;
    }
    x = 0;
    FOR (i, 1, len){
        if (ok[i]){
            ok[i] = 0;
            continue;
        }
        x++;
//        cout << i << endl;
        if (x == pos - sum)
            cout << s[i];
    }
    V.clear();
}

void read(){
    cin >> s >> pos;
}

int main(){
    gogobruhbruh;
    file("main");
    int test = 1;
    cin >> test;
    while (test--)
        read(),
        solve();
}


Comments

Submit
0 Comments
More Questions

148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters