Application of poj 3270 replacement

Source: Internet
Author: User

1. Find the initial and target statuses. Obviously, the target status is the sorted status.

2. Draw a replacement group and find a loop in it. For example, the number is 8 4 5 3 2 7, the target status is 2 3 4 5 7 8, and can be written into two loops: (8 2 7) (4 3 5 ).

3. observe one of the loops. Obviously, to minimize the exchange cost, use the smallest number 2 in the loop to exchange with the other two numbers 7 and 8. The price of this exchange is:

Sum-min + (LEN-1) * min

Simplified:

Sum + (LEN-2) * min

Sum is the sum of all numbers in the loop, Len is the length, and Min is the smallest number in the ring.

4. Considering another situation, we can call a number from another loop to enter this loop, reducing the exchange cost. For example, initial status: 1 8 9 7 6

Can be divided into two cycles: (1) (8 6 9 7), obvious, the second cycle is (8 6 9 7), the minimum number is 6. We can draw the smallest number 1 in the entire series to enter this loop. Change the second cycle to (8 1 9 7 ). Let this 1 complete the task, and then exchange with 6, let 6 return to the loop again. The cost of doing so is obvious:

Sum + min + (LEN + 1) * smallest

Sum is the sum of all the numbers in the cycle, Len is the length, Min is the smallest number in the ring, and smallest is the smallest number in the entire series.

5. therefore, the cost of sorting a loop is sum-min + (LEN-1) * min and sum + min + (LEN + 1) * small number of smallest. However, we do not know how to introduce the two formulas here.

6. When calculating a loop, we do not need to record all the elements of the loop. We only need to record the minimum number and sum of the loop.

7. When storing data, we can use a hash structure to map the elements and their locations to know the elements and quickly reverse query the element location. In this way, you do not need to search one by one.

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 10010; int A [maxn], B [maxn], dir [100005]; int vis [maxn]; int main () {int N; while (scanf ("% d", & N )! = EOF) {for (INT I = 0; I <n; I ++) {scanf ("% d", & A [I]); B [I] = A [I]; dir [A [I] = I;} Sort (B, B + n); memset (VIS, 0, sizeof (VIS); int ans = 0; For (INT I = 0; I <n; I ++) {If (! Vis [I]) {vis [I] = 1; int id = I, start = A [I]; int Len = 1, min _ = A [I], sum = A [I]; // find the replacement cycle; while (1) {If (B [ID] = Start) break; sum + = B [ID]; len ++; min _ = min (min _, B [ID]); Id = dir [B [ID]; vis [ID] = 1 ;} /// obtain the minimum cost in the current cycle; int TMP = min (min _ * (len-1) + sum-min _, B [0] * (LEN + 1) + sum + min _); ans + = TMP;} printf ("% d \ n", ANS);} return 0 ;}


Application of poj 3270 replacement

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.