UVa 311-packets

Source: Internet
Author: User

Topic: There are 1x1,2x2,3x3,4x4,5x5,6x6 of square blocks, ask at least a few 6x6 boxes can be put down.

Analysis: Greedy heart. 1x1 is used to fill the remaining space, any space can be put down;

First of all, each 6x6,5x5,4x4 to a single box, so the rest of the space to fill the 2x2,1x1 as the best;

Then, the rest of the 3x3,2x2, first filled with the placement (4 3x3,9 2x2 Group assigned to the best);

Finally, there are less than 4 3x3, 2x2 less than 9, up to two boxes, 1x1 last fill.

(proving that the second and third steps are optimal, with a total of 36 combinations, of which 2 boxes are:

<1,6>,<1,7>,<1,8>,<1,6>,

<2,4>,<2,5>,<2,6>,<2,7>,<2,8>

<3,2>,<3,3>,<3,4>,<3,5>,<3,6>,<3,7>,<3,8>

For each situation, add <4,9> redistribute, you can't get smaller boxes, so they are best allocated.

SAY: See is not see is greedy heart, use DP solution 3x3 and 2x2 allocation way ╮(╯▽╰)╭.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;int block[7];int maps[5][10] = { 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2,2,2,1,1,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2};int dp[505 ][505];int Main () {while (~SCANF ("%d", &block[1]) {int sum = block[1];for (int i = 2; I <= 6; + + i) {scanf ("%d", &amp ; block[i]); sum + = Block[i];} if (!sum) break;//6x6int count = Block[6];//5x5count + = block[5];block[1] = min (block[1], 11*block[5]);//4x4count + = Bloc K[4];if (5*block[4] > Block[2]) {block[1] = min (block[1], 20*block[4]-4*block[2]); block[2] = 0;} else {block[2]-= 5*block[4];} /*//dpfor (int i = 0; I <= block[3]; + + i) for (int j = 0; J <= Block[2]; + + j) Dp[i][j] = 100001;for (int i = 0; I &l T;= 4; + + i) for (int j = 0; J <= 9; + + j) Dp[i][j] = maps[i][j];for (int i = 0; I <= block[3]; + + i) for (int j = 0; J <= BLOCK[2]; + + j) {if (I >= 4 && J >= 0 && Dp[i][j] > dp[i-4][j-0]) dp[i][j] = dp[i-4][j-0]+1;if (i >= 3 && J >= 1 && DP I [j] > Dp[i-3][j-1]) dp[i][j] = dp[i-3][j-1]+1;if (i >= 2 && J >= 3 && dp[i][j] > dp[i-2][j-3]) d P[I][J] = dp[i-2][j-3]+1;if (i >= 1 && J >= 5 && Dp[i][j] > dp[i-1][j-5]) dp[i][j] = dp[i-1][j-5]+ 1;if (i >= 0 && J >= 9 && dp[i][j] > dp[i-0][j-9]) dp[i][j] = dp[i-0][j-9]+1;} DP *///greedy//3x3count + block[3]/4;block[3]%= 4;//2x2count + block[2]/9;block[2]%= 9;//greedy count + = Maps[block[3]][blo    Ck[2]];count + = (block[1]+9*block[3]+4*block[2]-36*maps[block[3]][block[2]]+35)/36;printf ("%d\n", count);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVa 311-packets

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.