USACO月赛2017.02 铂金组T2--NOCROSS

Description

Farmer John is continuing to ponder the issue of cows crossing the road through his farm,introduced in the preceding problem. He realizes that interaction between some pairs of breeds is actually acceptable if the breeds are friendly, a property that turns out to be easily characterized in terms of breed ID: breeds aa and bb are friendly if |a−b|≤4, and unfriendly otherwise. It is ok for cows to wander into fields designated for other breeds, as long as they are friendly.Given the ordering of NN fields on both sides of the road through FJ’s farm (again, with exactly one field for each breed on each side), please help FJ determine the maximum number of crosswalks he can draw over his road, such that no two intersect, and such that each crosswalk joins a pair of fields containing two breeds that are friendly. Each field can be accessible via at most one crosswalk (so crosswalks don’t meet at their endpoints).

INPUT FORMAT (file nocross.in):
The first line of input contains N(1≤N≤100,000). The next N lines describe the order, by
breed ID, of fields on one side of the road; each breed ID is an integer in the range 1…N.
The last N lines describe the order, by breed ID, of the fields on the other side of the road.
Each breed ID appears exactly once in each ordering.

OUTPUT FORMAT (file nocross.out):
Please output the maximum number of disjoint “friendly crosswalks” Farmer John can
draw across the road.

题解

如果写 N2 笨蛋的话,应该一看就知道了,直接套最长公共子序列的DP就可以。那么我们就要思考如何来优化这个DP。注意到题目中一个特殊的条件,只有相差小于等于4的牛才是友好的,这就限制了一头牛最多与9头牛友好,那么,由于第一行的牛肯定是按照排列顺序枚举,这就限制了之前更新过的状态一定在它之前,那么我们在枚举第二行的牛的时候,只需要保证前一状态的第二行的牛也在它之前就可以了。如何保证,我们可以把答案记在第二行的牛上,然后每次用树状数组维护前缀最大值即可。这里更新的时候要注意,枚举第二行的牛时一定要从大到小枚举(为了防止自己更新自己)。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100006
#define lowbit(x) (x&-x)
using namespace std;
inline char nc(){
    static char buf[100000],*i=buf,*j=buf;
    return i==j&&(j=(i=buf)+fread(buf,1,100000,stdin),i==j)?EOF:*i++;
}
inline int _read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
int n,a[maxn],p[maxn],f[maxn],tmp[16];
void put(int x,int y){for(;x<=n;x+=lowbit(x))f[x]=max(f[x],y);}
int get(int x){
    int sum=0;
    for(;x;x-=lowbit(x))sum=max(sum,f[x]);
    return sum;
}
bool cmp(int x,int y){return x>y;}
int main(){
    freopen("nocross.in","r",stdin);
    freopen("nocross.out","w",stdout);
    n=_read();
    for(int i=1;i<=n;i++)a[i]=_read();
    for(int i=1;i<=n;i++)p[_read()]=i;
    for(int i=1;i<=n;i++){
        tmp[0]=0;
        for(int j=a[i]-4;j<=a[i]+4;j++) if(j>0&&p[j])tmp[++tmp[0]]=p[j];
        sort(tmp+1,tmp+1+tmp[0],cmp);
        for(int j=1;j<=tmp[0];j++)put(tmp[j],get(tmp[j]-1)+1);
    }
    printf("%d",get(n));
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值