P1103 Book sorting and p1103 Book sorting

Source: Internet
Author: User

P1103 Book sorting and p1103 Book sorting
Description

Frank is a neat person. He has a lot of books and a bookshelf and wants to put the books on the shelf. The bookshelves can put down all the books, so Frank first arranged the books in High Order on the bookshelves. However, Frank found that because many books have different widths, the books still look very untidy. So he decided to remove k books from the book so that the bookshelves could look neat.

The non-uniformity of the bookshelves is defined as the sum of the absolute values of the difference in the width of each two books. For example, there are four books:

1x2 5x3 2x4 3x1 then Frank arranged it neatly as follows:

1x2 2x4 3x1 5x3 untidy is 2 + 3 + 2 = 7

We know that the height of each book is different. Please find the minimum untidy degree after removing k books.

Input/Output Format

Input Format:

 

The numbers n and k in the first line represent the number of books and the number of books removed from them. (1 <= n <= 100, 1 <= k <n)

In the n rows below, two numbers in each row indicate the height and width of a book, both smaller than 200.

Ensure high repetition

 

Output Format:

 

An integer in one row indicates the minimum untidy degree of the bookshelf.

 

Input and Output sample input sample #1:
4 11 22 43 15 3
Output sample #1:
3

We use reverse thinking.
Use dp [I] [j] to represent the previous I book, leaving the minimum value of j book

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 const int MAXN=301; 9 void read(int &n)10 {11     char c='+';int x=0;bool flag=0;12     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}13     while(c>='0'&&c<='9'){x=x*10+c-48;c=getchar();}14     flag==1?n=-x:n=x;15 }16 int n,k;17 struct node18 {19     int gao,kuan;20 }book[MAXN];21 int comp(const node &a,const node &b)22 {23     return a.gao<b.gao;24 }25 int dp[MAXN][MAXN];26 int presum[MAXN];27 int main()28 {29     read(n);read(k);30     k=n-k;31     for(int i=1;i<=n;i++)32     {33         read(book[i].gao);34         read(book[i].kuan);35     }36     sort(book+1,book+n+1,comp);37     for(int i=2;i<=n;i++)38         for(int j=2;j<=min(k,i);j++)39         {40             dp[i][j]=0x7fffff;41             for(int k=j-1;k<i;k++)42                 dp[i][j]=min(dp[i][j],dp[k][j-1]+abs(book[i].kuan-book[k].kuan));43         }44     int ans=0x7fffff;45     for(int i=k;i<=n;i++)46         ans=min(ans,dp[i][k]);47     printf("%d",ans);48     return 0;49 }

 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.