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 ...
随机推荐
- HttpWebRequest的使用之Get和Post的差别(C#)
这两天做的是通过一个HttpWebRequest将采集地址发送到服务端,服务端会返回一个JSON格式的字符串,然后我这边再对这个JSON进行反序列化,得到我想要的数据.在这篇文章里我简单介绍一下Htt ...
- Java的几种创建实例方法的性能对比
近来打算自己封装一个比较方便读写的Office Excel 工具类,前面已经写了一些,比较粗糙本就计划重构一下,刚好公司的电商APP后台原有的导出Excel实现出现了可怕的性能问题,600行的数据生成 ...
- 使用Redis为注册中心的Dubbo微服务架构(基于SpringBoot)
title: 使用Redis为注册中心的Dubbo微服务架构(基于SpringBoot) date: 2019-07-30 14:06:29 categories: 架构 author: mrzhou ...
- Android通过辅助功能实现抢微信红包原理简单介绍
简书文章:https://www.jianshu.com/p/e1099a94b979 附抢红包开源项目地址,代码已全改为Kotlin了,已适配到最新微信7.0.5版本,如果对你有所帮助赏个star吧 ...
- python 感悟
* 优美胜于丑陋.* 显式胜于隐式.* 简单胜于复杂.* 复杂胜于难懂.* 扁平胜于嵌套.* 稀疏胜于紧密.* 可读性应当被重视.* 尽管实用性会打败纯粹性,特例也不能凌驾于规则之上.* 不要忽略任何 ...
- TCP queue 的一些问题
转自Jasey Wang的blog,原文地址 首先回顾下三次握手里面涉及到的问题: 当 client 通过 connect 向 server 发出 SYN 包时,client 会维护一个 socket ...
- 【iOS】the executable was signed with invalid entitlements
又遇到了这个问题,貌似之前遇到过,如图所示: 原因:开发证书里没添加手机. PS: Xcode7 除外,据说已经不需要证书了,这里用的是 6.4
- Apache Flink 1.9 重大特性提前解读
今天在 Apache Flink meetup ·北京站进行 Flink 1.9 重大新特性进行了讲解,两位讲师分别是 戴资力/杨克特,zhisheng 我也从看完了整个 1.9 特性解读的直播,预计 ...
- dubbo是如何控制并发数和限流的?
ExecuteLimitFilter ExecuteLimitFilter ,在服务提供者,通过 的 "executes" 统一配置项开启: 表示每服务的每方法最大可并行执行请求数 ...
- Dubbo里面线程池的拒绝策略
Dubbo里面线程池的拒绝策略 public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy { protecte ...