uva11235(Frequent values)(HDU1806)

题目地址:Frequent values

题目大意、解题思路:  见白皮书p198.

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <algorithm>
 5 #include <vector>
 6 using namespace std;
 7 const int M=100100;
 8 int a[M];
 9 int val[M],cnt[M];
10 int num[M],left[M],right[M];
11 int d[M][20];
12 int id;
13 void RMQ_init(int A[])
14 {
15     int i,j;
16     for(i=0; i<id; i++)
17         d[i][0]=A[i];
18     for(j=1; (1<<j)<=id; j++)
19         for(i=0; i+(1<<j)-1<id; i++)
20             d[i][j]=max(d[i][j-1],d[i+(1<<(j-1))][j-1]);
21 }
22 int RMQ(int L,int R)
23 {
24     int k=0;
25     while((1<<(k+1))<=R-L+1)
26         k++;
27     return max(d[L][k],d[R-(1<<k)+1][k]);
28 }
29 int main()
30 {
31     int n;
32     while(scanf("%d",&n)&&n)
33     {
34         int q;
35         scanf("%d",&q);
36         int i,j;
37         id=0;
38         scanf("%d",&a[1]);
39         val[++id]=a[1];
40         left[id]=1;
41         int xu=1;
42         num[1]=xu;
43         for(i=2; i<=n; i++)
44         {
45             scanf("%d",&a[i]);
46             if (a[i]!=a[i-1])
47             {
48                 val[++id]=a[i];
49                 left[id]=i;
50                 right[id-1]=i-1;
51                 cnt[id-1]=i-left[id-1];
52                 xu++;
53             }
54             num[i]=xu;
55         }
56         right[id]=i-1;
57         cnt[id]=i-left[id];
58         RMQ_init(cnt);
59         int xu1,xu2;
60         int  sum1=0,sum2=0,sum3=0,sum=0;
61         while(q--)
62         {
63             int x,y;
64             scanf("%d%d",&x,&y);
65             xu1=num[x];
66             xu2=num[y];
67             if (xu1!=xu2)
68             {
69                 sum1=right[xu1]-x+1;
70                 sum3=y-left[xu2]+1;
71                 sum=sum1;
72                 if (sum<sum3)
73                     sum=sum3;
74                 if (xu1+1<=xu2-1)
75                 {
76                     sum2=RMQ(xu1+1,xu2-1);
77                     if (sum<sum2)
78                         sum=sum2;
79                 }
80             }
81             else
82             {
83                 sum=y-x+1;
84             }
85             printf("%d\n",sum);
86         }
87     }
88     return 0;
89 }
90 /*
91 10 3
92 -1 -1 1 1 1 1 3 10 10 10
93 2 3
94 1 10
95 5 10
96 0
97
98 */

时间: 2024-08-28 12:25:16

uva11235(Frequent values)(HDU1806)的相关文章

UVA-11235 Frequent values (RMQ)

题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素所属的段num(i).每一个元素所属段的左端点l(i)及右端点r(i).那么对于每次询问: ans(i,j)=max(max(r(i)-i+1,j-l(j)+1),rmq(num(i)+1,num(j)-1)). ans(i,j)=r-j+1 (如果i与j属于同一段) 代码如下: # include

UVA 11235 Frequent values(RMQ)

Frequent values TimeLimit:3000Ms You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most f

【POJ 3368】 Frequent values(RMQ)

[POJ 3368] Frequent values(RMQ) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15813   Accepted: 5749 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given seve

POJ 3368 Frequent values(RMQ)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15134   Accepted: 5519 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries cons

[uva11235]Frequent values(RMQ,ST,离散化)

题目链接:https://vjudge.net/problem/UVA-11235 题意:给一串不递减数字,q次询问,每次查询[l,r]内出现次数最多的数字出现的次数. 查询分两部分:一部分是[l,r]为同一个数的区间,另一部分则是在上下界处截取一部分的情况. 首先离散化,后用l[],r[],v[]分别记录每一段相同数字的左右区间和出现次数.v可以直接由r-l+1更新得到. 第一部分更新ret后,接下来的rmq分三部分: 第一部分查询左边界截取一部分的数字的当前长度,第二部分查询右边界的,第三部

UVA11235 - Frequent values(BMQ)

题目链接 题目大意:可以一串不递减的序列,然后给你一个范围L,R,要求你返回L,R出现最多次的那个值的出现次数. 解题思路:将这个序列重新编码一下,把相同的数字标记成一段,然后用num记录是哪一段,用cnt记录一下出现了多少个相同的.然后查询的时候因为可能出现从一段中的某个部分开始的情况,所以要先将头和尾处理一下,标记每一段的最左端和最右端位置.中间完整的部分用BMQ. #include <cstdio> #include <cstring> #include <vector

poj 3368 Frequent values(线段树解法)

题目链接:http://poj.org/problem?id=3368 题目大意:给你一段不下降的序列,求给定区间里出现次数最多的那个数字的次数. 思路:首先看到这题时,第一感觉线段树,但是仔细一看问题来啦,用线段数我怎么才能计算出某段区间里出现的那个数,因为出现最多的那个数可能不是在他它的左儿子上也不是在它的右儿子上,可能在当他们合并成一个区间时就出现啦,但是这儿我们需要注意的就是,题目给的是一段不下降的序列,那么突破口就出来啦,因为如果出现相同的数字,那么它们一定是连续的.所以我们只需要在普

poj 3368 Frequent values(线段树)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries cons

【暑假】[实用数据结构]UVa11235 Frequent values

UVa 11235 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11241   Accepted: 4110 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several qu