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

时间:2023-03-08 17:30:01

原题目

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

********************************************************************************************************************************************************************************************

题意:给你一个数字串 (每一个串有n个数字), 和一个m (要你删除m个数字 ), 使得剩下的数字组成的整数最小。

一开始想了一下删除m个,等价与在数字串中选择 n-m 个数字组成最小整数。

一开始一直做不出,贪心有点问题。后来被dalao教一下就懂了;

贪心的方法:

我们先假设 n 是串的个数, m 是要删除的个数, need 是需要选的个数;

1) 因为是按照顺序组合数字的, 所以第一个删除的数字一定在  [0, n-need] 之内;

证明: 假设数字有 a0, a1, a2, a3, a4, a5 个,要删除2个,n=6, m=2, need = 4;

因为是连续选择的,我们假设在 [0, 3]之内选择第一个,那么我们只剩下a4, a5了,无法构成4个;

所以假设最后的need-1个我们都要, 那么 从 0 ~ (n - 1) - ( need - 1 )==[0, n-need]之内第一个一定在里面(数组从0开始,所以 n-1);

2) 当在[0, n-need]之内选好了第一个,找到第一个的位置pre,那么下一个 数字一定在 [ pre + 1, n-need + 1 ], 就可以一直找下去了,记录你找到的数;

3) 最后在你找到的数里面讨论前导0的情况。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
const int maxn = +;
int d[maxn][maxn];
int a[maxn];
void RMQ_init(string s)
{
memset(d, , sizeof(d));
memset(a, , sizeof(a));
int n = s.size();
for(int i=;i< n ;i++) a[i]=d[i][]=s[i]-'';
for(int j = ; (<<j) <= n ;j++)
for(int i = ; i+(<<j ) - < n ;i++)
d[i][j] = min(d[i][j-] , d[i+(<<(j-))][j-] );
}
int RMQ(int L, int R)
{
int k=;
while((<<(k+)) <= R-L+) k++;
return min(d[L][k], d[R-(<<k) + ][k]);
}
int findmin(int L, int R, int Min)
{
for(int i=L; i <= R ;i++){
if(a[i] == Min) return i;
}
}
int main()
{
string s;
int m, n, need;
while(cin >> s >> m){
RMQ_init(s);
n = s.size() ;
need = n - m;
int Left=, Right = n - need;
// cout << Left << " " << Right << endl;
// cout << RMQ(Left, Right) << endl;
deque <int > q;
while(){
if(need == ) break;
int Min = RMQ(Left, Right);
// cout << Min << endl;
q.push_back(Min);
int pre = findmin(Left, Right, Min);
Left = pre+;
Right ++ ;
need --;
}
while(!q.empty()){
if(q.front() == ) q.pop_front();
else break;
}
if(q.empty()){
cout << "";
}
else{
while(!q.empty()){
cout << q.front();
q.pop_front();
}
}
cout << endl;
}
return ;
}