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. day10:vcp考试

    Q181. An administrator is deploying ESXi 6.x hosts using Auto Deploy and wants the image profile to ...

  2. mysqlbateis generator 当遇到tinyint 生成转化bool 解决方法

    当遇到tyint 生成转化bool  类型问题很恶心,记录一下解决方法 一. TinyInt转换规则 JAVA数据类型 和 MYSQL的数据类型转换,要注意tinyInt 类型,且存储长度为1的情况. ...

  3. 动态调用WebService方法

      好像很多人做WebService的时候都是直接添加引用的方式,然后调用服务端的方法.这样就个问题,就是每次我服务端添加了方法或者修改了方法后都要更新Web引用,这样比较麻烦.下面给一个不用添加引用 ...

  4. OC 和 swift 冒泡排序

    swift 版 var numbers = [17, 28, 36, 15, 39] print("排序前\(numbers)") for i in 0..<numbers. ...

  5. catkin_make 与cmake

    http://blog.csdn.net/zyh821351004/article/details/50388429 1.  catkin_make 与cmake的关系 程序在cmake编译的流程: ...

  6. SSH三大框架的关系、使用到的jar包、配置文件图解

  7. javascript正则表达式验证密码(必须含数字字符特殊符号,长度4-16位之间)

    var newpwd = $("#newpassword").val(); //var pattern = "([A-Za-z]|[0-9]|-|_){4,16}&quo ...

  8. ObjC.instancetype

    1. instancetype http://nshipster.com/instancetype/ 2. Objc的扩展 http://clang.llvm.org/docs/LanguageExt ...

  9. python的metaclass浅析-乾颐堂

    元类一般用于创建类.在执行类定义时,解释器必须要知道这个类的正确的元类.解释器会先寻找类属性__metaclass__,如果此属性存在,就将这个属性赋值给此类作为它的元类.如果此属性没有定义,它会向上 ...

  10. tp5链接访问

    方法名:admin/DayActive/statistic 访问:admin/day_active/statistic