【POJ3419】Difference Is Beautiful

本文介绍了一个算法问题,即如何找出一组公司营业额数据中,最长的没有重复元素的连续子序列长度。通过使用单调性、二分查找及区间最值查询等技术手段,提供了一种高效求解方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Difference Is Beautiful
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 2247 Accepted: 703

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 [LR] of the turnover sequence, can you help him?

Input

The first line of the input contains two integers N and MN is the number of companies. M is the number of queries. (1 ≤ NM ≤ 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 following M lines contain query descriptions, each description consists of two numbers: LR (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 [LR]  

Sample Input

9 2
2 5 4 1 2 3 6 2 4
0 8
2 6

Sample Output

6
5

Hint

The longest perfect sequence of the first query in the sample input is '5 4 1 2 3 6', so the answer for this query is 6.

Source


单调性+二分+RMQ;

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define M 1000000
using namespace std;
const int N = 200000+10;
int la[M*2+100],dp[N],a[N],mm[N][25],mi[N][25];
int n,m;
void RMQ() {
	for(int i=0; i<n; i++) {
		mm[i][0]=mi[i][0]=la[i];
	}
	for(int j=1; j<20; j++) {
		for(int i=0; i<n; i++) {
			if(i+(1<<j)-1<n) {
				mm[i][j]=max(mm[i][j-1],mm[i+(1<<(j-1))][j-1]);
				mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
			}
		}
	}
}
int max_rmq(int x,int y) {
	int cnt=(int)(log((double)(y-x+1))/log(2.0));
	return max(mm[x][cnt],mm[y-(1<<cnt)+1][cnt]);
}
int main() {
	while(scanf("%d%d",&n,&m)!=EOF) {
		for(int i=0; i<n; i++) {
			scanf("%d",&a[i]);
		}
		memset(la,-1,sizeof(la));
		la[a[0]+M]=0;
		dp[0]=0;
		for(int i=1; i<n; i++) {
			if(dp[i-1]>la[a[i]+M])
				dp[i]=dp[i-1];
			else
				dp[i]=la[a[i]+M]+1;
			la[a[i]+M]=i;
		}
		for(int i=0; i<n; i++) {
			la[i]=i-dp[i]+1;
		}
		RMQ();
		int x,y;
		while(m--) {
			scanf("%d%d",&x,&y);
			int temp=lower_bound(dp+x,dp+y+1,x)-dp;
			temp--;
			int ans=temp-x+1;
			if(temp+1<=y) {
				ans=max(ans,max_rmq(temp+1,y));
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}
题目地址:http://poj.org/problem?id=3419


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值