当前位置:网站首页>困牛排序(寒假每日一题 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.
边栏推荐
- IPC机制
- 弹性蛋白酶中英文说明书
- [leetcode] 11. Container with the most water
- Assembly language (3) 16 bit assembly basic framework and addition and subtraction loop
- WinXP内核驱动调试
- Some Modest Advice for Graduate Students - by Stephen C. Stearns, Ph.D.
- 现状分析:“一云多芯”如何推动信创项目快速部署
- Tencent cloud wecity Hello 2022!
- ICML2022 | 用神经控制微分方程建立反事实结果的连续时间模型
- 结合实操带你吃透Redis持久化
猜你喜欢

How to prepare for the last day of tomorrow's exam? Complete compilation of the introduction to the second building test site

Reading notes at night -- deep into virtual function

Icml2022 | establishing a continuous time model of counterfactual results using neural control differential equations

数组中关于sizeof()和strlen

Bi SQL constraints
![Experiment 5 8254 timing / counter application experiment [microcomputer principle] [experiment]](/img/e2/7da59a566e4ccb8e43f2a64c0420e7.png)
Experiment 5 8254 timing / counter application experiment [microcomputer principle] [experiment]

(CVPR 2020) Learning Object Bounding Boxes for 3D Instance Segmentation on Point Clouds

JS array object to object

Use redis' sorted set to make weekly hot Reviews

After integrating the aurora push plug-in in the uni app, the real machine debugging prompts "the currently running base does not contain the native plug-in [jg-jpush]..." Solutions to problems
随机推荐
Notes on BGP part of CCNP
js数组对象转对象
Hands on data analysis data modeling and model evaluation
Application session coverage solutions with different ports on the same server
Reverse ordinal number by merge sort
Install mysql5.6 under linux64bit - the root password cannot be modified
An Chaoyun: "one cloud with multiple cores" supports the implementation of the national information innovation government cloud
PMP考试“临门一脚”如何踢得漂亮?
股票开账户如何优惠开户?手机开户是安全么?
AssertionError: CUDA unavailable, invalid device 0 requested
2个NPN三极管组成的恒流电路
年已过半,年终立的Flag实现了几个?
Texture enhancement
你知道你的ABC吗(春季每日一题 1)
uni-app集成极光推送插件后真机调试提示“当前运行的基座不包含原生插件[JG-JPush]...”问题的解决办法
Chinese and English instructions of Papain
Unity C # e-learning (VI) -- FTP (I)
JS array object to object
Ps5 connected to oppo K9 TV does not support 2160p/4k
屡获大奖的界面控件开发包DevExpress v22.1官宣发布