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(置换)的更多相关文章

  1. poj 3270 Cow Sorting (置换入门)

    题意:给你一个无序数列,让你两两交换将其排成一个非递减的序列,每次交换的花费交换的两个数之和,问你最小的花费 思路:首先了解一下什么是置换,置换即定义S = {1,...,n}到其自身的一个双射函数f ...

  2. Cow Sorting(置换群)

    Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6664   Accepted: 2602 Descr ...

  3. 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...

  4. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  5. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

  6. hdu 2838 Cow Sorting(树状数组)

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. Cow Sorting hdu 2838

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心

    BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行 ...

  9. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

随机推荐

  1. MySql中常用语句

    1.查询语句: SELECT  查询字段  FROM  表名   WHERE 条件 查询字段可以使用 通配符* 字段名 别名(把长的名字命名一个别名,比较短的) 通配符:SELECT * FROM ' ...

  2. php计算最后一次,第一次字符串出现位置

    strpos($str, n) 首次,n在str第一次出现位置, strrpos($str, n) 最后一次,n在str最后一次出现位置 strripos区分大小写

  3. 自设chrome默认滚动条样式

    今天无聊,想着chrome这种全面使用html的浏览器,可不可以让我自行改变它的默认CSS呢,结果去查查,有,很好 win7/8目录为 C:\Users\[你的用户名]\AppData\Local\G ...

  4. JAVA NIO之Character Set

    明白以下几个概念: 字母集(Character Set),汉字,特殊符号,字母这些都是字符集: 字符编码集(Coded character set),将字符集的字符使用数字进行编码:比如ASCII,就 ...

  5. Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)

    Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...

  6. 发布FireBird数据库所需要DLL文件

    数据库版本:2.5.2 ib_util.dll; icudt30.dll; icuin30.dll icuuc30.dll

  7. 如何通过REST获取JENKINS的编译进度?

    第二版功能需要实现, 我看了一下,获取百分比进度不太可能了,,因为JENKINS本身都没有具体的百分比进度.. 那,,只好实现获取实时值,如果完成就显示完成. URL: http://1.2.3.4/ ...

  8. Hibernate 注解 动态插入( DynamicInsert) 动态更新(DynamicUpdate)

    @DynamicUpdate(value = true)@DynamicInsert(value = true) 这两个注解默认是false,经试验,如果使用了这两个注解,在一定程度上是可以提高插入和 ...

  9. 【转】来自GDXB大大大大的小总结

    一  最短路 模型一 增加限制 例:给定一个图,求起点到终点的最短路,其中你可以使用最多k次机会使某条边的边权变为x. 解法:把每个点拆成k个点,分别表示还能使用多少次机会,构造新图. 模型二 一个点 ...

  10. ANDROID_MARS学习笔记_S04_003_用HttpClent发http请求

    一.代码 1.xml(1)activity_main.xml <TextView android:layout_width="wrap_content" android:la ...