Cow Sorting
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6664   Accepted: 2602

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).
题解:要把一个数列通过交换变成一个递增的序列,交换的权值是两个元素的和;
通过这n个数组成的数列 我们可以得到若干个置换子群。
            比如 给定序列{
                                            3  1  7  10  6
                                            1  3  6   7  10
                                        }上面序列要变成下面序列。 我们可以得到2个置换子群(1 -> 3)( 6 -> 10 -> 7)。
接下来有两种方法变换序列:
 
(1)  在子群里面置换 -> 用最小的元素sonMin和其它 t - 1 元素交换 t - 1 次,就有花费1:sum + (t - 2) * sonMin;  (2)  借助于外界元素置换 -> 先让数列中最小元素Minn和群中最小元素sonMin交换,再用Minn与其它 t - 1个元素交换 t - 1 次,然后把Minn和sonMin交换回来
       有花费2: sum + sonMin + Minn * (t + 1)。 提醒: sum为当前群的数值之和。因此可以先统计所有元素之和,然后每次加上min(花费1, 花费2)即可;
 
对于每个置换群的求法,可以用结构体存储原数值以及原数值对应的位置,然后按数值升序排列就求出变换的位置。
 
序列{                                                                                   {
              数值 3  1  7  10  6         ------>                                    数值 1  3  6  7 10
              位置 1  2  3   4   5         ------>                                    位置   2  1  5  3  4
          }             
代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=;
int vis[MAXN];
struct Node{
int pos,val;
bool operator < (const Node &b)const{
if(val!=b.val)return val<b.val;
else return pos<b.val;
}
};
Node dt[MAXN];
int main(){
int N;
while(~SI(N)){
LL sum=;
int min_all=INF,min_area;
for(int i=;i<=N;i++){
SI(dt[i].val);
dt[i].pos=i;
sum+=dt[i].val;
min_all=min(min_all,dt[i].val);
}
sort(dt+,dt+N+);
mem(vis,);
int num;
for(int i=;i<=N;i++){
if(!vis[i]){
num=;
min_area=dt[i].val;
int j=i;
while(!vis[j]){
vis[j]=;
num++;
//printf("%d ",dt[j].val);
min_area=min(min_area,dt[j].val);
j=dt[j].pos;
}//puts("");
sum+=min((num-)*min_area,*(min_all+min_area)+(num-)*min_all-min_area);
}
}
printf("%lld\n",sum);
}
return ;
}

Cow Sorting(置换群)的更多相关文章

  1. 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 ...

  2. POJ 3270 Cow Sorting(置换群)

    题目链接 很早之前就看过这题,思路题把,确实挺难想的,黑书248页有讲解. #include <cstdio> #include <cstring> #include < ...

  3. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  4. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

  5. hdu 2838 Cow Sorting(树状数组)

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. Cow Sorting hdu 2838

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心

    BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行 ...

  8. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

  9. 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...

随机推荐

  1. COB對PCB設計的要求

    由於COB沒有IC封裝的leadframe(導線架),而是用PCB來取代,所以PCB的焊墊設計就便得非常的重要,而且Fihish只能使用電鍍金或是ENIG(化鎳浸金),否則金線或是鋁線,甚至是最新的銅 ...

  2. 电子科大POJ "a^b"

    a ^ b Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) C-sources: ...

  3. bat命令学习笔记

    1.一般在开始声明 setlocal enabledelayedexpansion 设置本地为延迟扩展.其实也就是:延迟变量,全称延迟环境变量扩展,使得批处理能够感知到变量的动态变化,在运行过程中给变 ...

  4. 网易云课堂_C++程序设计入门(上)_第6单元:丹枫虽老犹多态–继承与多态_第6单元作业【2】- 在线编程(难度:中)

    第6单元作业[2]- 在线编程(难度:中) 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提交答案,系统 ...

  5. Introduction to neural network —— 该“神经网络” 下拉“祭坛”

    Introduction to neural network 不能自欺欺人. 实干兴邦,空谈误国. -------------------------------------------------- ...

  6. Oracle 存储过程之通用分页查询

    在数据库中书写通用分页存储过程,有利于代码的维护以及执行效率的提升 create or replace procedure Sp_QueryDatePage ( tableName in varcha ...

  7. CSS样式做圆角

    我处理圆角的版本是由内置的绝对定位的四个div组成,每个div都有唯一的圆角图片作CSS Sprite操作.我们将会这样做:  是什么方式导致这项技术表现得这么了不起呢(What makes this ...

  8. css层叠机制说明

    css通过建立与文档的关联而实施效果.文档结构重要性不言而喻,对于建立良好的内容索引.提高可维护性.较好的可访问性:另,利于css选择器选择.继承机制. 概要地讲,层叠机制是处理对文档上应用样式时解决 ...

  9. windows下 Python 安装包的配置

    1.下载安装 Pythonpython-2.7.2.msi  http://www.python.org/download/如下载 Python 2.7.2,安装目录为 C:\Python27 2.添 ...

  10. python正则表达式练习篇2

    首先生成正则表达式练习的数据: #! /usr/bin/python from random import randint, choice from string import lowercase f ...