列表

详情


NC24103. [USACO 2017 Ope B]The Lost Cow

描述

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.

输入描述

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.

示例1

输入:

3 6

输出:

9

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3(3.9) 解法, 执行用时: 17ms, 内存消耗: 2680K, 提交时间: 2020-11-07 22:45:40

def soloution(x, y):
    ans = 0
    if x < y:
        count = 0

        while y - x > 2 ** count:
            count += 2

        ans = 2 ** (count + 1) - 2 + y - x
    else:
        count = 1

        while x - y > 2 ** count:
            count += 2

        ans = 2 ** (count + 1) - 2 + x - y

    return ans

x, y = input().split()
x = int(x)
y = int(y)
print(soloution(x, y))

C++(clang++11) 解法, 执行用时: 5ms, 内存消耗: 388K, 提交时间: 2020-11-07 11:11:00

#include<bits/stdc++.h>
#define ll long long 
using namespace std;

int main()
{
	ll x,y;
	scanf("%lld%lld",&x,&y);
	ll p=1,a=1;
	ll ans=0;
	while(1){
		if(a<0&&y<=x&&x-p<=y||a>0&&x<=y&&x+p>=y){
			ans+=abs(x-y);
			break;
		}
		p*=2;
		a*=(-1);
		ans+=p;
	}
	printf("%lld\n",ans );
	return 0;
}

C(clang11) 解法, 执行用时: 2ms, 内存消耗: 376K, 提交时间: 2020-11-07 10:22:01

#include<stdio.h>

int main()
{
    int x,y;
    scanf("%d%d",&x,&y);
    int i=1;
    int n=1;
    int sum=0;
    while((x+n*i<y&&x<y)||(x+n*i>y&&x>y))
    {
        sum+=n+n/2;
        i*=-1;
        n*=2;
    }
    sum+=abs(x-n/2*i-y);
    printf("%d",sum);
    return 0;
}

上一题