Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset
C. Little Elephant and Shifts
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/problemset/problem/220/C
Description
The distance between permutations a and b is the minimum absolute value of the difference between the positions of the occurrences of some number in a and in b. More formally, it's such minimum |i - j|, that ai = bj.
A cyclic shift number i (1 ≤ i ≤ n) of permutation b consisting from n elements is a permutation bibi + 1... bnb1b2... bi - 1. Overall a permutation has n cyclic shifts.
The Little Elephant wonders, for all cyclic shifts of permutation b, what is the distance between the cyclic shift and permutation a?
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the size of the permutations. The second line contains permutation a as n distinct numbers from 1 to n, inclusive. The numbers are separated with single spaces. The third line contains permutation b in the same format.
Output
In n lines print n integers — the answers for cyclic shifts. Print the answers to the shifts in the order of the shifts' numeration in permutation b, that is, first for the 1-st cyclic shift, then for the 2-nd, and so on.
Sample Input
2
1 2
2 1
Sample Output
1
0
HINT
题意
给你一个a数组,一个b数组
都只含1-n
俩数组的距离定义为,if(a[i]==b[j])dis=min(dis,abs(j-i))
然后对于每一个b的排列,让你输出距离
题解:
用multiset模拟一下就好了
对了,如果用multiset.erase(iterator)这样是只会删除一个的
如果multiset.erase(x),x是一个number的话,这样会把等于x的都删除
代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1050005
#define mod 10007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** int a[maxn];
int b[maxn];
multiset<int> s;
multiset<int>::iterator it;
int ans;
int main()
{
int n=read();
for(int i=;i<n;i++)
{
int x=read();
a[x]=i;
}
for(int i=;i<n;i++)
{
b[i]=read();
s.insert(i-a[b[i]]);
}
for(int i=;i<n;i++)
{
ans=inf;
it=s.lower_bound(i);
if(it!=s.end())
ans=min(ans,*it-i);
if(it!=s.begin())
ans=min(ans,i-*(--it));
printf("%d\n",ans);
it=s.find(i-a[b[i]]);
s.erase(it);
s.insert(i-a[b[i]]+n);
}
}
Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset的更多相关文章
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
		B. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ... 
- Codeforces Round #136 (Div. 2)
		A. Little Elephant and Function 逆推. B. Little Elephant and Numbers \(O(\sqrt n)\)枚举约数. C. Little Ele ... 
- Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索
		题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ... 
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
		E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ... 
- Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)
		数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ... 
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
		题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ... 
- [Codeforces Round #340 (Div. 2)]
		[Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ... 
- Codeforces Round #366 (Div. 2) ABC
		Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ... 
- Codeforces Round #354 (Div. 2) ABCD
		Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ... 
随机推荐
- 利用DescriptionAttribute定义枚举值的描述信息 z
			System.ComponentModel命名空间下有个名为DescriptionAttribute的类用于指定属性或事件的说明,我所调用的枚举值描述信息就是DescriptionAttribute类 ... 
- js保留小数点后N位的方法介绍
			js保留小数点后N位的方法介绍 利用toFixed函数 代码如下 复制代码 <script language="javascript"> document.write( ... 
- mssql 容易掉进的坑
			1. 重复 使用 into #tabel(不是在开头使用insert into ) 会报错 if 1=1 begin select * into #tabel from product ... 
- Android之操作SQLite
			一.SQLite的介绍 1.SQLite简介 SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入 式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的 ... 
- phpcms学习总结
			文件目录结构 根目录 | – api 接口文件目录 | – caches 缓存文件目录 | – configs 系统配置文件目录 | – caches_* 系统缓存目录 | – phpcms phpc ... 
- Java多线程学习总结--线程同步(2)
			线程同步是为了让多个线程在共享数据时,保持数据的一致性.举个例子,有两个人同时取钱,假设用户账户余额是1000,第一个用户取钱800,在第一个用户取钱的同时,第二个用户取钱600.银行规定,用户不允许 ... 
- Android Audio遇到播放无声时的分析
			在Android Audio开发过程中,有遇到播放ringtone时无声,但播放Music可以听到声音,关于无声问题的分析,在此做个笔记,方便以后回顾. 分析方向: 1:在音量控制面板中确认该音频流对 ... 
- ASP.NET 应用程序生命周期概述[转自MSDN]
			本文转自:http://msdn.microsoft.com/zh-cn/library/ms178473(VS.80).aspx 下表描述了 ASP.NET 应用程序生命周期的各个阶段. 阶段 ... 
- QCon 2013 上海 -- 互联网金融
			互联网金融应该是最近很火爆的一个领域.由于阿里小贷和余额宝的初步成功,这一块都被视为破除传统金融领域垄断的法宝.大家可能都知道,电商平台.金融和大数据是阿里集团未来的三个重要方面.而关于金融,马云最经 ... 
- Android 横屏时禁止输入法全屏
			在自己EditText的xml里加上属性 android:imeOptions="flagNoExtractUi" 
