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

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. WRTNode(MT7620n)USB启动总结

    一.改动mt7620.dtsi,去掉默认的bootargs,kernel_menuconfig取消buildin的command line 二.kernel_menuconfig增加scsi驱动.US ...

  2. git删除历史

    Git如何永久删除文件(包括历史记录)   有些时候不小心上传了一些敏感文件(例如密码), 或者不想上传的文件(没及时或忘了加到.gitignore里的), 而且上传的文件又特别大的时候, 这将导致别 ...

  3. 输入N,打印如图所看到的的三角形(例:N=3,N=4,N=5)1&lt;=N&lt;=26

    package demo; public class PrintDemo { public static void main(String[] args) { print(26); } private ...

  4. grid 布局 等比例

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  5. openerp编辑与非编辑下隐藏按钮的方法

    在form表单中,有些控件希望在编辑的时候显示,非编辑时不显示,如hr模块的员工表单,在非编辑模式中,姓名的title不显示,而在编辑模式中显示出来.我们可以在xml文件中给控件赋属性值,class= ...

  6. 【POJ 1080】 Human Gene Functions

    [POJ 1080] Human Gene Functions 相似于最长公共子序列的做法 dp[i][j]表示 str1[i]相应str2[j]时的最大得分 转移方程为 dp[i][j]=max(d ...

  7. Windows外壳名字空间的浏览

    Windows95/98对Dos/Win3.x作了许多重大改进,在文件系统方面,它除了采用长文件名替代Dos中的8.3文件名以外,引入外壳名字空间(Shell Name Space)来代Dos文件系统 ...

  8. 一次httpserver优化的经验和教训(silverlight游戏 - 金庸群侠传X0.5上线记)

    金X因为被推荐到ACFUN游戏排行第一名.并同一时候在17YY.7K7K.U77.17173等各大小游戏站点上线.迎来了在线用户数量的爆炸式增长.眼下各大站点使用外链方式.也就是实际链接到金X官网的s ...

  9. Notes from Data Guard

    There are two types of Standby databases: 1, Physical standby database block-for-block basis the phy ...

  10. Atitit.  Js 冒泡事件阻止 事件捕获   事件传递  事件代理

    Atitit.  Js 冒泡事件阻止 事件捕获   事件传递  事件代理   1. 事件冒泡1 2. 事件捕获1 3. 同时支持了事件捕获阶段和事件冒泡阶段ddEventListener的第三个参数1 ...