题意略。

思路:

我们可以把 bi[ i ] 在 ai[ ] 中的位置记录下来,然后算出 i - mp[ bi[i] ] ,再将它压入一个multiset。每次我们就二分地来寻找离0最近的数字来作为答案。

那当我们循环左移的时候怎么办呢?把每个数字都减一,把当前 bi[i] 产生的数字重新赋值再压入吗?

当然不是,我们可以改变0参考系,也即二分地来寻找离i最近的数字来作为答案。(i从0开始,因为第一个答案相当于循环左移0次)

那么要删去的那个数字怎么办呢?

我们将它删去后,压入的值是 (i + n) - mp[ bi[i] ]。可是为什么呢?

可以认为是    参考系的值 + n - mp[ bi[i] ] (原本应该改成的值)。

也可以认为是,当前在multiset中的值其实都已经减去了 i ,为了补上这个i,我要在 n - mp[ bi[i] ] 上加上 i。

详见代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ; int mp[maxn],bi[maxn],n;
multiset<int> st; int main(){
int n;
scanf("%d",&n);
int temp;
for(int i = ;i < n;++i){
scanf("%d",&temp);
mp[temp] = i;
}
for(int i = ;i < n;++i){
scanf("%d",&bi[i]);
st.insert(i - mp[bi[i]]);
}
for(int i = ;i < n;++i){
multiset<int>::iterator it = st.lower_bound(i);
int ans = maxn;
if(it != st.end()) ans = min(ans,(*it) - i);
if(it != st.begin()) ans = min(ans,i - *(--it));
printf("%d\n",ans);
int t = i - mp[bi[i]];
st.erase(st.find(t));
st.insert((i + n) - mp[bi[i]]);
}
return ;
}

Codeforces 220C的更多相关文章

  1. CF--思维练习--CodeForces - 220C Little Elephant and Shifts (STL模拟)

    ACM思维题训练集合 The Little Elephant has two permutations a and b of length n, consisting of numbers from ...

  2. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. Linux系统安装Tomcat——.tar.gz版(old)

    这里简单地阐述一下rpm.deb.tar.gz的区别. rpm格式的软件包适用于基于Red Hat发行版的系统,如Red Hat Linux.SUSE.Fedora. deb格式的软件包则是适用于基于 ...

  2. Could not link: /usr/local/etc/bash_completion.d/brew

    用终端 brew update 或 brew install ** 时遇到的问题,详细如下: Error: Could not link: /usr/local/etc/bash_completion ...

  3. 抓取崩溃的log日志

    1.下载adb工具包 也就是解锁软件,如果要解锁的话,需确认有fastboot 安装jdk.sdk 2.注意事项 请确保电脑上只连接了一台手机设备(最好只连接一条USB线),同时确保手机已开启USB调 ...

  4. python 处理json数据

    python 处理 json数据 以下是登录账号后获取的json数据,headers中注意加入cookie值 需要处理的数据如下: 全部代码如下 #!/usr/bin/env python # -*- ...

  5. 调测Onvif事件总结解决办法

    主要在调测事件用例的过程中,发现了大量的信息,和未曾碰到的场景和非法错误等信息,先总结解决办法如下: (1)测试过程中发现以前的一个难题解决了,原先在生成soap空间命名的文件中有部分需要下载,离线生 ...

  6. Why do I write a blog

    I believe the most beautiful and elegant answer to this question is from Churchill. "On a peace ...

  7. quick-cocos2dx在eclipse下的lua调试

    文中大部分内容来自http://cn.quick-x.com/?p=253,绿色标记部分为修改部分. 配置编译环境的基本步骤: 安装 Visual Studio 2012 安装 Java SDK 安装 ...

  8. exe4j打包--exe转安装包

    前面一篇已经详细的说明了打包成exe的步骤了,下面谈谈exe如何压缩成安装文件.这里用到之前的另外一个软件,具体软件看这篇文章 exe4j打包成exe 打开inno 编辑器 打开软件后我们选择 用[脚 ...

  9. js常见兼容性问题以及解决方法

    1.关于使用 event对象,出现的兼容性问题IE/Chrom: event.clientX;event.clientYFF/IE9以上/Chrom: 传参e   e.clientX;e.client ...

  10. Flutter学习笔记(16)--Scaffold脚手架、AppBar组件、BottomNavigationBar组件

    如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 今天的内容是Scaffold脚手架.AppBar组件.BottomNavigationBa ...