跳到正文

题解

题解:P11463 N角进攻

xiaowenxu_qwq约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年12月28日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟
题目
P11463

版本

  1. v1当前

我们来枚举一下,当传球次数达到 2n2n 次时,队列回到原样,样例二验算一下就会发现是正确的。

根据这条思路我们就可以将 kk 对 2n2n 取模,剩下的部分考虑枚举,用双端队列分别维护左边和右边,最后按照题目进行模拟。

代码 O(n)O(n)

C++
#include<bits/stdc++.h>
using namespace std;
deque<int> q1,q2;
long long k,n,m,ans;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		q1.clear();q2.clear();
		scanf("%lld%lld%lld",&n,&m,&k);
		k%=(n*2),ans=n/2+1;
		for(int i=1;i<=n/2;i++) q1.push_back(i);
		for(int i=n/2+2;i<=n;i++) q2.push_back(i);//初始化队列
		for(int i=1;i<=k;i++)
		{
			if(m)
			{
				q2.push_back(ans);
				ans=q2.front();
				q2.pop_front();
				m=0;
			}
			else
			{
				q1.push_front(ans);
				ans=q1.back();
				q1.pop_back();
				m=1;
			}
		}
		while(!q1.empty())
		{
			printf("%d ",q1.front());
			q1.pop_front();
		}
		printf("%lld ",ans);
		while(!q2.empty())
		{
			printf("%d ",q2.front());
			q2.pop_front();
		}
		printf("\n");//记得加换行 
	}
	return 0;
}
原文