"POJ 3270" Cow sorting (permutation Group sort)

Source: Internet
Author: User
Tags fread

Cow Sorting (permutation Group sort)
time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6909 Accepted: 2716

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 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:lineI+1 describes the grumpiness of cowI.

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




Main Topic:

John's Ranch has n cows, and each cow has a rank level, guaranteeing no cattle of the same grade.
Two cows with a known exchange level of a, A and B are spent a+b
Q. How much does it cost to be able to rank n cows as a sequence that increments from left to right?

The original replacement can also play this way! Rise Posture

First consider turning the original sequence into several permutations. Such as
3 4 1 5 2--(31) (425)
It is known that the displacement is not affected, so for each permutation, it is necessary (len-1) to return all the bits (len is the displacement length). So take This. min. min to the rest of the cattle the cost must be minimal, for sum+min* (len-1)

In addition, there is a situation that may be more optimal:
Take out all cows in the lowest rank of small, with the minimum value in the current permutation min Exchange location, spend small+min, in fact, is the interchange of the replacement, and then the addition of the cattle in the order, the cost of small* (len-1) + (sum-min)
Then interchange with min, spend Small+min

Total cost small* (len+1) +sum+min

Take a minimum of two cases.
As for the processing sequence, I sort it out after marking each cow where it should be, and then each permutation is done separately.



The code is as Follows:

#include <iostream>#include <cmath>#include <vector>#include <cstdlib>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <list>#include <algorithm>#include <map>#include <set>#define LL Long Long#define PR pair<int,int>#define Fread () freopen ("in.in", "r", stdin)#define Fwrite () freopen ("out.out", "w", stdout)using namespace STD;Const intINF =0x3f3f3f3f;Const intMSZ =10000;Const intMoD =1e9+7;Const DoubleEPS =1e-8;structgroup{intlen,mn,sum;};structmoon{intlevel,id;BOOL operator< (Const structMoon A)Const{returnLevel < a.level; }}; Moon mn[10010]; Group gp[10010];intTpintnum[10010];intval[10010];BOOLvis[10010];voidSolveintPosintId) {gp[tp].mn = INF; Gp[tp].len = Gp[tp].sum =0; while(!vis[id]) {vis[id] =1;        gp[tp].len++;        gp[tp].mn = Min (gp[tp].mn,val[id]);        Gp[tp].sum + = val[id];    id = num[id]; }}intMain () {//fread ();    //fwrite ();    intNscanf("%d", &n); for(inti =0; I < n; ++i) {scanf("%d", &mn[i].level);    Mn[i].id = i; } Sort (mn,mn+n); for(inti =0; I < n;        ++i) {num[mn[i].id] = i;    val[i] = mn[i].level; } TP =0;intMN = INF;memset(vis,0,sizeof(vis)); for(inti =0; I < n; ++i) {if(vis[i])Continue;        Solve (tp,i);    MN = Min (mn,gp[tp++].mn); }intAns =0; for(inti =0; I < tp; ++i) {ans + = min (gp[i].mn* (gp[i].len-1) +gp[i].sum-gp[i].mn, mn* (gp[i].len+1) +gp[i].mn+gp[i].sum); }printf("%d\n", ans);return 0;}

"POJ 3270" Cow sorting (permutation Group sort)

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.