E. Messenger Simulator--------思维/树状数组

Polycarp is a frequent user of the very popular messenger. He’s chatting with his friends all the time. He has n friends, numbered from 1 to n.

Recall that a permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array.

So his recent chat list can be represented with a permutation p of size n. p1 is the most recent friend Polycarp talked to, p2 is the second most recent and so on.

Initially, Polycarp’s recent chat list p looks like 1,2,…,n (in other words, it is an identity permutation).

After that he receives m messages, the j-th message comes from the friend aj. And that causes friend aj to move to the first position in a permutation, shifting everyone between the first position and the current position of aj by 1. Note that if the friend aj is in the first position already then nothing happens.

For example, let the recent chat list be p=[4,1,5,3,2]:

if he gets messaged by friend 3, then p becomes [3,4,1,5,2];
if he gets messaged by friend 4, then p doesn’t change [4,1,5,3,2];
if he gets messaged by friend 2, then p becomes [2,4,1,5,3].
For each friend consider all position he has been at in the beginning and after receiving each message. Polycarp wants to know what were the minimum and the maximum positions.

Input
The first line contains two integers n and m (1≤n,m≤3⋅105) — the number of Polycarp’s friends and the number of received messages, respectively.

The second line contains m integers a1,a2,…,am (1≤ai≤n) — the descriptions of the received messages.

Output
Print n pairs of integers. For each friend output the minimum and the maximum positions he has been in the beginning and after receiving each message.

Examples
inputCopy

5 4
3 5 1 4
outputCopy
1 3
2 5
1 4
1 5
1 5
inputCopy
4 3
1 2 4
outputCopy
1 3
1 2
3 4
1 4
Note
In the first example, Polycarp’s recent chat list looks like this:

[1,2,3,4,5]
[3,1,2,4,5]
[5,3,1,2,4]
[1,5,3,2,4]
[4,1,5,3,2]
So, for example, the positions of the friend 2 are 2,3,4,4,5, respectively. Out of these 2 is the minimum one and 5 is the maximum one. Thus, the answer for the friend 2 is a pair (2,5).

In the second example, Polycarp’s recent chat list looks like this:

[1,2,3,4]
[1,2,3,4]
[2,1,3,4]
[4,2,1,3]

题意:给n个数,m次操作
初始序列为:1,2,3,4,5.....n;
每次操作把一个数x移到第一位,其他数往后移。
问1---n个数,每个数在最左边的位置和在最右边的位置。


解法:我们可以在n个数前面填加m个空。
我们先初始化每个数的最左边的位置和最右边的位置都等于自己的下标,l[x]=r[x]=i。
进行第一个操作x,那么x的最左边位置一定为1,r[i]=max(x前面有多少个数,r[i])。
删去该位置的值,把x放在m个空的第m个位置上(继续往下就是m-1,m-2,m-3这些位置上)。
不断的进行m次操作。
最后重新遍历一遍更新r[i],因为有的数没有出现在操作上,而且相对位置发生了改变,所以需要更新一下。

比如:n=3,m=2
0,0,1,2,3

当x=2,先计算2前面有2个数,在比较r[i]=max(2,r[i]),然后删除该位置的数,把x放到m个空的第m个位置就是0,2,1,0,3;

当x=3,先计算3前面有3个数,在比较r[i]=max(3,r[i]),然后删除该位置的数,把x放到m个空的第m-1个位置,就是3,2,1,0,0;

然后重新遍历一遍,更新所有的r[i]。例如1不在操作里面,r[i]=1。但经过m次操作后,1最右边的位置为3了,所以需要更新一遍。


#include<bits/stdc++.h>
using namespace std;
const int N=6e5+1000;
int c[N],pos[N],x,l[N],r[N];
int a[N];
int n,m;
int lowbit(int x)
{
	return x&(-x); 
} 
void add(int i,int x)
{
	while(i<N)
	{
		c[i]+=x;
		i+=lowbit(i);
	}
}
int sum(int i)
{
	int res=0;
	while(i)
	{
		res+=c[i];
		i-=lowbit(i);
	}
	return res;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		l[i]=r[i]=i;
		add(i+m,1);
		pos[i]=i+m;
	}
	int last=m;
	while(m--)
	{
		cin>>x;
		l[x]=1;
		int res=sum(pos[x]); //当前位置前面有多少个数
		r[x]=max(r[x],res); 
		if(res==1) continue; //说明就在第一个
		add(pos[x],-1); //删去这个位置
		pos[x]=last; //放在前面补充的m个位置的最后一位
		add(pos[x],1); //然后插入这个值
		last--; 
	}
	for(int i=1;i<=n;i++)
	{
		r[i]=max(r[i],sum(pos[i])); //更新答案
	}
	for(int i=1;i<=n;i++)
	{
		cout<<l[i]<<" "<<r[i]<<endl;
	}
	return 0;
}


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值