P3482 [POI2009]SLO-Elephants
首先,交换关系肯定是构成一个环的时候最优
如果这个环是自环,不用交换了
如果环的大小为2,直接交换便是
否则的话,我们可以用环里最小的点最为交换媒介,然后去和其他交换直到到达正确的位置,那么环里最小的点交换了\(n-1\)次其他各一次
然而还有一种情况是交换媒介是环外的点,把它换进环里然后其他都换到正确的位置再把剩下那个换回来,那么换出去的那个肯定是环里权值最小的点,也就是说环里权值最小的点换了2次,全局权值最小的点交换了n+1次,其他的各交换一次
//minamoto
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define fp(i,a,b) for(register int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(register int i=a,I=b-1;i>I;--i)
#define go(u) for(register int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
int res,f=1;char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
const int N=1e6+5;
struct eg{int v,nx,w;}e[N<<1];int head[N],tot;
inline void add(int u,int v,int w){e[++tot]={v,head[u],w},head[u]=tot;}
int n,a[N],pos[N],tim,mn=inf,x;bool vis[N];vector<int>st[N];
void dfs(int u){
vis[u]=1;go(u)if(v!=u){
st[tim].push_back(e[i].w);
if(!vis[v])dfs(v);
}
}
ll ANS(){
ll ans=0;int tmp,tmpmn;
fp(i,1,tim){
tmp=0,tmpmn=inf;
fp(j,0,st[i].size()-1)ans+=st[i][j],tmpmn=min(tmpmn,st[i][j]);
if(st[i].size()>2){
int ex=st[i].size()-2;
ans+=min(1ll*ex*tmpmn,1ll*(ex+3)*mn+tmpmn);
}
}return ans;
}
int main(){
// freopen("testdata.in","r",stdin);
n=read();fp(i,1,n)a[i]=read(),mn=min(mn,a[i]);fp(i,1,n)x=read(),pos[x]=i;
fp(i,1,n)x=read(),add(pos[x],i,a[x]);
fp(i,1,n)if(!vis[i])++tim,dfs(i);
printf("%lld\n",ANS());return 0;
}
P3482 [POI2009]SLO-Elephants的更多相关文章
- BZOJ1119: [POI2009]SLO
1119: [POI2009]SLO Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 379 Solved: 181[Submit][Status] ...
- 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 ...
- 【BZOJ 1119】 1119: [POI2009]SLO (置换)
1119: [POI2009]SLO Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次 ...
- 【BZOJ】1119: [POI2009]SLO
题意 长度为\(n(1 \le n \le 1000000)\)的账单,\(+\)表示存1,\(-\)表示取1,任意时刻存款不会为负.初始有\(p\),最终有\(q\).每一次可以耗时\(x\)将某位 ...
- P1119: [POI2009]SLO
这题预处理稍微动动脑,其实还是个裸的置换群=-=,没什么压力. ; var n,i,j,minx,tem,now,tmin,len:longint; cursum,sum:int64; pos,num ...
- bzoj1697:[Usaco2007 Feb]Cow Sorting牛排序 & bzoj1119:[POI2009]SLO
思路:以bzoj1119为例,题目已经给出了置换,而每一次交换的代价是交换二者的权值之和,而置换一定是会产生一些环的,这样就可以只用环内某一个元素去置换而使得其余所有元素均在正确的位置上,显然要选择环 ...
- BZOJ 1119: [POI2009]SLO [置换群]
传送门:现在$POI$上的题洛谷都有了,还要$BZOJ$干什么 和$cow\ sorting$一样,只不过问$a_i \rightarrow b_i$ 注意置换是位置而不是数值...也就是说要$i$的 ...
- 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 ...
- bzoj 1119 [POI2009] SLO & bzoj 1697 牛排序 —— 置换+贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...
- [POI2009]SLO
Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次交换的代价之和.请问将(ai)变为(b ...
随机推荐
- hihoCoder#1054 滑动解锁
原题地址 回溯搜索 对于每个待枚举的点,检查: 1. 度数检查:是否违反了出度入度限制.因为生成的路径除了首尾节点外,其他节点的出度和入度只能为2 2. 共线检查:是否违反了共线条件.即跨越了尚未枚举 ...
- Flask(2):登陆验证
装饰器补充: import functools def auth(func): @functools.wraps(func) # 作用:把原函数的原信息封装到 inner 中 def inner(*a ...
- 国王游戏(codevs 1198)
题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王站在队伍的最 ...
- 洛谷 通天系列 P1760 P1757 P1759
P1760 通天之汉诺塔 汉诺塔问题.一个高精乘单精解决 ans=2^n-1 /*by SilverN*/ #include<algorithm> #include<iostream ...
- java中文乱码问题的处理方式
URL访问java时. 注意: URL: 编码二次 URLEncoder.encode(URLEncoder.encode(searchKey, "utf-8"),"ut ...
- 修改xampp的mysql默认密码
MySQL 的“root”用户默认状态是没有密码的,所以在 PHP 中您可以使用 mysql_connect("localhost","root"," ...
- Pagodas 等差数列
nn pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, ...
- [bzoj3879]SvT_后缀数组_RMQ_单调栈
SvT bzoj-3879 题目大意:给定一个字符串.每次询问给定$t$个位置,求两两位置开头的后缀的$LCP$之和. 注释:$1\le length\le 5\cdot 10^5$,$\sum t\ ...
- Same Tree (二叉树DFS)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- linux network name space
linux network namespace概念类似于网络中的 VRF (virtual routing and forwarding).但是,你不知道VRF的概念也没关系,下面我们通过一个简单的介 ...