CF221A Little Elephant and Function 题解
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 题解的更多相关文章
- 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 ...
- AC日记——Little Elephant and Function codeforces 221a
A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...
- codechef Little Elephant and Permutations题解
The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...
- codechef Little Elephant and Bombs题解
The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...
- Function题解
这个题最优策略一定是向左上走到某一列再往上一直走. n*q在线暴力可做,离线按y排序,单调栈维护凸壳. 具体来说:对于i<j若A[i]>A[j] 即j的斜率小而且纵截距小,一定比i优,并且 ...
- CF205A Little Elephant and Rozdil 题解
Content 有一头小象住在 \(\texttt{Rozdil}\) 小镇里,它想去其他的小镇旅行. 这个国家一共有 \(n\) 个小镇,第 \(i\) 个小镇距离 \(\texttt{Rozdil ...
- LuoguP7127 「RdOI R1」一次函数(function) 题解
Content 设 \(S_k\) 为直线 \(f(x)=kx+k-1\),直线 \(f(x)=(k+1)x+k\) 与 \(x\) 轴围成的三角形的面积.现在给出 \(t\) 组询问,每组询问给定一 ...
- 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 ...
- LeetCode Basic Calculator II
原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...
随机推荐
- Study Blazor .NET(三)组件
翻译自:Study Blazor .NET,转载请注明. 关于组件 blazor中组件的基础结构可以分为以下3部分, //Counter.razor //Directives section @pag ...
- 从零开始学Kotlin第四课
面向对象: //妹子 性格 声音 class Girl(var chactor:String,var voice:String) fun main(args: Array<String>) ...
- Java设计模式之(十一)——享元模式
1.什么是享元模式? Use sharing to support large numbers of fine-grained objects efficiently. 享元模式(Flyweight ...
- Node.js实现前后端交互——用户注册
我之前写过一篇关于使用Node.js作为后端实现用户登陆的功能,现在再写一下node.js做后端实现简单的用户注册实例吧.另外需要说的是,上次有大佬提醒需要加密数据传输,不应该使用明文传输用户信息.在 ...
- 关闭 IDEA 自动更新
关闭 IDEA 的自动检查更新(截图idea 2020 2.x) idea 右下角会有这样的更新提示 2. 关闭 idea 自动检查更新 取消勾选 Automatically check update ...
- Python基础笔记3
高级特性 代码不是越多越好,而是越少越好.代码不是越复杂越好,而是越简单越好.代码越少,开发效率越高. 1.切片 切片(Slice)操作符,取一个list或tuple的部分元素非常常见. 列表 L = ...
- nordic 51822 sdk. timer 的使用
它的源代码和头文件分别为app_timer.c/app_timer.h.这是Nordic为我们提供的虚拟定时器,这个定时器不同于硬件上的TIMER,而是基于RTC1实现的一种虚拟定时器,其将定时功能作 ...
- 普通用户iptables规则持久化,开机自动恢复
本文档对于docker环境下并不适用,docker环境的iptables持久化请参考https://www.cnblogs.com/wiseo/p/15000745.html 添加iptables规则 ...
- 『学了就忘』Linux文件系统管理 — 63、磁盘配额介绍
目录 1.磁盘配额概念 2.磁盘配额条件 3.磁盘配额的相关概念 4.磁盘配额实践规划 1.磁盘配额概念 磁盘配额是限制用户或者用户组在一个分区上可以使用的空间大小和文件个数的限制. 扩展: 管理员可 ...
- JVM结构详解
JVM 结构详解 JVM 结构图 程序计数器(PC 寄存器) 程序计数器的定义 程序计数器是一块较小的内存空间,是当前线程正在执行的那条字节码指令的地址.若当前线程正在执行的是一个本地方法,那么此时程 ...