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;
    }
  • 相关阅读:
    PyTorch之前向传播函数自动调用forward
    pytorch 调用forward 的具体流程
    Xcode5下使用纯代码构建简单的HelloWorld程序
    浅谈iOS 5的StoryBoard
    iOS单例
    instancetype 对比 id 的好处
    JSP的7个动作指令
    IOS UIView子类UIScrollView
    XCODE4.6从零开始添加视图
    NSSet类型 以及与NSArray区别
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/4845847.html
Copyright © 2011-2022 走看看