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)头牛排队以便于行动.因为脾气大 ...
随机推荐
- LeetCode 709 To Lower Case 解题报告
题目要求 Implement function ToLowerCase() that has a string parameter str, and returns the same string i ...
- 1、python同级目录及子目录模块引入
2个模块在同一个包内时(即引入和被引入的2个py文件在同一个目录下),直接引入模块名 1.引入与被引入模块或包在同一目录下时,直接引入模块名或者包名import modulename.py或者impo ...
- Java+Selenium 如何参数化验证Table表格数据
场景: 当我们编写脚本时候,需要验证某个表格某一列数据,或者多个列数据. 如果每验证一个就写一个方法,实在是太费事, 因此我们需要有参数化的思想,把某列数据看成固定的元素,然后去验证即可. 1. 示例 ...
- java 网络编程(五)Socket多线程上传文件
客户端: package cn.sasa.socketUploadFileDemo; import java.io.FileInputStream; import java.io.IOExceptio ...
- 写一致性原理以及quorum机制
(1)consistency,one(primary shard),all(all shard),quorum(default)我们在发送任何一个增删改操作的时候,比如 PUT /index/type ...
- C#-1-1-.net
1..Net (1).Net平台 (2). FrameWork框架 2..Net应用 (1).桌面应用程序(Win form(.Net开发的桌面应用程序叫做Win form应用程序)). (2).In ...
- ubuntu上编译linux内核
Linux 编译:1,首先解压缩内核.2,make ARCH=arm CROSS_COMPILE=arm-xilinx-linux-gnueabi- digilent_z ...
- insert select带来的问题
当使用 insert...select...进行记录的插入时,如果select的表是innodb类型的,不论insert的表是什么类型的表,都会对select的表的纪录进行锁定.对于那些从oracle ...
- what's the 套期保值
出自 MBA智库百科(https://wiki.mbalib.com/) 什么是套期保值 套期保值是指把期货市场当作转移价格风险的场所,利用期货合约作为将来在现货市场上买卖商品的临时替代物,对其现在买 ...
- 怎样打开U盘最安全
为了避免电脑使用U盘时,通过双击,或者右击盘符时,导致把病毒感染至整个电脑,因此使用下面的方法,可使U盘病毒不被激活传播. 在取消了U盘自动运行的情况下(在组策略中一定要关闭自动运行功能,否则只要一插 ...