luogu P3035 [USACO11DEC]Umbrellas for Cows S
题面传送门
一眼\(dp\)
但是题目中说价格没有单调性,所以要先预处理出后缀\(min\)
然后枚举断点即可。
代码实现:
#include<cstdio>
#include<algorithm>
#include<cstring>
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
int a[5039],f[5039],b[100039],n,m,k,x,y,ans,tot,pus,minn[100039];
int main(){
memset(f,0x3f,sizeof(f));
register int i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+n+1);
for(i=1;i<=m;i++) scanf("%d",&b[i]);
minn[m+1]=1e9;
for(i=m;i>=1;i--)minn[i]=min(minn[i+1],b[i]);
f[0]=0;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++) f[i]=min(f[i],f[j-1]+minn[a[i]-a[j]+1]);
}
printf("%d\n",f[n]);
}