poj 3419 Difference Is Beautiful (区间最长连续不重复数 dp+二分+RMQ)

26 篇文章 0 订阅
18 篇文章 0 订阅
Difference Is Beautiful
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 1893 Accepted: 579

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



题意:

给n个数(n<=200000),每个数的绝对值不超过(10^6),有m个查询(m<=200000),每次查询区间[a,b]中连续的没有相同数的的最大长度。


思路

先dp预处理,dp[i]表示以i结尾的最长连续不重复数,搞个辅助数组pos[]记录一个点最近一次出现的位置就递推了。

同时也可以处理pre[i](以i结尾的最长连续不重复串的初始位置)。

RMQ处理dp[],则对每个询问就可以直接得出答案了,但是会出现边界情况,求出的最长串的初始位置超出了u,故还需要更进一步思考怎样处理边界情况。

对每个询问u、v,二分找到u在pre中的位置pos,那么区间就可以分为两段了[u,pos-1]、[pos,v],第二段不会出现边界情况,直接RMQ查询,第一段全部都出现边界情况,所以最大值肯定在pos-1这点取了,这样问题就解决了。


思路来源于:男神博客

ps: 这题超经典,强烈推荐.


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 200005
#define MAXN 1000000
#define mod 1000000000
#define INF 0x3f3f3f3f
using namespace std;

int n,m,ans;
int f[2*maxn][20];
int a[maxn],dp[maxn],pre[maxn];
int pos[2*MAXN+5];


void init_rmq()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        f[i][0]=dp[i];
    }
    for(j=1;(1<<j)<=n;j++)
    {
        for(i=1;i+j-1<=n;i++)
        {
            f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
        }
    }
}
int query_rmq(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=r-l+1) k++;
    return max(f[l][k],f[r-(1<<k)+1][k]);
}
void presolve()
{
    int i,j;
    memset(pos,0,sizeof(pos));
    a[0]=-1;
    for(i=1;i<=n;i++)
    {
        if(a[i]==a[i-1]) dp[i]=1;
        else dp[i]=min(dp[i-1]+1,i-pos[a[i]]);
        pos[a[i]]=i;
        pre[i]=i-dp[i]+1;
    }
}
void solve()
{
    int i,j,t,u,v,pos;
    for(i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        u++,v++;
        ans=0;
        pos=lower_bound(pre+1,pre+n+1,u)-pre;
        if(pos<=v)  // 分成两段 [u,pos-1]中的元素各不相同 [pos,v]中的连段段首位置>u 可根据rmq求
        {
            ans=max(ans,query_rmq(pos,v));
            ans=max(ans,pos-u);
        }
        else ans=max(ans,v-u+1); // pos>v说明u~v中的元素各不相同
        printf("%d\n",ans);
    }
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            a[i]+=MAXN;
        }
        presolve();
        init_rmq();
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值