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 ...
随机推荐
- lookupedit清空选择 z
lookupedit绑定数据,选择以后.怎么点击按钮使lookupedit回到初始位置,即nulltext的值.注意,是点击按钮,不是按默认的CTRL+DELETE. txtKHXX.EditValu ...
- 我的window平台下的软件
SocksCap64-Portable-3.0(配合google drive 使用) ShadowsocksR-win-3.7.4 dropbox xx-net chrome switchyomega ...
- Metasploit启动
一.启动Kali的PostgreSQL服务 由于使用PostgreSQL作为数据库,因此,必须先运行它. root@kali:~# service postgresql start 使用ss -ant ...
- 组建你自己的Theme,组件你的Style
Andorid-Style,组建你自己的Theme,组件你的Style 前言: 今天,尝试了一个新的Demo,也尝试深入学习,话不多说,看一下,这个Demo如何实现的自定义主题与组件Style是如何绑 ...
- openstack-dbs
真正的服务器派生出线程 和子进程处理多个连接当允许客户端加入聊天室,他发送的任何一条文本都将广播给聊天室中的每个用户,除非文本是服务器CLI当广播一条消息,消息前面将加上发送者的昵称 以尖括号括住昵称 ...
- 第二百一十四天 how can I 坚持
今天肯定有需要记录的东西. html表格. table tr rd colspan rowspan, 还有呢.没了. 睡觉,明天石洋要来泛华. 睡觉.
- Linux下的hostname命令详解
1.临时修改: #hostname ***** //*****为修改的hostname 2.永久修改: # vi /etc/sysconfig/networkNETWORKING=yesHOSTNAM ...
- OpenGL复习要点II
[OpenGL复习要点II] 1.视图变换必须出现在模型变换之前. 2.glMatrixMode()参数有三个,GL_MODELVIEW,GL_PROJECTION,GL_TEXTURE. 3.变换顺 ...
- 对pymysql的简单封装
#coding=utf-8 #!/usr/bin/python import pymysql class MYSQL: """ 对pymysql的简单封装 "& ...
- ubuntu官方源列表网址
http://wiki.ubuntu.org.cn/%E6%BA%90%E5%88%97%E8%A1%A8 (推荐台湾的源)