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. msql 2000 使用DBCC CHECK DB 得出错误,槽引用错误

    转自:http://www.cnblogs.com/firstrose/p/4256257.html 某个SQL2000的数据库,在通过备份/还原的方法升级到2005时发生错误: 查找解决方法未果 正 ...

  2. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. Linux的压缩命令(tar,gzip,zip)

    打包和压缩.打包是指将一大堆文件或目录变成一个总的文件:压缩则是将一个大的文件通过一些压缩算法变成一个小文件. 这源于Linux中很多压缩程序只能针对一个文件进行压缩,这样当你想要压缩一大堆文件时,你 ...

  4. scope的继承

    本文转载自: http://www.tuicool.com/articles/63iEref angular中scope的继承与js的原型继承有密切的联系,首先回顾一下js的继承: function ...

  5. 五、golang实现排序

    实现排序: 1.实现一个冒泡排序 2.实现一个选择排序 3.实现一个插入排序 4.实现一个快速排序 冒泡排序 package main import( "fmt" ) func b ...

  6. Qt如何重写虚函数

    eg:QWidget的有个虚函数,KeyPressEvent,当它的子类获得焦点的时候,如果有任何按键按下,就会触发这个虚函数. 1.在mainwindow.h中声明此虚函数 protected:vo ...

  7. MSSqlserver的锁模式介绍

    一 SQL Server 锁类型的说明 在SQL Server数据库中加锁时,除了可以对不同的资源加锁,还可以使用不同程度的加锁方式,即有多种模式,SQL Server中锁模式包括: 1.共享锁(S) ...

  8. JAVA获取webapp路径

    1.使用ServletContext获取webapp目录 在Servlet中 String path = getServletContext().getRealPath("/"); ...

  9. eclipse异常关了,tomcat如何关

    eclipse异常关了,tomcat如何关 ? 1.bin文件夹下面有的shutdown.bat.双击即可! 2.在任务管理器里面关闭,看到javaw.exe关闭也行.            

  10. Apache与Tomcat三种连接方式JK、http_proxy、ajp_proxy

    为什么要让Apache与Tomcat之间进行连接?事实上Tomcat本身已经提供了HTTP服务,该服务默认的端口是8080,也可以改为80.既然Tomcat本身已经可以提供动态加静态web服务,为什么 ...