Content

小象有一个序列 \(a_1,a_2,a_3,...,a_n\) (其中 \(a_i=i\))和一个递归函数 \(f(x)\)。\(f(x)\) 的操作如下:

  • 初始时,\(x=n\)。
  • 如果 \(x=1\),退出函数;否则,交换 \(a_{x-1},a_x\),并且递归至 \(f(x-1)\)。

现在,小象想知道执行完函数以后序列的结果。

数据范围:\(1\leqslant n\leqslant 1000\)。

Solution

这道题目由于 \(n\) 很小,我们可以考虑多种做法。

Sol.1 递归暴力法

我们可以直接模拟出像题面中的 \(f(x)\) 那样的递归函数,并且直接按照题面所要求的操作模拟,最后输出结果。

Sol.2 非递归暴力法

我们可以发现,它无疑就是从 \(n\) 循环到 \(2\),每次就交换罢了,因此,我们可以不需要递归,直接用一个循环来一次一次交换就好。

Sol.3 更优的解法

我们还可以考虑出更简单的做法。

我们可以发现,所有的操作完了以后,原来在序列中排在最后的 \(n\) 直接调到了首位,其他的整体往后移了一位。

像这样:

1 2 3 4 5 6 7 8 9 10
操作1:
1 2 3 4 5 6 7 8 10 9
操作2:
1 2 3 4 5 6 7 10 8 9
操作3:
1 2 3 4 5 6 10 7 8 9
操作4:
1 2 3 4 5 10 6 7 8 9
操作5:
1 2 3 4 10 5 6 7 8 9
操作6:
1 2 3 10 4 5 6 7 8 9
操作7:
1 2 10 3 4 5 6 7 8 9
操作8:
1 10 2 3 4 5 6 7 8 9
操作9:
10 1 2 3 4 5 6 7 8 9
结束。

所以,操作完之后的序列就是 \(n,1,2,3,...,n-1\)。

Code

1

#include <cstdio>
#include <algorithm>
using namespace std; int n, a[1007]; void f(int x) {
if(x == 1) return;
swap(a[x - 1], a[x]);
f(x - 1);
} int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) a[i] = i;
f(n);
for(int i = 1; i <= n; ++i) printf("%d ", a[i]);
}

2

#include <cstdio>
#include <algorithm>
using namespace std; int n, a[1007]; int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) a[i] = i;
for(int i = n; i >= 2; --i) swap(a[i - 1], a[i]);
for(int i = 1; i <= n; ++i) printf("%d ", a[i]);
}

3

#include <cstdio>
#include <algorithm>
using namespace std; int n, a[1007]; int main() {
scanf("%d", &n);
printf("%d", n);
for(int i = 1; i < n; ++i) printf(" %d", i);
}

CF221A Little Elephant and Function 题解的更多相关文章

  1. Codeforces 221 A. Little Elephant and Function

    A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...

  2. AC日记——Little Elephant and Function codeforces 221a

    A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...

  3. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  4. codechef Little Elephant and Bombs题解

    The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...

  5. Function题解

    这个题最优策略一定是向左上走到某一列再往上一直走. n*q在线暴力可做,离线按y排序,单调栈维护凸壳. 具体来说:对于i<j若A[i]>A[j] 即j的斜率小而且纵截距小,一定比i优,并且 ...

  6. CF205A Little Elephant and Rozdil 题解

    Content 有一头小象住在 \(\texttt{Rozdil}\) 小镇里,它想去其他的小镇旅行. 这个国家一共有 \(n\) 个小镇,第 \(i\) 个小镇距离 \(\texttt{Rozdil ...

  7. LuoguP7127 「RdOI R1」一次函数(function) 题解

    Content 设 \(S_k\) 为直线 \(f(x)=kx+k-1\),直线 \(f(x)=(k+1)x+k\) 与 \(x\) 轴围成的三角形的面积.现在给出 \(t\) 组询问,每组询问给定一 ...

  8. Leetcode-237 Delete Node in a Linked List

    #237.    Delete Node in a Linked List Write a function to delete a node (except the tail) in a singl ...

  9. LeetCode Basic Calculator II

    原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...

随机推荐

  1. 【Azure 环境】在Windows环境中抓取网络包(netsh trace)后,如何转换为Wireshark格式以便进行分析

    问题描述 如何在Windows环境中,不安装第三方软件的情况下(使用Windows内置指令),如何抓取网络包呢?并且如何转换为Wireshark 格式呢? 操作步骤 1) 以管理员模式打开CMD,使用 ...

  2. 353 stars Java项目!Java小白必看!austin介绍 【第一话】

    有好几个群友问我为什么最近更新变慢了.工作忙是一方面,另一方面是我更新文章的动力确实下降了.近大半年一直在更新的<对线面试官>系列,到现在已经40篇了. 说实话,当时我更新该系列有很大一部 ...

  3. CF1539F Strange Array

    这玩意为啥是紫. 考虑对每个小于\(x\)的数值设为1,大于\(x\)的数值设为-1. 那么对于答案要求的就是绝对值最大的连续段. 线段树是很显然的. 考虑我们不能对每个数都进行一遍重构,这样就退化到 ...

  4. 洛谷 P3266 - [JLOI2015]骗我呢(容斥原理+组合数学)

    题面传送门 神仙题. 首先乍一看此题非常棘手,不过注意到有一个条件 \(0\le x_{i,j}\le m\),而整个矩阵恰好有 \(m\) 列,这就启发我们考虑将每个元素的上下界求出来,如果我们第一 ...

  5. 【豆科基因组】大豆(Soybean, Glycine max)泛基因组2020Cell

    目录 一.前沿概述 二.主要结果 重测序.组装与注释 泛基因组 SV特征 PAV与古多倍化,WGD事件 基因SV与基因融合 SV与大豆驯化 SV影响基因表达及其与性状关联 一.前沿概述 Pan-Gen ...

  6. Perl语言入门10-13

    ----------第十章 其他控制结构---------------- unless结构 unless($fred =~ /\A[A-Z_\w*\z]/i){print "yes" ...

  7. 【Perl示例】整合多个文件

    这个需求是在生信分析中几乎天天用到,各种语言都能实现,也都各有特点.这次以perl为例. 已知 文件CT-VS-CON.All.xls为全部蛋白表达矩阵及其差异分析结果. 文件Homo_sapiens ...

  8. C语言 fastq文件转换为fasta文件2

    修改可读取压缩格式文件 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #in ...

  9. 6. Reverse Linked List 逆转单链表

    逆转单链表,比较简单,不细讲,扫描依次改变指针指向. class Solution { public: ListNode* reverseList(ListNode* head) { if(head= ...

  10. 非标准的xml解析器的C++实现:一、思考基本数据结构的设计

    前言: 我在C++项目中使用xml作为本地简易数据管理,到目前为止有5年时间了,从最初的全文搜索标签首尾,直到目前项目中实际运用的类库细致到已经基本符合w3c标准,我一共写过3次解析器,我自己并没有多 ...