zoukankan      html  css  js  c++  java
  • poj 3270(置换 循环)

    经典的题目,主要还是考思维,之前在想的时候只想到了在一个循环中,每次都用最小的来交换,结果忽略了一种情况,还可以选所有数中最小的来交换一个循环。

    Cow Sorting
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6511   Accepted: 2532

    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).
     
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    using namespace std;
    #define N 100100
    
    struct node
    {
        int id;
        int num;
    }g[N];
    
    int mylink[N];
    int f[N],tf[N];
    int flag[N];
    long long ans;
    int mi;
    
    int cmp(node t,node t1)
    {
        return t.num<t1.num;
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            g[i].num=tmp;
            g[i].id=i;
        }
        sort(g+1,g+1+n,cmp);
        mi=g[1].num;
        ans=0;
        memset(flag,0,sizeof(flag));
        for(int i=1;i<=n;i++)
        {
            mylink[i]=g[i].num;
            f[ g[i].id ] = i;
            tf[ i ] = g[i].id;
        }
        for(int i=1;i<=n;i++)
        {
            if(flag[i]==1) continue;
            flag[i]=1;
            int s=i;
            int tmi=mylink[i];
            long long save=mylink[i];
            int cnt=0;
            while( f[s]!=i )
            {
                cnt++;
                s=f[s];
                flag[s]=1;
                save += mylink[s];
                tmi=min(mylink[s],tmi);
            }
            ans += min(cnt*tmi+save-tmi, save-tmi+2*tmi+(cnt+2)*mi );
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    Spring+MyBatis纯注解零XML整合(4)
    spring boot-mybatis全注解化(3)
    SpringBoot使用Mybatis注解进行一对多和多对多查询(2)
    Spring Boot中使用MyBatis注解配置详解(1)
    mysql 俩个时间相减后取分钟
    mysql加减时间-函数-时间加减
    十种常见排序算法(转)
    开源VS商用,IBM区块链从Hyperledger到商用平台之道 | 对话IBM高级架构师【 笔记】(转)
    java中正则表达式基本用法(转)
    《数学之美》——维特比和他维特比算法
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/4845847.html
Copyright © 2011-2022 走看看