HDU 3183 A Magic Lamp 【贪心】


传送门:HDU 3183


A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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



题意:
给出一个数n 再给一个数m 问 在n中拿掉m个数后,n最小是多少
n用字符串来存 最多1000位

题解:
要我们求去掉m个数后最小是多少,我们可以反向思考,将题目转化为在n中选(n-m)个数,能组成的最小的数。
所以我们从第m+1个数开始往前找,找最小的数(可以是0),当然找的数越往前越好,这样可以给后面的开辟空间。将这个位置标记下来,下次搜到这个位置时直接break;这个数就是我们最小数的第一个数。

下次从m+2这个位置开始搜,方法同上。如此往复搜n-m次,确定最小数。

然后对这个最小的数去前缀0即可。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define ll long long
const int N=11000;
char s[N];
int a[N],vis[N];
int m;
int main()
{
  while(~scanf("%s",s))
  {
    int cnt=0;
    memset(vis,0,sizeof(vis));
    scanf("%d",&m);
    int len=strlen(s);
    for(int i=m;i<len;i++)
    {
      int minn=10,k=i;
      for(int j=i;j>=0;j--)
      {
        if(vis[j]) break;
        if(s[j]-'0'<=minn)
        {
          minn=s[j]-'0';
          k=j;
        }
      }
      vis[k]=1;
      a[cnt++]=s[k]-'0';
    }
    int x=0;
    while(a[x]==0&&x<cnt)//去0
    {
      x++;
    }
    if(x==cnt) printf("0\n");//全是0的情况,输出0
    else
    {
      for(int i=x;i<cnt;i++)
      {
        printf("%d",a[i]);
      }
      printf("\n");
    }
  }
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值