Problem J: [POI2009]SLO

Time Limit: 30 Sec  Memory Limit: 162 MB
Submit: 622  Solved: 302
[Submit][Status][Discuss]

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

题解:想到置换,发现在一个循环中,我们尽量让每个点与权值小的进行交换,但是这样会是最优吗?

   显然不是,我们忽略一种情况,我们可以将另一个循环中的一个最小的值与一个循环的一个节点交换,

   然后重复上述操作,再将它换回原来循环来产生更优的解!

   细节见代码:

BZOJ1119

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x7fffffff
#define N 1000010
using namespace std;
int n;
ll v[N],minn=inf,a[N],ans;
int cnt[N];
bool vis[N];
ll read()
{
ll x=,f=; char ch;
while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
return x*f;
}
int main()
{
n=read();
for (int i=; i<=n; i++) v[i]=read(),minn=min(minn,v[i]);
for (int i=; i<=n; i++) a[i]=read();
for (int i=; i<=n; i++) cnt[read()]=i;
for (int i=; i<=n; i++)
{
if (!vis[i])
{
ll t=,mn=inf,sum=; int j=i;
while (!vis[j])
{
vis[j]=; t++; sum+=v[a[j]]; mn=min(mn,v[a[j]]); j=cnt[a[j]];
}
if (t>=)
{
ll t1=sum+1ll*mn*(t-),t2=sum+mn+1ll*minn*(t+);
ans+=min(t1,t2);
}
}
}
printf("%lld\n",ans);
return ;
}

BZOJ1697

 #include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#define ll long long
#define inf 0x7fffffff
#define N 100005
using namespace std;
struct point{
ll v,pos;
}tmp[N];
ll v[N],minn=inf,a[N],ans;
int cnt[N];
bool vis[N];
ll read()
{
ll x=,f=; char ch;
while (ch=getchar(),ch<''||ch>'') if (ch=='-') f=-;
while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
return x*f;
}
bool cmp(point a,point b){return a.v<b.v;
}
int main()
{
int n=read();
for (int i=; i<=n; i++) v[i]=tmp[i].v=read(),a[i]=tmp[i].pos=i,minn=min(minn,tmp[i].v);
sort(tmp+,tmp+n+,cmp);
for (int i=; i<=n; i++){cnt[tmp[i].pos]=i;}
for (int i=; i<=n; i++)
{
if (!vis[i])
{
ll t=,mn=inf,sum=; int j=i;
while (!vis[j])
{
vis[j]=; t++; sum+=v[a[j]]; mn=min(mn,v[a[j]]); j=cnt[a[j]];
}
if (t>=)
{
ll t1=sum+1ll*mn*(t-),t2=sum+mn+1ll*minn*(t+);
ans+=min(t1,t2);
}
}
}
printf("%lld\n",ans);
return ;
}

BZOJ1119[POI2009]SLO && BZOJ1697[Usaco2007 Feb]Cow Sorting牛排序的更多相关文章

  1. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

  2. [BZOJ1697][USACO2007 FEB]Cow Sorting牛排序:贪心+置换

    分析 一个月前做的一道题补一下题解,就简单写一写吧. 单独考虑每一个循环节,如果只进行内部的调整,最优方案显然是把最小的绕这个循环交换一圈. 但是借助全局最小值可能使答案更优,两种情况取个\(\max ...

  3. BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心

    BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行 ...

  4. 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...

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

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

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

  7. P1697: [Usaco2007 Feb]Cow Sorting牛排序

    这是一道置换群的裸题=-=,先拿来试试手对着打,以后应该会更加熟练吧! ; var n,i,j,maxx,minx,now,len,cursum,tmin,sum:longint; p:array[. ...

  8. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序

    Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都是一个 ...

  9. 【BZOJ】1697: [Usaco2007 Feb]Cow Sorting牛排序(置换群)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1697 置换群T_T_T_T_T_T_T 很久以前在黑书和白书都看过,,,但是看不懂... 然后找了本 ...

随机推荐

  1. codeforces 558/C Amr and Chemistry(数论+位运算)

    题目链接:http://codeforces.com/problemset/problem/558/C 题意:把n个数变成相同所需要走的最小的步数易得到结论,两个奇数不同,一直×2不可能有重叠枚举每个 ...

  2. 交换数组中两个元素的位置,元素包括key和value 一维数组

    /*author: yangyu@sina.cndescription: 交换数组中两个元素的位置,元素包括key和value,具体用法见下面的例子*/$arr = array(11=>'a', ...

  3. Chapter 1 First Sight——11

    I didn't relate well to people my age. 我没有向人们叙述清楚我的年龄 Maybe the truth was that I didn't relate well ...

  4. Django: 配置和静态文件

    运行django-admin.py startproject [project-name] 命令会生成一系列文件,在django 1.6版本以后的settings.py文件中有以下语句: # Buil ...

  5. ptrace

    http://zhangwenxin82.blog.163.com/blog/static/114595956201171510512459/

  6. FCKeditor文字编辑器

    FCKeditor文字编辑器的js调用1.需要fckeditor包 下载地址 http://dl.pconline.com.cn/html_2/1/776/id=48351&pn=0.html ...

  7. 一道变态的js题

    一道腾讯js面试题 题目如下: f = function() {return true;}; g = function() {return false;}; (function() { if (g() ...

  8. reactor 类库,基于事件编程

    https://github.com/reactor https://github.com/reactor/reactor-samples/ https://github.com/ReactiveX/ ...

  9. Android Wi-Fi Display(Miracast)介绍

    地址:http://blog.csdn.net/innost/article/details/8474683 Android Wi-Fi Display(Miracast)介绍 2012年11月中旬, ...

  10. PAT (Advanced Level) 1002. A+B for Polynomials (25)

    为0的不要输出. #include<iostream> #include<cstring> #include<cmath> #include<algorith ...