跳到正文

题解

题解:P14977 [USACO26JAN1] Lineup Queries S

DeaphetS约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年1月19日
收录
2026年9月8日
更新
2026年11月17日
篇幅
约 5 分钟
获赞
2
题目
P14977

版本

  1. v1当前

有一个初始为空 0-index 的序列 aa,在每一时刻 tt,序列会进行如下两次变化:

  • 若 t>0t\gt 0,将区间 [0,⌊t2⌋][0,\lfloor\frac{t}{2} \rfloor] 循环左移;
  • push_back 一个元素 tt。

QQ 次询问,两种类型:

  • 询问时刻 tt 时,数字 cc 在什么位置;
  • 询问时刻 tt 时,位置 xx 的数字是多少

Sol

神秘找规律题。

首先打表。

代码
t = 0 | 0
t = 1 | 0  1
t = 2 | 1  0  2
t = 3 | 0  1  2  3
t = 4 | 1  2  0  3  4
t = 5 | 2  0  1  3  4  5
t = 6 | 0  1  3  2  4  5  6
t = 7 | 1  3  2  0  4  5  6  7
t = 8 | 3  2  0  4  1  5  6  7  8
t = 9 | 2  0  4  1  3  5  6  7  8  9
t = 10 | 0  4  1  3  5  2  6  7  8  9  10
t = 11 | 4  1  3  5  2  0  6  7  8  9  10  11
t = 12 | 1  3  5  2  0  6  4  7  8  9  10  11  12
t = 13 | 3  5  2  0  6  4  1  7  8  9  10  11  12  13
t = 14 | 5  2  0  6  4  1  7  3  8  9  10  11  12  13  14
t = 15 | 2  0  6  4  1  7  3  5  8  9  10  11  12  13  14  15
t = 16 | 0  6  4  1  7  3  5  8  2  9  10  11  12  13  14  15  16
t = 17 | 6  4  1  7  3  5  8  2  0  9  10  11  12  13  14  15  16  17
t = 18 | 4  1  7  3  5  8  2  0  9  6  10  11  12  13  14  15  16  17  18
t = 19 | 1  7  3  5  8  2  0  9  6  4  10  11  12  13  14  15  16  17  18  19
t = 20 | 7  3  5  8  2  0  9  6  4  10  1  11  12  13  14  15  16  17  18  19  20

不难看出每个数字都是不断的重复左移然后重置,大致观察(或计算)一下发现每次重置后下一轮左移的步数会乘上大约 1.51.5,所以可以直接一轮轮模拟。第一问是可以直接这样做完的。

对于第二问,可以观察第一列的数字,发现每相邻三行一定是一个第一轮移动的数字和两个已经至少在第二轮的数字,而非第一轮的数字可以找规律发现上一轮的终点在哪,之后直接模拟即可。

Code

C++
#include<bits/stdc++.h>
using namespace std;
#define N 100010
#define LL long long
int a[N];
int main()
{
	/*
	for(int t=0;t<=30;t++){
		int tmp=a[0];
		for(int i=0;i<t/2;i++)
			a[i]=a[i+1];
		a[t/2]=tmp;
		a[t]=t;
		printf("t = %d | ",t);
		for(int i=0;i<=t;i++)printf("%d  ",a[i]);
		printf("\n");
	}
	*/
	int q;
	scanf("%d",&q);
	while(q--){
		int o;
		LL c,x,t;
		scanf("%d",&o);
		if(o==1){
			scanf("%lld%lld",&c,&t);
			if(t<2ll*c){
				printf("%lld\n",c);
				continue;
			}
			LL curp=c,curt=2ll*c-1,nxt=curt+curp+1;
			while(nxt<=t){
//				cout<<curp<<" "<<curt<<" "<<nxt<<endl;
				curp=nxt/2,curt=nxt;
				nxt=curt+curp+1;
			}
			printf("%lld\n",curp-(t-curt));
		}
		else{
			scanf("%lld%lld",&x,&t);
			if(2ll*x>t){
				printf("%lld\n",x);
				continue;
			}
			t+=x,x=0;
			while(t%3!=2 && t>1){
				LL o=t%3;
				t/=3,t*=2;
				t--,t+=o;
			}
			printf("%lld\n",(t+1)/3);
			
			//(x+c)=floor((t-c)/2)
			//2*(x+c)=t-c,2*(x+c)+1=t-c
			//3c=t-2x,3c=t-2x-1
			//t=20 x=1 c=3
		}
	}
}

代码里保留了之前的打表部分,以及对第二问的另一种尝试。之前是打算从每个位置移动到对应段的右上角去列了个方程,结果解了一下发现有可能无解,借此讨论无解情况也能发现跟模三有关的规律。

原文