hzau 1205 Sequence Number(二分)
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(二分)的更多相关文章
- HZAU 1205 Sequence Number(双指针)
题目链接:http://acm.hzau.edu.cn/problem.php?id=1205 [题意]给你一串数,要求你找到两个数a[i],a[j],使得a[i]<=a[j]且j>=i且 ...
- 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 ...
- [crypto][ipsec] 简述ESP协议的sequence number机制
预备 首先提及一个概念叫重放攻击,对应的机制叫做:anti-replay https://en.wikipedia.org/wiki/Anti-replay IPsec协议的anti-replay特性 ...
- Sequence Number
1570: Sequence Number 时间限制: 1 Sec 内存限制: 1280 MB 题目描述 In Linear algebra, we have learned the definit ...
- 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ from:ht ...
- InnoDB: The log sequence number in ibdata files does not match
InnoDB: The log sequence number in ibdata files does not matchInnoDB的:在ibdata文件的日志序列号不匹配 可能ibdata文件损 ...
- Thread <number> cannot allocate new log, sequence <number>浅析
有时候,你会在ORACLE数据库的告警日志中发现"Thread <number> cannot allocate new log, sequence <number> ...
- ORA-02287: sequence number not allowed here问题的解决
当插入值需要从另外一张表中检索得到的时候,如下语法的sql语句已经不能完成该功能:insert into my_table(id, name) values ((select seq_my_table ...
- [转] 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)
点击阅读原译文 原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ ...
随机推荐
- mono下c#和c交互,字符串处理
起因是ulua里,从luajit读字符串到c#里,做了编码转换,如下 public static string lua_tostring(IntPtr luaState, int index) { v ...
- java integer String之equals vs ==
Integer a = new Integer(123); Integer b = new Integer(123); System.out.println(a == b); System.out.p ...
- [转】[tip] localhost vs. (local) in SQL Server connection strings
主要区别在于连接协议不同,前者(localhost)使用TCP协议,后者("(local)")使用NamedPipe协议. Sample code with SQL Server ...
- 使用Kotlin开发Android应用(IV):自定义视图和Android扩展
在读完扩展函数和默认值这篇文章之后,那么接下来要介绍什么呢?在本系列第一篇文章中我们说过,Kotlin使得Android开发更加简单,本文我们将进一步作介绍. 自定义视图 你应该还记得,在说到Kotl ...
- 爬虫、网页测试 及 java servlet 测试框架等介绍
scrapy 抓取网页并存入 mongodb的完整示例: https://github.com/rmax/scrapy-redis https://github.com/geekan/scrapy-e ...
- ABAP权限检查,TCode与权限对象进行关联
一.确认权限对象,和关联字段: Tcode:SU21 维护权限对象例如"M_MSEG_WMB",它关联字段为'WERKS'M_MSEG_WMB 物料凭证:工厂 二.在ABAP代码中 ...
- HackerRank - maximum-perimeter-triangle 【水】
题意 给出一系列数字,判断其中哪三个数字可以构成一个三角形,如果有多个,输出周长最大的那个,如果没有输出 - 1 思路 数据较小,所有情况FOR一遍 判断一下 AC代码 #include <cs ...
- asp.net IRequiresSessionState
在一般处理程序中,使用context.Session对象,必须先继承IRequiresSessionState接口. System.Web.SessionState.IRequiresSessionS ...
- Ubuntu 12.04下boost库的交叉编译
oost Ver: 1.55.0Compiler : GNU gcc 4.6 for ARM 1. 确保ARM编译成功安装,并配置好环境变量.2. 解压boost压缩包 3. 进入目录执行./boot ...
- HAproxy 源码包安装
HAproxy 源码包安装 系统环境:Centos 7 x64位 服务版本:haproxy-1.7.8.tar.gz 编译工具:gcc 下载地址 HAproxy:https://pan.baidu.c ...