title: Blue bridge simulation _ _ Q10 party playbill
categories:
- ACM
- RMQ
tags: - ST
date: 2020-03-27 18:20:44
The most frequent queries value for the interval is traversed overhead is very large, it produces a range of best value queries this algorithm, we talk about the ST + RMQ, ST is sparse mean, this approach requires data must be static It can not change.
Issues into
There is a one array (of length n), frequent queries [s, e] most value interval.
Detailed algorithms
Define a two-dimensional array ST (size [n, lgn]). ST[i][j]
I represents the beginning from (including i) rearwardly 2 J-th element value most section, i.e. the interval [i, i + 2 J-. 1] most value, then it is obvious ST[i][0]=a[i]
(because [i, i] value is only a ). Now we start recursive derivation, [I, I + 2 J-. 1] can be divided into two according to the size of the intermediate 2 (. 1-J) interval [I, I + 2 (. 1-J) -1] and [I + 2 (-J. 1), I + 2 J-. 1], then [I, I + 2 J-. 1] value of the two most sections then most value, i.e. [I, I + 2 J -1] = min / max ([I, I 2 + (. 1-J) -1], [I 2 + (. 1-J), I 2 + J-. 1]) is:
ST[i][j]=min/max(ST[i][j-1],ST[i+2^j][j-1])
Then you can find an element to calculate the value of the j-th column is saying only two values of a previous column about, and we have just considered a good first row, first column can be calculated by the second column, the second column The third column can be calculated to count ...... so where one does? ST[i][j]
It represents the interval [I, I + 2 J-. 1] + 2 obviously I J-. 1 <=. 1-n-(n-elements, the subscript 0 ~ n-1) so there is a built templates ST:
void cal(int a[],int n)
{
for(int i=0;i<=n-1;i++)
ST[i][0]=a[i];
for(int j=1;(1<<(j))<=n;j++)
{
for(int i=0;i+(1<<j)-1<=n-1;i++)
{
st[i][j]=max/min(st[i][j-1],st[i+1<<(j-1)][j-1]);
}
}
}
With this table for a long time to say that how to use it? If we want to calculate the interval [s, e] the best value for how to count it? Is that we can use ST[此处可为任意数][此处必须为2的幂]
if we can find a k, so that the interval [s, e] into [S, S + 2 K-. 1] and `[s + 2 ^ k] [e]`, we can use the table , but you will not find a second interval that we can only express the length of the interval is a power of 2. So smart people will find that using e minus the power of two is not on the line! Thus the [s, e] into [S, S + 2 K-. 1] and [e-2 ^ k + 1 , e], so that the interval [s][e]
most value can be changed min/max(ST[s][k],ST[e-2^K+1][e])
but it was possible to turn the question, If the intersection of the two sets do not how to do? For example [2,6] into [2,3] and [5,6] (k = 1) , thus obtained, then obviously not ah! So we need to find a relatively large value of k, [S, 2 S + k-. 1] must be included in the `[s] [e]` inside, i.e. 2 + S k. 1-<solve for E =
and (int) removes the fractional portion k of k, that is 0 <= k- (int) kso the k-1 <(int) ksubstituting the original formula:
So when k = (int) (log2 ( e-s + 1)) , the ST[s][k],ST[e-2^K+1][e]
interval must be indicated by the intersection. So in claim [s, e] the best values requires calculation intervalmin/max(ST[s][k],ST[e-2^K+1][e]),k=(int)(log2(e-s+1))
example
Q10 party playbill
topic
【Problem Description】
Xiao Ming to organize a party, prepared a total of n program. Then the evening time is limited, he can only chose one of the m program.
This program is in the order of the n Xiaoming contemplated given the order can not be changed.
Xiao Ming found that the audience likes the degree of the party has a very large extent the relationship and look good the first few shows, he hopes the first program selected as good-looking, in this context you want the second program as good-looking, in turn analogy.
Xiao Ming to each program defines a nice value, you help Xiao Ming selected m programs, meet his demands.
[Input Format]
The first line of input contains two integers n, m, and the number represents the number of a program to be selected.
The second line contains n integers, followed Attractive value for each program.
[Output format]
M output line contains integers, looking for the value of the selected program.
[Sample input]
5 3
3 1 2 5 4
[Sample output]
3 5 4
[Sample Description]
Selecting the first, 4, 5 programs.
[Evaluation] Examples scale and agreed with
For 30% of the evaluation use cases, 1 <= n <= 20 ;
for 60% of the evaluation use cases, 1 <= n <= 100 ;
for all reviews use cases, 1 <= n <= Attractive value 100000,0 <= program <= 100,000.
Thinking O (N ^ 2)
ST+RMQ
Reference Code
#include<cstdio>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#define PP(a,b) cout<<a<<"="<<b<<endl
#define P(a) cout<<a<<endl
#define print(a) cout<<#a<<"="<<a<<endl
#define _for(i,s,e) for(int i=s;i<=e;i++)
using namespace std;
int n,m;
int a[100001];
int st[100001][20];
void cal(int a[],int n)
{
for(int j=1;(1<<(j-1))<=n;j++)
{
for(int i=0;i+(1<<j)-1<=n-1;i++)
{
st[i][j]=max(st[i][j-1],st[i+1<<(j-1)][j-1]);
}
}
}
int query(int s,int e)
{
int t=(int)(log(e-s+1)/log(2));
return max(st[s][t],st[e-(1<<t)+1][t]);
}
int main()
{
//freopen("input.txt","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
_for(i,0,n-1)
{
cin>>a[i];
st[i][0]=a[i];
}
cal(a,n);
int s=0,k=m;
while(k!=0)
{
int z=query(s,n-1-k+1);
cout<<z<<" ";
s=z+1;
k--;
}
}