JZOJ2133.2017.05.20【usaco2017_Mar Bronze & Silver】C组T1The Lost Cow

来源:互联网 发布:桶包 知乎 编辑:程序博客网 时间:2024/06/05 19:30

题目描述

    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 xand 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 xand 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…1,000. 

输出

  Print one line of output, containing the distance Farmer John will travel to reach Bessie. 

样例输入

3 6

样例输出

9

数据范围限制

译文:
题目描述
农民约翰失去了他的奶牛Bessie,他需要找到她!
幸运的是,整个农场只有一条长长的小路,农民约翰知道Bessie必须在这条路上的某个位置。如果我们认为的路径作为一个数字,然后农民约翰目前的位置和Bessie目前位置Y(未知的农夫约翰)。如果农民约翰只知道Bessie的位置,他可以直接走到她,行驶距离|−| X Y。不幸的是,外面很黑,农民约翰什么也看不见。他能找到Bessie的唯一办法就是来回走,直到他最终到达她的位置。
试图找出来回走在他的搜索的最佳策略,农民约翰咨询计算机科学研究文献,发现这样的问题不仅是由过去的计算机科学家研究了有点好笑,但它实际上是被称为“迷失的奶牛问题”(这是真的!)。
农民约翰找Bessie推荐的解决方案是移动位置x + 1,然后反方向移动到的位置−2,然后位置x + 4,等等,在一个“Z”的模式,每一步移动两倍远从他的起始位置之前。当他读了他的研究算法在求解丢牛的问题,这种方法保证他将至少9次旅行的直接距离| X−Y |自己和Bessie之间才找到她(这也是真的,和9的系数实际上是最小的最坏的情况下保证任何策略可以实现)。
农民约翰好奇地验证这个结果。给定的x和y,请计算总的距离,他会根据锯齿形搜索策略以上直到他找到Bessie。
输入
输入的单行包含两个不同的空间分离的整数x和y两者都在范围0…1000。
输出
打印一行输出,包含距离农民约翰将前往达到Bessie。
样例输入
3 6
样例输出

数据范围限制

思路:
看懂题,大水题
只需按题目意思,模拟即可
注意这里FJ移动是按照x为基础移动的
(意思就是,x+t是在x位移动t)
懂了吗?
每次FJ飞的时候,累加当前位置与目标位置的差的绝对值
最后超过目标位置再来一下累加,输出答案

代码:

var        n,x,y,t,ans:longint;begin        assign(input,'lostcow.in');reset(input);        assign(output,'lostcow.out');rewrite(output);        readln(x,y);        if x=y then        begin                writeln(0);                halt;        end;        n:=x;        t:=1;        repeat                ans:=ans+abs(t)+abs(n-x);                n:=x+t;                t:=-t*2;                if (n>=y)and(x<y)or(n<=y)and(x>y)then                begin                        ans:=ans-abs(n-y);                        break;                end;        until false;        writeln(ans);        close(input);close(output);end.