# (Abstract transformation backpack) Luo Gu P1282 domino (increase + / provincial election -)

Title Description

Domino upper and lower two blocks, each block has 1 to 6 points. Existing line up of

The number of points in the block above and denoted as S1, the number of points in the block and referred to as S2, the difference thereof | S1-S2 |. For example, in Figure 8-1, S1 = 6 + 1 + 1 + 1 = 9, S2 = 1 + 5 + 3 + 2 = 11, | S1-S2 | = 2. Each domino can be rotated 180 °, so that two upper and lower block swap positions. Dominoes is programmed to use a minimum number of revolutions of the difference between upper and lower rows of the minimum number of points.

For example in the drawings, as long as a final domino rotating 180 °, can make the difference between the upper and lower rows is 0 points.

Input Format

The first line of the input file is a positive integer n (1≤n≤1000), represents the number of domino dominoes. N represents the next row of n points dominoes. Each row has two positive integers separated by a space, the vertical box represents dominoes points and a to b, and 1≤a, b≤6.

Output Format

Output file only one line, contains an integer. It represents the minimum number of rotations obtained.

Sample input and output

Input # 1
4
6 1
1 5
1 3
1 2
Output # 1
1 
Analysis:
This question is actually a very abstract knapsack problem
(Are you kidding me that there is a backpack, obviously greedy good baa (≧ ∇ ≦) Techno??)
Is really backpack ah! QAQ
So how do we adjust it?
We first domino reversed, so that a large number of points at the top;
thus ensuring that the number of points above and below and must be greater than the number of points;
then change the number of points is the difference between the upper and lower points multiplied by 2 when the flip!
Thought of this, we can take into account: the difference between the upper and lower points of the beginning of the abstract into volume backpack, each domino as an object,
as we begin to put a large number of points on the top, and each put a flip on the number of +1. Consider: If I regret it later, I found not turn this domino better how to do? Then I'll turn it back, then there is no equivalent to turn the dominoes.

Thus, at the beginning of turn domino weight it is -1, the weight is not turned domino 1 (equivalent to the number of turns by weight)

Of course, the same domino is down to zero volume, weight items 0, because no matter how they turn, will not affect the upper and lower points difference.

At this point, the backpack model came out. This problem is simplified to: there are n items, given the volume of v [i] of each item, their weight is 1 or -1. Base for the weight of the backpack, volume TOT, now call this the n article go into the backpack, can not exceed the total volume of TOT, so that the weight of the article and the case where the smallest maximum volume.

Wherein, dp [i] [j] denotes the front i can hold items to a volume of the minimum weight j

VS [i] [j] denotes the front i j attached to suggest items Volume

#include <the iostream>
the using namespace STD;
int n-;
int DP [1005] [6005]; // DP [i] [j] j filled before the i-th item backpack
bool vs [1005] [6005] ; // VS [i] [j] before using the i-th tag articles filled j backpack feasible
int w [1005], base, tot; // w stored weight, base mass storage backpack, tot knapsack volume (initial maximum difference )
int Val [1005]; // val difference storage inverting domino each i can provide
inline int min (int X, Y int) //
{
return X <Y X:? Y;
}
int main ()
{
; int X, Y
n-CIN >>;
for (int I =. 1; I <= n-; I ++)
{
CIN >> >> X Y;
IF (X> Y)
{
Val [I] = 2 * (XY) ; // if more than the upper itself
w [i] = 1; // flip consideration. 1
TOT = XY +; // record volume backpack
}
IF (Y> X)
{
Val [I] = 2 * (YX); / / on itself at greater than
w [i] = - 1; // to reversing, flipping not directly equivalent to the original, i.e. the weight -1
tot + = yx; // Update the volume of
base ++; increase // backpack mass
}
}
for (int I =. 1; I <= n-; I ++) // order dominoes as stages
for (int j = 1; j <= tot ; j ++) // for all backpack volume, small to large push
{
DP [I] [J] DP = [. 1-I] [J]; // inherited previous state
vs [i] [j] = vs [ i-1] [J]; //
IF (VS [i-1] [J-Val [i]] || J-Val [i] == 0) // if the number i can migrate from the flip i-1 over
// advance the state as long as the presence or equal to a volume i contribution domino
if (! vs [i] [ j]) // If dp [i] [j] is not defined
{//
dp [i] [j] = dp [i-1] [ j-val [i]] + w [i]; // tag is directly updated and
VS [I] [J] =. 1; //
}
the else
DP [I] [J] = min (dp [i] [j] , dp [i-1] [j-val [i]] + w [i]); // otherwise, compared
// i-inverting domino from dp [i-1 ] [j-val [i] ] migrated, to add weight

}
int tmp;
for (int I = TOT; I> =. 1; I -) // looking backwards from the maximum volume, it can guarantee The minimum difference value
IF (VS [n-] [I]) //
{
tmp = I; //

BREAK;

}
COUT << Base + DP [n-] [tmp]; // change the state from the Base plus the amount needed to transfer the current, the current number of metastases determined
return 0;
}

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11409562.html