Cow Sorting(置换)
http://poj.org/problem?id=3270
// File Name: poj3270.cpp
// Author: bo_jwolf
// Created Time: 2013年10月09日 星期三 17:19:00 #include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime> using namespace std;
const int maxn = 1000005;
struct node{
int cnt, val;
}a[ maxn ]; int tot, t[ maxn ], m[ maxn ], sum, Min = 1, n;
bool flag[ maxn ];
void dfs( int u ){
for( int i = 0; i < n; ++i ){
if( !flag[ i ] && t[ i ] == u ){
a[ tot ].cnt++;
flag[ i ] = true;
a[ tot ].val = min( a[ tot ].val, t[ i ] );
dfs( m[ i ] );
}
}
} int main(){
while( scanf( "%d", &n ) != EOF ){
sum = 0, Min = 1 << 30;
for( int i = 0; i < n; ++i ){
scanf( "%d", &m[ i ] );
t[ i ] = m[ i ];
sum += m[ i ];
Min = min( Min, m[ i ] );
}
sort( m, m + n );
tot = 0;
for( int i = 0; i < n; ++i ){
if( flag[ i ] )
continue;
a[ tot ].val = t[ i ];
a[ tot ].cnt = 1;
flag[ i ] = true;
dfs( m[ i ] );
tot++;
}
for( int i = 0; i < tot; ++i ){
sum += min( a[ i ].val * ( a[ i ].cnt - 2 ), a[ i ].val + Min * ( a[ i ].cnt + 1 ) );
}
printf( "%d\n", sum );
}
return 0;
}
Cow Sorting(置换)的更多相关文章
- poj 3270 Cow Sorting (置换入门)
题意:给你一个无序数列,让你两两交换将其排成一个非递减的序列,每次交换的花费交换的两个数之和,问你最小的花费 思路:首先了解一下什么是置换,置换即定义S = {1,...,n}到其自身的一个双射函数f ...
- Cow Sorting(置换群)
Cow Sorting Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6664 Accepted: 2602 Descr ...
- 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序
1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...
- 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]的下标,然后将序列分成三部分:前缀部分( ...
随机推荐
- 接口Interface
接口的定义 声明接口的方式与声明类的方式相似,但使用的关键字是interface,而不是class interface IMyInterface { // 接口成员 } 访问修饰符关键字public ...
- 快速理解webStroage
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- JavaScript原型链demo
function Person(name){ this.name = name; } Person.prototype = { say: function(){ alert('hi'); }, say ...
- yii2源码学习笔记(十九)
view剩余代码 /** * @return string|boolean the view file currently being rendered. False if no view file ...
- MySQL数据库添加一个字段
MySQL数据库添加一个字段 1.添加一个字段 alter table tableName add 列名 数据类型; 2.添加一个字段设置默认值 alter table tableName add ...
- javascript进阶——面向对象特性
面向对象的javascript是这门语言被设计出来时就考虑的问题,熟悉OOP编程的概念后,学习不同的语言都会发现不同语言的实现是不同的,javascript的面向对象特性与其他具有面向对象特性的语言的 ...
- Poco之ftp获取文件列表以及下载文件
#include <iostream>#include <string>#include <vector>#include <algorithm>#in ...
- pyqt中使用matplotlib绘制动态曲线
一.项目背景: 看了matplotlib for python developers这本书,基本掌握了在pyqt中显示曲线的做法,于是自己写一个. 二.需求描述: 1)X轴显示时间点,显示长度为1分钟 ...
- Centos开启FTP及用户配置
vsftpd作为FTP服务器,在Linux系统中是非常常用的.下面我们介绍如何在centos系统上安装vsftp. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...
- 使用VisualStudio2010创建C#应用程序
打开VisualStudio2010,选择“文件”——“新建”——“项目”菜单命令.调出“新建项目”对话框.