首页 > 代码库 > [HDOJ1806]Frequent values(RMQ,ST)

[HDOJ1806]Frequent values(RMQ,ST)

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1806

题意:给1~n个单调不递减的数,求q次区间出现次数最多的那个数的次数。

由于单调不递减,是有一个顺序的。则可以考虑记录一个b(i)=第i个数字在前i个里出现了i次,一定是连续的。这样把它丢进st表里,query(l,r)表示的是区间l,r内出现最多的那个数的次数。但是有个问题,那就是最左端有可能不是从1开始的,则要先处理最左端,找到最左的第一个1的位置p,然后用p-l则就是那个数字出现的次数。

  1 /*  2 ━━━━━┒ギリギリ♂ eye!  3 ┓┏┓┏┓┃キリキリ♂ mind!  4 ┛┗┛┗┛┃\○/  5 ┓┏┓┏┓┃ /  6 ┛┗┛┗┛┃ノ)  7 ┓┏┓┏┓┃  8 ┛┗┛┗┛┃  9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &(a)) 43 #define Rll(a) scanf("%lld", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onenum(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long Uint; 62 typedef pair<int, int> pii; 63 typedef pair<LL, LL> pLL; 64 typedef pair<string, LL> psi; 65 typedef map<string, LL> msi; 66 typedef vector<LL> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70  71 const int maxn = 100100; 72 int n, q; 73 int a[maxn]; 74 int b[maxn]; 75 int dp[maxn][20]; 76  77 void st() { 78     for(int i = 1; i <= n; i++) dp[i][0] = b[i]; 79     int k = int(log(n+1.0)/log(2.0)); 80     for(int j = 1; j <= k; j++) { 81         for(int i = 1; i + (1 << j) - 1 <= n; i++) { 82             dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]); 83         } 84     } 85 } 86  87 int query(int l, int r) { 88     int j = int(log(r-l+1.0)/log(2.0)); 89     return max(dp[l][j], dp[r-(1<<j)+1][j]); 90 } 91  92 int main() { 93   // FRead(); 94     int l, r; 95     while(~Rint(n) && n) { 96         Rint(q); 97         For(i, 1, n+1) { 98             Rint(a[i]); 99             if(i == 1) {100                 b[i] = 1;101                 continue;102             }103             if(a[i] == a[i-1]) b[i] = b[i-1] + 1;104             else b[i] = 1;105         }106         st();107         W(q) {108             scanf("%d%d",&l,&r);109             int m = l;110             while(m <= r && a[m] == a[m-1]) m++;111             int ret = max(query(m, r), m - l);112             printf("%d\n", ret);113         }114     }115     RT 0;116 }

 

[HDOJ1806]Frequent values(RMQ,ST)