【POJ 3270】Cow Sorting(置换群排序)





Cow Sorting(置换群排序)

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6909 Accepted: 2716

Description

Farmer John’s N (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 would 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 total of X+Y units of time to exchange two cows whose grumpiness levels are X and Y.

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

3
2
3
1

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

题目大意:

John的牧场里有n头牛,每头牛有一个等级level,保证没有相同等级的牛。

已知交换等级为a,b的两头牛的花费为a+b

问最少需要多少花费,能把n头牛排为从左往右等级递增的序列?

原来置换还可以这么玩!涨姿势

首先考虑把原始序列变成几个置换。如

3 4 1 5 2 -> (31)(425)

已知置换间是不相影响的,那么对于每个置换,最少需要(len-1)次即可全部归位(len为置换长度)。那么拿这个置换中最小值min将其余牛归位花费一定是最小,为sum+min*(len-1)

除此之外还有一种情况可能更优:

取出所有牛中等级最低的small,与当前置换中的最小值min交换位置,花费small+min,其实也就是互换所属置换,然后将除其外的牛排序,花费为small*(len-1)+(sum-min)

然后与min互换,花费small+min

总花费small*(len+1)+sum+min

两种情况取最小即可。

至于处理序列,我排序后标记每个牛应在的位置,然后每个置换都单独搞一下就好。

代码如下:

#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 int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

struct Group
{
    int len,mn,sum;
};

struct Moon
{
    int level,id;
    bool operator <(const struct Moon a)const
    {
        return level < a.level;
    }
};

Moon mn[10010];
Group gp[10010];
int tp;
int num[10010];
int val[10010];
bool vis[10010];

void solve(int pos,int id)
{
    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];
    }
}

int main()
{
    //fread();
    //fwrite();

    int n;

    scanf("%d",&n);

    for(int i = 0; i < n; ++i)
    {
        scanf("%d",&mn[i].level);
        mn[i].id = i;
    }

    sort(mn,mn+n);

    for(int i = 0; i < n; ++i)
    {
        num[mn[i].id] = i;
        val[i] = mn[i].level;
    }

    tp = 0;
    int mn = INF;

    memset(vis,0,sizeof(vis));
    for(int i = 0; i < n; ++i)
    {
        if(vis[i]) continue;
        solve(tp,i);
        mn = min(mn,gp[tp++].mn);
    }

    int ans = 0;
    for(int i = 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;
}
时间: 2024-12-16 00:18:25

【POJ 3270】Cow Sorting(置换群排序)的相关文章

poj 3270 Cow Sorting 置换群 简单题

假设初始状态为 a:2 3 1 5 4 6 则目标状态为 b:1 2 3 4 5 6且下标为初始状态中的3 1 2 4 5 6(a[3],a[1]...) 将置换群写成循环的形式 (2,3,1),(5,4),6就不用移动了. 移动方式2种 1:选循环内最小的数和其他len-1个数交换 2:选整个序列最小的数和循环内最小的数交换,转到1,再换回来. #include<cstdio> #include<queue> #include<algorithm> #include&

poj 3270 Cow Sorting(初涉置换群)

http://poj.org/problem?id=3270 大致题意:给出n个整数,要将它们转化成递增序列,每交换其中两个数的代价是这两个数之和.问排序成功后的最小代价. 该题考察的是置换群知识.在黑书p247上有详细的讲解.总结下置换群,方便复习. 群:给定一个集合G={a,b,c...}和集合G上的二元运算 ·,如果满足封闭性,结合律,存在单位元和逆元,则成集合G在运算'·'之下是一个群. 置换:n个元素1,2,....,n之间的置换可表示为  1     2     3     ...

POJ 3270 Cow Sorting(置换群)

题目链接 题意 : N头牛,每个牛的坏脾气都有一个值,每个值都不相同,把这个值按照从小到大排序,如果两个值交换,那么会花掉这两个值之和的时间,让你花最少的时间将每个值从小到大排好序,求最小的总时间. 思路 : 这个在黑书上有写,就是置换群,248页有写.写的挺详细的.每个状态都可以分为若干个循环的乘积.对于任意循环 i ,设其长度为ki,则至少需要交换ki-1次,即每次让一个元素到达目标位置,而当第ki-1个元素到达目标以后显然第ki个也已经到达目标.第一个方法是让循环中最小的元素t参加所有的交

poj 3270 Cow Sorting

Cow Sorting 题意:有N头牛,每头牛都有不同的暴躁值ai,现在要将所有的牛按照暴躁值从小到大排序,每次交换两个元素(任意交换)时,花费就是两头牛的暴躁值之和:问排序的最小花费为多少? 数据:(1 ≤ N ≤ 10,000) (1 <= ai <= 100,000); 思路:很明显的贪心:(下面讲的循环是置换群里的循环) 策略:我们从没在最终位置且值最小的牛看起,如果每次都是将当前考虑的牛直接与它最终的位置的牛交换,那么这样递推下去,将形成的就是一个循环(一定有牛的最终位置为考虑的起始

POJ 3270 Cow Sorting(置换环)

Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6393   Accepted: 2476 Description Farmer John's N (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...

[ACM] POJ 3270 Cow Sorting (置换,贪心)

Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5946   Accepted: 2263 Description Farmer John's N (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...

POJ 3270. Cow Sorting &amp; 51nod 1125 交换机器的最小代价

根据题意,需要交换的部分会形成若干个不相交的环,独立处理每个环. 每个环可以用环内的最小值去和其它元素交换,或者用全局最小值和环上最小值交换,做一遍再交换回去. #include <cstdio> #include <cstring> const int MOD = 9973; int m, n, k; void M(int &a) { if (a >= MOD) a -= MOD; if (a < 0) a += MOD; } struct Mat { int

[BZOJ1697][Usaco2007 Feb]Cow Sorting牛排序

1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 712  Solved: 416 [Submit][Status][Discuss] Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都是一个在1到100,000之间的整数并且没有两头牛的脾气值相同.

Cow Sorting(置换群)

Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6664   Accepted: 2602 Description Farmer John's N (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...

bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】

至今都不知道置换群是个什么东西--题解说什么就是什么.jpg 以下来自hzwer:http://hzwer.com/3905.html #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int N=10005; int n,w[N],a[N],v[N]; struct qwe { int x,id; }b[N]; bool cmp(const qwe &a