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 ...
随机推荐
- Cocos2d-android (06) 屏幕触摸事件及坐标转换
为屏幕添加触摸事件,将左上角坐标转换为左下角坐标 package com.arlen.cocos2d.touch01; import org.cocos2d.layers.CCLayer; impor ...
- 在刚接触TI-DM8127-ipnc框架时注意的问题
1. 修改内存分配不成功? 解决方法: 修改内存分配后需要重新编译mcfw.它影响3个核. 如果修改了cmem需要修改boostara. 2. 命令make clean后在make相机跑不起来? 解决 ...
- 初识-----基于Socket的UDP和TCP编程及测试代码
一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...
- strcpy基本用法
C语言标准库函数strcpy,把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间. C语言函数 原型声明:extern char *strcpy(char* dest, co ...
- VHDL之Port map and open
编SPI的master控制器,使用公司基本的元件,有些端口用不着,恰巧好二哥(不知年龄的数字组组长,本名Holger)来了,于是请教之,告曰open关键词.后来深感自己VHDL水平太水,下了一本电子书 ...
- 45个有新意的Photoshop教程和技巧
图形制作者和网页设计师已经准备好迎接新的Adobe Photoshop 教程了.在大家喜欢背后有许多它的理由,诸如Adobe Photoshop很容易操作,学习起来十分简单,但最重要的一点是这款软件能 ...
- DOCTYPE的笔记
平时用HTML5 所以都直接简写doctype <!DOCTYPE html> <html> 从来没考虑这个东西全文是什么 <!DOCTYPE html PUBLIC & ...
- IE 兼容性写法
1. 条件注释语句 <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> <!--[if IE]> 所有的IE ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- HDU ACM 1134 Game of Connections / 1130 How Many Trees?(卡特兰数)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1134 [解题背景]这题不会做,自己推公式推了一段时间,将n=3和n=4的情况列出来了,只发现第n项与 ...