Poj 3270 Cow Sorting (initial replacement group)

Source: Internet
Author: User

 

Give n integers and convert them into an ascending sequence. The price of each two numbers exchanged is the sum of the two numbers. The minimum cost after sorting is successful.

 

This topic examines the knowledge of the replacement group. I have a detailed explanation on the black book p247. Summarize the replacement groups to facilitate review.

 

GROUP: given a set G = {a, B, c ...} and the binary operation on the set G. If the set G satisfies the closed condition and combines the law, there are units and inverse elements, then the set G is a group under the operation.

 

Replacement: Replacement between n elements 1, 2,... and n can be expressed

1 2 3... n

A1 a2 a3...

1 is replaced by a number of a1 in 1 to n, 2 is replaced by a number a2 in 1 to n, until n is replaced by a number an in 1 to n, and a1, a2 .... an is different from each other.

 

Replacement group: a set of replacements. The operation is a replacement connection.

 

A1 a2 a3...

Loop: Note (a1, a2... an) = a2 a3 a4... a1 is called an n-level loop. Each replacement can be written as the product of several cycles.

 

For example, replace 1 2 3 4 5 6 = (1 3 6) (2 5) (4)

3 5 6 4 2 1

The number of cyclic nodes for replacement is the number of the preceding cycles. For example, the number of cyclic nodes above is 3.

 

Return to this question. For example, there is a sequence: 1 8 9 7 6

To change 1 8 9 7 6 to an ordered sequence 1 6 7 8 9, it can be seen as a replacement.

1 8 9 7 6

1 6 7 8 9

Because each replacement can be seen as the product of several rotations (cycles), the above replacement can be expressed as the product of two cycles (1) (6, 7, 8, 9. Our goal is to change to a loop (1) (6) (7) (8) (9), so each swap must be between two elements in the same loop.

 

Then for a loop I, set its length to ki, then it should exchange at least the ki-1, that is, each time let an element reach the target position. Since the number of exchanges is certain, the minimum cost of the exchange is to exchange ti, the smallest element in the loop, with other elements. Based on this knowledge, we know that there are two solutions to solve the problem:

1. For each loop, the smallest element ti in the loop is exchanged with other elements, so sumi + (ki-2) * ti, (sumi is the sum of the elements of the loop)

2. let ti first exchange m with the smallest element of n elements, Let m enter this loop, and exchange them with the remaining ki-1 elements in turn to the target location, and then let m and ti exchange, ti exits this loop. In this way, sumi + ti + (ki + 1) * m is spent.

 

To sum up: All cycles are switched to the required minimum cost = sum + Sigma min {(ki-2) * ti, ti + (ki + 1) * m };

For each loop, record it back and forth with two arrays. From the above formula, we can see that we only need to record the ki (length) and ti (minimum element) of each loop ).

 

 

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; const int INF = 0x3f3f3f3f; struct node {int num; int Min;} ans [10010]; // record the length and minimum element of each loop int main () {int n, a [10010], B [1000010]; int vis [1000010]; int cnt, Min; int I, t; int ans_num; int sum; int Minn; while (~ Scanf (% d, & n) {sum = 0; Minn = INF; for (I = 1; I <= n; I ++) {scanf (% d, & a [I]); sum + = a [I]; B [a [I] = I; Minn = min (Minn, a [I]); // min number among n} sort (a + 1, a + 1 + n); memset (vis, 0, sizeof (vis); ans_num = 0; while (1) {// find an unaccessed number each time, and start from this number to find its loop for (I = 1; I <= n; I ++) if (! Vis [a [I]) break; if (I> n) break; cnt = 0; // The number of elements in the loop is Min = INF; // The smallest element in the loop t = a [I]; while (1) {vis [t] = 1; Min = min (Min, t); cnt ++; if (! Vis [a [B [t]) t = a [B [t]; else break;} ans [++ ans_num] = (struct node) {cnt, min };}for (int I = 1; I <= ans_num; I ++) {int num = ans [I]. num; int Min = ans [I]. min; sum + = min (num-2) * Min, Min + (num + 1) * Minn);} printf (% d, sum);} 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.