zoukankan      html  css  js  c++  java
  • Difference Is Beautiful

    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 [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
    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    const int N=200005;
    const int M=1000000;
    int n,m;
    int a[N];
    bool vis[2000005];
    int cnt[N],r[N];
    void solve()
    {
        int tt,pp;
       memset(vis,0,sizeof(vis));
       cnt[0]=1;
       r[0]=0;
       vis[a[0]+M]=1;
       for(int i=1;i<n;i++)
       {
           tt=a[i]+M;
           if(!vis[tt])
           {
               vis[tt]=1;
               cnt[i]=cnt[i-1]+1;
               r[i]=r[i-1];
           }
           else
           {
               pp=i-cnt[i-1];
               while(a[pp]!=a[i])
               {
                   vis[a[pp]+M]=0;
                   pp++;
               }
                cnt[i]=i-pp;
                r[i]=i;
    
           }
    
       }
    
    }
    int main()
    {
    
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        solve();
        while(m--)
        {
            int f,e;
            int ans=0;
            scanf("%d%d",&f,&e);
            while(1)
            {
                 if(e-f+1<=ans) break;
                 ans=max(ans,min(cnt[e],e-f+1));
                 e=r[e]-1;
            }
           printf("%d
    ",ans);
    
        }
        return 0;
    }
    

      

  • 相关阅读:
    通信错误:(-1)[描述:无法解析路由器DDNS地址,请检查DDNS状态.] 解析办法
    小数量宽带用户的福音,Panabit 云计费easyradius 接口隆重发布,PA宽带计费系统
    送给那些经常问我如何设置360测速结果为电信的朋友,360测速模块原理简单分析
    Universal-Image-Loader(UIL)使用方法&流程图&源码分析 ----- 未完
    Android开发框架
    Android线程池的使用(未完)
    Android LruCache究竟是什么
    Java finally语句到底是在return之前还是之后执行?
    Android自定义图片加载框架
    Android 自定义View实现单击和双击事件
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5733180.html
Copyright © 2011-2022 走看看