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;
    }
  • 相关阅读:
    bzoj 3027 [Ceoi2004]Sweet——生成函数
    bzoj 3028 食物——生成函数
    JZOJ 5461 购物 —— 贪心
    JZOJ 1003 [ 东莞市选 2007 ] 拦截导弹 —— 递推
    JZOJ 1667 ( bzoj 1801 ) [ AHOI 2009 ] 中国象棋 —— DP
    洛谷 P2055 [ ZJOI 2009 ] 假期的宿舍 —— 二分图匹配
    洛谷 P3398 仓鼠找sugar —— 树链剖分
    洛谷 P1083 [ NOIP 2012 ] 借教室 —— 线段树 / 二分差分数组
    bzoj 3895 取石子 —— 博弈论
    洛谷 P1312 [ NOIP 2011 ] Mayan游戏 —— 搜索+模拟
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/4845847.html
Copyright © 2011-2022 走看看