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

https://www.lydsy.com/JudgeOnline/problem.php?id=1697

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int const xn=1e4+5,inf=1e5+5;
int n,w[xn],t[xn],b[xn],mn,mnn,sta[xn],tot,ans,sum;
bool vis[xn];
int rd()
{
int ret=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}
while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
return f?ret:-ret;
}
void dfs(int x)
{
sta[++tot]=x; vis[x]=1;
mn=min(mn,w[x]); sum+=w[x];
if(!vis[b[x]])dfs(b[x]);
}
int main()
{
n=rd(); mnn=inf;
for(int i=1;i<=n;i++)w[i]=t[i]=rd(),mnn=min(mnn,w[i]);
sort(t+1,t+n+1);
for(int i=1,x;i<=n;i++)
{
x=lower_bound(t+1,t+n+1,w[i])-t;
b[x]=i;
}
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
tot=0; mn=inf; sum=0;
dfs(i); if(tot==1)continue;
ans+=min(sum+mn*(tot-2),sum+mn+mnn*(tot+1));
}
printf("%d\n",ans);
return 0;
}
bzoj 1697
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=1e6+5;
int n,w[xn],t[xn],a[xn],b[xn],mn,sta[xn],tot,mnn;
ll ans,sum;
bool vis[xn];
int rd()
{
int ret=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=0; ch=getchar();}
while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
return f?ret:-ret;
}
void dfs(int x)
{
sta[++tot]=x; vis[x]=1;
mn=min(mn,w[x]); sum+=w[x];
if(!vis[b[x]])dfs(b[x]);
}
int main()
{
n=rd(); mnn=6505;
for(int i=1;i<=n;i++)t[i]=rd(),mnn=min(mnn,t[i]);
for(int i=1,x;i<=n;i++)x=rd(),a[x]=i,w[i]=t[x];//
for(int i=1,x;i<=n;i++)x=rd(),b[i]=a[x];
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
tot=0; mn=6505; sum=0;
dfs(i);
ans+=min(sum+(ll)mn*(tot-2),sum+mn+(ll)mnn*(tot+1));
}
printf("%lld\n",ans);
return 0;
}
bzoj 1119

posted @ 2018-12-03 20:05  Zinn  阅读(124)  评论(0编辑  收藏  举报