我的思路就是枚举k天,求出每天抽去哪本书可以让不整齐度变的最小,然后标记这本书下一轮不去抽取,结果样例过了,提交0分,感觉思路没问题,求指教
/*
卫风·芄兰
芄兰之支,童子佩觿.
虽则佩觿,能不我知?
容兮遂兮,垂带悸兮.
芄兰之叶,童子佩韘.
虽则佩韘,能不我甲?
容兮遂兮,垂带悸兮.
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define tul tuple<int,int,int>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rep_(i,a,b) for(int i=a;i>=b;--i)
#define all(x) x.begin(),x.end()
#define bp(x) __builtin_popcountll(x)
#define ctz(x) __builtin_ctzll(x)
#define cy cout<<"Yes"<<endl
#define cn cout<<"No"<<endl
#define lc (rt<<1)
#define rc (rt<<1|1)
mt19937_64 rnd(time(0));
const int N=3e3+5,yyx=1e9+7;
vector<int> to[N];
int n,k,a[N];
int dp[N][N];
inline int mod(int x){
return (x%yyx+yyx)%yyx;
}
inline int cmin(int &x,int y){
return x>y?x=y,1:0;
}
inline int cmax(int &x,int y){
return x<y?x=y,1:0;
}
struct node{
int h,w;
}e[N];
inline void solve(){
cin>>n>>k;
vector<int> f(n+5,0),pre(n+5,0);
//rep(i,1,n) rep(j,1,k) dp[i][j]=2e14;
rep(i,1,n) cin>>e[i].h>>e[i].w;
sort(e+1,e+1+n,[&](node x,node y){
return x.h<y.h;
});
//dp[0][0]=dp[1][0]=0;
rep(i,2,n){
f[i]=abs(e[i].w-e[i-1].w);
pre[i]=pre[i-1]+f[i];
}
//rep(i,1,n) dp[i][0]=pre[i];
int sum=pre[n],mi=2e14;
map<int,int> mp;
rep(j,1,k){
int z=0;
rep(i,1,n){
if(mp[i]) continue;
if(i>=2&&i<=n-1&&cmin(mi,sum-f[i]-f[i+1]+abs(e[i+1].w-e[i-1].w))) z=i;
if(i==n||i==1){
if(cmin(mi,sum-f[i])) z=i;
}
}
mp[z]=1;
cmin(sum,mi);
}
cout<<mi<<endl;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
//freopen("D://321//in.txt","r",stdin);
//freopen("D://321//out.txt","w",stdout);
int _=1;
//cin>>_;
while(_--)
solve();
return 0;
}