POJ-3270 Cow Sorting

本文介绍了一种通过计数排序来解决CowSorting问题的方法,旨在寻找将一组具有不同‘暴躁值’的奶牛按升序排列所需的最小时间。文章详细解析了如何利用群置换的概念并采用特定算法实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cow Sorting
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6847 Accepted: 2684
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).

给出一列数,求将这列数排成升序的最小花费,这里花费定义为交换两个数的和。
例如给出一排数8 4 5 3 2 7,那么我们最终的状态为2 3 4 5 7 8,这里的轮换有(8 2 7)(4 5 3),这里8应该在第六个位置,
而第6个位置是7,7应该在5这个位置,而第5个位置为2,2应该在1这个位置,这样就到了8所在的位置,我们称这是一个轮换。
首先需要明确的一点是对于每个群,假设有k个数,那么我们需要交换k-1次得到升序。
对于每一个群,我们有两种换发:
1.群里换,拿群里最小的数t与其他每个数交换,共k-1次,花费为:sum+(k-2)*t.
2.将这个数列最小的数m,拉入这个群,与该群最小的数t交换,然后用这个最小的数与其他数交换k-1次,然后再将m与t换回来,这样
花费为:sum+t+(k+1)m
那么最小花费我们取两者中最小的,即sum+min{(k-2)*t,t+(k+1)*m}.
对于这个题关键就是怎么确定每个群,我们利用计数排序,得到每个数应该在的位置,然后循环判断这个位置是否已经被访问了。时间
复杂度为O(n).

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <string.h>
using namespace std;
const int N=100001;
const int INF=100000000;
int m[N],cnt[N+1];
bool used[N+1];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int i,j,tmp1,tmp2,len,t,maxValue,minValue,sum=0,curSum;
        memset(cnt,0,sizeof(cnt));
        memset(used,0,sizeof(used));
        minValue=INF;
        maxValue=0;
        for(i=1;i<=n;++i)             //计数,得到每个数的位置,我们按照上面的那排数
        {
            scanf("%d",m+i);
            cnt[m[i]]++;
            if(m[i]<minValue)
                minValue=m[i];
            if(m[i]>maxValue)
                maxValue=m[i];
        }
        for(i=1;i<=maxValue;++i)
            cnt[i]=cnt[i]+cnt[i-1];
        for(i=1;i<=n;++i)
        {
            if(!used[i]) 
            {
                j=i;
                t=m[i];
                len=0;
                curSum=0;
                while(!used[j])        //每一组的循环,也就是群置换的位置 
                {
                    ++len;
                    if(m[j]<t)
                        t=m[j];
                    curSum+=m[j];
                    used[j]=true;
                    j=cnt[m[j]];
                }
                if(1<len)
                    sum+=curSum;
                if(2<len)
                {
                    tmp1=(len-2)*t;
                    tmp2=(len+1)*minValue+t;
                    if(tmp1>tmp2)
                        tmp1=tmp2;
                    sum+=tmp1;
                }
            }
        }
        printf("%d\n",sum); 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值