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 ...
随机推荐
- java 必应壁纸批量下载
基于java 必应壁纸批量下载 - rookie丶k - 博客园 (cnblogs.com)实现 上面代码运行本地有点小问题,改了改 1.ssl验证 2.请求头 3.需要优化下载速度,多线程方式(还不 ...
- Python技法1:变长和定长序列拆分
Python中的任何序列(可迭代的对象)都可以通过赋值操作进行拆分,包括但不限于元组.列表.字符串.文件.迭代器.生成器等. 元组拆分 元组拆分是最为常见的一种拆分,示例如下: p = (4, 5) ...
- Topcoder 12519 ScotlandYard(点对 dp+最长路)
题面传送门 题意: 有两个人 A 和 B 玩一个游戏.游戏规则大致是这样的: 有 \(n\) 个城市和三种交通工具公交.地铁和出租车. 给出三个 \(n\times n\) 的字符矩阵 \(b,m,t ...
- P7708「Wdsr-2.7」八云蓝自动机 Ⅰ
*X. P7708「Wdsr-2.7」八云蓝自动机 Ⅰ. 摘自 分治与根号数据结构学习笔记 第三部分 莫队 例题 X.. 一道莫队好题.私以为本题最有价值的地方在于对单点修改的转化以及对交换两个数的处 ...
- R shinydashboard ——1. 基本用法
shiny和shinydashboard使用虽然简单,但控件众多,需及时总结归纳. install.packages("shinydashboard") shinydashboar ...
- 金蝶EAS——我的EAS报销流程怎么能让另一个人看到呢?即如何设置流程传阅功能?设置“代理报销”
代理的话只能看到被代理人能看到的流程.设置"代理报销":应用--财务会计--费用管理--代理报销 选择报销人公司--"他人代理我报销"--选择报销人(zhaof ...
- python—模拟生成双色球号
双色球规则:"双色球"每注投注号码由6个红色球号码和1个蓝色球号码组成.红色球号码从1--33中不重复选择:蓝色球号码从1--16中选择. # -*- coding:UTF-8 - ...
- SQL-增、删、改操作
#查看表 select * from `竟企区域数据分析` #在表第一列新增名为"年月"的列alter table `竟企区域数据分析` add column 年月 varchar ...
- KeepAlived双主模式高可用集群
keepalived是vrrp协议的实现,原生设计目的是为了高可用ipvs服务,keepalived能够配置文件中的定义生成ipvs规则,并能够对各RS的健康状态进行检测:通过共用的虚拟IP地址对外提 ...
- Sharding-JDBC 实现水平分库分表
1.需求分析