C-Cow Sorting (置换群, 数学)
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
Lines 2..
N+1: Each line contains a single integer: line
i+1 describes the grumpiness of cow
i.
Output
Sample Input
3
2
3
1
Sample Output
7
Hint
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<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 (置换群, 数学)的更多相关文章
- Cow Sorting(置换群)
Cow Sorting Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6664 Accepted: 2602 Descr ...
- 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 ...
- POJ 3270 Cow Sorting(置换群)
题目链接 很早之前就看过这题,思路题把,确实挺难想的,黑书248页有讲解. #include <cstdio> #include <cstring> #include < ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序
1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 387 Solved: 215[S ...
- hdu 2838 Cow Sorting(树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Cow Sorting hdu 2838
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心
BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行 ...
- 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting
题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...
- 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序
1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...
随机推荐
- 使用axios加入进度条
思路:(安慰剂按钮)首先当触发按钮时,设置拦截器,启动进度条从0开始到100满(html进度条用数值value来控制,默认为0),设置进度条的配置函数然后在后端返回函数中启动停止精度条的函数,为了保持 ...
- 洛谷P3354 Riv河流 [IOI2005] 树型dp
正解:树型dp 解题报告: 传送门! 简要题意:有棵树,每个节点有个权值w,要求选k个节点,最大化∑dis*w,其中如果某个节点到根的路径上选了别的节点,dis指的是到达那个节点的距离 首先这个一看就 ...
- 浏览器的兼容性(CSS浏览器兼容性、CSS hack)
一.关于CSS hack(尽量不用或者少用,减少页面复杂度) 1.条件注释法:(我的测试是IE9及其以下才有效) 这种方式是IE浏览器专有的Hack方式,微软官方推荐使用的hack方式.举例如下 只在 ...
- 程序------>算法
一.算法: 1.算法的定义: 算法是解决特定问题求解步骤的描述,在计算机中表现为指令的有序序列,并且每条指令表示一个或多个操作.即算法是描述解决问题的方法.(对于给定的问题是可以有多种 算法进行解 ...
- 10.3-uC/OS-III内部任务管理(TCB)
任务控制块 TCB 1.任务控制块是被uC/OS-III用于维护任务的一个结构体.每个任务都必须有自己的 TCB. uC/OS-III 在 RAM 中分配 TCB.当调用uC/OS-III提供的与任务 ...
- JavaScript学习(四)
- what's the 二叉树
what's the 树 在了解二叉树之前,首先我们得有树的概念. 树是一种数据结构又可称为树状图,如文档的目录.HTML的文档树都是树结构,它是由n(n>=1)个有限节点组成一个具有层次关系的 ...
- Python3学习之路~2.7 文件操作
对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 Somehow, it seems the love I knew was always the ...
- java poi生成excel(个人例子js-jsp-java)
js代码: function exportRepQudl(){ if(confirm("导出输出页面内容?")){ var id = $("input[name='id' ...
- ABP中针对sql2008的数据库配置