POJ 3270 Replacement Use

Source: Internet
Author: User

1. determine the initial and target status.

Clear. The sort state of the target State.

2. arrive at the permutation group. For example, the number is 8 4 5 3 2 7, the target state is 2 34 5 7 8. Can be written as two loops:(8 2 7) (4 3 5).

3. observe a cycle, obviously. to minimize the switching cost, use the smallest number 2in the loop. Go with the additional two numbers. 7 with 8 Exchange.

The cost of such an exchange is:

Sum-min + (len-1) * min

After the simplification:

Sum + (len-2) * min

Thesum of all the numbers in this loop,len is the length,min is the smallest number in the ring.

4. taking into account the second situation, We are able to adjust a number from another loop into the loop. Make the exchange cost less. For example, initial state:1 8 9 7 6

Can be decomposed into two loops:(1) (8 6 9 7), obviously. The second loop is (8 6 9 7)and the smallest number is 6.

We were able to deploy the smallest number 1 of the entire sequence into this cycle. Change the second loop to:(8 1 9 7).

Let this 1 Complete the task, and then swap with 6 . Let 6 go back to the loop again.

The cost of doing so is clearly:

sum + min + (len + 1) * Smallest

Sum of all the numbers in this loop. Len is the length,min is the smallest number in the ring, andsmallest is the smallest number in the whole sequence.

5. Thus, the price of a loop is sum-min + (len-1) * min and sum + min + (len + 1) * Smallest Small That number. But here are two formulas that do not yet know how to launch.

6. when we calculate the cycle, we do not need to record all the elements of the cycle, only the smallest number of the cycle and its corresponding.

7. we are able to use a hash structure when the number is stored. The element and its position are matched to achieve the purpose of knowing the element and being able to reverse the position of the element at high speed. so you don't have to search for them.

#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 permutation loop; while (1) {if (b[id] = = start) break;                    Sum + = B[id];                    Len + +;                    min_ = min (Min_,b[id]);                    id = dir[b[id]];                Vis[id] = 1; }///To find 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;}


POJ 3270 Replacement Use

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.