每只老鼠都最多有两个选择,每只老鼠的选择互不交叉,直接dp
1.dp[i][0] 老鼠i朝,离他最近的靠左的ch走
2.dp[i][1] 朝靠右的走
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int N=1e5+100;
int mi[N],ch[N],rr[N];
int dp[N][2];
int fun(int x1,int y1,int x2,int y2){
if(dp[x1][y1]==0)return 0;
int x=ch[rr[x1]-y1],y=ch[rr[x2]-y2];
if(x!=y)return 1;
if(abs(mi[x1]-x)==abs(mi[x2]-x))return 1;
return 0;
}
int main(){
#ifdef DouBi
freopen("in.cpp","r",stdin);
#endif // DouBi
int n,m,y1,y2;while(scanf("%d%d%d%d",&n,&m,&y1,&y2)!=EOF){
for(int i=0;i<n;i++)scanf("%d",&mi[i]);
for(int i=0;i<m;i++)scanf("%d",&ch[i]);
int j=0;
for(int i=0;i<n;i++){
while(j+1<m&&ch[j]<=mi[i])j++;
rr[i]=j;
}
memset(dp,0,sizeof(dp));
if(rr[0]>0){
int x=rr[0];
if(ch[x]-mi[0]==mi[0]-ch[x-1]){
dp[0][0]=dp[0][1]=1;
}
else if(ch[x]-mi[0]>mi[0]-ch[x-1])dp[0][1]=1;
else dp[0][0]=1;
}
else {
dp[0][0]=1;
}
for(int i=1;i<n;i++){
int x=rr[i];
if(rr[i]>0){
if(ch[x]-mi[i]==mi[i]-ch[x-1]){
dp[i][0]=max(fun(i-1,0,i,0)+dp[i-1][0],
fun(i-1,1,i,0)+dp[i-1][1]);
dp[i][1]=max(fun(i-1,0,i,1)+dp[i-1][0],
fun(i-1,1,i,1)+dp[i-1][1]);
}
else if(ch[x]-mi[i]>mi[i]-ch[x-1]){
dp[i][1]=max(fun(i-1,0,i,1)+dp[i-1][0],
fun(i-1,1,i,1)+dp[i-1][1]);
}
else {
dp[i][0]=max(fun(i-1,0,i,0)+dp[i-1][0],
fun(i-1,1,i,0)+dp[i-1][1]);
}
}
else {
dp[i][0]=max(fun(i-1,0,i,0)+dp[i-1][0],
fun(i-1,1,i,0)+dp[i-1][1]);
}
}
//for(int i=0;i<n;i++)printf("%d ",rr[i]);printf("\n");
//for(int i=0;i<n;i++)printf("%d %d\n",dp[i][1],dp[i][0]);
printf("%d\n",n-max(dp[n-1][0],dp[n-1][1]));
}
return 0;
}