http://acm.hdu.edu.cn/showproblem.php?pid=3183

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2556    Accepted Submission(s): 999

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
 
题目大意:给一个数,删除其中的m位是其值变得最小,求删除后最小值
 
看了样例之后,以为只要删除m个较大的数,就看可以了, 结果一直wa,后来发现 21435  1这组数据,
 
按照这个思路删掉最大的一个数5得到结果2143,发现这不是最小的结果,1435才是最小的结果,这种思路是不对的,
 
那么要让结果最小,那么如果高位数字比低位数字大的话,就删除这个高位数字,这样就让结果尽可能的小了
 
比如这个例子:
 
235564  3
 
要删除m = 3个数,发现2<3,3<5,5<=5,5<6,高位都不大于低位,而6>4,这时删去6(m = 2),23554, 发现5>4,删去5(m = 1)2354,
 
 发现5>4,删去5(m = 0)234,最终结果为234,这样循环遍历,具体看代码
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std; typedef long long ll;
const int N = ; int main()
{
char s[N];
int a[N], str[N];
int m, i;
while(~scanf("%s%d", s, &m))
{
memset(str, , sizeof(str));
int k = , l = strlen(s);
for(i = ; s[i] != '\0' ; i++)
a[i] = s[i] - '';
for(i = ; i < l && m > ; i++)
{
if(a[i] <= a[i + ])
str[k++] = a[i];
else
{
m--;
while(str[k - ] > a[i + ] && m > && k >= )
{
m--;
k--;
}
}
}
for(; i < l ; i++)
str[k++] = a[i];
for(i = ; i < k - m ; i++)
if(str[i] != )
break;
if(i == k - m)
printf("");
else
{
for(int j = i ; j < k - m ; j++)
printf("%d", str[j]);
}
printf("\n");
}
return ;
}
 
 

hdu A Magic Lamp的更多相关文章

  1. hdu 3183 A Magic Lamp(RMQ)

    题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...

  2. hdu 3183 A Magic Lamp RMQ ST 坐标最小值

    hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...

  3. HDU 3183 - A Magic Lamp - [RMQ][ST算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...

  4. A Magic Lamp -- hdu -- 3183

    http://acm.hdu.edu.cn/showproblem.php?pid=3183 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)   ...

  5. hdu 3183 A Magic Lamp rmq或者暴力

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

  6. hdu 3183 A Magic Lamp(RMQ)

    A Magic Lamp                                                                               Time Limi ...

  7. HDU 3182 ——A Magic Lamp(思维)

    Description Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lam ...

  8. A Magic Lamp HDU - 3183 (逆向贪心/RMQ)

    Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so ...

  9. HDU 3183 A Magic Lamp(RMQ问题, ST算法)

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

随机推荐

  1. Django的cookie学习

    为什么要有cookie,因为http是无状态的,每次请求都是独立的,但是我们还需要保持状态,所以就有了cookie cookie就是保存在客户端浏览器上的键值对,别人可以利用他来做登陆 rep = r ...

  2. OC 线程操作 - GCD使用 - 栅栏函数

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //同步函数无需栅栏函数 //栅栏 ...

  3. 1. Spring boot 之热部署

    1. spring boot 热部署 1.1. springloaded springloaded可以实现修改类文件的热部署.下载地址:springloaded 安装单击Run Configurati ...

  4. LocalBroadcastManager 的使用

    一.使用本地广播发送一条广播(本例为自己发送自己接收,本地广播也可以是其他应用接收)然后接收到广播时回调Receiver类中的回调方法onReceive()在此方法中自定义发出通知 代码 packag ...

  5. spring中配置监听队列的MQ

    一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置.  <bean id="axx" ...

  6. win2003上传文件限制

    Windows2003系统下,上传较大的文件时,会出现“Request 对象 错误 'ASP 0104 : 80004005'”错误或者出现空白页面的时候,此时,不要轻易的去找程序的问题,有可能是wi ...

  7. 图像获取与采集及图像格式与Region介绍——第2讲

    一.图像获取与采集 1.本地图片读取 ① 单张读取 直接传入图片路径即可,可以用绝对路径,也可以用相对路径: read_image (Image, 'C:/Users/Administrator/De ...

  8. PAT 1003 我要通过!(20)(代码+思路)

    1003 我要通过!(20)(20 分)提问 "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下 ...

  9. linux(rhel) rescue修复详解

    修复linux,先准备好一张安装光盘,光驱安装好后开机,选择从光驱启动.等待安装盘显示操作界面时选择"rescue"选项,如果有光标提示的话,也可以输入:linux rescue进 ...

  10. pyspider示例代码六:传递参数

    传递参数 示例一 #!/usr/bin/env python # -*- encoding: utf- -*- # vim: ts= sts= ff=unix fenc=utf8: # Created ...