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 ...
随机推荐
- 正则表达式表示 ja.resx 所在行
[^\n]*ja.resx[^\n]*\n?正则表达式表示 ja.resx 所在行 用ultraEdit 删除关键字所在行的下一行或是上一行,所在行保留 删除 关键字所在行 的前3行: (^.*?(\ ...
- SQL查询刚開始学习的人指南读书笔记(一)关系数据库和SQL介绍
PART I:Relational Databases and SQL Chapter2, Ensuring Your Database Structure IsSound. 数据库设计准则 什么是关 ...
- 【Android】The application has stopped unexpectedly.Please try again.
打出的android APK在android4.0.2手机上运行正常,但是在2.3.7的MOTO DEFY ME525上运行失败. android 2.3.3版本的模拟器上面运行失败: Sorry! ...
- Selenium webdriver Java 开始
最早接触的selenium是 selenium IDE,当时是为了准备论文.为了用IDE还下载了Firefox浏览器.后来接触过两个项目都需要selenium,一个采用selenium webdirv ...
- ODOO翻译导出窗口修正
当你辛苦修正odoo的翻译,想把它导出到其它系统的时候, 你会发现导出向导窗口无法显示下拉列表. 下面的方法修正此问题: 1.打开"开发者模式". 2.去到翻译导出向导:设置 - ...
- PLSQL中scott账户登录不上,报错ORA-01017: invalid username/password; logon denied
问题:PLSQL中scott账户登录不上,提示上述错误 解决方法: 1. cmd——>进入c目录; 2. 输入:sqlplus/nolog; 3. connect username/passwo ...
- Linux-信号详解
1.Linux支持的所有信号: $ kill -l ) SIGHUP ) SIGINT ) SIGQUIT ) SIGILL ) SIGTRAP ) SIGABRT ) SIGBUS ) SIGFPE ...
- mysql Connector C++ 操作数据库 vs2012
最近想写一个应用程序,要连接和操作mysql数据库,以前只是用c++ builder 操作过mysql数据库,那是用控件操作的,感觉比较弱智,但是c++ builder vcl控件感觉在多线程里比较坑 ...
- C# t4模版引擎笔记 引入外部dll
<#@ assembly name="D:\Documents\Downloads\Util\Util\bin\Debug\Util.dll" #> <#@ im ...
- 数据库面试题.net
1.ADO.net中常用的对象 connection, command, sqladapter, dataset, dataview. 2.net中读写数据库要用到哪些类 DataSet数据存储 Da ...