51nod 1125[交换机器的最小代价]【贪心】
来源:互联网 发布:大数据存储技术 编辑:程序博客网 时间:2024/06/05 00:16
Description
有N台机器重量各不相等,现在要求把这些机器按照重量排序,重量从左到右依次递增。移动机器只能做交换操作,但交换机器要花费一定的费用,费用的大小就是交换机器重量的和。例如:3 2 1,交换1 3后为递增排序,总的交换代价为4。给出N台机器的重量,求将所有机器变为有序的最小代价。(机器的重量均为正整数)
题解
首先,会发现,如果我每次交换都保证某一个机器到了它最终的位置,那么我最终最多交换
会发现,如果按照上面的原则进行交换,可以将原来的机器分成几个环,环内挑任意一个点按照上面的原则交换
代码
#include<cstdio>#include<cstring>#include<algorithm>#define maxn 50006#define LL long longusing namespace std;inline char nc(){ static char buf[100000],*i=buf,*j=buf; return i==j&&(j=(i=buf)+fread(buf,1,100000,stdin),i==j)?EOF:*i++;}inline int _read(){ char ch=nc();int sum=0; while(!(ch>='0'&&ch<='9'))ch=nc(); while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc(); return sum;}struct data{ int x,id; bool operator <(const data&c)const{return x<c.x;};}a[maxn];int n,Min,tot,id[maxn],num[maxn],mn[maxn];LL ans,w[maxn];int main(){ freopen("swap.in","r",stdin); freopen("swap.out","w",stdout); n=_read();Min=1e9; for(int i=1;i<=n;i++)a[i].x=_read(),a[i].id=i,Min=min(Min,a[i].x); sort(a+1,a+1+n); for(int i=1;i<=n;i++) if(!id[i]){ id[i]=++tot;num[tot]++;mn[tot]=a[i].x; while(!id[a[i].id]&&a[i].id!=i){ id[a[i].id]=tot;mn[tot]=min(mn[tot],a[a[i].id].x);num[tot]++; w[tot]+=a[i].x+a[a[i].id].x; swap(a[i].id,a[a[i].id].id); } } for(int i=1;i<=tot;i++)ans+=min(w[i],w[i]+(LL)(num[i]-1)*(Min-mn[i])+(LL)2*(Min+mn[i])); printf("%lld",ans); return 0;}
阅读全文
0 0
- 51nod 1125[交换机器的最小代价]【贪心】
- 51NOD 1125(交换机器最小代价) (贪心) 思想 !思想!
- 51nod 1125 机器交换最小代价(贪心)
- 51 nod 1125 交换机器的最小代价(强连通分量+贪心)
- 51nod 1125 交换机器的最小代价 (贪心 公式)
- 51nod 1125 交换机器的最小代价
- 51nod 1125 交换机器的最小代价
- 51Nod-1125-交换机器的最小代价
- 1125 交换机器的最小代价 51NOD
- 51Nod 1125 交换机器的最小代价
- 【置换群】51Nod 1125交换机器的最小代价
- 51NOD1125交换机器的最小代价(贪心算法)
- 51nod1125 交换机器的最小代价(贪心)
- 1125 交换机器的最小代价
- 【51Nod1125】交换机器的最小代价
- 【置换群+贪心】51Nod1125[交换机器的最小代价]题解
- 【贪心】51Nod 1476 括号序列的最小代价
- 51nod 1476 括号序列的最小代价【贪心】【堆】
- 【Data_Structure笔记11】查找算法之【二叉排序树】
- 算出 20 天后是周几?并用 2012-01-20 的方式输出
- 小米系列真机调试代码时遇到小米系列手机调试Installation failed with message Failed to establish session
- 在Javascript中的一些数组的相关操作——初学者的一点小笔记
- 文章标题
- 51nod 1125[交换机器的最小代价]【贪心】
- Python笔记(十五)--模块
- 适合Java开发者学习的Python入门教程
- 初探Android的View绘制过程
- TensorFlow极速入门
- A
- 数据结构学习心得——顺序队和链队
- lintcode:Guess Number Game
- SPOJ Query on a tree II (倍增LCA)