E. Playing with numbers

time limit per test

2.0 s

memory limit per test

64 MB

input

standard input

output

standard output

Folan and Eltan are brothers. However, as all brothers are, they always fight. One day their mom had to go to work, so she decided to give them a task to keep them busy while she is away.

She gave them two numbers S and N.

She told Folan that he has to delete exactly N digits from the number S, so that the resulting number is as small as possible.

Then, she told Eltan that he has to delete exactly N digits from the number S, so that the resulting number is as big as possible.

Folan and Eltan are ex ACMers. They decided to write a program to solve this problem, so they can go back to fighting again.

When their mom heard the evil plan, she decided to make the number S very big, and she may have added leading zeros to it.

The boys were really upset because they couldn't find a way to write a program that would solve this problem fast enough before their mom returns, so they asked for your help.

Can you help them with this program, so they can go back to fighting again?

Input

The first line of the input consists of a single integer t, the number of test cases. Each test case consists of two numbers S and N separated by a single space. where (0 ≤ S < 10100000) and it may contain leading zeros, and (0 ≤ N < |S|).

Note that |S| means the length of the number S.

Output

For each test case, print two lines.

The first line should contain the smallest number Folan can get after deleting exactly N digits from S.

The second line should contain the biggest number Eltan can get after deleting exactly N digits from S.

Please note that in case some of the leading zeros were not deleted, you have to print the resulting number with the remaining leading zeros.

Example
input

Copy
3
00123 2
00123 3
234714812741111111111111111111 4
output

Copy
001
123
00
23
14812741111111111111111111
74812741111111111111111111 题意:给出一个长度最大为100000位的数字,求从中剔除掉N个数字后得到到最大数和最小数。 题解:
若是找最小数,那么维持原来的数字单调递增,若递减,则把前面的数字删去直到递增,遍历一遍后如果没删够,则从后面删。 
若是找最大数,那么维持原来的数字单调递减,若递增,则把前面的数字删去直到递减,便利一遍后如果每删够,则从后面删。
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<stack>
#include<math.h>
#define mod 998244353
#define ll long long
#define MAX 0x3f3f3f3f
using namespace std;
string s;
string ss;
stack<char>p;
int n,len,cnt,t;
int main()
{
cin>>t;
while(t--)
{
cin>>s;
cin>>n;
cnt=n,ss.clear();
for(int i=;s[i];i++)
{
while(!p.empty()&&s[i]<p.top()&&cnt)//删除之后最小,维持栈单调递增
{
p.pop();
cnt--;
}
p.push(s[i]);
}
while(cnt--)//遍历完之后还没删够
{
p.pop();
}
while(!p.empty())
{
ss=ss+p.top();
p.pop();
}
len=ss.length();
for(int i=len-;i>=;i--)
cout<<ss[i];
cout<<endl;
cnt=n;
ss.clear();
for(int i=;s[i];i++)//删除之后最大,维持站内单调递减
{
while(!p.empty()&&s[i]>p.top()&&cnt)
{
p.pop();
cnt--;
}
p.push(s[i]);
}
while(cnt--)
{
p.pop();
}
while(!p.empty())
{
ss=ss+p.top();
p.pop();
}
len=ss.length();
for(int i=len-;i>=;i--)
cout<<ss[i];
cout<<endl; }
}

