Time Limit: 1 second
Memory Limit: 128 MB
【问题描述】
键盘输入一个高精度的正整数N,去掉其中任意M个数字后剩下的数字按原左右次序将组成一个新的正整数。编程对给定的N和M寻找一
种方案使得剩下的数字组成的新数最小。输出组成的新的正整数。
输入数据均不需判错。如果去掉了某几个位后得到的新整数开头为0,保留0。
【输入格式】
第一行为高精度正整数N(N的长度不超过10^6位)
第二行为M(0<=M<=N的长度)
【输出格式】
去掉M位后的最小新数。
Sample Input
82386782
3
Sample Output
23672
Sample Input2
505
1
Sample Output2
05
【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t053
【题解】
http://blog.csdn.net/harlow_cheng/article/details/51870979?locationNum=8&fps=1
是这题的升级版;
找到从左往右第一个递减区间的第一个数字把它删掉就好;
因为找到那个数字之后、前面的数字仍然是递增的,所以没必要再往前找;
直接再往后找就可以了;
用链表模拟删除的过程以节省时间;
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
const int MAXN = 1e6+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
struct abc
{
int l,r;
};
char s[MAXN];
int m;
abc a[MAXN];
int main()
{
//freopen("F:\rush.txt","r",stdin);
scanf("%s",s+1);
scanf("%d",&m);
int len = strlen(s+1);
if (m==len)
{
puts("0");
return 0;
}
rep1(i,1,len)
a[i].l = i-1,a[i].r=i+1;
a[0].r=1;
int i = 1;
while (m)
{
while (a[i].r<=len && s[i]<=s[a[i].r])
i=a[i].r;
if (i==len)
{
a[a[i].l].r = len+1;
i=a[i].l;
m--;
}
else
{
a[a[i].l].r = a[i].r;
a[a[i].r].l = a[i].l;
i=a[i].l;
m--;
}
}
int now = a[0].r;
while (now!=len+1)
{
putchar(s[now]);
now = a[now].r;
}
return 0;
}