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. 【转载】Fiddler抓包及模拟服务端

    此文章转载公众号‘云测学院'链接:https://mp.weixin.qq.com/s/qXmBDh980nBJ8IchbRGC3Q 及公众号gloryroadtrain 在HTTP接口的测试过程中, ...

  2. UNION All中ORDER By的使用

    一个sql中,union了几个子查询.单独执行每个子查询都没问题,但union后执行,报ORA-00904: "xxx": invalid identifier关于union的使用 ...

  3. Error: Could not link: /usr/local/share/doc/homebrew

    mac 执行brew update 报错 Error: Could not link: /usr/local/share/doc/homebrew 更新brew,报错 Error: Could not ...

  4. [转]C# 委托、事件,lamda表达式

    1. 委托Delegate C#中的Delegate对应于C中的指针,但是又有所不同C中的指针既可以指向方法,又可以指向变量,并且可以进行类型转换, C中的指针实际上就是内存地址变量,他是可以直接操作 ...

  5. 转载 ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(一) 整理基础数据

    ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(一) 整理基础数据   最近碰巧发现一款比较好的Web即时通讯前端组件,layim,百度关键字即可,我下面要做的就是基于这个前 ...

  6. Webpack下创建vue项目-非vue-cli

    开始准备 初始化工程目录 shell npm init -y 安装vue npm install vue 安装 webpack npm install webpack --save-dev webpa ...

  7. Python模块安装方式

    一.方法1: 单文件模块直接把文件拷贝到 $python_dir/lib/python3.4/site-packages/ 二.方法2: 多文件模块,带setup.py 下载模块包,进行解压,进入模块 ...

  8. 2019-7-15-win10-uwp-在笔迹开始书写拿到书写移动事件

    title author date CreateTime categories win10 uwp 在笔迹开始书写拿到书写移动事件 lindexi 2019-7-15 8:58:5 +0800 201 ...

  9. Redis源码解析:12AOF持久化

    除了RDB持久化功能之外,Redis还提供了AOF(AppendOnly File)持久化功能.与RDB持久化通过保存数据库中的键值对来记录数据库状态不同,AOF持久化是通过保存Redis服务器所执行 ...

  10. Django项目:CRM(客户关系管理系统)--20--12PerfectCRM实现King_admin分页上下页

    {#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...