POJ 3270 Cow sorting (replacement ring)

Source: Internet
Author: User

Cow sorting
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6393 Accepted: 2476

Description

Farmer John ' s n (1≤ n ≤10,000) Cows is lined up to being milked in the evening. Each cow have a unique "grumpiness" level in the range 1...100,000. Since Grumpy cows is more likely to damage FJ ' s milking equipment, FJ would like to reorder the cows in line so they is Lined up in increasing order of grumpiness. During This process, the places of any and cows (not necessarily adjacent) can be interchanged. Since Grumpy cows is harder to move, it takes FJ a total of X+Y units of time to exchange, cows whos E grumpiness levels is X and Y.

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 February Gold




Test Instructions: give you a sequence of numbers (each number is unique), each time you can exchange any two numbers, at the cost of these two numbers, and ask at least how much the price can be sorted in ascending order of this sequence.



#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include < Stdlib.h>using namespace Std;int a[100010],b[100100],p[100100];bool v[1000100];int ans;int n;int main () {while (scan        F ("%d", &n)!=eof) {ans = 0;            for (int i=0;i<n;i++) {scanf ("%d", &a[i]);        B[i] = A[i];        } sort (b,b+n);        memset (v,false,sizeof (v));        for (int i=0;i<n;i++) {P[b[i]] = i;////[b[i] position after fixed sort} for (int i=0;i<n;i++)            {int len = 0,sum = 0,min = 99999,start = A[i],id = i;                    if (v[i] = = False) {while (true)///Find permutation group, follow the code to know the meaning of permutation group {   sum + = start;   Find the sum of the permutation group v[id] = true;    Mark the member in the permutation group if (start<min) {min = start; Find the smallest element} id = P[starT];              ID is the position of the next permutation group member in array a len++;      The number of elements in the permutation group start = A[id];                        The value of the next element of the permutation group if (start = = A[i])///If equal, indicates that the end of the ring has been found, all elements of the permutation group are searched {    Break   Jump out of the loop}} int ans1 = Sum-min + (len-1) *min;                Formula, not yet know how to eject the int ans2 = sum + min + (len + 1) *b[0]; Ans + = ans1<ans2?    Ans1:ans2;    Want is two among the small that value}} printf ("%d\n", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3270 Cow sorting (replacement ring)

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.