首页 > 其他 > 详细

序列(sequence)

时间:2021-08-30 11:00:00      阅读:13      评论:0      收藏:0      [点我收藏+]

题目大意

现在有一个长度为 \(n\) 的整数序列 \(a_1,a_2,……,a_n\),接下来依次进行 \(n\) 次操作,其中第 \(i\) 次操作分为以下两步:

  1. \(a_i\) 加到序列 \(b\) 的尾部;
  2. 翻转序列 \(a\)(即 \(a_1\)\(a_2\),……,\(a_i\) 变成 \(a_i\)\(a_{i-1}\),……,\(a_1\))。

请输出 \(n\) 次操作之后序列 \(a\) 会是什么样的,你能帮助他吗?

对于 \(100\ \%\) 的数据,\(1 \leq n \leq 2*10^5\)\(0 \leq a_i \leq 10^9\)

解题思路

对于这种签到题,一般不粗心,都是可以拿到 AC 的。

首先肯定是找规律

\(a\) 数组为 \(1\)\(2\)\(3\),……,\(n\)。(这样是为了找规律,如果序列为其他数都是一个样的)

\(1\) \(1\)
\(2\) \(2,1\)
\(3\) \(3,1,2\)
\(4\) \(4,2,1,3\)
\(5\) \(5,3,1,2,4\)
\(6\) \(6,4,2,1,3,5\)
\(7\) \(7,5,3,1,2,4,6\)
\(8\) \(8,6,4,2,1,3,5,7\)
\(9\) \(9,7,5,3,1,2,4,6,8\)

得:

  • 如果 \(n\) 是奇数,则 \(a=a_n,a_{n-2},a_{n-4}, ... \ ,a_3,a_1,a_2,a_4,a_6, ... \ , a_{n-3},a_{n-1}\)
  • 如果 \(n\) 是偶数,则 \(a=a_n,a_{n-2},a_{n-4}, ... \ ,a_4,a_2,a_1,a_3,a_5, ... \ , a_{n-3},a_{n-1}\)

时间复杂度 \(O(n)\)

AC CODE

代码应该很好理解了。

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

int n;

int a[200007];

signed main()
{
	scanf("%lld", &n);
	for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
	if(n % 2 == 0)
	{
		for(int i = n; i >= 1; i -= 2)
		{
			printf("%lld ", a[i]);
		}
		for(int i = 1; i <= n; i += 2)
		{
			printf("%lld ", a[i]);
		}
	}
	else
	{
		for(int i = n; i >= 1; i -= 2)
		{
			printf("%lld ", a[i]);
		}
		for(int i = 2; i <= n; i += 2)
		{
			printf("%lld " , a[i]);
		}
	}
	return 0;
}

序列(sequence)

原文:https://www.cnblogs.com/orzz/p/15195278.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!