POJ 3419Difference Is Beautiful
来源:互联网 发布:c语言 汉诺塔 编辑:程序博客网 时间:2024/06/15 15:37
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 [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 22 5 4 1 2 3 6 2 40 82 6
Sample Output
65
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.
求区间里最长连续的没有重复数字的序列长度。
我们可以先预处理出从每个位置向右最远能到达的距离,可以想到,从左到右,
这个最远距离的终点一定在向右移动,于是我们可以用两个指针o(n)的求出。
然后对于每一个循环[l,r],我们只要用一个循环就可以搞定
for(int i=l;i<=r;i++) ans=max(ans,min(R[i],r)-i+1);
但是这样效率太低,我们可以对这个步骤进行优化,
由于R[i]是递增的,我们可以二分找到最小的j,使得R[j]>r
对于j右边的值来说,不会有比r-j+1更大的了,对于j左边的点来说,
我们把每个点的长度算出来先,L[i]=R[i]-i+1,然后统计从[l,j-1]的最大的L[i]即可,
统计区间最大值可以用rmq搞定,然后这题就结束了。
#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-8;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 2e5 + 10;int T, n, m, q, a[N], b[N], f[N], R[N];int dp[N][20], l, r;int rmqmax(int l, int r){int k = log(r - l + 1) / log(2.0);return max(dp[l][k], dp[r - (1 << k) + 1][k]);}int main(){while (scanf("%d%d", &n, &q) != EOF){rep(i, 1, n) scanf("%d", &a[i]), b[i] = a[i];sort(b + 1, b + n + 1); m = unique(b + 1, b + n + 1) - b;rep(i, 1, n) a[i] = lower_bound(b + 1, b + m, a[i]) - b;rep(i, 1, m) f[i] = 0;for (int i = 1, r = 1; i <= n; r++){while (r <= n){if (f[a[r]]++) break; else r++;}while (i <= n){R[i] = r - 1;if (--f[a[i++]]) break;}}rep(i, 1, n) dp[i][0] = R[i] - i + 1;for (int i = 1; (1 << i) <= n; i++){for (int j = 1; j <= n; j++){if (j + (1 << i) - 1 > n) break;dp[j][i] = max(dp[j][i - 1], dp[j + (1 << i - 1)][i - 1]);}}while (q--){scanf("%d%d", &l, &r);l++; r++;int q = l, h = r;while (q <= h){int mid = q + h >> 1;if (R[mid] > r) h = mid - 1; else q = mid + 1;}int ans = 0;if (l < q) ans = rmqmax(l, q - 1);if (q <= r) ans = max(ans, r - q + 1);printf("%d\n", ans);}}return 0;}
- POJ 3419Difference Is Beautiful
- 单调性+二分+RMQ-poj-3419-Difference Is Beautiful
- poj 3419 Difference Is Beautiful 离线线段树
- 【POJ3419】Difference Is Beautiful
- poj 3419 Difference Is Beautiful (区间最长连续不重复数 dp+二分+RMQ)
- poj 3419 Difference Is Beautiful (区间最长连续不重复数 dp+二分+RMQ)
- poj 3419 Difference Is Beautiful (dp+二分+RMQ或者dp+离线线段树)
- poj 3419 Difference Is Beautiful (开始的方法复杂度还是没降下去附o(n*log(n)))的方法
- Every Woman is Beautiful!
- Every woman is beautiful
- Number is beautiful
- 14382Life is beautiful
- Life is beautiful
- 17 small is beautiful
- Simple is beautiful - 关于Lua
- 布尔变量 - simple is beautiful
- Today is a beautiful day!!!
- Amsterdam is a beautiful city
- HDU 5605 geometry(水~)
- 使用Jmeter测试Webservice简单示例
- jQuery.Validate验证库的使用介绍
- AsyncTask是什么鬼
- 【cdoj 1335】柱爷与三叉戟不得不说的故事 状压dp+子集枚举
- POJ 3419Difference Is Beautiful
- 网易16年研发实习生笔试题 - 比较重量
- get与post的区别get请求方式与post请求方式的区别
- MAC下清理工具Omnidisksweeper
- Java将多个文件压缩为一个文件
- 实现在有限的label上动态的显示所有文字
- Swiper常用于移动端网站的内容触摸滑动
- 客户端与服务器交互---http协议
- hud1233还是畅通工程并 最小生成树 Prim算法