G. Sequence Number

In Linear algebra, we have learned the definition of inversion number: Assuming A is a ordered set with n numbers ( n > 1 ) which are different from each other. If exist positive integers i , j, ( 1 ≤ i < j ≤ n and A[i] > A[j]), <a[i], a[j]=""> is regarded as one of A’s inversions. The number of inversions is regarded as inversion number. Such as, inversions of array <2,3,8,6,1> are <2,1>, <3,1>, <8,1>, <8,6>, <6,1>,and the inversion number is 5. Similarly, we define a new notion —— sequence number, If exist positive integers i, j, ( 1 ≤ i ≤ j ≤ n and A[i] <= A[j], <a[i], a[j]=""> is regarded as one of A’s sequence pair. The number of sequence pairs is regarded as sequence number. Define j – i as the length of the sequence pair. Now, we wonder that the largest length S of all sequence pairs for a given array A.

Input

There are multiply test cases. In each case, the first line is a number N(1<=N<=50000 ), indicates the size of the array, the 2th ~n+1th line are one number per line, indicates the element Ai (1<=Ai<=10^9) of the array.

Output

Output the answer S in one line for each case.

Sample Input

5 2 3 8 6 1

Sample Output

3

题意:找出最远的i<=j&&a[i]<=a[j]的长度;

思路:这是一道排序可以过的题,也可以rmq+二分 最快的写法可以用单调栈做到O(n)

   我是求后面的最大值后缀,二分后缀;

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=; int a[N],nex[N];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(nex,,sizeof(nex));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int j=n;j>=;j--)
nex[j]=max(a[j],nex[j+]);
int ans=;
for(int i=;i<=n;i++)
{
int s=i,e=n,pos=-;
while(s<=e)
{
int mid=(s+e)>>;
if(nex[mid]>=a[i])
pos=mid,s=mid+;
else e=mid-;
}
ans=max(ans,pos-i);
}
printf("%d\n",ans);
}
return ;
}

单调栈做法,如果当前元素小于栈顶元素入栈,否则依次和栈中元素计算相对距离且取最长的那一个。

 #include <cstdio>
#include <algorithm>
using namespace std;
struct jj
{
int pos,x;
}a[];
int main()
{
int n,i,i1,x,top,maxnum;
while(scanf("%d",&n)!=EOF)
{
top=;
maxnum=;
for(i=;n>i;i++)
{
scanf("%d",&x);
if(top==||a[top-].x>x)
{
a[top].x=x;
a[top].pos=i;
top++;
}
else
{
for(i1=top-;i1>=&&a[i1].x<=x;i1--)
{
maxnum=max(maxnum,i-a[i1].pos);
}
}
}
printf("%d\n",maxnum);
}
return ;
}

双指针做法,维护左指针和右指针,使左指针的值小于等于右指针的值

 #include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int N = 1e5+;
const double eps = 1e-;
int T,n,w[N],sum[N<<],p[N<<],cnt,m,ret[N];
int k,a[N],mi[N];
int main() {
while(~scanf("%d",&n)){ int ans=;
mi[]=1e9+;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
mi[i]=1e9+;
}
for(int i=;i<=n;i++){
mi[i]=min(mi[i-],a[i]);
}
for(int l=n,r=n;l>=;l--){
if(mi[l]>a[r]){
while(a[r]<mi[l]){
r--;
}
ans=max(ans,r-l);
}
else {
ans=max(ans,r-l);
}
}
printf("%d\n",ans);
}
return ;
}

hzau 1205 Sequence Number(二分)的更多相关文章

  1. HZAU 1205 Sequence Number(双指针)

    题目链接:http://acm.hzau.edu.cn/problem.php?id=1205 [题意]给你一串数,要求你找到两个数a[i],a[j],使得a[i]<=a[j]且j>=i且 ...

  2. mysql oom之后的page 447 log sequence number 292344272 is in the future

    mysql oom之后,重启时发生130517 16:00:10 InnoDB: Error: page 447 log sequence number 292344272InnoDB: is in ...

  3. [crypto][ipsec] 简述ESP协议的sequence number机制

    预备 首先提及一个概念叫重放攻击,对应的机制叫做:anti-replay https://en.wikipedia.org/wiki/Anti-replay IPsec协议的anti-replay特性 ...

  4. Sequence Number

    1570: Sequence Number 时间限制: 1 Sec  内存限制: 1280 MB 题目描述 In Linear algebra, we have learned the definit ...

  5. 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)

    原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ from:ht ...

  6. InnoDB: The log sequence number in ibdata files does not match

    InnoDB: The log sequence number in ibdata files does not matchInnoDB的:在ibdata文件的日志序列号不匹配 可能ibdata文件损 ...

  7. Thread <number> cannot allocate new log, sequence <number>浅析

    有时候,你会在ORACLE数据库的告警日志中发现"Thread <number> cannot allocate new log, sequence <number> ...

  8. ORA-02287: sequence number not allowed here问题的解决

    当插入值需要从另外一张表中检索得到的时候,如下语法的sql语句已经不能完成该功能:insert into my_table(id, name) values ((select seq_my_table ...

  9. [转] 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)

    点击阅读原译文 原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ ...

随机推荐

  1. el-tree 设置目录树中的某个节点为高亮状态

    现在可以实现,点击某个节点,该节点会红色高亮,那怎么让这个树加载出来的时候 默认某个节点高亮呢?element ui里面带勾选框的可以默认勾选上,这个没有勾选框 其实很简单.element ui的树形 ...

  2. Django 进阶(分页器&中间件)

    分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views h ...

  3. MySQL 第五天

    回顾 连接查询: 多张表连接到一起, 不管记录数如何,字段数一定会增加. 分类: 内连接,外连接,自然连接和交叉连接 交叉连接: cross join(笛卡尔积) 内连接: inner join, 左 ...

  4. Js全局异常捕获

    重新window.onerror方法就行了 window.onerror = handleError function handleError(msg,url,l) { var txt="T ...

  5. Linux中的正则表达式

    *               前一个字符匹配0次或任意次.               匹配除了换行符外任意一个字符^              匹配行首$              匹配行尾[] ...

  6. PAAS、IAAS和SAAS区别

    IaaS: Infrastructure-as-a-Service(基础设施即服务) 有了IaaS,你可以将硬件外包到别的地方去.IaaS公司会提供场外服务器,存储和网络硬件,你可以租用.节省了维护成 ...

  7. java中使用axis发布和调用webService及dom4j解析xml字符串

    工作中需要调用webService服务,这里记录一下如何在java中发布和调用webService. 需要的jar包: webService服务端: import javax.jws.WebMetho ...

  8. C# 函数4

    //数据库     public class GF_DA     {         /// <summary>         /// 执行SQL语句 sConnStr 连接字符串,sq ...

  9. JavaScript:学习笔记(6)——New运算符

    JavaScript:学习笔记(6)——New运算符 new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例. 快速开始 当你使用new关键字的时候,会 创建一个新的对象 将th ...

  10. 用js来实现那些数据结构 第一章

    在开始正式的内容之前,不得不说说js中的数据类型和数据结构,以及一些比较容易让人混淆的概念.那么为什么要从数组说起?数组在js中是最常见的内存数据结构,数组数据结构在js中拥有很多的方法,很多初学者记 ...