poj 3419 Difference Is Beautiful (区间最长连续不重复数 dp+二分+RMQ)
2013-10-02 19:20
525 查看
Difference Is Beautiful
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 [L, R] of the turnover sequence, can you help him? Input The first line of the input contains two integers N and M. N is the number of companies. M is the number of queries. (1 ≤ N, M ≤ 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: L, R (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 [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 POJ Monthly--2007.10.06, SHOIT@ZSU |
给n个数(n<=200000),每个数的绝对值不超过(10^6),有m个查询(m<=200000),每次查询区间[a,b]中连续的没有相同数的的最大长度。
思路:
先dp预处理,dp[i]表示以i结尾的最长连续不重复数,搞个辅助数组pos[]记录一个点最近一次出现的位置就递推了。
同时也可以处理pre[i](以i结尾的最长连续不重复串的初始位置)。
RMQ处理dp[],则对每个询问就可以直接得出答案了,但是会出现边界情况,求出的最长串的初始位置超出了u,故还需要更进一步思考怎样处理边界情况。
对每个询问u、v,二分找到u在pre中的位置pos,那么区间就可以分为两段了[u,pos-1]、[pos,v],第二段不会出现边界情况,直接RMQ查询,第一段全部都出现边界情况,所以最大值肯定在pos-1这点取了,这样问题就解决了。
思路来源于:男神博客
ps: 这题超经典,强烈推荐.
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> //#pragma comment (linker,"/STACK:102400000,102400000") #define maxn 200005 #define MAXN 1000000 #define mod 1000000000 #define INF 0x3f3f3f3f using namespace std; int n,m,ans; int f[2*maxn][20]; int a[maxn],dp[maxn],pre[maxn]; int pos[2*MAXN+5]; void init_rmq() { int i,j; for(i=1;i<=n;i++) { f[i][0]=dp[i]; } for(j=1;(1<<j)<=n;j++) { for(i=1;i+j-1<=n;i++) { f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); } } } int query_rmq(int l,int r) { int k=0; while((1<<(k+1))<=r-l+1) k++; return max(f[l][k],f[r-(1<<k)+1][k]); } void presolve() { int i,j; memset(pos,0,sizeof(pos)); a[0]=-1; for(i=1;i<=n;i++) { if(a[i]==a[i-1]) dp[i]=1; else dp[i]=min(dp[i-1]+1,i-pos[a[i]]); pos[a[i]]=i; pre[i]=i-dp[i]+1; } } void solve() { int i,j,t,u,v,pos; for(i=1;i<=m;i++) { scanf("%d%d",&u,&v); u++,v++; ans=0; pos=lower_bound(pre+1,pre+n+1,u)-pre; if(pos<=v) // 分成两段 [u,pos-1]中的元素各不相同 [pos,v]中的连段段首位置>u 可根据rmq求 { ans=max(ans,query_rmq(pos,v)); ans=max(ans,pos-u); } else ans=max(ans,v-u+1); // pos>v说明u~v中的元素各不相同 printf("%d\n",ans); } } int main() { int i,j; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) { scanf("%d",&a[i]); a[i]+=MAXN; } presolve(); init_rmq(); solve(); } return 0; }
相关文章推荐
- poj 3419 Difference Is Beautiful (区间最长连续不重复数 dp+二分+RMQ)
- UESTC 1384 卿大爷的多个女友 后缀数组、最长连续重复子串
- HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)
- 求第k大连续区间和/第k大子序列和 - 二分+树状数组+前缀和(或主席树+堆)
- 字符串中连续最长重复子串
- PKU 3667 Hotel (线段树,区间合并,最长连续区间)
- hdu 5726 区间gcd RMQ+二分 || 暴力枚举
- 任意区间的最长连续递增子序列,最大连续子序列和
- HDU 3308——LCIS(线段树,区间合并,最长连续递增子序列)
- java 找出所有最长连续重复子串及其个数
- hdu 1540 Tunnel Warfare (线段树维护左右最长连续区间)
- 在字符串中查找连续重复的最长子串的C++实现
- 【维护区间最长连续子序列 && 线段树 && 区间归并】HDU - 1540 Tunnel Warfare
- spoj 1557 线段树 区间最大连续和 (不重复数)
- poj1743 Musical Theme(后缀数组--不可重叠最长重复子串+二分)
- PKU1743(Musical Theme)求不可重叠最长重复子串(后缀数组+二分)
- HDU 5726 求gcd=k的区间的个数 (二分+RMQ)
- poj1743(后缀数组+二分--不可重叠最长重复子串)
- hdu 3656 Fire station 重复覆盖 DLX+二分答案 给出一些城市及一些救火站的坐标,要求这些救火站覆盖所有的城市,问从救火站到城市的最长时间至少是多少。
- 找出所有最长连续重复子串及其个数