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).
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
const int k=1e5+10;
int t;
int a[k],id[k],maxx[k],minn[k];
int main()
{
int n,m,i;
scanf("%d",&n);
scanf("%d",&m);
for(i=1; i<=n; i++)
{
a[i]=i;
id[i]=i;
maxx[i]=i;
minn[i]=i;
}
for(i=1; i<=m; i++)
{
scanf("%d",&t);
int w=id[t];
int change;
if(w!=1)
{
int r=a[w-1];
change=id[t];
id[t]=id[r];
id[r]=change;
change=a[w];
a[w]=a[w-1];
a[w-1]=change;
maxx[t]=max(maxx[t],id[t]);
minn[t]=min(minn[t],id[t]);
maxx[r]=max(maxx[r],id[r]);
minn[r]=min(minn[r],id[r]);
}
}
for(i=1; i<=n; i++)
{
printf("%d %d\n",minn[i],maxx[i]);
}
return 0;
}
本文介绍了一个关于社交网络Berstagram中点赞后帖子排序变化的问题,并提供了一段C++代码实现,该算法通过记录每个帖子最高和最低位置来跟踪其在新闻流中的变化。
424

被折叠的 条评论
为什么被折叠?



