当前位置:网站首页>困牛排序(寒假每日一题 40)
困牛排序(寒假每日一题 40)
2022-06-24 21:50:00 【51CTO】
Farmer John 正在尝试将他的 头奶牛,方便起见编号为
,在她们前往牧草地吃早餐之前排好顺序。
当前,这些奶牛以 的顺序排成一行,Farmer John 站在奶牛
前面。
他想要重新排列这些奶牛,使得她们的顺序变为 ,奶牛
在 Farmer John 旁边。
今天奶牛们有些困倦,所以任何时刻都只有直接面向 Farmer John 的奶牛会注意听 Farmer John 的指令。
每一次他可以命令这头奶牛沿着队伍向后移动 步,
可以是范围
中的任意数。
她经过的 头奶牛会向前移动,腾出空间使得她能够插入到队伍中这些奶牛之后的位置。
例如,假设 ,奶牛们开始时是这样的顺序:
FJ: 4, 3, 2, 1
唯一注意 FJ 指令的奶牛是奶牛 。
当他命令她向队伍后移动 步之后,队伍的顺序会变成:
FJ: 3, 2, 4, 1
现在唯一注意 FJ 指令的奶牛是奶牛 ,所以第二次他可以给奶牛
下命令,如此进行直到奶牛们排好了顺序。
Farmer John 急欲完成排序,这样他就可以回到他的农舍里享用他自己的早餐了。
请帮助他求出将奶牛们排好顺序所需要的最小操作次数。
输入格式
输入的第一行包含 。
第二行包含 个空格分隔的整数,
,表示奶牛们的起始顺序。
输出格式
输出一个整数,为 Farmer John 采用最佳策略可以将这 头奶牛排好顺序所需要的操作次数。
数据范围
输入样例:
4
1 2 4 3
输出样例:
3
using
namespace
std;
const
int
N
=
110;
int
n;
int
a[
N];
int
main(){
scanf(
"%d",
&
n);
for(
int
i
=
1;
i
<=
n;
i
++)
scanf(
"%d",
&
a[
i]);
a[
0]
=
n
+
1;
for(
int
i
=
n;
i
>=
1;
i
--)
if(
a[
i]
<
a[
i
-
1]){
printf(
"%d",
i
-
1);
break;
}
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
边栏推荐
猜你喜欢
![全排列II[存在相同元素去重 + 标准回溯]](/img/d3/93ddb49e580be60be4f056f141b782.png)
全排列II[存在相同元素去重 + 标准回溯]

天书夜读笔记——深入虚函数virtual

Numerical scheme simulation of forward stochastic differential equations with Markov Switching

创新药二级市场审饼疲劳:三期临床成功、产品获批也不管用了

PS5连接OPPO K9电视不支持2160P/4K

Boutique enterprise class powerbi application pipeline deployment
![Experiment 5 8254 timing / counter application experiment [microcomputer principle] [experiment]](/img/e2/7da59a566e4ccb8e43f2a64c0420e7.png)
Experiment 5 8254 timing / counter application experiment [microcomputer principle] [experiment]

JVM directive
![最长连续序列[扩散法+空间换时间]](/img/db/7b0d1b0db7015e887340723505153a.png)
最长连续序列[扩散法+空间换时间]

Icml2022 | establishing a continuous time model of counterfactual results using neural control differential equations
随机推荐
现状分析:“一云多芯”如何推动信创项目快速部署
"One good programmer is worth five ordinary programmers!"
修身励学篇
Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
结合实操带你吃透Redis持久化
弹性蛋白酶中英文说明书
同一服务器两个端口不同的应用session覆盖解决方案
Redis and jedis
MPLS notes Part 1
Fatigue liée à l'examen du marché secondaire des médicaments innovants: succès clinique de la phase III et approbation du produit
(CVPR 2020) Learning Object Bounding Boxes for 3D Instance Segmentation on Point Clouds
广发期货安全吗?开户需要什么东西?
Ps5 connected to oppo K9 TV does not support 2160p/4k
第04天-文件IO
Abnova丨CSV 磁珠中英文说明
Bi-sql create
Numerical scheme simulation of forward stochastic differential equations with Markov Switching
write a number of lines to a new file in vim
AutoCAD - two extension modes
php easywechat 和 小程序 实现 长久订阅消息推送