poj 3419 Difference Is Beautiful (开始的方法复杂度还是没降下去附o(n*log(n)))的方法

Difference Is Beautiful
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1863   Accepted: 569

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 containsN 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

POJ Monthly--2007.10.06, SHOIT@ZSU

题意:

给你你含n个整数的数组。整数的绝对值不超过10^6.然后给你q组询问。每组询问。给你一个区间l,r。问你这个区间内

最长连续无重复(没有相同的数字)序列的长度。

思路:

由于查询操作很多。所以只有在预处理上下工夫。用数组p[]来记录数组中的整数。用ml[i]表示以角标i结尾的最长无重复序列的长度。rep[i]表示i结尾的最长无重复序列与前面序列重复元素的位置。维护这个数组方便后面的询问区间定位。

详细见代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=200010;
const int up=1000000;//把负数转化为正数
int p[maxn],ml[maxn],rep[maxn];
bool vis[2000010];//判重
int main()
{
    int n,q,i,tt,pp,l,r,ans;

    while(~scanf("%d%d",&n,&q))
    {
        memset(vis,0,sizeof vis);
        for(i=0;i<n;i++)
            scanf("%d",p+i);
        ml[0]=1;
        rep[0]=0;
        vis[p[0]+up]=true;
        for(i=1;i<n;i++)
        {
            tt=p[i]+up;
            if(!vis[tt])
            {
                ml[i]=ml[i-1]+1;
                rep[i]=rep[i-1];
                vis[tt]=true;
            }
            else
            {
                pp=i-ml[i-1];//p-1-ml[i-1]+1.pp为以i-1结尾的序列(简写)的首元素
                while(p[pp]!=p[i])//找到重复元素。重复元素之前的元素都不能要
                {
                    vis[p[pp]+up]=false;
                    pp++;
                }
                ml[i]=i-pp;//以i结尾序列的长度
                rep[i]=i;//和前面序列重复的自己
            }
            //printf("ml[%d] %d\n",i,ml[i]);
        }
        while(q--)
        {
            scanf("%d%d",&l,&r);
            ans=0;
            while(1)
            {
                if(r-l+1<=ans)
                    break;
                ans=max(ans,min(ml[r],r-l+1));
                r=rep[r]-1;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

开始的方法北大的数据可以水过。

鉴赏男神的方法后。着实佩服男神强大的思维。自己还有很长的路要走呀。

思路:

la[i]记录数字i最后一次出现的位置。lp[i]记录以i结尾最长不重复数组的左区间。

通过维护la直接省去判重了。对于lp[i]如果la[p[i]]<lp[i-1]的话。lp[i]=lp[i-1]。

否则lp[i]=la[p[i]]+1。递推得十分漂亮!

然后算出各区间的长度。rmq维护区间最大值。

对于查询l,r之间的最大长度。有一点麻烦的是区间l,r间的点最大长度左端点有可能小于a(下称溢出区间)而这部分不能要。

所以只要二分找到第一个左端点大于等于a的点的下标pos。那么pos-1即该溢出区间。它的长度为pos-1-l+1.l,r区间剩下的部分直接rmq查询就行了。因为剩下的部分不会溢出了。

详细见代码:

#include <iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=200010;
const int up=1000000;//把负数转化为正数
int lp[maxn],p[maxn];//lp记录左端点
int la[2000010];//数字最后出现的位置
int rmq[25][maxn],lg[maxn],n;
void rmq_init()
{
    int i,j;
    for(i=0;i<n;i++)
        rmq[0][i]=la[i];
    for(i=1;i<=lg[n];i++)//枚举长度
        for(j=0;j+(1<<i)-1<n;j++)//枚举起点注意边界
            rmq[i][j]=max(rmq[i-1][j],rmq[i-1][j+(1<<(i-1))]);
}
int rmq_max(int l,int r)
{
    int tmp=lg[r-l+1];
    return max(rmq[tmp][l],rmq[tmp][r-(1<<tmp)+1]);
}
int main()
{
    int q,i,tmp,l,r,ans;
    lg[0]=-1;
    for(i=1;i<maxn;i++)
        lg[i]=lg[i>>1]+1;
    while(~scanf("%d%d",&n,&q))
    {
        memset(la,-1,sizeof la);
        for(i=0;i<n;i++)
            scanf("%d",p+i);
        lp[0]=0;
        la[p[0]+up]=0;//转化为正值
        for(i=1;i<n;i++)
        {
            if(la[p[i]+up]<lp[i-1])
                lp[i]=lp[i-1];
            else
                lp[i]=la[p[i]+up]+1;
            la[p[i]+up]=i;
        }
        for(i=0;i<n;i++)//la又用于记录长度。节约内存。
            la[i]=i-lp[i]+1;
        rmq_init();
        while(q--)
        {
            scanf("%d%d",&l,&r);//二分找第一个端点大于等于l的下标
            tmp=lower_bound(lp+l,lp+r+1,l)-lp;//前闭后开,所以右端点加1
            tmp--;//溢出区间右端点
            ans=tmp-l+1;//溢出区间有效长度
            if(tmp+1<=r)//必须取等号。当l==r且l为左端点时。ans=-1。
                ans=max(ans,rmq_max(tmp+1,r));
            printf("%d\n",ans);
        }
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)