删数问题

题目: 删数问题

题目链接:https://www.luogu.com.cn/problem/T241086?contestId=69902

题目描述

一个集合有如下元素:11 是集合元素;若 PP 是集合的元素,则 2\times P+12×P+1,4\times P+54×P+5 也是集合的元素。

取出此集合中最小的 kk 个元素,按从小到大的顺序组合成一个多位数,现要求从中删除 mm 个数位上的数字,使得剩下的数字最大,编程输出删除前和删除后的多位数字。

注:不存在所有数被删除的情况。

输入格式

只有一行两个整数,分别代表 kk 和 mm。

输出格式

输出为两行两个整数,第一行为删除前的数字,第二行为删除后的数字。

输入输出样例

输入 #1
5  4
输出 #1
137915
95

说明/提示

数据规模与约定

  • 对于 30\%30% 的数据,保证 1\le k,m\le3001k,m300。
  • 对于 100\%100% 的数据,保证 1\le k,m\le3\times10^41k,m3×104。

解题思路:

本题使用了优先队列加链表的形式,先用q 作为优先队列记录从小到大的集合元素,再反向拆解q中的元素到ans数组中,最后使用next作为链表

 

参考代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+50;
const int N=1e6+5;
const ll mod =1e9+7;
const int inf=2e9+7;
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;
int a[30005],ans[5000010],topa,topans,next[5000010];
int main()
{
int l,m;
cin>>l>>m;
memset(ans,0x3f,sizeof(ans));
q.push(1);
while(1)
{
int x=q.top(),d=0;q.pop();
q.push(2*x+1);q.push(4*x+5);
a[++topa]=x;
while(x)
{
d=d*10+x%10;
x/=10;
}
while(d)
{
ans[++topans]=d%10;
d/=10;
}
if(topa>=l) break;
}
for(int i=1;i<=topa;i++) cout<<a[i];
cout<<endl;
for(int i=0;i<topans;i++) next[i]=i+1;
while(m)
{
int l=0;
while(ans[next[l]]>=ans[next[next[l]]])
l=next[l];
next[l]=next[next[l]];
m--;
}
for(int i=0;next[i];i=next[i]) cout<<ans[next[i]];
return 0;
}

posted @ 2022-06-25 22:23  kenty-time  阅读(81)  评论(0)    收藏  举报