poj 3270(置换 循环)
经典的题目,主要还是考思维,之前在想的时候只想到了在一个循环中,每次都用最小的来交换,结果忽略了一种情况,还可以选所有数中最小的来交换一个循环。
| 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
Lines 2..N+1: Each line contains a single integer: line i+1 describes the grumpiness of cow i.
Output
Sample Input
3
2
3
1
Sample Output
7
Hint
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(置换 循环)的更多相关文章
- poj 3270 置换
poj 置换的应用 黑书原题P248 /** 题意: 给定序列, 将其按升序排列, 每次交换的代价是两个数之和, 问代价最小是多少 思路:1.对于同一个循环节之内的,肯定是最小的与别的交换代价最小 2 ...
- poj 3270 Cow Sorting (置换入门)
题意:给你一个无序数列,让你两两交换将其排成一个非递减的序列,每次交换的花费交换的两个数之和,问你最小的花费 思路:首先了解一下什么是置换,置换即定义S = {1,...,n}到其自身的一个双射函数f ...
- POJ 3270 【组合数学】
题意: 给长度为N的学列,然后让你通过置换来使其递增.原序列没有相同的数字. 1 ≤ N ≤ 10,000 ai<=100000 思路: 先找到循环,然后根据贪心只有两种比较好的情况,让循环里边 ...
- POJ 3270 Cow Sorting(置换群)
题目链接 题意 : N头牛,每个牛的坏脾气都有一个值,每个值都不相同,把这个值按照从小到大排序,如果两个值交换,那么会花掉这两个值之和的时间,让你花最少的时间将每个值从小到大排好序,求最小的总时间. ...
- LA 3510 (置换 循环分解) Pixel Shuffle
思路挺简单的,题目中的每个命令(包括命令的逆)相当于一个置换. 用O(n2k)的时间复杂度从右往左求出这些置换的乘积A,然后求m使Am = I(I为全等置换) 还是先把A分解循环,m则等于所有循环节长 ...
- LA 3641 (置换 循环的分解) Leonardo's Notebook
给出一个26个大写字母的置换B,是否存在A2 = B 每个置换可以看做若干个循环的乘积.我们可以把这些循环看成中UVa 10294的项链, 循环中的数就相当于项链中的珠子. A2就相当于将项链旋转了两 ...
- poj 3270 更换使用
1.确定初始和目标状态. 明确.目标状态的排序状态. 2.得出置换群,.比如,数字是8 4 5 3 2 7,目标状态是2 3 4 5 7 8.能写为两个循环:(8 2 7)(4 3 5). 3.观察当 ...
- poj 3270(置换群+贪心)
Cow Sorting Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6993 Accepted: 2754 Descr ...
- Cow Sorting POJ 3270 & HDU 2838
题目网址:http://poj.org/problem?id=3270 题目大意是:一串无序的数字,要排成增序的数列,可以交换不相邻的数,每交换两个数,sum+这两个数,使得sum最小,求最小的sum ...
随机推荐
- 在 Word 中的受支持的区域设置标识符的列表
这篇文章包含当前可用的区域设置 ID (LCID) 语言代码的列表. 概要 这篇文章包含当前可用的区域设置 ID (LCID) 语言代码的列表. 区域设置 ID (LCID): 由 Microso ...
- Sublime Theme
Package Control Messages======================== Theme - Spacegray----------------- Thanks for insta ...
- NodeJS实战——创建基础应用并应用模板引擎
本次的目的是搭建一个最基础忽地可以实现功能的NodeJSserver,可以体现出NodeJS的工作流程以及开发的基本框架. 需求:已经安装了nodejs以及express. 一.构建基础的NodeJS ...
- Unity AssetBundle 踩坑记录
Unity AssetBundle 踩坑记录 editor 下选择什么平台的 ab 加载 Material doesn't have a color property '_Color' UnityEd ...
- Android调试方法总结
Android程序调试过程中,通常需要在控制台或者AVD中弹出相关信息作为调试使用,以下是调试使用中会用到的Log类和Toast类的使用方法: 1.Toast Toast是在AVD上显示信息的一种机制 ...
- Odoo8中“更多”下拉菜单选项指定后台执行代码
在Odoo8中的仓库模块,根据每日最小安全库存数量,系统会自动生成一些补货单,而且是一个产品会生成一笔,如果产品比较多,这里生成的补货单也会很多. 如果这里的补货单没有即时处理,那相同产品后续不会再生 ...
- Visual studio C++ MFC的库函数末尾添加“A”和“W”的意义
背景 在使用Visual studio C++ MFC库函数ModifyMenu(...)函数,在Visual studio自动代码补全里提示没有ModifyMenu(...)这个函数,倒是有Modi ...
- 通过内存映射文件来颠倒文本内容(暂没有处理Unicode和换行符)
// ReverseFileDemo.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> ...
- Python进行数值计算
1.计算积分 (1)计算定积分 from scipy import integrate #定义函数def half_circle(x): return (1-x**2)**0.5 pi_half, e ...
- CentOS6.5配置PHP CI程序
步骤: 1.安装CentOS6.5系统: 1.选择PHP+Mysql环境 2.关闭防火墙和SeLinux 1.chkconfig --level 35 iptables off ...