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 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个数之后要求的数最小。那么肯定保留n-m个数字。那么每次在区间[i,i+m]查询最小值即可。

但是有个坑点,虽然这道题没有,但是底下的那道有,就是比较的时候,如果值相等,要选位置靠前的。

这也是贪心的思想。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int maxn=1100;
int dp[maxn][20];
int num[maxn];
char str[maxn],ans[maxn];
int n,m;
int MIN(int a,int b)
{
    if(num[a]<=num[b]) return a;
    return b;
}
void init()
{
    for(int i=1;i<=n;i++)
        dp[i][0]=i;
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
            dp[i][j]=MIN(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int RMQ(int l,int r)
{
    int k=(int)(log(r-l+1)/log(2.0));
    return MIN(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
    while(~scanf("%s%d",str+1,&m))
    {
        n=strlen(str+1);
        REPF(i,1,n)
            num[i]=str[i]-'0';
        init();CLEAR(ans,0);
        int l=1,r=1+m;
        int cnt=0,pos;
        while(cnt<n-m)
        {
            pos=RMQ(l,r);
            ans[cnt++]=str[pos];
            l=pos+1;
            r++;
        }
        int i;
        for(i=0;i<cnt;i++)
            if(ans[i]!='0') break;
        if(i==cnt)
        {
            puts("0");
            continue;
        }
        for(;i<cnt;i++)
            printf("%c",ans[i]);
        puts("");
    }
    return 0;
}

NBU 1449:

Description

在给定的n个数字的数字串中,删除其中k(k< n)个数字后,剩下的数字按原次序组成一个新的正整数。请确定删除方案,使得剩下的数字组成的新正整数最大。

Input

输入一个由n个数字组成的正整数(1< n<=100000),再输入一个整数k(0<=k<n),表示要删除k位数字。

< n),输入的数字保证没有前导0。<n<=100000),再输入一个整数k(0<=k<n),表示要删除k位数字。

Output

输出删除k位后的最大整数

Sample Input

102 1

Sample Output

12

求删掉之后最大值,同样的方法。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int maxn=1e5+10;
int dp[maxn][20];
int num[maxn];
char str[maxn],ans[maxn];
int n,m;
int MAX(int a,int b)
{
    if(num[a]>=num[b]) return a;
    return b;
}
void init()
{
    for(int i=1;i<=n;i++)
        dp[i][0]=i;
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
            dp[i][j]=MAX(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int RMQ(int l,int r)
{
    int k=(int)(log(r-l+1)/log(2.0));
    return MAX(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
    while(~scanf("%s%d",str+1,&m))
    {
        n=strlen(str+1);
        REPF(i,1,n)
            num[i]=str[i]-'0';
        init();CLEAR(ans,0);
        int l=1,r=1+m;
        int cnt=0,pos;
        while(cnt<n-m)
        {
            pos=RMQ(l,r);
            ans[cnt++]=str[pos];
            l=pos+1;
            r++;
        }
        int i;
        for(i=0;i<cnt;i++)
            if(ans[i]!='0') break;
        if(i==cnt)
        {
            puts("0");
            continue;
        }
        for(;i<cnt;i++)
            printf("%c",ans[i]);
        puts("");
    }
    return 0;
}
时间: 2024-10-10 07:01:10

HDU 3183 A Magic Lamp(贪心+RMQ)的相关文章

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 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 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 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, unfortunately the genie in the lamp is not so kind. Kiki must answer a question

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

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

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数中第一个数,记录所选的位置

hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)

1.题目大意是,给你一个1000位的数,要你删掉m个为,求结果最小数. 思路:在n个位里面删除m个位,也就是找出n-m个位组成最小数 所以在区间 [0, m]里面找最小的数,对应的下标标号i 接着找区间 [i+1,m++]里面的最小数,对于下标为ii 接着找区间 [ii+1,m++]里面的最小数-- 这样就会找n-m个数了.区间这样安排的目的是为了保证取出来的数的顺序. 2代码: #include<cstdio> #include<cstring> #include<cmat

HDU_3183_A Magic Lamp(贪心)

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 geni

【HDOJ】3183 A Magic Lamp

RMQ. 1 /* 3183 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 1005 7 8 char s[MAXN], ans[MAXN]; 9 int dp[MAXN][MAXN]; 10 int n,len,m; 11 12 int min(int x, int y) { 13 return s[x]<=s[y] ? x:y; 14 } 15