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 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 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).
Source
#include <stdio.h>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 110000
using namespace std; int n;
int a[MAXN];
int pos[MAXN];
int use[MAXN]; int main(int argc, char *argv[])
{
int maxValue;
int minValue;
while( scanf("%d" ,&n)!=EOF ){
maxValue=;
minValue=inf;
memset(pos , ,sizeof(pos));
memset(use , ,sizeof(use));
for(int i=; i<=n; i++){
scanf("%d",&a[i]);
pos[a[i]]++;
if(a[i]>maxValue){
maxValue=a[i];
}
if(a[i]<minValue){
minValue=a[i];
}
}
for(int i=; i<=maxValue; i++){
pos[i]=pos[i-]+pos[i];
}
int sum=;
for(int i=; i<=n; i++){
//找循环节
if(!use[i]){
int j=i;
int len=;
int t=a[j];
int ts=;
while(!use[j]){
//找到置换群里最小的数
if(a[j]<t){
t=a[j];
}
//求置换群的和
ts+=a[j];
use[j]=;
j=pos[a[j]];
len++;
}
if(<len){
sum+=ts;
}
if(<len){
int t1=(len-)*t;
int t2=t+(len+)*minValue;
if(t1<t2){
sum+=t1;
}
else{
sum+=t2;
}
}
}
}
printf("%d\n",sum);
}
return ;
}
TOJ 1690 Cow Sorting (置换群)的更多相关文章
- Cow Sorting(置换群)
Cow Sorting Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6664 Accepted: 2602 Descr ...
- 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)头牛排队以便于行动.因为脾气大 ...
随机推荐
- Sharepoint2013搜索学习笔记之自定义结果源(七)
搜索中心新建好之后在搜索结果页上会默认有所有内容,人员,对话,视频这四个结果分类,每个分类会返回指定范围的搜索结果,这里我再添加了部门日志结果分类,搜索这个分类只会返回部门日志内容类型的搜索结果,要实 ...
- 合并datagridview 条件合并行数据
public void HeBing() { int rowsCount; int CellCount; rowsCount = FG1.Rows.Count; CellCount = FG1.Col ...
- C#多线程编程实战1.2暂停线程(休眠)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- rsync服务搭建--2018.5.8 [优化后最终版]
2018年5月8日 22:09:38 第一步配置基础环境(按照自己的规划配置并非每人的环境都一致) 第一台服务器(RSYNC服务器): rsync外网地址:10.0.0.41 rsync内网地址:1 ...
- Markdown使用样例
# 欢迎使用 Cmd - 在线 Markdown 编辑阅读器 ------ 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,**Cmd Markdown** 是 ...
- 【TJOI2017】异或和
题目描述 在加里敦中学的小明最近爱上了数学竞赛,很多数学竞赛的题目都是与序列的连续和相关的.所以对于一个序列,求出它们所有的连续和来说,小明觉得十分的简单.但今天小明遇到了一个序列和的难题,这个题目不 ...
- 【转】asp.net中设置弹出谈话框的几种方式详解
源地址:https://blog.csdn.net/zou15093087438/article/details/79637114 注:可用winform的MessageBox的方法,只需引用Syst ...
- 【bzoj4987】Tree 树形dp
Description 从前有棵树. 找出K个点A1,A2,-,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. Input 第一行两个正整数n,k,表示数的顶点数和需要 ...
- loj #2051. 「HNOI2016」序列
#2051. 「HNOI2016」序列 题目描述 给定长度为 n nn 的序列:a1,a2,⋯,an a_1, a_2, \cdots , a_na1,a2,⋯,an,记为 a[1: ...
- linux防火墙(二)—— iptables语法之选项和控制类型
一.语法: iptables [-t 表名] 选项 [链名] [匹配条件] [-j 控制类型] 未指定表名时,默认用filter表:链名,控制类型要大写:除非设置默认策略,否则必须指定匹配条件:不指定 ...