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:

 /*************************************************************************
> File Name: 3603.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年07月10日 星期四 21时17分41秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAX_N = ; int n, m;
int vis[MAX_N], len[MAX_N], rmq[MAX_N][]; void rmq_init() {
for (int i = ; i <= n; i++) rmq[i][] = len[i];
for (int j = ; (<<j) <= n; j++) {
for (int i = ; i+(<<(j-))- <= n; i++) {
rmq[i][j] = max(rmq[i][j-], rmq[i+(<<(j-))][j-]);
}
}
} int RMQ(int L, int R) {
int k = (int)(log(R - L + 1.0) / log(2.0));
return max(rmq[L][k], rmq[R-(<<k)+][k]);
} int solve(int x, int y) {
int = x, R = y, ans = -;
while (L <= R) {
int M = (L + R) / ;
if (len[M] >= M - x + ) {
L = M + ;
} else {
ans = M;
R = M - ;
}
}
if (ans == -) return y - x + ;
return max(ans - x, RMQ(ans, y));
} int
main(void) {
while (~scanf("%d", &n)) {
memset(vis, , sizeof(vis));
memset(len, , sizeof(len));
int begin = ;
for (int i = ; i <= n; i++) {
int tmp;
scanf("%d", &tmp);
len[i] = min(i-begin+, i-vis[tmp]);
begin = max(begin, vis[tmp]+);
vis[tmp] = i;
}
//for (int i = 1; i <= n; i++) printf("%d\n", len[i]);
rmq_init();
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d %d", &x, &y);
// 测试数据可能会有 x > y 的情况
if (x > y) swap(x, y);
printf("%d\n", solve(x, y));
} } return ;
}

最新文章

  1. (jms)ActiveMQ 安装配置.
  2. ios 区域检测 使用coreLocation
  3. DOMContentLoaded和load
  4. PHP--目录处理
  5. KMP学习总结
  6. android91 代码注册广播接收者
  7. 【小程序开发】微信小程序开发中遇到的那些坑...
  8. sql中select语句的逻辑执行顺序
  9. php-GD库函数(三)
  10. MEAN教程2-Nodejs安装
  11. ldap数据库--ODSEE--ACI
  12. openssl中RSA数字签名的使用
  13. [转] 使用babel-plugin-react-css-modules简化CSS Modules的使用
  14. Python 装饰器(笔记,非原创)
  15. [ctsc2018] 混合果汁 【可持久化线段树】【二分答案】
  16. elasticsearch基本操作之--使用QueryBuilders进行查询
  17. Java并发编程的艺术(五)——中断
  18. Python 函数装饰器简明教程
  19. 假·最大子段和 (sdutoj 4359 首尾相连)(思维)
  20. OpenCV Harris 角点检测子

热门文章

  1. Python学习day06-Python基础(4)流程控制之while和for循环
  2. CAS添加验证码功能
  3. Python3读取深度学习CIFAR-10数据集出现的若干问题解决
  4. Tomcat--远程Debug以及参数配置调优
  5. https://blog.csdn.net/u012235003/article/details/54576737
  6. MySQL时间格式转换
  7. jmeter设置代理
  8. ECMAScript 5 严格模式
  9. A20地址线科普【转载】
  10. 探索云网络技术前沿,Sigcomm 2019 阿里云参会分享