14990: [Usaco2017 Feb]Why Did the Cow Cross the Road II

#include <iostream>
#include <cstdio> 
#include <algorithm>
#include <cstring>
using namespace std;
#define reg register
inline char gc() {
    static const int bs = 1 << 22;
    static unsigned char buf[bs], *st, *ed;
    if (st == ed) ed = buf + fread(st = buf, 1, bs, stdin);
    return st == ed ? EOF : *st++;
}
#define gc getchar
inline int read() {
    int res=0;char ch=gc();bool fu=0;
    while(!isdigit(ch))fu|=(ch=='-'), ch=gc();
    while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48), ch=gc();
    return fu?-res:res;
}
#define N 100005
int n;
int a[N], b[N];
int pos[N];
int c[N*9], cnt, tmp[10];
int low[N*9], ans;

int main()
{
    n = read();
    for (reg int i = 1 ; i <= n ; i ++) a[i] = read();
    for (reg int i = 1 ; i <= n ; i ++) pos[b[i] = read()] = i;
    for (reg int i = 1 ; i <= n ; i ++)
    {
        int top = 0;
        for (reg int j = max(1, a[i] - 4) ; j <= min(n, a[i] + 4) ; j ++)
            tmp[++top] = pos[j];
        sort(tmp + 1, tmp + 1 + top);
        for (reg int j = top ; j >= 1 ; j --) c[++cnt] = tmp[j];
    }
    low[++ans] = c[1];
    for (reg int i = 2 ; i <= cnt ; i ++)
    {
        if (c[i] > low[ans]) low[++ans] = c[i];
        else {
            int t = lower_bound(low + 1, low + 1 + ans, c[i]) - low;
            low[t] = c[i];
        }
    }
    cout << ans << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值