## bzoj 1119 [POI2009]SLO && bzoj 1697 [Usaco2007 Feb]Cow Sorting牛排序——思路（置换）

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e6+5,M=6505;
int n,w[N],a[N],b[N],tot; ll ans;
bool vis[N];
int rdn()
{
int ret=0;bool fx=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();}
while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return fx?ret:-ret;
}
ll Mn(ll a,ll b){return a<b?a:b;}
int main()
{
n=rdn();  int tmn=M;
for(int i=1;i<=n;i++)w[i]=rdn(),tmn=Mn(tmn,w[i]);
for(int i=1;i<=n;i++)a[i]=rdn();
for(int i=1;i<=n;i++)b[rdn()]=i;
for(int i=1;i<=n;i++)
if(!vis[i])
{
int cr=i,mn=M;ll sm=0; tot=0;
while(!vis[cr])
{
tot++; vis[cr]=1;
mn=Mn(mn,w[a[cr]]); sm+=w[a[cr]];
cr=b[a[cr]];
}
ans+=Mn((ll)mn*(tot-1)+sm-mn,(ll)tmn*(tot+1)+sm+mn);
}
printf("%lld\n",ans);
return 0;
}
View Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e4+5,M=1e5+5;
int n,a[N],b[N],c[M],ans;
bool vis[N];
int rdn()
{
int ret=0;bool fx=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();}
while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return fx?ret:-ret;
}
int Mn(int a,int b){return a<b?a:b;}
int main()
{
n=rdn(); int tmn=M;
for(int i=1;i<=n;i++)a[i]=rdn(),tmn=Mn(tmn,a[i]);
for(int i=1;i<=n;i++)b[i]=a[i];
sort(b+1,b+n+1);
for(int i=1;i<=n;i++)c[b[i]]=i;
for(int i=1;i<=n;i++)
if(!vis[i])
{
int cr=i,tot=0,mn=M,sm=0;
while(!vis[cr])
{
tot++; vis[cr]=1;
mn=min(mn,a[cr]); sm+=a[cr];
cr=c[a[cr]];
}
ans+=Mn( mn*(tot-1)+sm-mn,tmn*(tot+1)+sm+mn );
}
printf("%d\n",ans);
return 0;
}
View Code

posted on 2018-12-03 18:07  Narh  阅读(86)  评论(0编辑  收藏  举报