(贪心) hdu 3183

A Magic Lamp

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

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

Source

HDU 2009-11 Programming Contest

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace  std;
char s[10010];
int n,len;
void work()
{
      int temp=n;
      while(temp--)
      {
            int i=0;
            while(i<len-1&&s[i]<=s[i+1]) i++;
            for(int j=i+1;j<len;j++)
                  s[j-1]=s[j];
      }
}
int main()
{
      while(scanf("%s%d",s,&n)!=EOF)
      {
            len=strlen(s);
            if(n>=len)
            {
                  printf("0\n");
                  continue;
            }
            work();
            bool flag=false;
            for(int i=0;i<len-n;i++)
            {
                  if(!flag&&(s[i]==‘0‘&&i!=len-n-1))
                        continue;
                  else
                  {
                        printf("%c",s[i]);
                        flag=1;
                  }
            }
            printf("\n");
            memset(s,‘0‘,sizeof(s));
      }
      return 0;
}

  

时间: 2024-10-11 12:34:41

(贪心) hdu 3183的相关文章

hdu 3183 A Magic Lamp(RMQ)

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, u

贪心/hdu 1009 FatMouse&#39; Trade

题意 有n种物品,每一种需要不同的消费,现在手里有m块钱,求问最多可以买多少 分析 贪心 把每一种物品的价格算出来,然后sort一下,按照价格从便宜到贵排序,能买多少买多少,买买买! Accepted Code 1 /* 2 PROBLEM:hdu1009 3 AUTHER:Nicole Lam 4 MEMO:贪心 5 */ 6 7 #include<cstdio> 8 #include<algorithm> 9 using namespace std; 10 11 12 stru

贪心 --- hdu : Road Trip

Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29, Accepted users: 29 Problem 12882 : No special judgement Problem description You are planning a road trip to visit your friends, each of whom live in

hdu 3183 A Magic Lamp(RMQ)

题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个,使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数,每次取的时候保证后面能取的个数足够,并且取的数最小,查询最小的操作用RMQ优化. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 10005; int N, M, d[m

HDU 3183 贪心

A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3019    Accepted Submission(s): 1172 Problem Description Kiki likes traveling. One day she finds a magic lamp, unfortunately the geni

HDU 3183 A Magic Lamp(贪心+RMQ)

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 allowe

hdu 3183 A Magic Lamp (贪心)

///给你一数字,删除其中的m位使其最小 ///贪心:前面的数要小于后面的数 # include <stdio.h> # include <algorithm> # include <iostream> # include <string.h> # include <math.h> using namespace std; char str[1010]; int a[1010]; int c[1010]; int main() { int k,m

[贪心] hdu 4415 Assassin’s Creed

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4415 题目意思:  要杀死n个敌人,每个敌人有两个属性a和b,a表示杀他所需要的能力值,b表示杀掉他后可以免费再杀b个敌人.告诉初始能力值,求能杀的最多的敌人,及杀掉那么多敌人的最小花费. 解题思路: 分类+贪心 这道题比较难,也比较经典.好题. 首先把敌人按b值是否为零分成两类A和B.A类表示b值不为零,B类表示b值为零.A.B分别按a从小到大排序 首先明确: 1.如果要杀A类,至少需要A[0]

HDU 3183 A Magic Lamp

第一种做法是贪心做法,只要前面的数比后面的大就把他删掉,这种做法是正确的,也比较好理解,这里就不说了,我比较想说一下ST算法,RMQ的应用 主要是返回数组的下标,RMQ要改成<=(这里是个坑点,取连续数是可以的),他的转移方程为x = dp[i-1][j],y = dp[i-1][j+1<<(i-1)]; dp[i][j] = a[x] <= a[y] ? x : y; 这里既是转化为取n-m个数,首先在1 ~ m+1中必然选一个数,所以这个数就是n-m数中第一个数,记录所选的位置