POJ 3419 Difference Is Beautiful (DP + 二分 + rmq)
题意:给n个数(n<=200000),每个数的绝对值不超过(10^6),有m个查询(m<=200000),每次查询区间[a,b]中连续的没有相同数的的最大长度。
析:由于n太大,无法暴力,也承受不了O(n*n)的复杂度,只能是O(nlogn),首先是用f[i] 表示每个数 i 为左端点,向右可以最多到达的点为f[i],
那么这个dp很好转移,f[i] = max(f[i+1], last[a[i]]),其中last数组是用来记录上次序列数中a[i]的出现的位置。
那么对于给定的区间[l, r] g[i] = min(r, f[i]) - i + 1,而答案为 ans = max{ g[i] l <= i<= r}。这样复杂度也太大,我们从f上下手、
f 数组 一定是非降序的,所以一定存在一个位置pos,
当 i < pos 时,g[i] = f[i] - i + 1,这个我们可以用rmq 来维护最大值。
当 i >= pos 时,g[i] = r - i + 1,这个我们可以直接求出。
对于pos我们可以用二分快速求出,利用 f 的单调性。总时间复杂度就是 O(nlogn)。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 2e5 + 10;
const int mod = 1e6 + 10;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn];
int f[maxn], last[mod*3]; struct Rmq{
int dp[maxn][30];
void init(int *a){
for(int i = 0; i < n; ++i) dp[i][0] = a[i] - i + 1;
for(int j = 1; (1<<j) <= n; ++j)
for(int i = 0; i + (1<<j) <= n; ++i)
dp[i][j] = max(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
} int query(int l, int r){
int k = log(r-l+1.0) / log(2.0);
return max(dp[l][k], dp[r-(1<<k)+1][k]);
}
}; Rmq rmq; int main(){ while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < n; ++i){
scanf("%d", a+i);
a[i] += mod;
last[a[i]] = n;
}
f[n-1] = n-1; last[a[n-1]] = n-1;
for(int i = n-2; i >= 0; --i){
f[i] = min(f[i+1], last[a[i]]-1);
last[a[i]] = i;
}
rmq.init(f);
while(m--){
int l, r;
scanf("%d %d", &l, &r);
int pos = lower_bound(f+l, f+n, r) - f;
int ans = r - pos + 1;
--pos;
if(pos > l) ans = max(ans, rmq.query(l, pos));
printf("%d\n", ans);
}
}
return 0;
}
POJ 3419 Difference Is Beautiful (DP + 二分 + rmq)的更多相关文章
- POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)
Difference Is Beautiful Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %l ...
- POJ 3419 Difference Is Beautiful(RMQ变形)
题意:N个数,M个询问,每个询问为一个区间,求区间最长连续子序列,要求每个数都不同(perfect sequence,简称PS). 题解:很容易求出以每个数为结尾的ps,也就是求区间的最大值.有一个不 ...
- POJ 3419 Difference Is Beautiful
先处理出每一个i位置向左最远能到达的位置L[i].每一次询问,要找到L,R区间中的p位置,p位置左边的L[i]都是小于L的,p位置开始,到R位置,L[i]都大于等于L,对于前者,最大值为p-L,后者求 ...
- 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...
- 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分
原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。
/** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...
- HDU 3433 (DP + 二分) A Task Process
题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...
- POJ-2533最长上升子序列(DP+二分)(优化版)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41944 Acc ...
随机推荐
- weex 项目 创建 远程 icon
一.创建 远程 icon 步骤一:打开 阿里巴巴矢量图标库 官网:http://www.iconfont.cn/ 步骤二:选择项目需要的 icon 步骤三:添加到项目(没有项目会自动创建) 步骤四: ...
- SDIO总线(一)
SDIO接口是在SD内存卡接口的基础上发展起来的接口.SDIO接口兼容曾经的SD内存卡.而且能够连接SDIO接口的设备. SDIO1.0标准定义了两种类型的SDIO卡: 1.全速的SDIO卡.传输率能 ...
- HDU 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDOJ 5091 Beam Cannon 扫描线
线段树+扫描线: 我们用矩形的中心点来描写叙述这个矩形,然后对于每一个敌舰,我们建立一个矩形中心的活动范围,即矩形中心在该范围内活动就能够覆盖到该敌舰.那么我们要求的问题就变成了:随意一个区域(肯定也 ...
- 聊聊高并发(三十二)实现一个基于链表的无锁Set集合
Set表示一种没有反复元素的集合类,在JDK里面有HashSet的实现,底层是基于HashMap来实现的.这里实现一个简化版本号的Set,有下面约束: 1. 基于链表实现.链表节点依照对象的hashC ...
- OpenStack源码系列---起始篇
近一年来我负责公司云点的自动化部署工作,包括公司自有云平台方案.XenServer.vSphere.Ovirt和OpenStack的自动化安装部署,目前已经到了OpenStack这一部分.自动化部署首 ...
- solr单机多实例部署文件锁冲突解决的方法
给出一个有问题的单机多tomcat实例引用同一个solr实例部署图. 这样的部署必定造成一个问题.启动第二个tomcat实例时,一定会报索引目录文件锁已经被占用. 最初的解决的方法是.有多少个tomc ...
- Java中的常用异常处理方法
觉得自己是一个Java专家吗?是否肯定自己已经全面掌握了Java的异常处理机制?在下面这段代码中,你能够迅速找出异常处理的六个问题吗? 1 OutputStreamWriter out = ... 2 ...
- NameNode和JobTracker的网络接口
Hadoop快速入门 http://hadoop.apache.org/docs/r1.0.4/cn/quickstart.html
- Provided Maven Coordinates must be in the form 'groupId:artifactId:version'.
[hadoop@hadoop1 bin]$ ./spark-shell --packages org.mongodb.spark:mongo-spark-connector_2.10-2.2.1 Ex ...