poj3270
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 6750 | Accepted: 2633 |
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
to reorder the cows in increasing order of grumpiness.
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).
Source
输入
line1行:一个整数:N
line2行:N + 1:每行包含一个整数:line+ 1描述了牛一、暴躁
输出
line 1:一个需要在暴躁的情绪增加的顺序排列的最小时间行牛。
样例解释
3 1 2:初始订单。
2 1 3:交换后奶牛暴躁3和1(时间= 1 + 3 = 4)。
1 2 3:交换后奶牛暴躁1和2(时间= 2 + 1 = 3)。
解析思路
同:给你一列数,需要将这些数按升序排列。你可以每次交换任意两个数的位置,而一次交换的代价被定义成交换的两个数的和。写一程序,用最小代价来完成这项无聊的排序工作。
则 最终的式子就是 ans=sum(整个数列的和)+sigma(min((k-2)*mini,(k+1)*min+mini));
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 101000
int a[N],b[N],c[N];
bool d[N];
int n,ans=,mi;
int main(){
//freopen("sh.txt","r",stdin);
mi=0x7f;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",a+i);
b[i]=a[i];
mi=min(mi,a[i]);
c[a[i]]=i;//记录原来数字的下标
}
sort(b,b+n);//调整成正确的队列便于比较
memset(d,,sizeof(d));
while(n){
int sum=,t=,i=,mmm=0x7f;
while(d[i]) i++;
int beg=i;
do{
t++;
d[i]=;
mmm=min(mmm,a[i]);
sum+=a[i];
i=c[b[i]];
}while(i!=beg);
n-=t;
if(t==)continue;
int v1=(t-)*mmm;
int v2=mmm+(t+)*mi;
ans+=min(v1,v2);//累加最小次数
ans+=sum;
}
printf("%d\n",ans);
return ;
}
poj3270的更多相关文章
- 【poj3270】 Cow Sorting
http://poj.org/problem?id=3270 (题目链接) 题意 n个数要要按从小到大的顺序排列,每次只能交换任意两个数,交换的代价为这两个数之和,问最小代价. Solution 题目 ...
- 【置换群】poj3270 Cow Sorting
并不应该叫置换群……只是用到了置换而已,并没有群. 题解看这个吧,我就不写了:http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669013.ht ...
- poj3270 Cow Sorting
给定有序数组a[1...n]的一个置换a[σ(1)...σ(n)], 通过交换数组元素把置换后的数组恢复为有序, 定义进行一次交换的代价为两元素之和,试问此过程的最小总代价. 实际上一种置换即定义S ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- poj题目
poj2965 poj1753:标准的BFS+位运算优化 poj1328:线段覆盖变种,把圆对应到线段上,贪心求解 poj2109:高精度开根,二分+高精度,注意要判断答案的位数,如果按照题目给的范围 ...
- poj分类 很好很有层次感。
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- 【转】POJ题目分类推荐 (很好很有层次感)
OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...
- 【转】ACM训练计划
[转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
随机推荐
- iOS 关于手势
不同视图不能同时添加相同手势!
- 深入.net(多态一)
代码优化技术: 当您在 编写一个类时,如果您发现你需要编写的“属性”和“方法”曾经在已有的类中实现,则,您可以将其共用的“属性”和“方法”剪切到一个新的“类”中,然后,让两个类共同继承这个“新类”.( ...
- 【代码笔记】iOS-带索引的tableView
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- OC 多态
OC 多态 多态的概念 没有继承,就没有多态 不同类的对象能够定义相同名称的方法 当父类对象的指针或引用指向子类对象时,会进行动态监测,调用真实的方法 C++只有虚函数才能实现多态,OC中所有方法都可 ...
- location of the android sdk has not been setup in the preferences
点eclipse的android virtual device manager提示错误:error:location of the android sdk has not been setup in ...
- Charles中如何对https抓包
前言:下面介绍关于Charles中如何对https抓包 1.在默认没有相关设置HTTPS需要设置相关操作的时候,会出现下面的情况: 2.下面就是设置SSL Proxying,然后443是默可用的端口 ...
- @在php中的作用
@是PHP提供的错误信息屏蔽的专用符号. 使用@mysql_query 不会出现Warning,而原来mysql_query 在遇到错误时会在页面上访提示Warning. 看PHP的代码,总有些行前边 ...
- Virtual Box 杂记
1. Virtual Box后台运行 a. VBoxManage startvm yourvmname --type headlessb. VBoxHeadless --startvm yourvmn ...
- Chrome浏览器二维码生成插件
猛击就可以使用啦->>>猛击使用 源码如下: 源码打包 源码: jquery-2.1.3.min.js jquery.qrcode.min.js https://gith ...
- 【hadoop】——window下elicpse连接hadoop集群基础超详细版
1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...