Hdu 3603

在多大学训练中,教练要求所有ACM队员早起,但总是有人迟到。教练使用了一个特殊的序列算法来惩罚迟到者,通过从序列中选择子序列并找出最长的不重复子序列长度来决定惩罚。该算法利用了二分查找和RMQ技巧解决复杂查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Coach Yehr’s punishment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 397    Accepted Submission(s): 114


Problem Description
During the Mult-University Trainging,Coach Yehr asks all the ACM teammates to jog at 6:30AM.But 6:30 is too early,there are always somebody might be late.Coach Yehr likes AC sequence very much,the AC sequence is a number sequence with all the elements different.A sequence (S1 ,S2 ,S3 ……Sn ) is a AC sequence if S1 ,S2 ,S3 ……Sn are all different. There are N teammates,the time(in second time) every teammate’arrival make a number sequence with length N. In order to punish the laters,Coach Yehr give them a puzzle,Coach Yehr choose a subsequence from Sa to Sb ,the laters must tell Coach Yehr the longest length of AC sequence in the subsequence as soon as possible.

 

Input
There are multiply text cases.You must deal with it until the end of file.
The first line of each test case is an interger N,indicates the number of ACM teammates;
The second line have N intergers,the i-th number indicates the i-th teammate’s arrival time.
The third line is an interger M indicates Coach Yehr will ask M times;
The follow M lines,each line have two intergers a and b,indicate the interval of the sequence.

 

Output
For each query,you have to print the longest length of AC sequence in the subsequence in a single line.

 

Sample Input
8 3 2 5 6 8 3 2 6 2 2 4 1 8 6 5 3 1 2 3 4 1 6 3 3

 

Sample Output
3 5 4 2

 二分+RMQ, 特别注意存在 x > y的查询。

Accepted Code:

 1 /*************************************************************************
 2     > File Name: 3603.cpp
 3     > Author: Stomach_ache
 4     > Mail: sudaweitong@gmail.com
 5     > Created Time: 2014年07月10日 星期四 21时17分41秒
 6     > Propose: 
 7  ************************************************************************/
 8 
 9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <fstream>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 
18 const int MAX_N = 300002;
19 
20 int n, m;
21 int vis[MAX_N], len[MAX_N], rmq[MAX_N][25];
22 
23 void rmq_init() {
24       for (int i = 1; i <= n; i++) rmq[i][0] = len[i];
25     for (int j = 1; (1<<j) <= n; j++) {
26           for (int i = 1; i+(1<<(j-1))-1 <= n; i++) {
27               rmq[i][j] = max(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);
28         }
29     }
30 }
31 
32 int RMQ(int L, int R) {
33       int k = (int)(log(R - L + 1.0) / log(2.0));     
34     return max(rmq[L][k], rmq[R-(1<<k)+1][k]);
35 }
36 
37 int solve(int x, int y) {
38       int  = x, R = y, ans = -1;
39     while (L <= R) {
40         int M = (L + R) / 2;
41         if (len[M] >= M - x + 1) {
42               L = M + 1;
43         } else {
44               ans = M;
45             R = M - 1;
46         }
47     }
48     if (ans == -1) return y - x + 1;
49     return max(ans - x, RMQ(ans, y));
50 }    
51 
52 int 
53 main(void) {
54     while (~scanf("%d", &n)) {
55           memset(vis, 0, sizeof(vis));
56           memset(len, 0, sizeof(len));
57         int begin = 1;
58           for (int i = 1; i <= n; i++) {
59               int tmp;
60             scanf("%d", &tmp);
61             len[i] = min(i-begin+1, i-vis[tmp]);
62             begin = max(begin, vis[tmp]+1);
63             vis[tmp] = i;
64         }
65         //for (int i = 1; i <= n; i++) printf("%d\n", len[i]);
66         rmq_init();
67         scanf("%d", &m);
68         while (m--) {
69               int x, y;
70             scanf("%d %d", &x, &y);
71             // 测试数据可能会有 x > y 的情况
72             if (x > y) swap(x, y);
73             printf("%d\n", solve(x, y));
74         }
75     
76     }
77 
78     return 0;
79 }

 

转载于:https://www.cnblogs.com/Stomach-ache/p/3837269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值