A1112 | [COCI-2007_2008-contest2]#6 TURBO
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Frane has been given the task of sorting an array of numbers. The array consists of N integers, each between 1 and N (inclusive), with each of those appearing exactly once in the array. Frane has come up with the following sorting algorithm which operates in N phases, and named it turbosort:
• In the first phase, the number 1 is moved to position 1 by repeatedly swapping consecutive elements.
• In the second phase, the number N is moved to position N in the same manner.
• In the third phase, the number 2 is moved to position
2.
• In the fourth phase, the number N−1 is moved to position N−
1.
• And so on.
In other words, when the number of the phase is odd, Frane will choose the smallest number not yet chosen, and move it to its final position. In even phases he chooses the largest number not yet chosen.
Write a program which, given the initial array, output the number of swaps in each phase of the algorithm.
• In the first phase, the number 1 is moved to position 1 by repeatedly swapping consecutive elements.
• In the second phase, the number N is moved to position N in the same manner.
• In the third phase, the number 2 is moved to position
2.
• In the fourth phase, the number N−1 is moved to position N−
1.
• And so on.
In other words, when the number of the phase is odd, Frane will choose the smallest number not yet chosen, and move it to its final position. In even phases he chooses the largest number not yet chosen.
Write a program which, given the initial array, output the number of swaps in each phase of the algorithm.
输入格式
The first line contains an integer N (1 ≤ N ≤ 100000), the number of elements in the array.
Each of the following N lines contains an integer between 1 and N (inclusive), the array to be sorted.
The array will contain no duplicates.
Each of the following N lines contains an integer between 1 and N (inclusive), the array to be sorted.
The array will contain no duplicates.
输出格式
For each of the N phases, output the number of swaps on a single line.
输入输出样例
输入 #1
3 2 1 3
输出 #1
1 0 0
输入 #2
5 5 4 3 2 1
输出 #2
4 3 2 1 0
输入 #3
7 5 4 3 7 1 2 6
输出 #3
4 2 3 0 2 1 0
In test cases worth 70% of points, N will be less than 100.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted