zoukankan      html  css  js  c++  java
  • POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)

    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 followingM 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<cstdio> #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int up=1000000; int p,q,i; bool vis[2*up+2];//记录有没有访问过 int dis[2*up+2];//到该点i的最长连续长度 int f[2*up+2]; //记录前一个(最长连续串的终点位置+1),即f[i]-1 int a[2*up+2];// 读入使用的数组 int main() { scanf("%d%d",&p,&q); for(i=1;i<=p;i++) scanf("%d",&a[i]); memset(vis,0,sizeof(vis)); f[0]=1; dis[0]=0; for(i=1;i<=p;i++) { if (!vis[a[i]+up])//如果没有被访问过 { dis[i]=dis[i-1]+1;//那么长度+1 vis[a[i]+up]=1;//标记访问过 f[i]=f[i-1];//前一个最长串的终点+1 } else { int start=i-dis[i-1]; while(a[start]!=a[i])//将重复的点前的点全部还原成未访问 { vis[a[start]+up]=0; start++; } dis[i]=i-start;//从重复点后面的都可以利用 f[i]=i;//前一个连续串的终点是i-1 } } for(;q>0;q--) { int l,r,ans; scanf("%d%d",&l,&r); l++; r++; ans=0; while(r-l+1>ans)//根据f数组来确定答案 { ans=max(ans,min(dis[r],r-l+1)); r=f[r]-1; } printf("%d\n",ans); } return 0; }

    转自:http://www.cnblogs.com/zufezzt/p/5740789.html

    先处理出每一个i位置向左最远能到达的位置L[i]。每一次询问,要找到L,R区间中的p位置,p位置左边的L[i]都是小于L的,p位置开始,到R位置,L[i]都大于等于L,对于前者,最大值为p-L,后者求一个区间最大值即可。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    const double pi=acos(-1.0),eps=1e-8;
    void File()
    {
        freopen("D:\\in.txt","r",stdin);
        freopen("D:\\out.txt","w",stdout);
    }
    inline int read()
    {
        char c = getchar();  while(!isdigit(c)) c = getchar();
        int x = 0;
        while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
        return x;
    }
    
    const int maxn=200000+10;
    int n,a[maxn],b[maxn],c[maxn],q,L[maxn];
    int dp[maxn][30],f[maxn];
    
    void RMQ_init()
    {
        for(int i=0;i<n;i++) dp[i][0]=f[i];
        for(int j=1;(1<<j)<=n;j++)
            for(int i=0;i+(1<<j)-1<n;i++)
                dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    }
    
    int RMQ(int L,int R)
    {
        int k=0;
        while((1<<(k+1))<=R-L+1) k++;
        return max(dp[L][k],dp[R-(1<<k)+1][k]);
    }
    
    int main()
    {
        scanf("%d%d",&n,&q);
        for(int i=0;i<n;i++) scanf("%d",&a[i]),b[i]=a[i];
        sort(b, b + n); int sz = unique(b, b + n) - b;
        for(int i=0;i<n;i++) a[i]=lower_bound(b, b + sz, a[i])-b+1;
        memset(c,-1,sizeof c);
        for(int i=0;i<n;i++)
        {
            if(i==0) L[0]=0,c[a[i]]=0;
            else L[i]=max(L[i-1],c[a[i]]+1),c[a[i]]=i;
        }
        for(int i=0;i<n;i++) f[i]=i-L[i]+1;
        RMQ_init();
        for(int i=1;i<=q;i++)
        {
            int LL,RR; scanf("%d%d",&LL,&RR);
            int l=LL,r=RR,p=-1;
            while(l<=r)
            {
                int mid=(l+r)/2;
                if(L[mid]<LL) l=mid+1,p=mid;
                else r=mid-1;
            }
            int ans;
            if(p==-1) ans=RMQ(LL,RR);
            else if(p==RR) ans=RR-LL+1;
            else ans=max(p-LL+1,RMQ(p+1,RR));
            printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    线性方程组迭代法
    统计学习方法——朴素贝叶斯法、先验概率、后验概率
    信息熵、相对熵(KL散度)、交叉熵、条件熵
    六级听力词组积累
    样本均值和样本方差的无偏性证明、样本方差的方差
    Python 矩阵相关
    Python 绘图
    win10、VSCode、python3数据科学库
    Python杂记
    Gradient descend 梯度下降法和归一化、python中的实现(未完善)
  • 原文地址:https://www.cnblogs.com/stepping/p/5742669.html
Copyright © 2011-2022 走看看