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 hasNcompanies, and he numbered them with 0 toN– 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 integerPi: 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 sequencePi, theith represents the turnover of theith 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 [L,R] of the turnover sequence, can you help him?

Input

The first line of the input contains two integersNandM.Nis the number of companies.Mis the number of queries. (1 ≤N,M≤ 200000). The second line containsNinteger numbers not exceeding 106by their absolute values. Theith of them represents the turnover of theith company this year. The followingMlines contain query descriptions, each description consists of two numbers:L,R(0 ≤LRN– 1) and represents the interval that Mr. Flower concerned.

Output

The output containsMlines. For each query, output the length of the longest consecutive perfect sequence between [L,R]  

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

题意:

给你你含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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一道比较经典的计数问题。题目描述如下: 给定一个 $n \times n$ 的网格图,其中一些格子被标记为障碍。一个连通块是指一些被标记为障碍的格子的集合,满足这些格子在网格图中连通。一个格子是连通的当且仅当它与另一个被标记为障碍的格子在网格图中有公共边。 现在,你需要计算在这个网格图中,有多少个不同的连通块,满足这个连通块的大小(即包含的格子数)恰好为 $k$。 这是一道比较经典的计数问题,一般可以通过计算生成函数的方法来解决。具体来说,我们可以定义一个生成函数 $F(x)$,其中 $[x^k]F(x)$ 表示大小为 $k$ 的连通块的个数。那么,我们可以考虑如何计算这个生成函数。 对于一个大小为 $k$ 的连通块,我们可以考虑它的形状。具体来说,我们可以考虑以该连通块的最左边、最上边的格子为起点,从上到下、从左到右遍历该连通块,把每个格子在该连通块中的相对位置记录下来。由于该连通块的大小为 $k$,因此这些相对位置一定是 $(x,y) \in [0,n-1]^2$ 中的 $k$ 个不同点。 现在,我们需要考虑如何计算这些点对应的连通块是否合法。具体来说,我们可以考虑从左到右、从上到下依次处理这些点,对于每个点 $(x,y)$,我们需要考虑它是否能够与左边的点和上边的点连通。具体来说,如果 $(x-1,y)$ 和 $(x,y)$ 都在该连通块中且它们在网格图中有公共边,那么它们就是连通的;同样,如果 $(x,y-1)$ 和 $(x,y)$ 都在该连通块中且它们在网格图中有公共边,那么它们也是连通的。如果 $(x,y)$ 与左边和上边的点都不连通,那么说明这个点不属于该连通块。 考虑到每个点最多只有两个方向需要检查,因此时间复杂度为 $O(n^2 k)$。不过,我们可以使用类似于矩阵乘法的思想,将这个过程优化到 $O(k^3)$ 的时间复杂度。 具体来说,我们可以设 $f_{i,j,k}$ 表示状态 $(i,j)$ 所代表的点在连通块中,且连通块的大小为 $k$ 的方案数。显然,对于一个合法的 $(i,j,k)$,我们可以考虑 $(i-1,j,k-1)$ 和 $(i,j-1,k-1)$ 这两个状态,然后把点 $(i,j)$ 加入到它们所代表的连通块中。因此,我们可以设计一个 $O(k^3)$ 的 DP 状态转移,计算 $f_{i,j,k}$。 具体来说,我们可以考虑枚举连通块所包含的最右边和最下边的格子的坐标 $(x,y)$,然后计算 $f_{x,y,k}$。对于一个合法的 $(x,y,k)$,我们可以考虑将 $(x,y)$ 所代表的点加入到 $(x-1,y,k-1)$ 和 $(x,y-1,k-1)$ 所代表的连通块中。不过,这里需要注意一个细节:如果 $(x-1,y)$ 和 $(x,y)$ 在网格图中有相邻边,那么它们不能算作连通的。因此,我们需要特判这个情况。 最终,$f_{n,n,k}$ 就是大小为 $k$ 的连通块的个数,时间复杂度为 $O(n^2 k + k^3)$。 参考代码:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值