UV-10239 the book-shelver's Problem

Source: Internet
Author: User

Description

Problem d
The book-shelver's Problem

Input:Standard Input
Output:Standard output
Time limit:5 seconds
Memory limit:32 MB

 

You are given a collection of books, which must be shelved in a library bookcase ordered (from top to bottom in the bookcase and from left to right in each shelf) by the books 'catalogue numbers. the bookcase has a fixed width, but you may have any height you like. the books are placed on shelves in the bookcase in the usual upright manner (I. E ., you cannot lay a book on its side ). you may use as your shelves as you like, placed wherever you like up to the height of the bookcase, and you may put as your books on each shelf as you like up to the width of the bookcase. you may assume that the shelves have negligible thickness.

 

Now, given an ordered (by catalogue numbers) List of the heights and widths of the books and the width of the bookcase, you are expected to determine what is the minimum height bookcase that can shelve all those books.

 

Input

The input file may contain in multiple test cases. The first line of each test case contains an integerN (1 hour n hour 1000)That denotes the number of books to shelve, and a floating-point numberW (0 <W £ 1000)That denotes the width of the bookcase in centimeters. Then followNLines whereI-th (1 then I got N)Line contains two floating-point numbersHi (0 <Hi Hei 100)AndWI (0 <Wi-Fi W)Indicating the height and width (both in centimeters) ofI-thBook in the list ordered by catalogue numbers. Each floating-point number will have four digits after the decimal point.

 

A test case containing two zerosNAndWTerminates the input.

 

Output

For each test case in the input print a line containing the minimum height (in centimeters, up to four digits after the decimal point) of the bookcase that can shelve all the books in the list.

 

Sample Input
5 30.0000
30.0000 20.0000
20.0000 10.0000
25.0000 10.0000
30.0000 15.0000
10.0000 5.0000
10 20.0000
10.0000 2.0000
15.0000 10.0000
20.0000 5.0000
6.0000 2.0000
10.0000 3.0000
30.0000 6.0000
5.0000 3.0000
35.0000 2.0000
32.0000 4.0000
10.0000 6.0000
0 0.0000

 

Sample output
60.0000

65.0000

N books, wide and high. You can use wooden boards to place them on the shelves one by one. The width of each layer cannot exceed M.

Idea: The requirement should be put in sequence, so there are two possibilities for each book to put this layer and not put this layer, memory-based search, pay attention to the situation that all should be put at one layer

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>#include <cstdlib>using namespace std;const int maxn = 1005;const double inf = 0x3f3f3f3f3f3f3f3f;double m, h[maxn], w[maxn], dp[maxn];int n, vis[maxn];double dfs(int cur) {if (cur >= n+1)return 0;if (vis[cur])return dp[cur];double &ans = dp[cur];ans = inf;vis[cur] = 1;double H = h[cur], W = w[cur];for (int u = cur+1; u <= n+1; u++) {ans = min(ans, dfs(u)+H);W += w[u];H = max(H, h[u]);if (W-m > 1e-9)break;}return ans;}int main() {while (scanf("%d%lf", &n, &m) != EOF && n) {memset(vis, 0, sizeof(vis));for (int i = 1; i <= n; i++)scanf("%lf%lf", &h[i], &w[i]);double ans = dfs(1);printf("%.4lf\n", ans);}return 0;}



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.