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)头牛排队以便于行动.因为脾气大 ...
随机推荐
- Android之AlterDialog介绍
效果: 1.简单对话框 AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this); builder . ...
- [python]模块及包
一 .module 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc&quo ...
- Vue 编程式导航,路由history模式
import Vue from 'vue' import App from './App.vue' import Home from './components/Home.vue' import Ne ...
- Setter
这个还是比较好理解的. 设置器. 用法还是比较简单的. 语法特征: 设置属性[Property] 填充值[Value] 注意这个是封闭单行闭合标签,可以换行,但只允许在同一个标签闭合. 事例用法: & ...
- redis可视化辅助工具
安装链接: http://docs.redisdesktop.com/en/latest/quick-start/ 图标
- 定时器timer类
timer类 Timer(定时器)是Thread的派生类,用于在指定时间后调用一个方法. 构造方法: Timer(interval, function, args=[], kwargs={}) in ...
- 美团Java实习面试经历(拿到Offer)
美团我是在拉勾网上投的简历,之前也投过一次,简历都没通过删选,后来让学姐帮我改了一下简历,重新投另一个部门,获得了面试机会.10月23日中午HR打电话过来预约了下午4点半面试,说会在线写代码,让我 ...
- clustalX2使用以及相关的问题
Clustalx的操作 第一步:输入序列文件. 第二步:设定比对的一些参数. 参数设定窗口. 第三步:开始序列比对. 第四步:比对完成,选择保存结果文件的格式 相关问题 CLUSTALX-是CLUST ...
- C++_类和动态内存分配1—动态内存和类
静态类成员 num_strings成员声明为静态存储类.静态类成员有一个特点:无论创建了多少对象,程序都只创建一个静态类变量副本.也就是说,类的所有对象共享一个静态成员.num_strings成员可以 ...
- 创建一个流(Stream)可以让Bitmap或Image保存到流里面
创建一个流(Stream)可以让Bitmap或Image保存到流里面 http://blog.csdn.net/angxiao/article/details/7481465 写文件流 ...