http://codeforces.com/problemset/problem/1250/A
Polycarp recently signed up to a new social network Berstagram. He immediately published n posts there. He assigned numbers from 1 to n to all posts and published them one by one. So, just after publishing Polycarp’s news feed contained posts from 1 to n — the highest post had number 1, the next one had number 2, …, the lowest post had number n.
After that he wrote down all likes from his friends. Likes were coming consecutively from the 1-st one till the m-th one. You are given a sequence a1,a2,…,am (1≤aj≤n), where aj is the post that received the j-th like.
News feed in Berstagram works in the following manner. Let’s assume the j-th like was given to post aj. If this post is not the highest (first) one then it changes its position with the one above. If aj is the highest post nothing changes.
For example, if n=3, m=5 and a=[3,2,1,3,3], then Polycarp’s news feed had the following states:
before the first like: [1,2,3];
after the first like: [1,3,2];
after the second like: [1,2,3];
after the third like: [1,2,3];
after the fourth like: [1,3,2];
after the fifth like: [3,1,2].
Polycarp wants to know the highest (minimum) and the lowest (maximum) positions for each post. Polycarp considers all moments of time, including the moment “before all likes”.
Input
The first line contains two integer numbers n and m (1≤n≤105, 1≤m≤4⋅105) — number of posts and number of likes.
The second line contains integers a1,a2,…,am (1≤aj≤n), where aj is the post that received the j-th like.
Output
Print n pairs of integer numbers. The i-th line should contain the highest (minimum) and the lowest (maximum) positions of the i-th post. You should take into account positions at all moments of time: before all likes, after each like and after all likes. Positions are numbered from 1 (highest) to n (lowest).
题目大意: n n n张海报,初始时它们的编号和排名分别为 1 − n 1-n 1−n,现在有 m m m个数, m i m_i mi表示给编号为 m i m_i mi的海报点赞,此时如果这张海报的排名是 1 1 1,则没有任何变化产生,否则把这张海报的排名与它前一张海报的排名交换(其实就是排名上升了 1 1 1)。最后请输出 n n n张海报的最靠前的排名和最靠后的排名。
思路:模拟就行了,开四个数组, a [ i ] a[i] a[i]表示第排名为 i i i的海报的编号, i d [ i ] id[i] id[i]表示编号为 i i i的海报的排名, l [ i ] l[i] l[i]表示编号为 i i i的海报的最靠前的排名, r [ i ] r[i] r[i]表示编号为 i i i的海报的最靠后的排名。
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int a[maxn],id[maxn],l[maxn],r[maxn];
int n,m;
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
a[i]=id[i]=l[i]=r[i]=i;
while(m--)
{
int q;
scanf("%d",&q);
int pos=id[q];
if(pos==1)
continue;
int pre=a[pos-1];
swap(id[q],id[pre]);
swap(a[pos],a[pos-1]);
l[q]=min(l[q],id[q]);
r[q]=max(r[q],id[q]);
l[pre]=min(l[pre],id[pre]);
r[pre]=max(r[pre],id[pre]);
}
for(int i=1;i<=n;i++)
printf("%d %d\n",l[i],r[i]);
return 0;
}