0
点赞
收藏
分享

微信扫一扫

HDU_3183_A Magic Lamp(贪心/线段树)

zhoulujun 2023-02-07 阅读 31


A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2274    Accepted Submission(s): 902

Problem Description
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams.
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
 

Input
There are several test cases.
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.
 

Output
For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it. 
 

Sample Input
178543 4 1000001 1 100001 2 12345 2 54321 2
 

Sample Output
13 1 0 123 321

题意:一个数字串s,删除n个数字后所得的最小数字串是多少。

分析:

贪心思想,删除n个相当于从左到右选出s.len-n个,我们知道第一个数字要在(0,n)里面选,因为还有s.len-n-1个需要选,所以至少要留下s.len-n-1个,然后假设选的为pos位置(注意越往左越好),(pos+1,n+1),以此类推

我尽然WA前导0上,气人

线段树做法:

根据上述思想,线段树的作用就是区间最小值和选择最小值的位置,就是位置的时候有点不好返回,在pushup的时候注意尽量往左选择。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define lson i<<1
#define rson i<<1|1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define LL long long
#define N 305
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
char ans[1005];
int main()
{
char s[1005];
int n;
while(scanf("%s%d",s,&n)!=EOF)
{
int m=strlen(s)-n;
int pre=0;
int cnt=0;
while(m--)
{
//cout<<m<<endl;
int pos=pre;
for(int i=pre;i<=n;i++)
{
if(s[pos]>s[i])
{
pos=i;
}
}
ans[cnt++]=s[pos];

pre=pos+1;
n++;
}

int pos=cnt;
for(int i=0;i<cnt;i++)
{
//cout<<" "<<ans[i]<<endl;
if(ans[i]!='0')
{
pos=i;
break;
}
}
if(pos==cnt)
cout<<0<<endl;
else
{
for(int i=pos;i<cnt;i++)
printf("%c",ans[i]);
cout <<endl;
}
}
return 0;
}

 

线段树

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 1010
using namespace std;
struct tr
{
int l,r,mi,p;
}tree[MAXN*4];
int n,m,a[MAXN],pos,minn,last,ans[MAXN];
char s[MAXN];
void pushup(int i)
{
if(tree[i*2].mi == tree[i*2+1].mi)
{
tree[i].mi = tree[i*2].mi;
tree[i].p = min(tree[i*2].p,tree[i*2+1].p);//多个相同选取前面的
}
else if(tree[i*2].mi < tree[i*2+1].mi)
{
tree[i].mi = tree[i*2].mi;
tree[i]. p = tree[i*2].p;
}
else
{
tree[i].mi = tree[i*2+1].mi;
tree[i].p = tree[i*2+1].p;
}
}
void build_tree(int i,int l,int r)
{
tree[i].l = l;
tree[i].r = r;
if(l == r)
{
tree[i].mi = a[l];
tree[i].p = l;
return;
}
int mid = (l+r)/2;
build_tree(i*2,l,mid);
build_tree(i*2+1,mid+1,r);
pushup(i);

}
void query(int i,int l,int r)
{
if(tree[i].l>=l&&tree[i].r<=r)
{
if(tree[i].mi < minn)
{
minn = tree[i].mi;
pos = tree[i].p;
}
else if(tree[i].mi == minn&&tree[i].p < pos)
{
pos = tree[i].p;
}
return;
}
int mid = (tree[i].l+tree[i].r)>>1;
if(l<=mid) query(i<<1,l,r);
if(r>mid) query(i<<1|1,l,r);
pushup(i);
}
int main()
{
while(scanf("%s",s) != EOF)
{
memset(a,0,sizeof a);
memset(ans,0,sizeof ans);
int cnt = 0;
n = strlen(s);
scanf("%d",&m);
m = n-m;
for(int i = 1; i <= n; i++) a[i] = s[i-1]-'0';
build_tree(1,1,n);
pos = 0;
while(m > 0)
{
minn = 1e9;
query(1,pos+1,n-m+1);
m--;
// cout<<minn<<endl;
ans[++cnt] = minn;
}
int start = 1;
for(; start <= cnt; start++)
if(ans[start] != 0) break;
if(start > cnt) printf("0");
else for(int i = start; i <= cnt; i++) printf("%d",ans[i]);
printf("\n");
memset(s,0,sizeof s);
}
}

 

举报

相关推荐

0 条评论