4810. 【NOIP2016提高A组五校联考1】道路规划 (Standard IO)
作者:互联网
Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits
Description
Input
Output
Sample Input
5 1 4 5 2 3 3 4 2 1 5
Sample Output
3
Data Constraint
Hint
Source / Author: 学军中学 road
题解:
显然最长下降子序列。
O(n log n)算法:
扫一遍a,对于新的一个a,二分一个恰比他小的替换掉,没有比他小的放在队尾。
#include<bits/stdc++.h>
#define N 100010
#define inf 2147483647
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
#define mem(a,b) memset(a,b,sizeof(a))
#define mcy(a,b) memcpy(a,b,sizeof(a))
using namespace std;
int n,i,j,len,ans;
int a[N],b[N],c[N],g[N];
int find(int x)
{
int l=0,r=len,k=0;
while(l<=r)
{
int mid=(l+r)/2;
if(g[mid] < x)
{
k=mid;
r = mid -1;
} else l = mid +1;
}
return k;
}
void dp()
{
g[0] = inf;
for(i=1;i<=n;i++)
{
if(a[i] < g[len]) g[++len] = a[i];
else
{
int k = find(a[i]);
g[k] = a[i];
}
ans = max(ans , len);
}
return ;
}
int main()
{
open("road");
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&a[i]);
for(i=1;i<=n;i++) scanf("%d",&b[i]) , c[b[i]]=i;
for(i=1;i<=n;i++) a[i] = c[a[i]];
dp();
printf("%d",ans);
return 0;
}
标签:NOIP2016,4810,Limits,int,freopen,Input,sizeof,联考,define 来源: https://blog.csdn.net/Com_man_der/article/details/94572234