HDU - 3183Magic Lamp (贪心或者(RMQ+鸽巢原理/抽屉定理))

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

我是贪心+STL操作写的,等会补下RMQ的操作。

这一题很明显可以贪心,删除第一个数位的时候,肯定是删除第一个非增相邻数的前一个。这样能保证最小。

因为首先你改变的数位数越大贡献越高,(即前面改变1,都比后面改变10的变小的多)。然后改变位数大的同时,保证改变后整体的数是减小的才行。所以这样是可贪的。

我查询了C++prime。。和别人的题解,终于贪出来了。。没学过C++的弊端太多了。。

#include <cstdio>
#include <string>
#include<list>
#include<cstring>
#include <algorithm>
#include<iostream>
#define M 1000000
using namespace std;
int main()
{
    int m;
    string s,q;
	while(cin>>s>>m)
    {
        int cn=-1;
        int l=s.length();
        string::iterator iter;
        for(iter=s.begin();iter!=s.end()-1;)
        {
            if(*iter<=*(iter+1))
            {
                iter++;
            }
            else
            {
                m--;
                s.erase(iter);
                if(iter!=s.begin())
                    iter--;
            }
            if(m==0)
                break;
        }
        s.erase(s.end()-m,s.end());
        iter=s.begin();
      //  cout<<*iter<<endl;
        while(*iter=='0'&&iter!=s.end())
            s.erase(iter);
        if(s.empty())
        cout<<0<<endl;
        else
            cout<<s<<endl;
    }
	return 0;
}

睡前补一下RMQ+抽屉定理的思路;

https://baike.baidu.com/item/%E6%8A%BD%E5%B1%89%E5%AE%9A%E7%90%86/10661533

这是抽屉定理百度百科,看下就应该明白。

这道题N位数,去掉m位,求剩下的数最大值。假设剩下M位,M=N-m;

贪心是贪删去的数,那我们这样想,M位数一位一位的取,

第一位肯定是在1,N-M+1这个区间里。

因为要剩下M位数,第一位假如在N-M+2位,后面只有M-2位数,加上第一位总共M-1位数,不可能构成M位数了,所以第一位在

1,N-M+1区间里。为保证第一位数最小,取1,N-M+1这个区间的最小数,(由于第一位数变化影响最大,只要满足第一位数最小,就可以把选第一位的后效影响去掉,也是一种贪心的思想)。

取完第一位后假设I位,再取I,N-I+1-M+1区间里的最小值,依次取到M位即可。

 

具体RMQ操作和代码明天再打,困了~~~

我的RMQ操作比较直接,就模拟刚才的思想,找的区间最小值,扫一遍找下标。。比较好理解,然后再删去前置0

 

我看别人大佬操作直接RMQ找最小值下标。。Orz。。

#include <cstdio>
#include <string>
#include<list>
#include<cstring>
#include <algorithm>
#include<iostream>
#define M 1000000
using namespace std;
char a[1010];
int b[1010];
int c[1010];
char mi[1010][30];
void RMQ(int n)
{
    for(int i=1;i<=n;i++)
        mi[i][0]=b[i];
    //puts("++");
    for(int j=1;1<<j<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            mi[i][j]=min(mi[i][j-1],mi[i+(1<<j-1)][j-1]);
        }
     //   puts("--");
}
int query(int l,int r)
{
    int k=0;
    while((1<<k+1)<=r-l+1)
        k++;
    return min(mi[l][k],mi[r-(1<<k)+1][k]);
}
int main()
{
    int m;
    while(~scanf("%s %d",a,&m))
    {
        int l=strlen(a);
        for(int i=0;i<l;i++)
            b[i+1]=a[i]-'0';
      /*  for(int i=0;i<l;i++)
            cout<<b[i+1];*/
        RMQ(l);
        int flag=1,cnt=0;
        for(int i=l-m;i>0;i--)
        {
            int now=query(flag,l-i+1);
           // printf("%d+++%d---  %d\n",now,flag,l-i+1);
            for(int j=flag;j<=l;j++)
                if(b[j]==now)
                {
                    flag=j+1;
                    break;
                }
            c[++cnt]=b[flag-1];
        }
        int w=1;
        while(c[w]==0)
            w++;
        if(w>cnt)
            cout<<0;
        for(int i=w;i<=cnt;i++)
            cout<<c[i];
        puts("");
    }
	return 0;
}
//56183 3

下面就是直接ST()表实现直接查询最小值的下标。

#include <cstdio>
#include <string>
#include<list>
#include<cstring>
#include<iostream>
#define M 1000000
using namespace std;
char a[1010];
int b[1010];
int c[1010];
int mi[1010][30];
int min(int i,int j)
{
    return b[i]<=b[j]?i:j;
}
void RMQ(int n)
{
    for(int i=1;i<=n;i++)
        mi[i][0]=i;
    //puts("++");
    for(int j=1;1<<j<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            mi[i][j]=min(mi[i][j-1],mi[i+(1<<j-1)][j-1]);
          //  cout<<mi[i][j]<<" "<<i<<" "<<j<<endl;
        }
     //   puts("--");
}
int query(int l,int r)
{
    int k=0;
    while((1<<k+1)<=r-l+1)
        k++;
    return min(mi[l][k],mi[r-(1<<k)+1][k]);
}
int main()
{
    int m;
    while(~scanf("%s %d",a,&m))
    {
        int l=strlen(a);
        for(int i=0;i<l;i++)
            b[i+1]=a[i]-'0';
        RMQ(l);
        int flag=1,cnt=0;
       /* for(int i=1;i<=l;i++)
            cout<<query(1,i)<<endl;*/
        for(int i=l-m;i>0;i--)
        {
            int now=query(flag,l-i+1);
          //  printf("%d+++%d---  %d\n",now,flag,l-i+1);
            c[++cnt]=b[now];
            flag=now+1;
        }
        int w=1;
        while(c[w]==0&&w<=cnt+2)
            w++;
          //  cout<<w<<"+++"<<cnt<<endl;
        if(w>cnt)
            cout<<0;
        for(int i=w;i<=cnt;i++)
            cout<<c[i];
        puts("");
    }
	return 0;
}
//56183 3

人一我百,人百我万。夕林山寸,寻梦指尖!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值