POJ 3270 Cow Sorting (replacement)

Source: Internet
Author: User
Cow Sorting
Time Limit:2000 MS   Memory Limit:65536 K
Total Submissions:4753   Accepted:1734

Description

Farmer John'sN(1 ≤N≤ 10,000) cows are lined up to be milked in the evening. each cow has a unique "grumpiness" level in the range 1... 100,000. since grumpy cows are more likely to damage FJ's milking equipment, FJ wowould like to reorder the cows in line so they are lined up in increasing order of grumpiness. during this process, the places of any two cows (not necessarily adjacent) can be interchanged. since grumpy cows are harder to move, it takes FJ a totalX+YUnits of time to exchange two cows whose grumpiness levels areXAndY.

Please help FJ calculate the minimal time required to reorder the cows.

Input

Line 1: A single integer: N.
Lines 2 .. N+ 1: Each line contains a single integer: line I+ 1 describes the grumpiness of cow I.

Output

Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.

Sample Input

3231

Sample Output

7

Hint

2 3 1: Initial order.
2 1 3: After interchanging cows with grumpiness 3 and 1 (time = 1 + 3 = 4 ).
1 2 3: After interchanging cows with grumpiness 1 and 2 (time = 2 + 1 = 3 ).

Source

USACO 2007 February Gold

Farmer John has a nheaded ox (1 ≤ N ≤ 10000). This nheaded ox is very suitable, each has a different temper index L (1 ≤ L ≤ 100000), which is disordered by temper index. The larger the index, the more likely it will damage farmer's milking machine, so farmer wants to sort the cows in an ascending order of temper indexes to protect their facilities, but the price for exchanging the two cows is the temper index of the two cows, now, I will tell you the number of cows N and the temper index Li OF THE nheaded cows, at the minimum cost.

This is the same as the "boring sorting" (P247) in the black book, and it turns out to be the Final question of ACM/ICPC 02. I will go, after a long time, I thought it was only half-right ......, decisive YM God question.

Train of Thought: Read the questions carefully and see that FARMER is sorting the order of exchange cattle in pairs, so we should move closer to the replacement, and this question is indeed the application of replacement (in some problem-solving reports, it is called the replacement group, in fact, this is only a single replacement, so you don't need to make it a group ). Let's abstract these unordered cows into a replacement. One movement is to replace a pair of elements. For example:

                     

                      

Then we use a replacement ring to represent two third-order rings (1 6 5) (2 3 4). That is to say, these six cows are two unordered subsequences, in each subsequence, the order is arranged in the order of coordinates. To minimize the cost, we should minimize the number of replicas, and minimize the number of replicas. According to the above division, we know that in a ring with a length of m, we should move at least the number of m-1 times to make it orderly, so that we can ensure the minimum number of times. So how can we minimize the number of replacements? We know that all replicas can be split into the form of the product of the transpose (level 2 ring). We may split the above ring (1 6 5) into (1 6) (1 5 ), or (6 5) (6 1), or (5 1) (5 6), this is actually three replacement solutions. It is not difficult to find out that the optimal solution is to find the smallest element in the ring and replace him with other elements in the ring. The image point is to find the medium with the smallest temper from this wave of cows, let it change its temper to the appropriate place. It seems that this problem has been solved, and my idea is just here.

However, this idea is not completely correct. If the smallest element in each ring in the given replacement is the smallest element in the replacement, the above idea must be correct. But if the smallest element is not in this ring, is there a better strategy? The answer is yes. Try the data set 1, 9, 7, and 6. Divide it into (8, 6, 9, 7) (1) (directly divided by right without writing a subscript), then we exchange 1 and 6, let 1 be added to the previous ring to replace the other three big numbers, and finally change 6. If we find that, the cost is better than the above strategy. That is to say, the minimum number of replacements may not be optimal. Are there any other better policies besides the two policies described above? From the greedy point of view, it cannot be better, so we only need to select a smaller one from the above two strategies. Two calculation schemes are provided:

First strategy: sum1 = (L1 + Min (L) + (L2 + Min (L) +... + (Lm-1 + Min (L) among them: sum1 is the total cost, Li is the number of beef temper in the cycle, except that a very small total of S-1, Min (L) is the temper of the smallest cattle.

Sort out and get: sum1 = sum (L) + (m-2) * Min (L)

Second strategy: sum2 = (L1 + MIN) + (L2 + MIN) +... + (Lm-1 + MIN) + 2 * (Min (L) + MIN) Where MIN is the minimum temper of ntou ox

The result is as follows: sum2 = sum (L) + Min (L) + (m + 1) * MIN

 

Each cycle uses two methods to find a small sum, that is, the total minimum phone fee. As for how to calculate the cyclic section, you can sort it by count, because counting sorting can find the subscript of the element in the forward order, and the time complexity of counting sorting is O (n + k) where n is the number of sorting elements, k is the maximum value of elements, and count sorting is a non-Comparative Sorting Algorithm with low time complexity. However, it is less applicable and can be Wikipedia without understanding the principles, I spoke very well. The question is difficult, and it is a question of mathematics + greed.

 

 

/* POJ 3270 Cow Sorting replaces each cycle to determine the cost of a cycle. There are two possible scenarios: optimal 1. exchange with the minimum cost of the cycle, and other cattle. 2. Global minimum cost. the ox first exchanges the minimum cost of the ox in the ring and then finally exchanges it back. */# include <stdio. h> # include <iostream> # include <algorithm> # include <string. h> # include <math. h> using namespace std; const int MAXN = 10010; bool vis [MAXN]; struct Node {int id; int num;} node [MAXN]; bool cmp (Node, node B) {return. num <B. num;} int MIN; int solve (int n) {memset (vis, false, sizeof (vis) ); Int ans = 0; for (int I = 1; I <= n; I ++) {if (! Vis [I]) {vis [I] = true; int cnt = 1; int sum = node [I]. num; int m = node [I]. num; int l = node [I]. id; while (l! = I) {vis [l] = true; cnt ++; sum + = node [l]. num; if (m> node [l]. num) m = node [l]. num; l = node [l]. id;} ans = ans + min (sum-m + m * (cnt-1), sum + m + (cnt + 1) * MIN);} return ans ;} int main () {int n; while (scanf ("% d", & n )! = EOF) {MIN = 10000000; for (int I = 1; I <= n; I ++) {scanf ("% d", & node [I]. num); node [I]. id = I; if (MIN> node [I]. num) MIN = node [I]. num;} sort (node + 1, node + n + 1, cmp); printf ("% d \ n", solve (n);} 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.