A926 | The Lost Cow--Bronze
来源USACO
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Farmer John has lost his prize cow Bessie, and he needs to find her!
Fortunately, there is only one long path running across the farm, and Farmer
John knows that Bessie has to be at some location on this path. If we think of
the path as a number line, then Farmer John is currently at position $x$ and
Bessie is currently at position $y$ (unknown to Farmer John). If Farmer John
only knew where Bessie was located, he could walk directly to her, traveling a
distance of $|x - y|$. Unfortunately, it is dark outside and Farmer John can't
see anything. The only way he can find Bessie is to walk back and forth until
he eventually reaches her position.
Trying to figure out the best strategy for walking back and forth in his
search, Farmer John consults the computer science research literature and is
somewhat amused to find that this exact problem has not only been studied by
computer scientists in the past, but that it is actually called the "Lost Cow
Problem" (this is actually true!).
The recommended solution for Farmer John to find Bessie is to move to position
$x+1$, then reverse direction and move to position $x-2$, then to position
$x+4$, and so on, in a "zig zag" pattern, each step moving twice as far from
his initial starting position as before. As he has read during his study of
algorithms for solving the lost cow problem, this approach guarantees that he
will at worst travel 9 times the direct distance $|x-y|$ between himself and
Bessie before he finds her (this is also true, and the factor of 9 is actually
the smallest such worst case guarantee any strategy can achieve).
Farmer John is curious to verify this result. Given $x$ and $y$, please
compute the total distance he will travel according to the zig-zag search
strategy above until he finds Bessie.
Fortunately, there is only one long path running across the farm, and Farmer
John knows that Bessie has to be at some location on this path. If we think of
the path as a number line, then Farmer John is currently at position $x$ and
Bessie is currently at position $y$ (unknown to Farmer John). If Farmer John
only knew where Bessie was located, he could walk directly to her, traveling a
distance of $|x - y|$. Unfortunately, it is dark outside and Farmer John can't
see anything. The only way he can find Bessie is to walk back and forth until
he eventually reaches her position.
Trying to figure out the best strategy for walking back and forth in his
search, Farmer John consults the computer science research literature and is
somewhat amused to find that this exact problem has not only been studied by
computer scientists in the past, but that it is actually called the "Lost Cow
Problem" (this is actually true!).
The recommended solution for Farmer John to find Bessie is to move to position
$x+1$, then reverse direction and move to position $x-2$, then to position
$x+4$, and so on, in a "zig zag" pattern, each step moving twice as far from
his initial starting position as before. As he has read during his study of
algorithms for solving the lost cow problem, this approach guarantees that he
will at worst travel 9 times the direct distance $|x-y|$ between himself and
Bessie before he finds her (this is also true, and the factor of 9 is actually
the smallest such worst case guarantee any strategy can achieve).
Farmer John is curious to verify this result. Given $x$ and $y$, please
compute the total distance he will travel according to the zig-zag search
strategy above until he finds Bessie.
输入格式
The single line of input contains two distinct space-separated integers $x$
and $y$. Both are in the range $0 \ldots 1,000$.
and $y$. Both are in the range $0 \ldots 1,000$.
输出格式
Print one line of output, containing the distance Farmer John will travel to
reach Bessie.
reach Bessie.
输入输出样例
输入 #1
3 6
输出 #1
9
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted