35分WA,求助巨佬

P2769 猴子上树

@[2019HeYongTao](/user/262401) long long
by 做梦想Peach @ 2020-08-04 21:56:47


我是这么改的
by 做梦想Peach @ 2020-08-04 21:57:14


@[2019OuYouKang](/user/239030) 难道不会MLE吗?
by 2019HeYongTao @ 2020-08-04 21:57:57


@[2019HeYongTao](/user/262401) 目测不会
by Dimly_dust @ 2020-08-04 21:58:34


@[Dimly_dust](/user/316896) MLE了
by 2019HeYongTao @ 2020-08-04 22:01:01


@[2019HeYongTao](/user/262401) 真会MLE
by 做梦想Peach @ 2020-08-04 22:01:13


难道是算法有问题吗?
by 2019HeYongTao @ 2020-08-04 22:07:06


@[2019HeYongTao](/user/262401) ```cpp #include<stdio.h> #include<ctype.h> #include<algorithm> #define min(x,y) (x<y?x:y) #define ll long long template <typename Tp> inline void read(Tp &x) { x = 0; int w = 1; char c=getchar(); for (;c<'0'||c>'9';c=getchar()) if (c == '-') w = -1; for (;c<='9'&&c>='0';c=getchar()) x=x*10+c-'0'; x *= w; } int n,m,a[5005],b[5005];ll f[2][5005]; inline ll abs(ll x) {return (x>0?x:-x);} int main(){ read(n); for(int i=1;i<=n;i++) read(a[i]); read(m); for(int i=1;i<=m;i++) read(b[i]); std::sort(a+1,a+n+1);std::sort(b+1,b+m+1); for(int i=1;i<=n;i++) for(int j=m;j>=1;j--) if(j==1) f[i%2][j]=f[(i+5)%2][j]+abs(a[i]-b[j]); else if(i==j) f[i%2][j]=f[(i+5)%2][j-1]+abs(a[i]-b[j]); else f[i%2][j]=min(f[(i+5)%2][j-1],f[(i+5)%2][j])+abs(a[i]-b[j]); return printf("%lld\n",f[n%2][m]),0; } ```
by Alphaban @ 2020-08-04 22:32:30


@[2019HeYongTao](/user/262401) 算法没问题,用滚动数组优化一下空间
by Alphaban @ 2020-08-04 22:33:14


@[CodePower](/user/112109) 谢谢巨佬!!!
by 2019HeYongTao @ 2020-08-05 23:08:19


|