提交时间:2024-06-23 12:01:12
运行 ID: 152363
#include<iostream> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int dp[101][101]; struct Book{ int x; int y; }a[101]; bool cmp(Book A,Book B){ return A.x<B.x; } int main(){ int n,k; cin>>n>>k; for(int i=1;i<=n;i++){ cin>>a[i].x>>a[i].y; } sort(a+1,a+n+1,cmp); memset(dp,0x3f,sizeof(dp)); dp[0][0]=0; for(int i=1;i<=n;i++){ dp[i][1]=0; } for(int i=1;i<=n;i++){ for(int j=1;j<=min(i,n-k);j++){ for(int k=1;k<i;k++){ dp[i][j]=min(dp[i][j],dp[k][j-1]+abs(a[i].y-a[k].y)); } } } int ans=0x3f3f3f3f; for(int i=n-k;i<=n;i++){ ans=min(ans,dp[i][n-k]); } cout<<ans<<endl; return 0; }