经典的题目,主要还是考思维,之前在想的时候只想到了在一个循环中,每次都用最小的来交换,结果忽略了一种情况,还可以选所有数中最小的来交换一个循环。

Cow Sorting
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6511   Accepted: 2532

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 3 1 : Initial order. 
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).
 
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define N 100100 struct node
{
int id;
int num;
}g[N]; int mylink[N];
int f[N],tf[N];
int flag[N];
long long ans;
int mi; int cmp(node t,node t1)
{
return t.num<t1.num;
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
g[i].num=tmp;
g[i].id=i;
}
sort(g+,g++n,cmp);
mi=g[].num;
ans=;
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
mylink[i]=g[i].num;
f[ g[i].id ] = i;
tf[ i ] = g[i].id;
}
for(int i=;i<=n;i++)
{
if(flag[i]==) continue;
flag[i]=;
int s=i;
int tmi=mylink[i];
long long save=mylink[i];
int cnt=;
while( f[s]!=i )
{
cnt++;
s=f[s];
flag[s]=;
save += mylink[s];
tmi=min(mylink[s],tmi);
}
ans += min(cnt*tmi+save-tmi, save-tmi+*tmi+(cnt+)*mi );
}
cout<<ans<<endl;
return ;
}

poj 3270(置换 循环)的更多相关文章

  1. poj 3270 置换

    poj 置换的应用 黑书原题P248 /** 题意: 给定序列, 将其按升序排列, 每次交换的代价是两个数之和, 问代价最小是多少 思路:1.对于同一个循环节之内的,肯定是最小的与别的交换代价最小 2 ...

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

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

  3. POJ 3270 【组合数学】

    题意: 给长度为N的学列,然后让你通过置换来使其递增.原序列没有相同的数字. 1 ≤ N ≤ 10,000 ai<=100000 思路: 先找到循环,然后根据贪心只有两种比较好的情况,让循环里边 ...

  4. POJ 3270 Cow Sorting(置换群)

    题目链接 题意 : N头牛,每个牛的坏脾气都有一个值,每个值都不相同,把这个值按照从小到大排序,如果两个值交换,那么会花掉这两个值之和的时间,让你花最少的时间将每个值从小到大排好序,求最小的总时间. ...

  5. LA 3510 (置换 循环分解) Pixel Shuffle

    思路挺简单的,题目中的每个命令(包括命令的逆)相当于一个置换. 用O(n2k)的时间复杂度从右往左求出这些置换的乘积A,然后求m使Am = I(I为全等置换) 还是先把A分解循环,m则等于所有循环节长 ...

  6. LA 3641 (置换 循环的分解) Leonardo's Notebook

    给出一个26个大写字母的置换B,是否存在A2 = B 每个置换可以看做若干个循环的乘积.我们可以把这些循环看成中UVa 10294的项链, 循环中的数就相当于项链中的珠子. A2就相当于将项链旋转了两 ...

  7. poj 3270 更换使用

    1.确定初始和目标状态. 明确.目标状态的排序状态. 2.得出置换群,.比如,数字是8 4 5 3 2 7,目标状态是2 3 4 5 7 8.能写为两个循环:(8 2 7)(4 3 5). 3.观察当 ...

  8. poj 3270(置换群+贪心)

    Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6993   Accepted: 2754 Descr ...

  9. Cow Sorting POJ 3270 & HDU 2838

    题目网址:http://poj.org/problem?id=3270 题目大意是:一串无序的数字,要排成增序的数列,可以交换不相邻的数,每交换两个数,sum+这两个数,使得sum最小,求最小的sum ...

随机推荐

  1. mysql增量备份(2/2)

    前言 这是在百度文库里看到的文章,原名叫做<MYSQL 完全与增量备份及恢复文档 >,是关于完全备份和增量备份以及恢复文档的...... 文档介绍 本文档采用 mysqldump  对数据 ...

  2. lodash round

    _.round(number, [precision=0]) 根据 precision 四舍五入 number. _.round(4.006); // => 4 _.round(4.006, 2 ...

  3. 经验总结54--搭建linux虚拟机环境

    好久没写博客了.写一篇环境搭建吧. 自己做java,有时会接触linux环境,自己能够搭建一个,方便自己做实验和学习. 1.下载VM11. 下载并依照VM:http://rj.baidu.com/so ...

  4. Android 必知必会 - 依据包名推断 App 执行状态

    假设移动端訪问不佳,请訪问: 掘金版 Github 版 获取指定包名的 APP 是否还在后台执行,推断 APP 是否存活. 背景 能够依据 App 是否有 Service 分两类情况处理: 没有 Se ...

  5. 基于React实现的【绿色版电子书阅读器】,支持离线下载

    代码地址如下:http://www.demodashi.com/demo/12052.html MyReader 绿色版电子书阅读器 在线地址:http://myreader.linxins.com ...

  6. SpringCloud系列十一:自定义Feign

    1. 回顾 上文我们讲解了如何为服务消费者配置Feign. 在Spring Cloud中,Feign的默认配置类是FeignClientsConfiguration,该类定义了Feign默认使用的编码 ...

  7. altera tcl

    例子:https://www.altera.com/support/support-resources/design-examples/intellectual-property/embedded/n ...

  8. C# 操作超时正常还是错

    net(客户端)调用IIS(服务端)出现503后,就报操作超时错误 问题描述:服务端环境:IIS 客户端环境:windowsxp + iis + .net 调用时出现如下错误:System.Net.W ...

  9. windows system.exe占用文件

    1)问题的原因是出于一个服务Application Experience,如果装好系统后就把此服务设为手动启动了,平时运行也没什么异常.但是win7在运行exe时如果没有这个服务的辅助就会长时间的占用 ...

  10. 用@spy模拟真实对象的部分行为

    1.说明在某些情况下,我们需要使用一个真实对象.但是,我们同时需要自定义该对象的部分行 为,此时用@spy 就可以帮我们达到这个目的. 2.用法: categoryService = PowerMoc ...