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
题意: 有N头奶牛,现在你要将这全部奶牛按照升序排序,但是每次两头奶牛交换是有权值的X+Y.
解题思路:
代码:
#include
<cstdio>
#include
<iostream>
#include
<cstring>
#include
<cmath>
#include
<algorithm>
using namespace std;
#define MAX 100005
int n;
int a[MAX],b[MAX];
int next[MAX];
bool vis[MAX];
inline int minsize(int a,int b)
{
}
int polya()
{
}
int main()
{
//
}
本文介绍了一种关于奶牛排序的问题,通过优化算法减少排序过程中因交换不同“暴躁值”的奶牛产生的总成本。利用置换群的概念,将排序过程抽象为一系列循环,并通过两种方法确定最小交换成本。
737

被折叠的 条评论
为什么被折叠?



