1119: [POI2009]SLO

Time Limit: 30 Sec  Memory Limit: 162 MB
Submit: 379  Solved: 181
[Submit][Status]

Description

对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次交换的代价之和。请问将(ai)变为(bi)所需的最小代价是多少。

Input

第一行N。 第二行N个数表示wi。 第三行N个数表示ai。 第四行N个数表示bi。 2<=n<=1000000 100<=wi<=6500 1<=ai,bi<=n ai各不相等,bi各不相等 (ai)<>(bi) 样例中依次交换数字(2,5)(3,4)(1,5)

Output

一个数,最小代价。

Sample Input

6
2400 2000 1200 2400 1600 4000
1 4 5 3 6 2
5 3 2 4 6 1

Sample Output

11200

HINT

感谢MT大牛贡献译文.

Source

题解:

同 排序的代价,只不过要把数组元素重新编号。还要记得保存原来的元素值

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 1000000+1000
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
ll n,mi=inf,cs=,a[maxn],b[maxn],c[maxn],d[maxn],w[maxn];
bool check[maxn];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)w[i]=read(),mi=min(w[i],mi);
for1(i,n)d[i]=a[i]=read();
for1(i,n)b[i]=read();
for1(i,n)c[b[i]]=i;
for1(i,n)a[i]=c[a[i]];
ll ans=;
for1(i,n)
if(!check[i])
{
ll j=i,len=,tmp=inf,sum=;
while(!check[j])
{
check[j]=;
tmp=min(tmp,w[d[j]]);
sum+=w[d[j]];
len++;
j=a[j];
}
ans+=sum+min((len-)*tmp,mi*(len+)+tmp);
}
cout<<ans<<endl;
return ;
}

BZOJ1119: [POI2009]SLO的更多相关文章

  1. BZOJ1119[POI2009]SLO && BZOJ1697[Usaco2007 Feb]Cow Sorting牛排序

    Problem J: [POI2009]SLO Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 622  Solved: 302[Submit][Sta ...

  2. 【BZOJ 1119】 1119: [POI2009]SLO (置换)

    1119: [POI2009]SLO Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次 ...

  3. bzoj1697:[Usaco2007 Feb]Cow Sorting牛排序 & bzoj1119:[POI2009]SLO

    思路:以bzoj1119为例,题目已经给出了置换,而每一次交换的代价是交换二者的权值之和,而置换一定是会产生一些环的,这样就可以只用环内某一个元素去置换而使得其余所有元素均在正确的位置上,显然要选择环 ...

  4. 【BZOJ】1119: [POI2009]SLO

    题意 长度为\(n(1 \le n \le 1000000)\)的账单,\(+\)表示存1,\(-\)表示取1,任意时刻存款不会为负.初始有\(p\),最终有\(q\).每一次可以耗时\(x\)将某位 ...

  5. P1119: [POI2009]SLO

    这题预处理稍微动动脑,其实还是个裸的置换群=-=,没什么压力. ; var n,i,j,minx,tem,now,tmin,len:longint; cursum,sum:int64; pos,num ...

  6. BZOJ 1119: [POI2009]SLO [置换群]

    传送门:现在$POI$上的题洛谷都有了,还要$BZOJ$干什么 和$cow\ sorting$一样,只不过问$a_i \rightarrow b_i$ 注意置换是位置而不是数值...也就是说要$i$的 ...

  7. bzoj 1119 [POI2009]SLO && bzoj 1697 [Usaco2007 Feb]Cow Sorting牛排序——思路(置换)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...

  8. bzoj 1119 [POI2009] SLO & bzoj 1697 牛排序 —— 置换+贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...

  9. [POI2009]SLO

    Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次交换的代价之和.请问将(ai)变为(b ...

随机推荐

  1. Object-C 函数定义 -- 笔记

    -(int) f:(int)x; 1.这里 - 表示对象方法, +表示类的方法 2.参数分割使用 : 号来分开 多参数情况: 1.函数不带参数 (函数名: f) -(int) f 2.带一个参数(函数 ...

  2. ubuntu eclipse CDT 问题

    问题一:ubuntu eclipse c++ launch failed binary not found 解决:建完项目后 查看在项目中是不是有debug目录,说明没有编译.仅仅是须要做例如以下操作 ...

  3. 详述iOS国际化

    在真正将国际化实践前,只知道通过NSLocalizedString方法将相应语言的字符串加载进来即可.但最近公司项目的新需求增加英文版本,并支持应用内无死角切换~,这才跳过各种坑实现了应用内切换语言, ...

  4. QP01 BAPI、QP02 BDC

    近期在改动一个检验计划分配的一个程序.上网查了一些资料,分别对QP01检验计划创建.改动QP02.删除物料等操作.分享一下. 一.QP01 BAPI BAPI_INSPECTIONPLAN_CREAT ...

  5. [转] splice系列系统调用

    关注splice系列系统调用(包括splice,tee和vmsplice)已经有一段时间了,开始的时候并未能领会splice的意义所在,致使得出了“splice系列系统调用不怎么实用”的错误结论.随着 ...

  6. 打勾显示输入的密码 --EditText与setTransformationMethod

    实现目标: 实现原理: 为CheckBox添加一个监听器事件; 实现的源码: package edu.cquptzx.showPassword; import android.app.Activity ...

  7. Android中使用ListView绘制自定义表格(2)

    上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 ...

  8. 队列(链式存储)JAVA代码

      publicclassLinkQueue<T>{       //结点类     publicclassNode{         public T data;         pub ...

  9. 比较好的总结runtime

    http://www.cocoachina.com/ios/20160128/15154.html

  10. xxx is not in the sudoers file. This incident will be reported的解决方法

    1>.进入超级用户模式.也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式.(当然,你也可以直接用root用户登录,因为红旗安装过后默认的登录 ...