Hdu 3603
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): 114Problem DescriptionDuring 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.InputThere 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.OutputFor each query,you have to print the longest length of AC sequence in the subsequence in a single line.Sample Input8
3 2 5 6 8 3 2 6
2
2 4
1 8
6
5 3 1 2 3 4
1 6
3 3Sample Output3
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的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- java主函数参数传递args
a.javaJava应用程序的主入口方法main(String[] args),表示该方法需要接收一个字符串数组类型的参数, 如果该参数不指定,agrs接收的是null. 程序: public C ...
- java基础之DateFormat类
DateFormat DateFormat类概述 DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateFor ...
- 通过media媒体查询设置ie7/8样式、使用media判断各机型、手淘flexible.js
@media all and (min-width:1280px){ /* 所有设备宽度大于1280干嘛干嘛嘞... */ body{ background:#f00; } } @media (min ...
- light oj 1079 01背包
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- 分享非常漂亮的WPF界面框架源码及插件化实现原理
在上文<分享一个非常漂亮的WPF界面框架>中我简单的介绍了一个界面框架,有朋友已经指出了,这个界面框架是基于ModernUI来实现的,在该文我将分享所有的源码,并详细描述如何基于Mod ...
- linux学习(二)-----Linux 的目录结构、远程登录、vi和vim
linux目录结构 基本介绍 linux 的文件系统是采用级层式的树状目录结构,在此结构中的最上层是根目录“/”,然后在此 目录下再创建其他的目录. 目录结构具体介绍 Linux 目录总结 1.lin ...
- vue 路由重定向
- GVEdit中使用graphviz
官方文档 安装完graphviz后,文档在安装目录下,位置如下 E:\Gra2.38\share\graphviz\doc\html 中文乱码解决 将文件保存为utf-8编码 fontname=&qu ...
- JAVA面试常见问题之常见集合篇
1.List 和 Set 区别 List 可以允许重复的对象. 可以插入多个null元素. 有序容器 Set 不允许重复的对象. 只能插入1个null元素 无序容器,可以使用TreeSet实现有序 2 ...
- 使用代码创建rabbitmq交换机和队列绑定
1.获取channel对象 2.声明(创建)对列 // 第一个参数,queueName:对列名称.数据类型:String// 第二个参数,durable:是否持久化, 队列的声明默认是存放到内存中的, ...