这题怎么用单调队列
查看原帖
这题怎么用单调队列
1025105
liuyuheng0622楼主2025/1/7 16:31

把“点赞”按时间排个序,用1e5个队列存每个帖子的“赞”,每次把过时的“赞”从队首去掉,把新的“赞”入队,算长度,大于等于K就记录一下就行了。

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

struct node
{
	int ts , id ;
} a[100005] ;

int n , d , k ;
bool f[100005] ;
queue <int> q[100005] ;

bool cmp( node x , node y )
{
	return x.ts < y.ts ;
}

int main()
{
	cin >> n >> d >> k ;
	for( int i = 1 ; i <= n ; i ++ )
	{
		cin >> a[i].ts >> a[i].id ;
	}
	sort( a + 1 , a + n + 1 , cmp ) ;
	for( int i = 1 ; i <= n ; i ++ )
	{
		while( q[a[i].id].size() > 0 && q[a[i].id].front() + d - 1 < a[i].ts )
		{
			q[a[i].id].pop() ;
		}
		q[a[i].id].push( a[i].ts ) ;
		if( q[a[i].id].size() >= k )
		{
			f[a[i].id] = true ;
		}
	}
	for( int i = 0 ; i <= 100000 ; i ++ )
	{
		if( f[i] == true )
		{
			cout << i << endl ;
		}
	}
	return 0 ;
}

轻松水过

2025/1/7 16:31
加载中...