zoukankan      html  css  js  c++  java
  • POJ 3270 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)
    给数列每一位编号
    1 2 3 4 5 6 7 8 9
    3 9 8 10 15 11 12 5 7
    将序列排序:
    1 8 9 3 2 4 6 7 5
    我们要将1 2 3 4 5 6 7 8 9 变成 1 8 9 3 2 4 6 7 5
    这就是一个置换
    (1)(2 8 7 6 4 3 9 5)
    只要把长度为k的循环内的最小值x与其他交换k-1次就行
    式子是sum-x+x*(k-1)
    但是还可能是把其他循环最小值换到该循环再交换k-1次
    式子是2*(x+minx)+sum-x+minx*(k-1)
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 struct ZYYS
     8 {
     9   int tmp,cnt,sum;
    10 }s[10001];
    11 struct Node
    12 {
    13   int x,id;
    14 }a[10001];
    15 int n,minx,vis[10001],cnt,tmp,sum,tot,ans1,ans2,ans;
    16 bool cmp(Node a,Node b)
    17 {
    18   return a.x<b.x;
    19 }
    20 void dfs(int x)
    21 {
    22   if (vis[x]) return;
    23   vis[x]=1;
    24   cnt++;
    25   sum+=a[x].x;
    26   tmp=min(tmp,a[x].x);
    27   dfs(a[x].id);
    28 }
    29 int main()
    30 {int i;
    31   cin>>n;
    32   minx=2e9;
    33   for (i=1;i<=n;i++)
    34     {
    35       scanf("%d",&a[i].x);
    36       a[i].id=i;
    37       minx=min(minx,a[i].x);
    38     }
    39   sort(a+1,a+n+1,cmp);
    40   for (i=1;i<=n;i++)
    41     if (vis[i]==0)
    42     {
    43       tot++;
    44       tmp=2e9;
    45       cnt=0;
    46       sum=0;
    47       dfs(i);
    48       s[tot].tmp=tmp;
    49       s[tot].cnt=cnt;
    50       s[tot].sum=sum;
    51     }
    52   for (i=1;i<=tot;i++)
    53     {
    54       ans1=s[i].sum-s[i].tmp+(s[i].cnt-1)*s[i].tmp;
    55       ans2=2*(s[i].tmp+minx)+s[i].sum-s[i].tmp+(s[i].cnt-1)*minx;
    56       ans+=min(ans1,ans2);
    57     }
    58   cout<<ans;
    59 }
  • 相关阅读:
    linux shell基本知识
    chkconfig命令 centos 开机启动命令
    centos 7修改网卡名称
    centos 系统安装基本步骤,面试必考
    nginx 服务脚本编写模板
    nginx 服务脚本编写模板
    Mysql 多实例实施步骤
    shell常用监控脚本
    nginx做负载均衡 tomcat获得客户端真实ip
    vmvare安装系统提示vmci.sys 版本不正确解决方法
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/8421018.html
Copyright © 2011-2022 走看看