poj 3419 Difference Is Beautiful (開始的方法複雜度還是沒降下去附o(n*log(n

來源:互聯網
上載者:User

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

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?

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.

The output contains M lines. For each query, output the length of the longest consecutive perfect sequence between [LR]  

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.

POJ Monthly--2007.10.06, SHOIT@ZSU

#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;}

#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;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.