码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3183 A Magic Lamp rmq或者暴力

时间:2016-04-13 20:37:55      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

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
题意:给你一个数,在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");
    }
}
View Code

暴力思路:在字符串中查找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;
}
View Code

 

 

hdu 3183 A Magic Lamp rmq或者暴力

标签:

原文地址:http://www.cnblogs.com/jhz033/p/5388524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!