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)头牛排队以便于行动.因为脾气大 ...
随机推荐
- 抽屉之Tornado实战(1)--分析与架构
抽屉之Tornado实战(1)--分析与架构 项目模拟地址:http://dig.chouti.com/ 知识点应用: AJAX 用于偷偷发请求 原生ajax jQuery ajax($.aj ...
- python摸爬滚打之day02----while循环,运算符,格式化输出
1.while循环 1.1 结构:while +条件判断: while 循环体 else: 条件不成立时语句块 while...else...是一个循环整体,当循环条件成立时执行while循环体内容 ...
- Orchard Core 增加了一个API模块,要怎么调用
如下,我在Orchard Core框架中添加了一个API的模块,并且定义了对应的权限才可以调用,那么我们现在考虑的就是要怎么去调用它. 首先,我们用Fiddler查看下我们正常的登录的http报文,直 ...
- MongoDB pymongo模块 查询
查询 mongo_db 类似于 服务器命令行的db 我们可以db.user.find() 查询 find() 需要加上列表 import pymongo mongo_client = pymongo. ...
- python基础教程 变量/输入输出/if判断
python的运用越来越多.大数据经常被人谈及,数据从何而来?通过各个平台.app.网站数据的收集,分析,过滤,生成报告,这些都可以用python来处理,并且有很多成熟的库可以直接用了.那还不赶紧深入 ...
- JDK源代码学习系列04----ArrayList
JDK源代码学习系列04----ArrayList 1 ...
- Windows系统重装工具 WinToHDD Enterprise v2.8 企业破解版
WinToHDD 是一款实用的 Windows 系统硬盘安装工具,有点类似于NT6 HDD Installer,它能不依靠光驱和U盘,让你直接通过本机硬盘来新装.重装和克隆 Windows 系统,支持 ...
- CentOS 7 :Failed to start IPv4 firewall with iptables.
用iptables开启防火墙报错: Failed to start IPv4 firewall with iptables. 转载于:https://blog.csdn.net/ls1645/art ...
- 2017-2018-2 20165236 实验四《Android开发基础》实验报告
2017-2018-2 20165236 实验四<Android开发基础>实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:郭金涛 ...
- ABAP 创建function model 返回参数为内表类型
1:通过T-CODE se11 创建一个structure ZSTRU2. 2: 创建一个table type, 表名 ZTAB1. 3: 表的row type 选择 ZSTRU2 4: ...