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终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...
随机推荐
- 重装系统之后Hexo快速配置
安装准备软件 Node.js Git 打开 Git Bash hexo文件夹,右键: 配置SSH Keys 检查SSH keys设置,看一下电脑现有的ssh key cd ~/. ssh 检查本机的s ...
- aspnet5备忘
1. 在Windows下面运行cmd,然后执行下面的命令 @powershell -NoProfile -ExecutionPolicy unrestricted -Command "&am ...
- SASL - 简单认证和安全层
转自:http://blog.csdn.net/id19870510/article/details/8232509 SASL - 简单认证和安全层 SASL是一种用来扩充C/S模式验证能力的机制认证 ...
- A - Space Elevator(动态规划专项)
A - Space Elevator Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- Saltstack 服务器基本安装
Salt介绍 Salt是一个基础平台管理工具 Salt是一个配置管理系统,能够维护预定义状态的远程节点 Salt是一个分布式远程执行系统,用来在远程节点上执行命令和查询数据 Salt核心功能 使命令发 ...
- URL匹配与req参数解析
通配URL*(可代表任何字符串) 例如: app.get('/test/*', function(req, res){ res.send(req.query.aa); }) '/test/*通配tes ...
- HDU 5765 Bonds
比赛时候想了好久,不会.看了官方题解才会...... Bond是极小割边集合,去掉一个Bond之后,只会将原图分成两个连通块. 假设某些点构成的集合为 s(点集中的点进行状压后得到的一个十进制数),那 ...
- win8删除无线网络其中的一项配置
netsh wlan delete profile name="无线名称"
- 【转】使用DirectUI技术实现QQ界面
转自http://bbs.csdn.net/topics/350023031 一.介绍 DirectUI技术说白了就是XML配置文件+图片+JavaScript控制界面.这点与网页css+图片+Jav ...
- Javascript和HTML dom
今天在看DOM那一章的时候突然想到一个问题,众所周知的js的数据类型有两种:原始类型和对象类型.其中原始类型又包括以下几种类型:数字型.字符串型.布尔值.null和undefined.其中对象类型包括 ...