Berstagram CodeForces - 1250A(双端链表)

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).

Examples
Input
3 5
3 2 1 3 3
Output
1 2
2 3
1 3
Input
10 6
7 3 5 7 3 6
Output
1 2
2 3
1 3
4 7
4 5
6 7
5 7
8 8
9 9
10 10
思路:每次进来新消息,都只有两个交换位置,我们可以用双端链表记录他们之间的位置关系,然后开个数组记录他们的位置,然后不断的更新答案就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxm=4e5+100;
const int maxx=1e5+100;
int pre[maxx],nxt[maxx];
int a[maxm],pos[maxx],ansl[maxx],ansr[maxx];
int n,m,x;

inline void init()
{
	for(int i=1;i<=n;i++) pre[i]=i-1,nxt[i]=i+1,pos[i]=i,ansl[i]=i,ansr[i]=i;
}
int main()
{
	scanf("%d%d",&n,&m);
	init();
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&x);
		if(pre[x]!=0)
		{
			int y=pre[x];
			pos[x]--;
			ansl[x]=min(ansl[x],pos[x]);
			pos[y]++;
			ansr[y]=max(ansr[y],pos[y]);
			pre[x]=pre[y];
			pre[nxt[x]]=y;
			nxt[pre[y]]=x;
			nxt[y]=nxt[x];
			pre[y]=x;
			nxt[x]=y;
		}
	}
	for(int i=1;i<=n;i++) cout<<ansl[i]<<" "<<ansr[i]<<endl;
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值