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)头牛排队以便于行动.因为脾气大 ...
随机推荐
- window.document 对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 数据库索引与b+树
数据库索引详解 索引 当我们在设计数据库的时候,对表的一些属性有时会加上索引,但索引为什么能提高检索速率呢?是不是用了索引就一定可以提高效率呢?不同索引之间有什么区别呢?搞懂这些问题是灵活运用索引的必 ...
- 死磕Java之聊聊ThreadLocal源码(基于JDK1.8)
记得在一次面试中被问到ThreadLocal,答得马马虎虎,所以打算研究一下ThreadLocal的源码 面试官 : 用过ThreadLocal吗? 楼主答 : 用过,当时使用ThreadLocal的 ...
- k8s学习笔记(一)
你将学到什么 如何部署k8s集群 网络拓扑 主机名 网络地址 角色 study 92.0.0.50(内网) 192.168.203.250/19(外网) Master fnode 92.0.0.16( ...
- ubuntu14.04,安装Gnome 15.10 (桌面)
Linux:ubuntu14.04 Gnome:15.10 更新最新版Gnome的一个好处:更新了ubuntu的软件源,我们可以使用ubuntu的软件中心获取更多需要的软件!! ubuntu默认的桌面 ...
- 20165219 2017-2018-2《Java程序设计》课程总结
20165219 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 20165219 我期望的师生关系 20165219学习基础与C语言基础调查 20165219 ...
- c++多线程基础2(命名空间 this_thread)
整理自:zh.cppreference.com/w/cpp/thread std::this_thread::yield: 定义于头文件 <thread> 函数原型:void yield( ...
- Java网络编程客户端和服务器通信
在java网络编程中,客户端和服务器的通信例子: 先来服务器监听的代码 package com.server; import java.io.IOException; import java.io.O ...
- sqlalchemy orm数据类型验证方法比较
1.在定义ORM模型时校验 sqlalchemy提供validates函数支持对字段的校验 from sqlalchemy.orm import validates class EmailAddres ...
- maven No compiler is provided environment
eclipse maven操作正常出现的No compiler is provided in this environment. Perhaps you are running on a JRE ra ...