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. yii2 ActiveRecord常用用法

    User::find()->all();    返回所有数据   User::findOne($id);   返回 主键 id=1  的一条数据   User::find()->where ...

  2. lucene开发序之luke神器

    lucene是一款很优秀的全文检索的开源库,目前最新的版本是lucene4.4,关于lucene的历史背景以及发展状况,在这里笔者就不多介绍了,如果你真心想学习lucene,想必在这之前你已经对此作过 ...

  3. Xcode7.01相对于底版本的变动小结

    1.在Xcode7中系统不再自动支持http请求,需要配置plist才能使用http: 2.appdelegate中的self.window不再支持直接往window上加view,必须先给window ...

  4. PHP获得文件的md5并检验是否被修改

    由于需要判断上传的文件是否被修改过,需要记录上传文件的md5值,所以这里说一下一下获取文件md5值的方法.   md5_file() md5_file() 函数计算文件的 MD5 散列.md5() 函 ...

  5. bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形 容斥

    1914: [Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 272  Sol ...

  6. Android Studio 使用笔记:Git 的配置和第一次提交到仓库

    Git客户端网址:http://git-scm.com/download/ 根据自己的使用平台下载对应的客户端.这里以Mac系统为例,当客户端软件安装配置完毕后,打开AS的配置面板,找到Git的选项 ...

  7. just so you're clear

    The Google Resume的第一句话是: Just so you're clear: it was not my idea to give a talk to Microsoft Resear ...

  8. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  9. POJ_2184_Cow_Exhibition_(动态规划,背包)

    描述 http://poj.org/problem?id=2184 n只奶牛,每只都有智商s_i和情商f_i,取出若干只,保证智商之和与情商之和都不为负的情况下,让两者之和最大. Cow Exhibi ...

  10. 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 三)

    原文网址:http://www.cnblogs.com/ChinaKingKong/p/4964745.html 前言部分 part三 部分主要讲解 Xcode关联绑定发布证书的配置.创建App信息. ...