A968 | Sleepy Cow Sorting--Gold
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Farmer John is attempting to sort his $N$ cows ($1 \leq N \leq 10^5$),
conveniently numbered $1 \dots N$, before they head out to the pastures for
breakfast.
Currently, the cows are standing in a line in the order $p_1, p_2, p_3, \dots,
p_N$, and Farmer John is standing in front of cow $p_1$. He wants to reorder
the cows so that they are in the order $1, 2, 3, \dots, N$, with cow $1$ next
to Farmer John.
Today the cows are a bit sleepy, so at any point in time the only cow who is
paying attention to Farmer John's instructions is the cow directly facing
Farmer John. In one time step, he can instruct this cow to move $k$ paces down
the line, for any $k$ between $1$ and $N-1$ inclusive. The $k$ cows whom she
passes will amble forward, making room for her to insert herself in the line
after them.
For example, suppose that $N=4$ and the cows start off in the following order:
FJ: 4, 3, 2, 1
The only cow paying attention to FJ is cow $4$. If he instructs her to move
$2$ paces down the line, the order will subsequently look like this:
FJ: 3, 2, 4, 1
Now the only cow paying attention to FJ is cow $3$, so in the second time step
he may give cow $3$ an instruction, and so forth until the cows are sorted.
Farmer John is eager to complete the sorting, so he can go back to the
farmhouse for his own breakfast. Help him find a sequence of instructions that
sorts the cows in the minimum number of time steps.
conveniently numbered $1 \dots N$, before they head out to the pastures for
breakfast.
Currently, the cows are standing in a line in the order $p_1, p_2, p_3, \dots,
p_N$, and Farmer John is standing in front of cow $p_1$. He wants to reorder
the cows so that they are in the order $1, 2, 3, \dots, N$, with cow $1$ next
to Farmer John.
Today the cows are a bit sleepy, so at any point in time the only cow who is
paying attention to Farmer John's instructions is the cow directly facing
Farmer John. In one time step, he can instruct this cow to move $k$ paces down
the line, for any $k$ between $1$ and $N-1$ inclusive. The $k$ cows whom she
passes will amble forward, making room for her to insert herself in the line
after them.
For example, suppose that $N=4$ and the cows start off in the following order:
FJ: 4, 3, 2, 1
The only cow paying attention to FJ is cow $4$. If he instructs her to move
$2$ paces down the line, the order will subsequently look like this:
FJ: 3, 2, 4, 1
Now the only cow paying attention to FJ is cow $3$, so in the second time step
he may give cow $3$ an instruction, and so forth until the cows are sorted.
Farmer John is eager to complete the sorting, so he can go back to the
farmhouse for his own breakfast. Help him find a sequence of instructions that
sorts the cows in the minimum number of time steps.
输入格式
The first line of input contains $N$. The second line contains $N$ space-
separated integers: $p_1, p_2, p_3, \dots, p_N$, indicating the starting order
of the cows.
separated integers: $p_1, p_2, p_3, \dots, p_N$, indicating the starting order
of the cows.
输出格式
The first line should contain a single integer, $K$, giving the minimum number
of time steps required to sort the cows.
The second line should contain $K$ space-separated integers, $c_1, c_2, \dots,
c_K$, each in the range $1 \ldots N-1$. Furthermore, if in the $i$-th time
step FJ instructs the cow facing him to move $c_i$ paces down the line, then
after $K$ time steps the cows should be in sorted order.
If there are multiple optimal instruction sequences, your program may output
any of them.
of time steps required to sort the cows.
The second line should contain $K$ space-separated integers, $c_1, c_2, \dots,
c_K$, each in the range $1 \ldots N-1$. Furthermore, if in the $i$-th time
step FJ instructs the cow facing him to move $c_i$ paces down the line, then
after $K$ time steps the cows should be in sorted order.
If there are multiple optimal instruction sequences, your program may output
any of them.
输入输出样例
输入 #1
4 1 2 4 3
输出 #1
3 2 2 3
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted