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. java扫描某个包下的所有java类并加载

    最近在学习java的反射和注解,实际情景中需要扫描某个包下的所有java类,然后使用类加载器加载类. 基本思路,获得程序的路径扫描src下某个包内的子包和java类,实现也比较简单. 运行环境:win ...

  2. 20-iframe

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. C++ 赋值函数为什么返回reference to *this?

    赋值操作为什么要返回 reference to *this? 要弄清这个问题之前,先了解函数的返回值类型:返回值类型,返回引用类型 返回值类型:返回的是一个对象的副本. test operator= ...

  4. 实现一个koa-logger中间件

    //koa-logger.js module.exports = async(ctx,next)=>{ const start = new Date().getTime() // 中间件异步处理 ...

  5. JAXL连接Openfire发送房间消息

    使用composer形式安装的JAXL <?php require_once "vendor/autoload.php"; $client = new JAXL(array( ...

  6. 关于set的unordered特性

    关于set排序无序的问题,原因是set使用哈希表做内存索引. 详细介绍可见: https://stackoverflow.com/questions/12165200/order-of-unorder ...

  7. Django项目:CRM(客户关系管理系统)--57--47PerfectCRM实现CRM客户报名流程02

    图片另存为  16*16  名字修改为      bpm_logo.jpg /*! *bootstrap.js * * Bootstrap v3.3.7 (http://getbootstrap.co ...

  8. VS 快捷键和正则替换

    本文在VS2017中可用 1.注释 :Ctrl  K C 取消注释: Ctrl K U 2.整理代码格式: Ctrl K D 或者 Ctrl K F 3.快速切换不同的代码窗口  Ctrl+Tab 4 ...

  9. CCS开发指南

    第一章  CCS概述 1 1.1 CCS概述 1 1.2 代码生成工具 3 1.3 CCS集成开发环境 5 1.3.1 编辑源程序 5 1.3.2创建应用程序6 1.3.3 调试应用程序 6 1.4  ...

  10. Windows下shell神器

    想找一个可以在Windows平台玩命令行的东西,不想装虚拟机搞linux,所以找到两个神器 如何升级Babun中的Git Babun中默认已经集成Git,只是有可能不是最新的版本 如果只是更新Babu ...