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)

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

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<string>
 6 #include<queue>
 7 #include<algorithm>
 8 #include<stack>
 9 #include<cstring>
10 #include<vector>
11 #include<list>
12 #include<set>
13 #include<map>
14 using namespace std;
15 #define ll long long
16 #define pi (4*atan(1.0))
17 #define eps 1e-4
18 #define bug(x)  cout<<"bug"<<x<<endl;
19 const int N=1e5+10,M=1e6+10,inf=2147483647;
20 const ll INF=1e18+10,mod=2147493647;
21
22 int a[N],nex[N];
23 int main()
24 {
25     int n;
26     while(~scanf("%d",&n))
27     {
28         memset(nex,0,sizeof(nex));
29         for(int i=1;i<=n;i++)
30             scanf("%d",&a[i]);
31         for(int j=n;j>=1;j--)
32             nex[j]=max(a[j],nex[j+1]);
33         int ans=0;
34         for(int i=1;i<=n;i++)
35         {
36             int s=i,e=n,pos=-1;
37             while(s<=e)
38             {
39                 int mid=(s+e)>>1;
40                 if(nex[mid]>=a[i])
41                     pos=mid,s=mid+1;
42                 else e=mid-1;
43             }
44             ans=max(ans,pos-i);
45         }
46         printf("%d\n",ans);
47     }
48     return 0;
49 }

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

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 struct jj
 5 {
 6     int pos,x;
 7 }a[50000];
 8 int main()
 9 {
10     int n,i,i1,x,top,maxnum;
11     while(scanf("%d",&n)!=EOF)
12     {
13         top=0;
14         maxnum=0;
15         for(i=0;n>i;i++)
16         {
17             scanf("%d",&x);
18             if(top==0||a[top-1].x>x)
19             {
20                 a[top].x=x;
21                 a[top].pos=i;
22                 top++;
23             }
24             else
25             {
26                 for(i1=top-1;i1>=0&&a[i1].x<=x;i1--)
27                 {
28                     maxnum=max(maxnum,i-a[i1].pos);
29                 }
30             }
31         }
32         printf("%d\n",maxnum);
33     }
34     return 0;
35 }

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

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cstring>
 4 #include <string>
 5 #include <cstdlib>
 6 #include <iostream>
 7 #include <map>
 8 #include <cmath>
 9 #include <algorithm>
10 using namespace std;
11 typedef long long LL;
12 typedef pair<int,int>pii;
13 const int N = 1e5+5;
14 const double eps = 1e-8;
15 int T,n,w[N],sum[N<<2],p[N<<2],cnt,m,ret[N];
16 int k,a[N],mi[N];
17 int main() {
18     while(~scanf("%d",&n)){
19
20         int ans=0;
21         mi[0]=1e9+10;
22         for(int i=1;i<=n;i++){
23             scanf("%d",&a[i]);
24             mi[i]=1e9+10;
25         }
26         for(int i=1;i<=n;i++){
27             mi[i]=min(mi[i-1],a[i]);
28         }
29         for(int l=n,r=n;l>=1;l--){
30             if(mi[l]>a[r]){
31                 while(a[r]<mi[l]){
32                     r--;
33                 }
34                 ans=max(ans,r-l);
35             }
36             else {
37                 ans=max(ans,r-l);
38             }
39         }
40         printf("%d\n",ans);
41     }
42    return 0;
43 }

时间: 2024-10-28 21:25:37

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且j-i最大. [分析]预处理1~i的最小值,然后从右往左双指针,维护右端点>左端点,如果右端点<1~L的最小值,则移动右端点. #include <cstdio> #include <vector> #include <cstring> #include <

Sequence Number

Sequence 在当前transaction scope之外产生,当事务回滚时,Sequence number 不会回滚. 1,Create Sequence syntax CREATE SEQUENCE [schema_name . ] sequence_name [ AS [ built_in_integer_type | user-defined_integer_type ] ] [ START WITH <constant> ] [ INCREMENT BY <constant

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

当插入值需要从另外一张表中检索得到的时候,如下语法的sql语句已经不能完成该功能:insert into my_table(id, name) values ((select seq_my_table.nextval from dual), ‘runto30′); 会报“ORA-02287: sequence number not allowed here”的错误,可以使用如下语句来完成: insert into my_table(id, name) select seq_my_table.ne

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

InnoDB: The log sequence number in ibdata files does not matchInnoDB的:在ibdata文件的日志序列号不匹配 可能ibdata文件损坏了解决办法如下:在my.cny 的[mysqld]中加入 # Size of each log file in a log group. You should set the combined size # of log files to about 25%-100% of your buffer

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

原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ from:https://blog.csdn.net/a19881029/article/details/38091243 如果你正在读这篇文章,很可能你对TCP“非著名”的“三次握手”或者说“SYN,SYN/ACK,ACK”已经很熟悉了.不幸的是,对很多人来说,对TCP的学习就仅限于此了.尽管年代久远,TCP仍

ORA-02287:此处不允许序号(sequence number not allowed here) 的避免以及强制实现

问题场景一: name FROM (select SEQ_B_LOG_ID.NEXTVAL id , 'elong_deo' name from dual);   问题场景二: into b_authority (id,role_id,authority,remark,url,yn,parent_id,authority_type,log_flag) select SEQ_B_AUTHORITY_ID.NEXTVAL,1, 'admin:role:listRole', '角色分页查询', '/a

UVa 10534 Wavio Sequence ( DP 二分 最长递增子序列 )

题意  求一个序列a某一位的最长递增序列(lis)和最长递减序列(lds)中最小值的最大值 开始直接用DP写了   然后就超时了  后来看到别人说要用二分把时间复杂度优化到O(n*logn)   果然如此   用一个栈s保存长度为i的LIS的最小尾部s[i]  top为栈顶即当前LIS的长度  初始s[1]=a[1]  top=1 遍历整个序列  当a[i]>s[top]时  a[i]入栈 in[i]=top   否则 在栈中查找(二分)第一个大于等于a[i]的下标pos  并替换  这样就增加

hdu 6231 -- K-th Number(二分+尺取)

题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an array B by a parameter K as following rules: Initially, the array B is empty. Consider each interval in array A. If the length of this interval is le

zoj 3816 Generalized Palindromic Number (二分+贪心)

题目连接 : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5348 牡丹江网络赛的题,比赛的时候想到做法的,但是一直没调出来,赛后也调了些时间,最近代码能力堪忧啊~ 有好多做法, 我的做法是二分下界low,即判断在low到n-1之间是否存在符合要求的数.然后贪心去构造一个解(直觉告诉我直接构造最大解好像很麻烦的样子). 构造解是这样的:首先low和n相同的前几位先放好,因为不管怎样这几位都是不变的,然后假如某一位不相同了,