POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
Mr. Flower's business is growing much faster than originally planned. He has now become the CEO of a world-famous beef corporation. However, the boss never lives a casual life because he should take charge of the subsidiary scattered all over the world. Every year, Mr. Flower needs to analyze the performance reports of these subsidiary companies.
Mr. Flower has N companies, and he numbered them with 0 to N – 1. All of the companies will give Mr. Flower a report about the development each year. Among all of the tedious data, only one thing draws Mr. Flower's attention – the turnover. Turnover of a company can be represented as an integer Pi: positive one represents the amount of profit-making while negative for loss-making.
In fact, Mr. Flower will not be angry with the companies running under deficit. He thinks these companies have a large room for future development. What dissatisfy him are those companies who created the same turnover. Because in his eyes, keeping more than one companies of the same turnover is not necessary.
Now we know the annual turnover of all companies (an integer sequence Pi, the ith represents the turnover of the ith company this year.). We say a number sequence is perfect if all of its numbers are different from each other. Mr. Flower wants to know the length of the longest consecutive perfect sequence in a certain interval [L, R] of the turnover sequence, can you help him?
Input
The first line of the input contains two integers N and M. N is the number of companies. M is the number of queries. (1 ≤ N, M ≤ 200000). The second line contains N integer numbers not exceeding 106 by their absolute values. The ith of them represents the turnover of the ith company this year. The followingM lines contain query descriptions, each description consists of two numbers: L, R (0 ≤ L ≤ R ≤ N – 1) and represents the interval that Mr. Flower concerned.
Output
The output contains M lines. For each query, output the length of the longest consecutive perfect sequence between [L, R]
Sample Input
9 2
2 5 4 1 2 3 6 2 4
0 8
2 6
Sample Output
6
5
//模拟的方法: #include<cstdio>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int up=;
int p,q,i;
bool vis[*up+];//记录有没有访问过
int dis[*up+];//到该点i的最长连续长度
int f[*up+]; //记录前一个(最长连续串的终点位置+1),即f[i]-1
int a[*up+];// 读入使用的数组 int main()
{
scanf("%d%d",&p,&q);
for(i=;i<=p;i++)
scanf("%d",&a[i]);
memset(vis,,sizeof(vis));
f[]=;
dis[]=;
for(i=;i<=p;i++)
{
if (!vis[a[i]+up])//如果没有被访问过
{
dis[i]=dis[i-]+;//那么长度+1
vis[a[i]+up]=;//标记访问过
f[i]=f[i-];//前一个最长串的终点+1
} else
{
int start=i-dis[i-];
while(a[start]!=a[i])//将重复的点前的点全部还原成未访问
{
vis[a[start]+up]=;
start++;
}
dis[i]=i-start;//从重复点后面的都可以利用
f[i]=i;//前一个连续串的终点是i-1
}
}
for(;q>;q--)
{
int l,r,ans;
scanf("%d%d",&l,&r);
l++; r++;
ans=;
while(r-l+>ans)//根据f数组来确定答案
{
ans=max(ans,min(dis[r],r-l+));
r=f[r]-;
}
printf("%d\n",ans);
}
return ;
}
转自:http://www.cnblogs.com/zufezzt/p/5740789.html
先处理出每一个i位置向左最远能到达的位置L[i]。每一次询问,要找到L,R区间中的p位置,p位置左边的L[i]都是小于L的,p位置开始,到R位置,L[i]都大于等于L,对于前者,最大值为p-L,后者求一个区间最大值即可。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} const int maxn=+;
int n,a[maxn],b[maxn],c[maxn],q,L[maxn];
int dp[maxn][],f[maxn]; void RMQ_init()
{
for(int i=;i<n;i++) dp[i][]=f[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
} int RMQ(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
return max(dp[L][k],dp[R-(<<k)+][k]);
} int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<n;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b, b + n); int sz = unique(b, b + n) - b;
for(int i=;i<n;i++) a[i]=lower_bound(b, b + sz, a[i])-b+;
memset(c,-,sizeof c);
for(int i=;i<n;i++)
{
if(i==) L[]=,c[a[i]]=;
else L[i]=max(L[i-],c[a[i]]+),c[a[i]]=i;
}
for(int i=;i<n;i++) f[i]=i-L[i]+;
RMQ_init();
for(int i=;i<=q;i++)
{
int LL,RR; scanf("%d%d",&LL,&RR);
int l=LL,r=RR,p=-;
while(l<=r)
{
int mid=(l+r)/;
if(L[mid]<LL) l=mid+,p=mid;
else r=mid-;
}
int ans;
if(p==-) ans=RMQ(LL,RR);
else if(p==RR) ans=RR-LL+;
else ans=max(p-LL+,RMQ(p+,RR));
printf("%d\n",ans);
}
return ;
}
POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)的更多相关文章
- POJ 3419 Difference Is Beautiful (DP + 二分 + rmq)
题意:给n个数(n<=200000),每个数的绝对值不超过(10^6),有m个查询(m<=200000),每次查询区间[a,b]中连续的没有相同数的的最大长度. 析:由于n太大,无法暴力, ...
- 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,后者求 ...
- HDU 5089 Assignment(rmq+二分 或 单调队列)
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- *HDU3486 RMQ+二分
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- poj 3111 K Best 最大化平均值 二分思想
poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...
- hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 ...
- hdu 3486 Interviewe (RMQ+二分)
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分
原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...
随机推荐
- Gentoo: !!! existing preserved libs问题
问题描述 !!! existing preserved libs: >>> package: media-libs/libmng-2.0.2-r1 * - /usr/lib/libm ...
- Hanoi汉诺塔问题——递归与函数自调用算法
题目描述 Description 有N个圆盘,依半径大小(半径都不同),自下而上套在A柱上,每次只允许移动最上面一个盘子到另外的柱子上去(除A柱外,还有B柱和C柱,开始时这两个柱子上无盘子),但绝不允 ...
- Openjudge-计算概论(A)-奇数求和
描述: 计算非负整数 m 到 n(包括m 和 n )之间的所有奇数的和,其中,m 不大于 n,且n 不大于300.例如 m=3, n=12, 其和则为:3+5+7+9+11=35. 输入两个数 m 和 ...
- WinHex V18.7(16进制编辑器) 多国语言绿色版
软件名称: WinHex V18.7(16进制编辑器)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.7MB图片预览: ...
- 一把刀终极配置 For XP v2.0 免费绿色版
软件名称: 一把刀终极配置 For XP 软件语言: 简体中文 授权方式: 免费软件 运行环境: WinXP 软件大小: 924KB 图片预览: 软件简介: 一把刀终极配置 For XP,用于快速方便 ...
- 【ADT】链表的基本C语言实现
什么是抽象数据类型?首先,这一概念是软件开发人员在力求编写的代码健壮.易维护且可以复用的过程中产生的.英文是AbstractData Type.有人将其比作"抽象"的墙壁,&quo ...
- Sublime 3 and Python
1. 安装Anaconda插件 Ctrl+Shift+P打开控制面板,找到Install Package,回车 在弹出框中输入Anaconda,回车安装即可. 2.配置Anaconda文件 Prefe ...
- 用python写刷票程序
刷票一般要突破以下限制: 1.验证码识别 2.同一ip不可连续投票 解决办法 1.用tesseract工具,链接在此 https://code.google.com/p/tesseract-ocr/ ...
- eclipse4.3 安装tomcat8
Go to the WTP downloads page, select the 3.6.0 version , and download the zip (under Traditional Zip ...
- js基础和工具库
/* * 作者: 胡乐 * 2015/4/18 * js 基础 和 工具库 * * * */ //根据获取对象 function hGetId(id){ return document.getElem ...