Title Description
Frank is a man of great love and neatness. He has a lot of books and a bookcase, and he wants to put the books on the shelves. The bookshelf can lay down all the books, so Frank first arranges the books in a high order on the shelves. But Frank found that because a lot of books were different in width, the book looked very untidy. So he decided to take the K book from it and make the bookshelf look neat.
The bookshelf's degree of ataxia is defined as: the sum of the absolute values of the difference in width of each of the two books. For example there are 4 books:
1x2 5x3 2x4 3x1 then Frank arranges it neatly:
1x2 2x4 3x1 5x3 ataxia is 2+3+2=7
It is known that the height of each book is different, ask you to get rid of the K book the smallest degree of ataxia.
input/output format
Input Format:
The first line is two digits N and K, and there are several copies of the book, from which several copies are removed. (1<=n<=100, 1<=k<n)
The following n lines, two digits per line, indicate the height and width of a book, both less than 200.
Guaranteed high Non-repetition
output format:
An integer line that represents the minimum degree of ataxia for the bookshelf.
input/Output sample
Input Sample # #:
4 11 22 43 15 3
Sample # # of output:
3
The idea of conversion, the best solution of the K book is removed, which is equivalent to preserving the optimal solution of N-k book.
F[I][J] indicates that the best solution of J book is taken from the previous I book.
1 /**/2#include <iostream>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <algorithm>7 using namespacestd;8 Const intmxn= $;9 structbook{Ten intw,h; One }A[MXN]; A intcmpConstBook A,ConstBook B) { - returna.h<b.h; - } the intF[MXN][MXN]; - intn,k; - intMain () { -scanf"%d%d",&n,&k); + inti,j; - for(i=1; i<=n;i++){ +scanf"%d%d",&a[i].h,&A[I].W); A } atk=n-K; -Sort (A +1, a+n+1, CMP); - for(i=1; i<=n;i++){ - for(j=2; J<=min (i,k); j + +) {//If you take only one copy, the degree of ataxia is 0, starting from 2 . -f[i][j]=100000; - for(intm=j-1; m<i;m++) {//enumerates the positions of the last book taken inF[i][j]=min (f[i][j],f[m][j-1]+abs (a[i].w-a[m].w));; - } to } + } - intans=100000; the for(i=k;i<=n;i++){ *ans=min (ans,f[i][k]); $ }Panax Notoginsengcout<<ans; - return 0; the}
Rokua P1103 Book finishing