HELP
查看原帖
HELP
1404123
liuruihang2027楼主2025/1/18 10:06

50分求调

#include <bits/stdc++.h>
using namespace std;
int n,m,a[501],f[4000101],c[4000101],maxa,ans=4000101,x;
int main(){
	cin>>n>>m;
	for (int i=1;i<=n;i++){
		cin>>a[i];
		maxa=max(maxa,a[i]);
		c[a[i]]++;
	}
	memset(f,127,sizeof(f));
	int p=maxa+m-1;
	for (int i=0;i<=p;i++){
		if (i<=m-1){
			x=0;
			for (int j=0;j<=i;j++){
				x=x+(i-j)*c[j];
			}
			f[i]=min(f[i],x);
			continue;
		}
		for (int j=i-m-m+1;j<=i-m;j++){
			x=0;
			for (int k=j+1;k<=i;k++){
				x=x+(i-k)*c[k];
			}
			f[i]=min(f[i],f[j]+x);
		}
	}
	for (int i=maxa;i<=p;i++){
		ans=min(ans,f[i]);
	}
	cout<<ans;
	return 0;
}
2025/1/18 10:06
加载中...