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).
 
题意:有一群牛, 没头牛都有一个独一无二的暴躁度, 农夫想把暴脾气的牛排在后面, 他会将两头牛交换位置, 代价是两头牛的暴躁度之和;将所有的牛排好序, 最小的代价是多少?
 
思路:因为交换自然想到置换群,我们可以用循环里最小的数做媒介将较大的数换到相应的位置, 易得排好一个循环的代价为 :ans1 = sum+(cnt-2)*min;(cnt为循环长度,sum为循环的和,min为该循环的最小值)但这样做并一定不是最小的, 比如序列 : 17896; 两个循环(1)(7896), 用上面的公式得到 ans1 = 42, 但如果我们把1和6交换,用1作为媒介,代价为:
ans2 = sum+(cnt+1)*MIN+min(MIN为全局最小数), 通过比较ans1, ans2得到最小值;
 
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio> #define maxn 100010 using namespace std; int hay[maxn], shay[maxn], vis[maxn], pos[maxn];
int mi = , ans = , MI = ;
int n;
int main()
{
memset(vis, , sizeof(vis));
ios::sync_with_stdio(false); cin >> n;
for(int i = ; i <= n; i++)
{
cin >> hay[i];
shay[i] = hay[i];
mi = min(mi, hay[i]);
}
sort(hay+, hay+n+);
for(int i = ; i <= n ; i++)
{
pos[hay[i]] = i;
}
for(int i = ;i <= n; i++)
{
if(vis[i] == )
{
int tmp = i;
int cnt = ;
int sum = ;
MI = shay[tmp];
while(vis[tmp] == )
{
vis[tmp] = ;
cnt++;
sum += shay[tmp];
tmp = pos[shay[tmp]];
MI = min(MI, shay[tmp]);
}
ans += (sum + min((cnt-)*MI, MI+(cnt+)*mi));
}
}
cout << ans << endl;
return ;
}

这里没有代码。。。

参考链接 :http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669013.html

 
 
 

C-Cow Sorting (置换群, 数学)的更多相关文章

  1. Cow Sorting(置换群)

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

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

  3. POJ 3270 Cow Sorting(置换群)

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

  4. HDU Cow Sorting (树状数组)

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

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

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

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

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

  7. Cow Sorting hdu 2838

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

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

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

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

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

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

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

随机推荐

  1. python学习目录(转载)

    python基础篇 python 基础知识    python 初始python    python 字符编码    python 类型及变量    python 字符串详解 python 列表详解 ...

  2. Bootstrap3隐藏滑动侧边栏菜单代码特效

    链接:https://pan.baidu.com/s/1syV3ZFg-RqfCv0HS5K0vug 提取码:yjex

  3. DevOps理论与实践总结

    DevOps指导理论与实践 [第01篇]:郭宏泽:全开源架构下的DevOps实践(转) SonarQube应用指南 [第一篇]:SonarQube Scanner报svn: E170001错误 che ...

  4. luogu4389 付公主的背包

    题目链接:洛谷 题目大意:现在有$n$个物品,每种物品体积为$v_i$,对任意$s\in [1,m]$,求背包恰好装$s$体积的方案数(完全背包问题). 数据范围:$n,m\leq 10^5$ 这道题 ...

  5. es6 学习四 数组的学习

    1. Array.from() 语法: Array.from(arrayLike[, mapFn[, thisArg]]) arrayLike 类数组对象 mapFn 如果指定了该参数,新数组中的每个 ...

  6. 使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...

  7. mysqldump备份数据出错

    收到nagios报警,提示mysql备份失败,线上使用的是逻辑备份,也就是使用mysqldump,由于数据比较小,也就没在乎速度神马的问题.好吧,那就查查是什么原因导致备份失败,由于备份是写成脚本定时 ...

  8. SQL存储过程分页

    CREATE PROC ZDY_FY(@Pages INT, @pageRow INT) --@Pages第几页 @pageRow每页显示几行 AS BEGIN DECLARE @starNum IN ...

  9. 百度接口test

    https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Mk2Orf5pqEOXvYR ...

  10. ABPIAbpSession

    AbpSession定义了几个关键属性: UserId:当前用户的Id或空(如果没有当前用户),如果调用需要授权的代码,它就不能为空. TenantId:当前租户的Id或空(如果没有当前租户:尚未登录 ...