1765N - Number Reduction - CodeForces Solution


greedy *1500

Please click on ads to support us..

Python Code:

import bisect
def rem(ss,kk):
    ret=[]
    for c in ss:
        while (len(ret)>0 and kk and ret[-1]>c):
            ret.pop();kk-=1
        ret.append(c)
    while len(ret)>0 and kk:
        ret.pop();kk-=1
    return ''.join(ret)
t=int(input())
for tc in range(t):
    s=input()
    n=len(s)
    k=int(input())
    i=0;m=s[0]
    for j in range(1,k+1):
        if '0'<s[j]<m:m=s[j];i=j
    print(m+rem(s[i+1:],k-i))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
/////////////////////////////////////////DEFINE//////////////////////////////////////////

#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define MAXN                      10000001

typedef long long ll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef map<long long, long long> mll;
typedef vector<pair<long, long>> vpll;

////////////////////////////////////////CONST////////////////////////////////////////////

const int inf = 0x3f3f3f3f;
const int maxn = 2e6 + 6;
const double eps = 1e-8;
const int mod = 1000000007;
const int N = 1e7;

///////////////////////////////////////FUNCTION//////////////////////////////////////////

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
bool cmp(int c, int d) { return c > d;}
void solve()
{
     string s;cin>>s;
     ll k;cin>>k;
     ll n=s.length();
     string ans="";
     for(ll i=0;i<n;i++)
     {
         if(s[i]=='0')
         {
            while(ans.size()>1 && k>0 && ans.back()!='0')
            {
                ans.pop_back();
                k--;
            }
         }
         else{
            while(ans.size()>0 && k>0 && ans.back()>s[i])
            {
                ans.pop_back();
                k--;
            }
            if(ans.size()>0 && k>0 && ans.back()=='0')
            {
                if(ans[0]>s[i] && k>=ans.size())
                {
                    k-=ans.size();
                    ans="";
                }
            }
         }
         ans.pb(s[i]);
         }

         while(k--)
          {
            ans.pop_back();
          }
          cout<<ans<<'\n';
     }
      
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
ll t;
cin>>t;
while(t--)
solve();
return 0;
}


Comments

Submit
0 Comments
More Questions

765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven