RMQ(鸽巢原理或字符串操作)
Posted nonames
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RMQ(鸽巢原理或字符串操作)相关的知识,希望对你有一定的参考价值。
http://acm.hdu.edu.cn/showproblem.php?pid=3183
A Magic Lamp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8310 Accepted Submission(s): 3296
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?
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.
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.
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
Recommend
思路主要是这样的: 因为需要你删除m个数使得结果最小,所以每次对字符串进行一次遍历,从前往后,只要a[i]>a[j] (j的位置为i后面未标记的第一个) 则对a[i]进行一次标记(赋赋值)将其除外,进行n次遍历,这样就删除了n个数字。因为删除的都是和后面比较相比下大的,所得结果当然就最小了。
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <algorithm> #include <iostream> #include<cstdio> #include<string> #include<cstring> #include <stdio.h> #include <string.h> #include <vector> #define ME(x , y) memset(x , y , sizeof(x)) #define SF(n) scanf("%d" , &n) #define rep(i , n) for(int i = 0 ; i < n ; i ++) #define INF 0x3f3f3f3f using namespace std; char s[1009] ; int a[1009]; int b[1009]; int main() int n , m ; while(~scanf("%s%d" , s , &m)) n = strlen(s); memset(a , 0 , sizeof(a)); for(int i = 0 ; i < n ; i++) a[i] = s[i] - ‘0‘; for(int i = 0 ; i < m ; i++)//找到m个要删除的数 for(int j = 0 ; j < n ; j++) // if(a[j] > 0)//排除标记的 // int k ; for(k = j + 1 ; k < n ; k++) if(a[k] >= 0)//排除标记的 break ; if(a[j] > a[k])//找第一个开始递减的数 a[j] = -1 ; break ; // int flag = 0 , len = 0 ; for(int i = 0 ; i < n ; i++) if(!flag && a[i] == 0)//去前置零 continue ; if(a[i] < 0) continue ; b[len++] = a[i]; if(len > 0) flag = 1 ; for(int i = 0 ; i < len ; i++) printf("%d" , b[i]); if(len == 0) printf("0"); printf("\n"); return 0 ;
RMQ:
因为要找n-m个数,删除m个数。所以原数的第1位到m+1位的数字中最小的那位(假设是第i位)肯定是n-m位数的第一位。(想想为什么)
这样我们就找到了第一位a[i],接下来我们在从第i+1位数到m+2位数中找最小的那位,这个肯定是n-m位数的第二位。
以此类推,找够n-m位即可。
RMQ函数要做点修改。dmin[i][j]=k表示的是区间[i,i+(1<<j)-1]内最小值的下标而不是值了。
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <algorithm> #include <iostream> #include<cstdio> #include<string> #include<cstring> #include <stdio.h> #include <string.h> #include <vector> #define ME(x , y) memset(x , y , sizeof(x)) #define SF(n) scanf("%d" , &n) #define rep(i , n) for(int i = 0 ; i < n ; i ++) #define INF 0x3f3f3f3f using namespace std; char s[1009] ; int a[1009]; int b[1009] , dp[1009][25]; int m , n ; int Min(int x , int y) return s[x] <= s[y] ? x : y ; void RMQ() memset(dp , 0 , sizeof(dp)); for(int i = 0 ; i < n ; i++) dp[i][0] = i ; for(int j = 1 ; j < 20 ; j++) for(int i = 0 ; i < n ; i++) if(i + (1 << j) - 1 < n) dp[i][j] = Min(dp[i][j-1] , dp[i+(1<<j-1)][j-1]); int query(int l , int r) int k = (int)log2(r-l+1); return Min(dp[l][k] , dp[r-(1<<k)+1][k]); vector<int>v; int main() while(~scanf("%s%d" , s , &m)) v.clear(); n = strlen(s); RMQ(); m = n - m ; int pos = 0 ; while(m--) // 在 n - m 区间至少留一个数 pos = query(pos , n - m - 1);//求的是最小值的下标 v.push_back(pos); pos += 1 ; int flag = 0 ; for(int i = 0 ; i < v.size() ; i++) if(flag) flag = 1 ; cout << s[v[i]]; else if(s[v[i]] != ‘0‘) flag = 1 ; cout << s[v[i]] ; if(!flag) cout << 0 ; cout <<endl ; return 0 ;
以上是关于RMQ(鸽巢原理或字符串操作)的主要内容,如果未能解决你的问题,请参考以下文章
Two progressions(CodeForces-125D)[鸽巢原理]
Codeforces 1178E Archaeology (鸽巢原理)