Adjacencies exchange Sort greedy class problem

This fact can be considered way constitute a class of problem-solving ideas, so I decided to do a summary, but did not write a complete paper like ouuan Juan guy.

Ouuan Juan guy's blog: important reference Problem of o-term exchange sequenced application and need to pay attention

Example 1 Kings game

Title Description

Coincides with \ (H \) National Day, the king invited \ (n \) ministers to play a game with prizes. First, he let each minister in the left and right hand on top of each wrote an integer king himself was left, each to write an integer on the right hand. Then, let the \ (n \) ministers in a row, the king stood in the front ranks. After lined up, all the ministers will receive a number of gold coins king reward, the number of coins for each minister were obtained: the product of the row on the left hand in front of all the ministers of the number divided by the number on his right hand, the result is then rounded down integer obtained.

The king does not want a minister to get a particularly large prize, so he would like to ask you to help him rearrange the order of the team, so to get the most reward minister, received the reward as little as possible. Note that the king's position has always been in the front ranks.

Input Format

The first line contains an integer \ (the n-\) , represents the number of ministers.

The second line contains two integers \ (A \) and \ (B \) , separated by a space between, each represent an integer of left and right on the king.

Next \ (n-\) rows, each row comprising two integers \ (A \) and \ (B \) , separated with a space between, each represent an integer minister on each left and right.

Output Format

An integer representing the team after winning re-arranged tours in the most number of gold coins minister obtained.

Resolve

This is the first problem I've seen a neighbor term exchange of optimality consider the subject, speaking a word, for a minister \ (i \) , his score \ (c_i \) this definition:
\ [C_i = \ lfloor \ frac {\ prod_ {j
= 1} ^ {i} l_j} {r_i} \ rfloor \] we want to minimize \ (C \) is the maximum value of this time we consider two Minister \ (a, b \ ) , if the \ (a \) ranked \ (B \) in front, there is \ (ANS1 = max (\ FRAC L {} {} R_A, \ FRAC {L \ r_B Times L_A} {}) \) , If \ (B \) ranked \ (A \) in front, there \ (ANS2 = max (\ FRAC r_B} {} {L, \ FRAC {L \ R_A Times L_B} {}) \) , wherein \ (L \) represents the number of the king left. we assume more preferably not switched, i.e. \ (ANS1 <ANS2 \) , it means that \ (max (\ frac {L } {r_a}, \ frac {L \ times l_a r_B {}}) <max (\ FRAC r_B} {} {L, \ FRAC {L \ R_A Times L_B} {}) \) . to show simplicity, we let\ (k1 = \ frac {L } {r_a}, k2 = \ frac {L \ times l_a} {r_b}, k3 = \ frac {L} {r_b}, k4 = \ frac {L \ times l_b} {r_a } \) , this time we have found a certain \ (K1 <K4, K2> K3 \) , then to get \ (ans1 <ans2 \) set up, only need to let \ (K4> K2 \) , you can ensure that \ (K4 \) is the largest of the four number, is written
\ [\ frac {L \ times
l_b} {r_a}> \ frac {L \ times l_a} {r_b} \] finishing available \ (l_ar_a <l_br_b \) , this is our sort of conditions.

Queen example 2 game

Title Description

Queen have \ (n \) ministers, left and right above each minister were written on a positive integer. Coincides with the advent of the National Day, the Queen decided to \ (\ n) ministers prize money, the first of which \ (i \) the number of bonus ministers obtained for the first \ (i-1 \) the number of former ministers and bonuses obtained \ (i \) is greater and the number of ministers on the left hand plus the first \ (i \) number of ministers on his right hand.

Formal speaking: We set up the first \ (\ i) positive integer ministers on the left hand as \ (a_i \) , the right hand is a positive integer \ (b_i \) , then the first \ (i \) ministers obtained bonus number is \ (C_i \) can be expressed as:
\ [C_i = \ the begin {Cases} & A_1 + B_1 & & {I =. 1} \\ & max (C_ {I-. 1}, \ sum_ {J =. 1} ^ { i} a_j) + b_i & &
{2 \ le i \ le n} \\ \ end {cases} \] , of course, mean the Queen does not want too much of the bonus is issued to the Minister, so she would like to invite you to rearrange about the order of the team, so as to obtain the largest bonus minister, received the bonus number as low as possible.

Note: Re-arrange the team does not mean you have to disrupt the order, we do not allow any change in the position of a minister.

Input Format

The first line contains a positive integer \ (T \) , represents a group of test data.

Next \ (T \) portions, a first portion of each row contains a positive integer \ (n-\) , representing the number minister.

Next, each part \ (n-\) rows, each row two positive integers, respectively, \ (a_i \) and \ (B_i \) , the meanings as described above.

Output Format

Total \ (T \) lines, each line contains an integer representing the number of bonus up to get bonus minister obtained.

Resolve

This question looks and Kings games are very similar, so we consider the use of o-key exchange method. After variousI will notSimplifying, we get the conditions should be \ (min (a_i, b_j) <min (a_j, b_i) \) , looks very correct, even in Los goo really \ (A \) out

but

It is wrong

Because we have come to the condition does not meet the strict weak ordering . \ (C ++ \) requires the sort operator must meet strict weak ordering:

(The following is an excerpt from ouuan Juan guy's blog)

  1. \ (x≮x \) (non-reflexive)
  2. If \ (X <Y \) , then \ (y≮x \) (asymmetric)
  3. If \ (X <Y, Y <Z \) , then \ (X <Z \) (transitivity)
  4. If \ (x≮y, y≮x, y≮z, z≮y \) , then \ (x≮z, z≮x \) (not comparable transmissibility)

In fact, we concluded that it is not satisfied with fourth, that is, when \ (min (a_i, b_j) = min (a_j, b_i) \) when, according to our conditions should exchange \ (a, b \) , but we can not guarantee that after the exchange the final solution produced better, and possibly even worse. we want to be treated separately are equal. for global consider it, \ (a \) prefix and can produce answers Effects Therefore, in order to minimize the maximum, we should \ (a \) smaller on the front, we determined the application of this embodiment if and only if \ (min (a_i, b_j) = min (a_j, b_i) \ ) .

inline bool cmp(node a, node b) {
    return min(a.l, b.r) == min(a.r, b.l) ? a.l < b.l : min(a.l, b.r) < min(a.r, b.l);
}

This is the ultimate practice of solving the problem.

to sum up

By comparing the two neighboring stars or exchange when the exchange will not worse, the optimal solution can be obtained by ortho exchange of items ordered.

O A Sorting item comparison function need to satisfy strict weak ordering and the ordering of adjacent elements will not be exchanged any more preferably after completion.

Using this algorithm, we must pay attention to the above two points, can only be really correct algorithm.

(Above excerpt from the blog guys ouuan Juan %%%)

Guess you like

Origin www.cnblogs.com/i-cookie/p/11429984.html