提交时间:2024-05-25 14:44:26

运行 ID: 148768

#include<bits/stdc++.h> using namespace std; const int N=200; struct Node{ int a,b; }node[N]; bool cmp(Node a,Node b){ return a.a <b.a ; } int f[N][N],minn=0x7fff; int main(){ int n,k,m; memset(f,1145,sizeof(f)); cin>>n>>k;m=n-k; for(int i = 1; i <= n; i++)f[i][1] = 0; for(int i=1;i<=n;i++)cin>>node[i].a >>node[i].b ; sort(node+1,node+1+n,cmp); for(int i=1;i<=n;i++){ for(int j=i-1;j>=1;j--){ for(int l=2;l<=min(i,m);l++){ f[i][l]=min(f[i][l],f[j][l-1]+abs(node[i].b-node[j].b )); } } } for(int i=m;i<=n;i++)minn=min(minn,f[i][m]); cout<<minn; }