POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)

Difference Is Beautiful

Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status Practice POJ 3419

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;
}
时间: 2024-10-10 10:57:32

POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)的相关文章

POJ 3419 Difference Is Beautiful(RMQ变形)

题意:N个数,M个询问,每个询问为一个区间,求区间最长连续子序列,要求每个数都不同(perfect sequence,简称PS). 题解:很容易求出以每个数为结尾的ps,也就是求区间的最大值.有一个不同就是长度可能会超出询问范围,所以先对PS的首位置二分,然后RMQ.注意一点,序列有可能出现负数,所以先加最大值变为正数.其实也不算变形,挺裸的…… 这题卡线段树,然而我只会线段树,心塞…… 代码(树状数组): #include <iostream> #include <cstring>

POJ 3419 Difference Is Beautiful

先处理出每一个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&

POJ 3419 (rmq)

这道题是rmq,再加上一个解决溢出. 刚开始我也想过用rmq,虽然不知道它叫什么,但是我知道应该这样做.可是后来没想到这道题的特殊性,也就是解决溢出的方法,就放弃了. rmq可以用线段树,也可以用dp.  这道题都可以过的,而且线段树要快一些. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define m

HDU 5089 Assignment(rmq+二分 或 单调队列)

Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 557    Accepted Submission(s): 280 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fr

poj 3264 区间最大最小值 RMQ问题之Sparse_Table算法

Balanced Lineup Time Limit: 5000 MS Memory Limit: 0 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order

hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 思路:求出区间的最大最小值,只要他们的差值小于k,那么这个区间就符合要求,但是由于n较大,用暴力一定超时,所以就要用别的方法了:而RMQ是可以求区间的最值的,而且预处理的复杂度只有O(nlogn),而查询只是O(1)处理,这样相对来说节约了时间,再根据右端点来二分枚举左端点(其实不用二分好像更快,估

poj 1905 Expanding Rods (数学 计算方法 二分)

题目链接 题意:将长度为L的棒子卡在墙壁之间.现在因为某种原因,木棒变长了,因为还在墙壁之间,所以弯成了一个弧度,现在求的是弧的最高处与木棒原先的地方的最大距离. 分析: 下面的分析是网上别人的分析: 设弦长为L0(即原长),弧长为L1=(1+n*C)*l0,目标值为h,半径为R,弧所对圆心角为2θ(弧度制).可以得到以下方程组:圆的弧长公式:L1=2θR三角函数公式:L0=2*R*sinθ,变换得θ=arcsin(L0/(2*R))勾股定理:R^2=(R-h)^2+(0.5*L0)^2,变换得

Poj 2289 Jamie&#39;s Contact Groups (二分+二分图多重匹配)

题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多少人? 解题思路: 二分图多重匹配相对于二分匹配来说不再是节点间的一一对应,而是Xi可以对应多个Yi.所以我们就需要一个限制(Xi最多匹配几个Yi).当Yi需要匹配Xi的时候,Xi的匹配未到上限,直接匹配,否则进行增广路.其实是二分图多重匹配的模板题,再套一个二分枚举最多组的人数就OK咯.下面就上板

POJ 1007 Difference Between Primes(线性筛法求N以内的素数表)

Difference Between Primes Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, sk