博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3419 Difference Is Beautiful(RMQ+二分 或者 模拟)
阅读量:5222 次
发布时间:2019-06-14

本文共 4330 字,大约阅读时间需要 14 分钟。

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

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 22 5 4 1 2 3 6 2 40 82 6

Sample Output

65
//模拟的方法: #include
#include
#include
#include
#include
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]-1int 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
#include
#include
#include
#include
#include
#include
#include
#include
#include
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

 

转载于:https://www.cnblogs.com/stepping/p/5742669.html

你可能感兴趣的文章
[Linux]PHP-FPM与NGINX的两种通讯方式
查看>>
Java实现二分查找
查看>>
优秀员工一定要升职吗
查看>>
[LintCode] 462 Total Occurrence of Target
查看>>
springboot---redis缓存的使用
查看>>
架构图-模型
查看>>
sql常见面试题
查看>>
jQuery总结第一天
查看>>
Java -- Swing 组件使用
查看>>
Software--Architecture--DesignPattern IoC, Factory Method, Source Locator
查看>>
poj1936---subsequence(判断子串)
查看>>
黑马程序员_Java基础枚举类型
查看>>
[ python ] 练习作业 - 2
查看>>
一位90后程序员的自述:如何从年薪3w到30w!
查看>>
在.net core上使用Entity FramWork(Db first)
查看>>
System.Net.WebException: 无法显示错误消息,原因是无法找到包含此错误消息的可选资源程序集...
查看>>
UIImage 和 iOS 图片压缩UIImage / UIImageVIew
查看>>
MongoDB的数据库、集合的基本操作
查看>>
ajax向后台传递数组
查看>>
疯狂JAVA16课之对象与内存控制
查看>>