Ultraviolet A 11235-frequent values (rmq)

Source: Internet
Author: User

Ultraviolet A 11235-frequent values

Question Link

Given an ascending sequence, each time you ask for a range [L, R], you can find the maximum number of identical numbers.

Idea: rmq, because it is in ascending order, numbers of the same size must be connected to one piece. First, we can pre-process the total number of segments and the number of numbers contained in each segment, which segment corresponds to each position in the original array, the leftmost position and rightmost position. Then, you can divide the Query [L, R] into three segments each time you ask:
1. L to R [l] is a segment and the number is R [l]-l + 1.
2. L [R] To R is a segment, and the number is r-l [R] + 1
3. All the segments in the middle can be solved using rmq, with the number as the value.
The answer is a large number in the last three paragraphs.
Make sure that the selected [L, R] has only one and two segments.
Code:

# Include <cstdio >#include <cstring >#include <algorithm> using namespace STD; const int n = 100005; int N, Q, A [n], L [N], R [N], num [N], CNT [N], V, rmq [N] [20]; void Init () {v = 0; int count, pre; scanf ("% d", & Q); For (INT I = 0; I <n; I ++) {scanf ("% d ", & A [I]); If (! I | A [I]! = A [I-1]) {pre = I; if (I) CNT [V ++] = count; Count = 0;} count ++; num [I] = V; L [I] = pre;} CNT [V ++] = count; For (INT I = n-1; I> = 0; I --) {if (I = n-1 | A [I]! = A [I + 1]) {pre = I;} R [I] = pre;} void build_rmq (int * a, int N) {for (INT I = 0; I <n; I ++) rmq [I] [0] = A [I]; for (Int J = 1; (1 <j) <= N; j ++) {for (INT I = 0; I + (1 <j)-1 <n; I ++) {rmq [I] [J] = max (rmq [I] [J-1], rmq [I + (1 <(J-1)] [J-1]) ;}}} int query (int l, int R) {If (L> r) return 0; int K = 0; while (1 <(k + 1) <= r-L + 1) K ++; return max (rmq [l] [K], rmq [R-(1 <k) + 1] [k]);} Void solve () {build_rmq (CNT, V); int L, R; while (Q --) {scanf ("% d", & L, & R ); l --; r --; If (Num [l] = num [R]) printf ("% d \ n", R-l + 1 ); else printf ("% d \ n", max (query (Num [l] + 1, num [R]-1), max (R [l]-l + 1, r-l [R] + 1);} int main () {While (~ Scanf ("% d", & N) {Init (); solve ();} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.