题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119

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

原序列到目标序列的置换,可以找出一些循环节;

发现如果按顺序把一个循环节换下来,如果长度为 l,则第一个数被算了 l-1 次,其它数都是 1 次;

为了代价最小,还可以利用循环节外面的数,做法就是先把它换进来,然后让它算 l-1 次,再换出去;

贪心地取个 min 即可;原来还想过一开始把数字作为位置,位置作为数字,则代价就算在位置上了,但是这样找出了循环节,似乎不一定是能按顺序换的...

代码如下:

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

bzoj 1697

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const xn=1e6+;
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=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return f?ret:-ret;
}
void dfs(int x)
{
sta[++tot]=x; vis[x]=;
mn=min(mn,w[x]); sum+=w[x];
if(!vis[b[x]])dfs(b[x]);
}
int main()
{
n=rd(); mnn=;
for(int i=;i<=n;i++)t[i]=rd(),mnn=min(mnn,t[i]);
for(int i=,x;i<=n;i++)x=rd(),a[x]=i,w[i]=t[x];//
for(int i=,x;i<=n;i++)x=rd(),b[i]=a[x];
for(int i=;i<=n;i++)
{
if(vis[i])continue;
tot=; mn=; sum=;
dfs(i);
ans+=min(sum+(ll)mn*(tot-),sum+mn+(ll)mnn*(tot+));
}
printf("%lld\n",ans);
return ;
}

bzoj 1119

bzoj 1119 [POI2009] SLO & bzoj 1697 牛排序 —— 置换+贪心的更多相关文章

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

  2. BZOJ 1119: [POI2009]SLO [置换群]

    传送门:现在$POI$上的题洛谷都有了,还要$BZOJ$干什么 和$cow\ sorting$一样,只不过问$a_i \rightarrow b_i$ 注意置换是位置而不是数值...也就是说要$i$的 ...

  3. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)

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

  4. 【BZOJ 1119】 1119: [POI2009]SLO (置换)

    1119: [POI2009]SLO Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次 ...

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

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

  6. 【BZOJ】1119: [POI2009]SLO

    题意 长度为\(n(1 \le n \le 1000000)\)的账单,\(+\)表示存1,\(-\)表示取1,任意时刻存款不会为负.初始有\(p\),最终有\(q\).每一次可以耗时\(x\)将某位 ...

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

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

  8. BZOJ1119: [POI2009]SLO

    1119: [POI2009]SLO Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 379  Solved: 181[Submit][Status] ...

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

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

随机推荐

  1. C#与Java在修饰符上的不同

    1.readonly 修饰符仅用于修饰类的数据成员.正如其名字说的,一旦它们已经进行了写操作.直接初始化或在构造函数中对其进行了赋值,数据成员就只能对其进行读取. readonly 和 const 数 ...

  2. 32.10 使用模板更改控件的UI

    32.10  使用模板更改控件的UI 样式是改变WPF控件基本外形的非常好(且非常简单)的方式,它通过为窗口部件的特性设置建立一组默认的值,从而改变WPF控件的基本外形.但是,即使样式允许我们改变各种 ...

  3. Flask,ORM及模板引擎Jinja2

    跨域:http://blog.csdn.net/yannanxiu/article/details/53036508 下载flask_cors包 pip install flask-cors 使用fl ...

  4. 九度OJ 1165:字符串匹配 (模式匹配)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3219 解决:1149 题目描述: 读入数据string[ ],然后读入一个短字符串.要求查找string[ ]中和短字符串的所有匹配,输出 ...

  5. linux rz sz命令

    rz是receive zmodem的缩写,sz是send zmodem的缩写. 传输文件使用的是zmodem协议,所以叫zmodem. r和s是以服务器为主体的,服务器接收就是r,服务器发送就是s.

  6. 在linux通过源码编译安装redis详细步骤

    1.下载源码包 [root@localhost opt]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz 2.解压缩redis ...

  7. 在JDK 6和JDK 7的substring()方法的区别?

    原文链接:https://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/ 在JDK 6和JDK 7中subs ...

  8. JS dom最常用API

    //document方法:    var cont = document.getElementByIdx_x('cont'); //className给标签添加class    cont.classN ...

  9. ABAP 调用webservice 错误

    错误:1.soamanager 配置端口错误: 调整端口后报错: java端回复: 嗯 有问题了我待会儿看看应该是数据有问题 

  10. [容易]Fizz Buzz 问题

    题目来源:http://www.lintcode.com/zh-cn/problem/fizz-buzz/