Codeforces 220C
题意略。
思路:
我们可以把 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的更多相关文章
- 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 ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- .NET Core CSharp初级篇 1-6 类的多态与继承
.NET Core CSharp初级篇 1-6 本节内容为类的多态与继承 简介 终于讲到了面向对象三大特性中的两大特性--继承与多态.通过继承与多态,我们能很好的将类的拓展性发挥到了极致.在下面的内容 ...
- C 单链表 实现约瑟夫环
list.h #ifndef _List_H #define _List_H typedef int ElementType; struct Node; typedef struct Node *Pt ...
- PHP--仿微信, 通过登陆者用户名显示好友列表,显示头像和昵称
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- c#六大设计原则(以仪器代码为例)
[有格式的原文请到https://www.cnc6.cn/c六大设计原则/文末下载] 软件设计原则常见的有6大原则,分别为: ①单一职责原则: ②开闭原则: ③依赖倒置原则: ④里氏替换原则: ⑤接口 ...
- django第二次 (转自刘江)
除了我们前面说过的普通类型字段,Django还定义了一组关系类型字段,用来表示模型与模型之间的关系. 一.多对一(ForeignKey) 多对一的关系,通常被称为外键.外键字段类的定义如下: clas ...
- TestNG在Eclipse中运行的几种方法
目录 1 在Eclipse Outline视图中,点右键run as TestNG Test (不推荐) 2 在Eclipse类编辑界面,直接点击右键run as TestNG Test 3 通过Te ...
- RobotFramework_2.新建项目、新建用例、运行用例和查看测试报告
RobotFramework的使用 新建一个项目 Robotframework-ride的界面 分了四个区域:菜单栏.工具栏.案例及资源区.工作区,如下图: 创建项目 首先,点击File-New Pr ...
- 【Java】Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 1099
详细信息如下: Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: ...
- angularjs通信以及postmessage与iframe通信
这篇文章是用markdown工具写的,有需要的可以使用vscode打开 # angularjs 控制器.组件之间的通信 ## 一.基于事件的方式 此方式下,主要通过 angularjs 内置指令` ...
- python 实现两个文本文件内容去重
实现两个文本内容去重,输出两个文本不重复的结果 两个测试文本内容如下 1.txt中内容为 1 2 3 4 5 6 7 8 2.txt中内容为 1 2 3 4 5 6 7 8 9 10 11 12 13 ...