bzoj 1119 [POI2009] SLO & bzoj 1697 牛排序 —— 置换+贪心
题目: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 牛排序 —— 置换+贪心的更多相关文章
- 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 [置换群]
传送门:现在$POI$上的题洛谷都有了,还要$BZOJ$干什么 和$cow\ sorting$一样,只不过问$a_i \rightarrow b_i$ 注意置换是位置而不是数值...也就是说要$i$的 ...
- BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)
题面 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都 ...
- 【BZOJ 1119】 1119: [POI2009]SLO (置换)
1119: [POI2009]SLO Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若干次交换的代价为每次 ...
- BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心
BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行 ...
- 【BZOJ】1119: [POI2009]SLO
题意 长度为\(n(1 \le n \le 1000000)\)的账单,\(+\)表示存1,\(-\)表示取1,任意时刻存款不会为负.初始有\(p\),最终有\(q\).每一次可以耗时\(x\)将某位 ...
- [BZOJ1697][USACO2007 FEB]Cow Sorting牛排序:贪心+置换
分析 一个月前做的一道题补一下题解,就简单写一写吧. 单独考虑每一个循环节,如果只进行内部的调整,最优方案显然是把最小的绕这个循环交换一圈. 但是借助全局最小值可能使答案更优,两种情况取个\(\max ...
- BZOJ1119: [POI2009]SLO
1119: [POI2009]SLO Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 379 Solved: 181[Submit][Status] ...
- 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序
1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...
随机推荐
- 【Java】 Spring依赖注入小试牛刀:编写第一个Spring ApplicationContext Demo
0 Spring的依赖注入大致是这样工作的: 将对象如何构造(ID是什么?是什么类型?给属性设置什么值?给构造函数传入什么值?)写入外部XML文件里.在调用者需要调用某个类时,不自行构造该类的对象, ...
- Microsoft-office 常见问题
1.工作表写入保护,忘记密码,解决办法: 流程如下: 1打开文件2工具---宏----录制新宏---输入名字如:aa3停止录制(这样得到一个空宏)4工具---宏----宏,选aa,点编辑按钮5删除窗口 ...
- Top 10 Open Source Bug Tracking System系统
Bugzilla http://www.bugzilla.org/ Mantis php http://www.mantisbt.org/ Trac Python also provides wiki ...
- AWS:5.公有云编程
主要内容 1.AWS接口 2.使用AWS命令行 3.使用python sdk编程 AWS接口 Console:web控制台 登录amazon后在"我的账户" -> AWS管理 ...
- iOS App打包上架超详细流程
https://www.jianshu.com/p/817686897ec1?open_source=weibo_search
- PHP 格式化数字串
在xls或csv文件中, 超过12位以上的数字会被"科学计数", 所以当php读取这些文件的时候, 会读成 420E+16 , 显然这不是我们想要的, 所以就要用到数字格式化了! ...
- 创建node.js一个简单的应用实例
在node.exe所在目录下,创建一个叫 server.js 的文件,并写入以下代码: //使用 require 指令来载入 http 模块 var http = require("http ...
- postgres 备份数据库
https://www.postgresql.org/docs/9.1/static/app-pgdump.html bash-4.2$ pg_dump -Fc xianlan_prod > / ...
- High Performance Browser Networking
Chapter 1. Primer on Latency and Bandwidth As a result, to improve performance of our applications, ...
- Java for LeetCode 094 Binary Tree Inorder Traversal
解题思路: 中序遍历,左子树-根节点-右子树 JAVA实现如下: public List<Integer> inorderTraversal(TreeNode root) { List&l ...