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]的下标,然后将序列分成三部分:前缀部分( ...
随机推荐
- 支付宝开发(一)-认识php openssl RSA 非对称加密实现
获取支付宝公钥 本地服务器生成私钥和公钥 运用php中openssl相关函数加密解密验证身份 以下是php中openssl相关函数实现的验证,来自php官方demo //需要签名的数据 $data = ...
- php最短的HTTP响应代码
刚刚发现在CodeProject给我推送了一篇文章叫:the Shortest PHP code for Returning HTTP Response Code 翻译过来就是(PHP最短的HTTP ...
- SDUT 1305 查找基因序列问题 dp
题目: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1305 这个题就是一个类似公共子串的dp ...
- servlet 默认是线程安全的吗?
由于Servlet默认是以多线程模式执行的,所以,在编写代码时需要非常细致地考虑多线程的安全问题.然而,很多人编写Servlet程序时并没 有注意到多线程安全的问题,这往往造成编写的程序在少量用户访问 ...
- Session里的对象是不可靠的!
最近在做Database课程的final project,foodiePharos, 重新认识了JSP里容易出现的一些问题. 比如我们这个项目使用了JPA,就涉及到entity对象的状态问题,Enti ...
- Unreal Engine4 蓝图入门
微信公众号:UE交流学习 UE4开发群:344602753 蓝图是Unreal Engine的特点,用C++编程固然好,但是效率要低很多,主要是国内资料比较少,所以不太容易学习,用蓝图编程可以节 ...
- OneAlert 入门(三)——事件分析
OneAlert 是国内首个 SaaS 模式的云告警平台,集成国内外主流监控/支撑系统,实现一个平台上集中处理所有 IT 事件,提升 IT 可靠性.有了 OneAlert,你可以更快更合理地为事件划分 ...
- 李洪强漫谈iOS开发[C语言-028]-sizeof运算符
- delphi-json组件,速度非常快,要比superobject快好几倍
delphi-json组件,速度非常快,要比superobject快好几倍https://github.com/ahausladen/JsonDataObjectshttp://bbs.2ccc.co ...
- SlidingMenu+ViewPager实现侧滑菜单效果
先简单介绍下SlidingMenu和ViewPager. ViewPager就是一个官方提供的多页面滑动组件,需要一个适配器来构建多个页面. 先来看看ViewPager对应的基本适配器PageAdap ...