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

题意:给你一个数,在1000位内(字符串),删掉m个数,得到最小的数输出;

rmq :思路:第一位在len-m内;找到第n位以后,最后留下没有找数的数目(len-m-n),在n之后的区间找n+1;(0ms)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - ‘0‘ ;
    while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ )
        res = res * 10 + ( ch - ‘0‘ ) ;
    return res ;
}
char a[10100];
int dp[10100][30];//存位置
char ans[10100];
int minn(int x,int y)
{
    return a[x]<=a[y]?x:y;
}
void rmq(int len)
{
    for(int i=0; i<len; i++)
    dp[i][0]=i;
    for(int j=1; (1<<j)<len; j++)
    for(int i=0; i+(1<<j)-1<len; i++)
    dp[i][j]=minn(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int query(int l,int r)
{
    int x=(int)(log((double)(r-l+1))/log(2.0));
    return minn(dp[l][x],dp[r-(1<<x)+1][x]);
}
int main()
{
    int x,y,z,i,t;
    while(~scanf("%s%d",a,&x))
    {
        int len=strlen(a);
        rmq(len);
        int st=0,en=x;
        int flag=0;
        for(i=0; i<len-x; i++)
        {
            int number=query(st,en);
            //cout<<st<<" "<<en<<" "<<number<<endl;
            st=number+1;
            en=i+x+1;
            ans[flag++]=a[number];
        }
        for(i=0; i<flag; i++)
            if(ans[i]!=‘0‘)
                break;
        for(t=i; t<flag; t++)
            printf("%c",ans[t]);
        if(i==flag)
            printf("0");
        printf("\n");
    }
}

暴力思路:在字符串中查找a[i]>a[i+1];找到第一个删掉;如果没有,说明是一直上升;最后的数最大,删掉最后一个;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) )
    {
        if( ch == EOF ) return 1 << 30 ;
    }
    res = ch - ‘0‘ ;
    while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ )
        res = res * 10 + ( ch - ‘0‘ ) ;
    return res ;
}
vector<int>v;
char a[1010];
int main()
{
    int x,y,z,i,t;
    while(~scanf("%s%d",a,&x))
    {
        v.clear();
        int len=strlen(a);
        for(i=0; i<len; i++)
            v.push_back(a[i]-‘0‘);
        while(x--)
        {
            int flag=1;
            int len=v.size();
            for(i=0; i<len-1; i++)
            {
                if(v[i]>v[i+1])
                {
                    flag=0;
                    v.erase(v.begin()+i);
                    break;
                }
            }
            if(flag)
                v.erase(v.begin()+len-1);
        }
        for(i=0; i<v.size(); i++)
            if(v[i])
                break;
        for(t=i; t<v.size(); t++)
            printf("%d",v[t]);
        if(i==v.size())
            printf("0");
        printf("\n");
    }
    return 0;
}

时间: 2024-10-17 16:05:15

hdu 3183 A Magic Lamp rmq或者暴力的相关文章

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问题, 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(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(贪心+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

第一种做法是贪心做法,只要前面的数比后面的大就把他删掉,这种做法是正确的,也比较好理解,这里就不说了,我比较想说一下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 (贪心)

///给你一数字,删除其中的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-3183A Magic Lamp (RMQ)

hdu-3183A Magic Lamp 题意:对给定m位数删除其中n位,不改变剩余数字排列,求剩余数字组成的最小数. 思路:选m - n个数 RMQ求最小值 t数组求最小值第一次出现的位置 左右区间随之改变 wa了两次 没特判 m = n 的情况 / t数组存了最小值最后一次出现的位置(25行没用小于等于号用了小于号) 1 #include<map> 2 #include<set> 3 #include<cmath> 4 #include<cstdio>

【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