Codeforces Round #743 (Div. 2) B. Swaps(思维)
https://codeforces.com/contest/1573/problem/B
给定两个长度为n的数组,数组a和数组b
数组a包含从1到2*n的任意顺序的奇数,数组b包含从1到2*n的任意偶数
可执行的操作如下:
从两个数组中选择一个,从1到n-1中选择一个索引
交换第i和第i+1个元素
计算使得数组a在字典序上小于数组b的所需要的最少的移动次数。
input
3
2
3 1
4 2
3
5 3 1
2 4 6
5
7 5 9 1 3
2 4 6 10 8
output
0
2
3
- 从数组a的小个数依从往右边取,合成minn
- 与此同时,数组b也需要达到首位数字大于数组a的首位数字的效果
- 就可以在从左往右中同步取最终最小的移动代价
#include<bits/stdc++.h>
using namespace std;
const int N=200200,M=2002;
int a[N],b[N];
int main()
{
cin.tie(0); ios::sync_with_stdio(false);
int T=1;
cin>>T;
while(T--)
{
int n;
cin>>n;
map<int,int> mp;
for(int i=1;i<=n;i++)
{
cin>>a[i];
mp[a[i]]=i;
}
for(int i=1;i<=n;i++)
{
cin>>b[i];
mp[b[i]]=i;
}
sort(a+1,a+1+n);
sort(b+1,b+1+n);
//for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl;
// for(int i=1;i<=n;i++) cout<<b[i]<<" "; cout<<endl;
int minn=1e9,ans=1e9;
for(int i=1;i<=n;i++)
{
minn=min(minn,mp[a[i]]);
ans=min(ans,minn+mp[b[i]]-2);
//cout<<minn<<" "<<ans<<endl;
}
cout<<ans<<endl;
}
return 0;
}
本文作者:Vijurria
本文链接:https://www.cnblogs.com/Vivian-0918/p/16610422.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
贪心
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步