BZOJ1119: [POI2009]SLO
1119: [POI2009]SLO
Time Limit: 30 Sec Memory Limit: 162 MB
Submit: 379 Solved: 181
[Submit][Status]
Description
Input
Output
Sample Input
2400 2000 1200 2400 1600 4000
1 4 5 3 6 2
5 3 2 4 6 1
Sample Output
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的更多相关文章
- 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) 若干次交换的代价为每次 ...
- bzoj1697:[Usaco2007 Feb]Cow Sorting牛排序 & bzoj1119:[POI2009]SLO
思路:以bzoj1119为例,题目已经给出了置换,而每一次交换的代价是交换二者的权值之和,而置换一定是会产生一些环的,这样就可以只用环内某一个元素去置换而使得其余所有元素均在正确的位置上,显然要选择环 ...
- 【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 ...
- 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 ...
随机推荐
- Linux系统下查看某文件修改的时间戳
Linux系统下查看某文件修改的时间戳查看文件时间戳命令:stat awk.txt File: `awk.txt' Size: 20 Blocks: 8 I ...
- 微软下一代云环境Web开发框架ASP.NET vNext预览
微软在2014年5月12日的TechEd大会上宣布将会公布下一代ASP.NET框架ASP.NET vNext的预览.此次公布的ASP.NET框架与曾经相比发生了根本性的变化,凸显了微软"云优 ...
- webpy:页面下载的三种实现方式
python: 1.import urllib urlretrieve() 方法直接将远程数据下载到本地. >>> help(urllib.urlretrieve)Help on f ...
- python Sina微博自动转发带抽奖字样的微博,添加关注,取消关注
项目地址:https://github.com/chengshuyi/SinaWeibo 具有的功能 转发带抽奖字样的微博并可以@相应数量的好友 提取关注并添加关注 取消关注 获取粉丝列表
- centos6 安装vsftpd
centos6 安装vsftpd vsftpd一般选择yum安装,以下是安装和配置过程 如果是centos6想要安装的话一般是编译安装 1.安装 yum安装 yum install vsftpd 编译 ...
- fork安全的gettid高效实现
进程有id,可以通过getpid()获得,线程也有id,但是glibc没有提供封装.需要自己发出系统调用.在关键路径,系统调用还是对性能有影响的.因此我们可以想到类似glibc对getpid做的cac ...
- 必须声明标量变量 "@列名"
这个主要是因为变量没有赋上值(见下图)
- 通用对象转换Json格式
public static string ObjectToJson<T>(IList<T> IL, params string[] args) { var Json = new ...
- C#链接远程SQL 服务器方法
C#链接远程SQL 服务器方法第一步:申请花生壳内网版,要求交1块钱给花生壳服务器做验证.第二步:把你自己主机本地连接那里的内网地址不要自动获取,写成192.168.0.105,子网掩码255.25 ...
- Error prompt:“xxx is not in the sudoers file”----Solution
//Situation System prompts "xxx is not in the sudoers file"(xxx equals the user name) w ...