单调栈应用--将一个数删除n各数字之后的最大\最小值的更多相关文章

  1. [51nod1482]部落信号 单调栈

    ~~~题面~~~ 题解: 可以发现这是一道单调栈的题目,首先来考虑数字没有重复时如何统计贡献. 因为这是一个环,而如果我们从最高的点把环断开,并把最高点放在链的最后面(顺时针移动),那么因为在最高点两 ...

  2. Codeforces 1175F - The Number of Subpermutations(线段树+单调栈+双针/分治+启发式优化)

    Codeforces 题面传送门 & 洛谷题面传送门 由于这场的 G 是道毒瘤题,蒟蒻切不动就只好来把这场的 F 水掉了 看到这样的设问没人想到这道题吗?那我就来发篇线段树+单调栈的做法. 首 ...

  3. Max answer(单调栈+ST表)

    Max answer https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value o ...

  4. 2019牛客暑期多校训练营(第一场) - A - Equivalent Prefixes - 单调栈

    A - Equivalent Prefixes - 单调栈 题意:给定两个n个元素的数组a,b,它们的前p个元素构成的数组是"等价"的,求p的最大值."等价"的 ...

  5. 2019牛客暑期多校训练营(第一场)A - Equivalent Prefixes(单调栈)

    题意 给定两个$n$个元素的数组$a,b$,它们的前$p$个元素构成的数组是"等价"的,求$p$的最大值."等价"的意思是在其任意一个子区间内的最小值相同. $ ...

  6. UVa 1451 (数形结合 单调栈) Average

    题意: 给出一个01串,选一个长度至少为L的连续子串,使得串中数字的平均值最大. 分析: 能把这道题想到用数形结合,用斜率表示平均值,我觉得这个想法太“天马行空”了 首先预处理子串的前缀和sum,如果 ...

  7. 小Z爱序列(NOIP信(sang)心(bin)赛)From FallDream(粗制单调队列&单调栈的算法解析)

    原题: 小Z最擅长解决序列问题啦,什么最长公共上升然后下降然后上升的子序列,小Z都是轻松解决的呢. 但是小Z不擅长出序列问题啊,所以它给了你一道签到题. 给定一个n个数的序列ai,你要求出满足下述条件 ...

  8. 数据结构录 之 单调队列&单调栈。

    队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇的队列和栈的扩展. 其中最出名的应该是优先队列吧我觉得,然后还有两种比较小众的扩展就是单调 ...

  9. BZOJ_1345_[Baltic2007]序列问题Sequence_单调栈

    BZOJ_1345_[Baltic2007]序列问题Sequence_单调栈 Description 对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai ...

随机推荐

  1. Linux centosVMware LAMP php-fpm的pool、php-fpm慢执行日志、open_basedir、php-fpm进程管理

    一.php-fpm的pool vim /usr/local/php/etc/php-fpm.conf//在[global]部分增加 include = etc/php-fpm.d/*.conf mkd ...

  2. word无法切换中文输入法的解决方法

    问题: 在word编辑文字的时候,莫名其妙地出现只能输入英文字母无法输入中文的现象,输入法状态条也不显示,而输入法是正常的(在其他编辑器中可正常输入汉字) 解决方法如下:word 2003:菜单工具- ...

  3. Day1-A-POJ-3295

    由题意知,有5种操作,5个未知数,可0可1,一串操作问是否恒为1,最多100个字符,直接栈模拟所有情况即可 代码如下: int p, q, r, s, t; bool calculate(string ...

  4. C++函数的理解思考

    函数指针调用方式 void testmy(int k) { cout << "testzhixing " <<k << endl; } int ...

  5. 【剑指Offer面试编程题】题目1517:链表中倒数第k个结点--九度OJ

    题目描述: 输入一个链表,输出该链表中倒数第k个结点. (hint: 请务必使用链表.) 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每个测试案例,输入的第一行为两个整数n和k(0< ...

  6. ionic3记录之APP运行时网络判断

    判断设备网路是否正常: 安装插件: ionic cordova plugin add cordova-plugin-network-information npm install --save@nat ...

  7. 图片IO域 旋转画面的组态 图片是4个静止的风扇 PLC的MW6为风扇指针..

    图片IO域 旋转画面的组态 图片是4个静止的风扇 PLC的MW6为风扇指针.. Plc在循环中断组织块 OB35 中 将MW6 每100ms 加1 加到4 清0 图片[MW6] MW6 是图片指针 对 ...

  8. 在linux环境中如何删除文件

    使用rm -rf 目录名字 命令即可 -r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 eg 删除文件夹实例:rm -rf /var/log/httpd/acc ...

  9. android 桌面透明

      目录(?)[-] public void setWallpaperOffsetSteps float xStep float yStep Parameters public void setWal ...

  10. java小项目之:抽奖系统!java初学者必备(内附源码)

    [Java]Java摇奖源码,Java抽奖源码,Java随机抽奖源码 任务描述 本次任务要求为某商场开发一套幸运抽奖系统,客户必须首先注册成为该商场会员,会员登录成功后,就可以参加抽奖活动了.注册 用 ...