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 ;
}

Hdu 3603的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. sip会话流程以及sip介绍(2)

    下面我们通过一个简单的场景例子来简单介绍一下 SIP 会话流程. Tom 和 Jerry 是非常好的伙伴,Tom 在他的 PC 上使用一个 SIP 的应用程序呼叫 Internet 上另一个 SIP ...

  2. Linux CentOS CapsLock 大小写反转问题 解决

    虚拟机centos7,输入大小写字母反了,开启capslock的时候变成小写字母了,关闭则变成大写了... 只需要执行 setleds +caps 或 setleds -caps 即可,如图:

  3. enctype="multipart/form-data"的form传参

    1.jsp <li class="btns"><input id="btnImport" class="btn btn-primar ...

  4. 在scrapy中将数据保存到mongodb中

    利用item pipeline可以实现将数据存入数据库的操作,可以创建一个关于数据库的item pipeline 需要在类属性中定义两个常量 DB_URL:数据库的URL地址 DB_NAME:数据库的 ...

  5. PKU 百炼OJ 简单密码

    http://bailian.openjudge.cn/practice/2767/ #include<iostream> #include <cmath> #include ...

  6. HBase实际应用中的性能优化方法

  7. istringstream字符串流对象

    1.读取字符串流对象 istringstream类用于执行C++风格的字符串流的输入操作. ostringstream类用于执行C++风格的字符串流的输出操作. strstream类同时可以支持C++ ...

  8. vue.js_04_vue.js的for循环,if判断和show显示

    1.for循环 <body> <div id="app"> <p>{{list1[0]}}</p> <hr /> < ...

  9. utils05_git在idea下的操作

    1.idea下将工程添加到本地仓库 1>找到自己的git.exe 2>创建本地的git仓库,将项目放入本地仓库 3> *从本地仓库更新 *提交到本地仓库 *比较版本差异 *丢弃我的修 ...

  10. httpclient遇到java.net.URISyntaxException: Illegal character in scheme name at index 0:

    正准备按照大佬的解决办法处理, 看会一条回复,说url有空格 检查了一下,还真是有空格 去除url中的空格,问题解除