Problem:
Swapping Characters

You are given a string and a number k. You are suggested to generate new strings by swapping any adjacent pair of characters in the string up to k times. Write a program to report the lexicographically smallest string among them.

Input

The input is given in the following format.

s
k

The first line provides a string s. The second line provides the maximum number of swapping operations k (0 ≤ k ≤ 109). The string consists solely of lower-case alphabetical letters and has a length between 1 and 2 × 105.

Output

Output the lexicographically smallest string.

Sample Input 1

pckoshien
3

Sample Output 1

ckopshien

Sample Input 2

pckoshien
10

Sample Output 2

cekophsin

Solution in C++ (judge verdict: Accepted)

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
 
 
#define NUM 2147483647
 
struct Info{
    int value,index;
};
 
int N,length;
Info* table;
 
int BIT[200001];
 
void add(int loc,int value){
 
    BIT[loc] += value;
 
    loc += loc & -loc;
 
    while(loc <= length){
        BIT[loc] += value;
        loc += loc & -loc;
    }
}
 
int getSum(int loc){
 
    int sum = BIT[loc];
 
    loc -= loc & -loc;
 
    while(loc > 0){
        sum += BIT[loc];
        loc -= loc & -loc;
    }
    return sum;
}
 
int calc(int left,int right){
    return getSum(right)-getSum(left-1);
}
 
 
void init(int first_N){
    while(N < first_N)N *= 2;
}
 
void update(int loc,int value){
    loc += N-1;
 
    table[loc].value = value;
    table[loc].index = loc-(N-1);
 
    if(N == 1)return;
 
    int parent = (loc-1)/2;
 
    while(true){
 
        if(table[2*parent+2].value < table[2*parent+1].value){
            table[parent].value = table[2*parent+2].value;
            table[parent].index = table[2*parent+2].index;
        }else{
            table[parent].value = table[2*parent+1].value;
            table[parent].index = table[2*parent+1].index;
        }
 
        if(parent == 0)break;
        else{
            parent = (parent-1)/2;
        }
    }
}
 
 
Info query(int search_left,int search_right,int node_id,int node_left,int node_right){
 
    Info ret;
 
    if(search_right < node_left || search_left > node_right){
        ret.value = NUM;
        return ret;
    }
 
    if(search_left <= node_left && search_right >= node_right){
        return table[node_id];
    }
 
    Info left_min = query(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2);
    Info right_min = query(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right);
 
    if(left_min.value <= right_min.value){
        return left_min;
    }else{
        return right_min;
    }
}
 
 
int main(){
 
    char* buf = new char[200001];
    scanf("%s",buf);
 
    for(length = 0;buf[length] != '\0'; length++);
 
    int first_N;
    table = (Info*)malloc(sizeof(Info)*550000);
    N = 1;
 
    first_N = length;
    init(first_N);
 
    for(int i = 0; i <= length; i++)BIT[i] = 0;
    for(int i = 1; i <= length; i++)add(i,1);
 
    for(int i = 0; i <= 2*N-2; i++)table[i].value = NUM;
 
    for(int i = 0; i < length; i++)update(i,buf[i]-'a');
 
    int K;
    scanf("%d",&K);
 
    if(K == 0){
        printf("%s\n",buf);
        return 0;
    }
 
    Info ret_query;
 
    queue<char> Q;
 
    int left,right,m;
    int calc_right,tmp;
 
    for(int i = 0; i <= length-2; ){
 
        if(calc(i+1,i+1) == 0){
            i++;
            continue;
        }
 
        left = i+1,right = length-1,m = (left+right)/2;
        calc_right = BIG_NUM;
 
        while(left <= right){
            tmp = calc((i+1)+1,m+1);
            if(tmp == K){
                calc_right = m;
                break;
            }else if(tmp < K){
                calc_right = m;
                left = m+1; //????????????
            }else{
                right = m-1;
            }
            m = (left+right)/2;
        }
 
        if(calc_right == BIG_NUM)break;
 
        ret_query = query(i+1,calc_right,0,0,N-1);
 
        if(buf[i]-'a' > ret_query.value){
 
            Q.push(buf[ret_query.index]);
            update(ret_query.index,NUM);
            add(ret_query.index+1,-1);
 
            K -= (calc(i+1,ret_query.index));
            if(K <= 0)break;
        }else{
            Q.push(buf[i]);
            update(i,NUM);
            add(i+1,-1);
            i++;
        }
    }
 
    while(!Q.empty()){
        printf("%c",Q.front());
        Q.pop();
    }
 
    for(int i = 1; i <= length; i++){
        if(calc(i,i) == 1){
            printf("%c",buf[i-1]);
        }
    }
    printf("\n");
 
    return 0;
}