1119: [POI2009]SLO

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大牛贡献译文.

【分析】

  跟BZOJ 1119一模一样,记得用LL。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define Maxn 1000010
#define INF 0xfffffff
#define LL long long LL w[Maxn]; struct node
{
LL a,b;
}t[Maxn]; bool cmp(node x,node y) {return x.a<y.a;}
LL mymin(LL x,LL y) {return x<y?x:y;} bool vis[Maxn]; int main()
{
LL n,mnn=INF;
scanf("%lld",&n);
for(LL i=;i<=n;i++) {scanf("%lld",&w[i]);mnn=mymin(mnn,w[i]);}
for(LL i=;i<=n;i++) scanf("%lld",&t[i].a);
for(LL i=;i<=n;i++) scanf("%lld",&t[i].b);
sort(t+,t++n,cmp);
memset(vis,,sizeof(vis));
LL ans=;
for(LL i=;i<=n;i++) if(vis[i]==)
{
LL x=i,mn=INF,h=;
LL cnt=;
while(vis[x]==)
{
mn=mymin(mn,w[x]);
vis[x]=;
cnt++;
h+=w[x];
x=t[x].b;
}
ans+=mymin(h+(cnt-)*(LL)mn,h+mn+(cnt+)*(LL)mnn);
}
printf("%lld\n",ans);
return ;
}

2017-01-12 20:13:51

【BZOJ 1119】 1119: [POI2009]SLO (置换)的更多相关文章

  1. BZOJ1119: [POI2009]SLO

    1119: [POI2009]SLO Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 379  Solved: 181[Submit][Status] ...

  2. 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 ...

  3. BZOJ_1119_[POI2009]SLO_置换+贪心

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

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

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

  5. 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 ...

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

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

  7. 【BZOJ】1119: [POI2009]SLO

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

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

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

  9. bzoj 3202 [Sdoi 2013] 项链 —— 置换+计数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3202 参考了博客: https://www.cnblogs.com/zhoushuyu/p/ ...

随机推荐

  1. 【BZOJ】1700: [Usaco2007 Jan]Problem Solving 解题

    [题意]给定n道题,每月末发放工资m,要求从1解到n,每道题需要在当月初付费ai,下月初付费bi,多道题可以安排在同月,求最少月数. [算法]DP [题解]参考自:[bzoj1700]Problem ...

  2. Spring基础使用(一)--------IOC、Bean的XML方式装配

    基础 1.xml文件基础格式: <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns=&q ...

  3. Git 常用命令(二)

    用 git init 在目录中创建新的 Git 仓库.  $ mkdir test $ cd test/ $ git init Initialized empty Git repository in ...

  4. EditText中inputType详解

    <EditText Android:layout_width="fill_parent" android:layout_height="wrap_content&q ...

  5. Eureka服务续约(Renew)源码分析

    主要对Eureka的Renew(服务续约),从服务提供者发起续约请求开始分析,通过阅读源码和画时序图的方式,展示Eureka服务续约的整个生命周期.服务续约主要是把服务续约的信息更新到自身的Eurek ...

  6. python之提速千倍爆破一句话

    看了一下冰河大佬写的文章特别有感:https://bbs.ichunqiu.com/thread-16952-1-1.html 简单描述一下: 利用传统的单数据提交模式. 比如下面这个一句话木马: & ...

  7. CreateProcess中的部分参数理解

    函数原型,这里写Unicode版本 WINBASEAPIBOOLWINAPICreateProcessW( _In_opt_ LPCWSTR lpApplicationName, //可执行文件名字 ...

  8. 在linux程序里面,知道一个函数地址,改函数是属于某个动态库的,怎么样得到这个动态库的全【转】

    转自:http://www.360doc.com/content/17/1012/11/48326749_694292472.shtml 另外dl_iterate_phdr可以查到当前进程所装在的所有 ...

  9. 2015多校第6场 HDU 5354 Bipartite Graph CDQ,并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5354 题意:求删去每个点后图是否存在奇环(n,m<=1e5) 解法:很经典的套路,和这题一样:h ...

  10. python中烦人的锟斤拷(\xef\xbf\xbd)

    首先要知道\xef\xbf\xbd是啥东西 >>> u'\uFFFD'.encode('utf-8') '\xef\xbf\xbd' 由此我们可以知道\xef\xbf\xbd是utf ...