zoukankan      html  css  js  c++  java
  • TOJ 1690 Cow Sorting (置换群)

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

     

     1 #include <stdio.h>
     2 #include <iostream>
     3 #define inf 0x3f3f3f3f
     4 #define MAXN 110000
     5 using namespace std;
     6 
     7 int n;
     8 int a[MAXN];
     9 int pos[MAXN];
    10 int use[MAXN];
    11 
    12 int main(int argc, char *argv[])
    13 {
    14     int maxValue;
    15     int minValue;
    16     while( scanf("%d" ,&n)!=EOF ){
    17         maxValue=0;
    18         minValue=inf;
    19         memset(pos ,0 ,sizeof(pos));
    20         memset(use ,0 ,sizeof(use));
    21         for(int i=1; i<=n; i++){
    22             scanf("%d",&a[i]);
    23             pos[a[i]]++;
    24             if(a[i]>maxValue){
    25                 maxValue=a[i];
    26             }
    27             if(a[i]<minValue){
    28                 minValue=a[i];
    29             }
    30         }
    31         for(int i=1; i<=maxValue; i++){
    32             pos[i]=pos[i-1]+pos[i];
    33         }
    34         int sum=0; 
    35         for(int i=1; i<=n; i++){
    36             //找循环节
    37             if(!use[i]){            
    38                 int j=i;
    39                 int len=0;
    40                 int t=a[j];
    41                 int ts=0;
    42                 while(!use[j]){
    43                     //找到置换群里最小的数 
    44                     if(a[j]<t){
    45                         t=a[j];
    46                     }
    47                     //求置换群的和 
    48                     ts+=a[j];
    49                     use[j]=1;
    50                     j=pos[a[j]];                
    51                     len++;
    52                 }            
    53                 if(1<len){
    54                     sum+=ts;
    55                 }
    56                 if(2<len){
    57                     int t1=(len-2)*t;
    58                     int t2=t+(len+1)*minValue;
    59                     if(t1<t2){
    60                         sum+=t1;                    
    61                     }
    62                     else{
    63                         sum+=t2;                    
    64                     }
    65                 }
    66             }
    67         }
    68         printf("%d
    ",sum);
    69     }        
    70     return 0;
    71 }
  • 相关阅读:
    No module named yum错误的解决办法
    Linux下redis的安装
    Linux crontab命令的使用方法
    mysql时间查看以及定时器相关操作
    python zookeeeper 学习和操作
    使用 python 操作 redis
    Linux命令(2)- mv
    mysql 命令行参数
    框架设计
    MediatR使用
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3605347.html
Copyright © 2011-2022 走看看