HDU 3830 Checkers(LCA)

Checkers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 875    Accepted Submission(s): 250


Problem Description
Little X, Little Y and Little Z are playing checkers when Little Y is annoyed. So he wants to make the chessboard much bigger. Although Little Z insists the original version, Little X stands by Little Y. After they enlarge the chessboard, the chessboard turns to an infinite line. 
The chessboard is like the Number Axes now, with each integer point able to hold a checker. At initial status there are three checkers on three different integer points , and through the game there always are three checkers. Every time, they can choose a checker A to jump across a pivot checker B to a new position(but the distance between old A and B equals to new A and B, and there should be no other checkers except B in the range [old A, new A]).
After playing for a while, they wonder whether an given status a,b,c can be transferred to x,y,z. obeying the rules. Since the checkers are considered the same, it is unnecessary for a must jump to x. 
 

Input
The first line is a,b,c.
The second line is x,y,z.
They are all integers in range (-10^9, 10^9) and the two status are valid.
 

Output
The first line is YES or NO, showing whether the transfer can be achieved.
If it is YES, then output the least steps in the second line.
 

Sample Input
  
  
1 2 3 0 3 5
 

Sample Output
  
  
YES 2
Hint
The middle checker jumps to position 0, and the status is 0 1 3 Then , the middle one jumps to 5.
 

Source

2011 Multi-University Training Contest 1 - Host by HNU


对于一个状态

例如2 3 7

中间可以往两侧跳,即2 3 7->1 2 7 / 2 3 7->2 7 11

两侧仅有一个能往中间跳,即2 3 7->3 4 7

那么所有的状态就能表示为一棵二叉树,第一种情况为其两个儿子,第二种为其父亲

问题转换为给定树上的两个结点,求其距离

我们发现若记前两个数差t1,后两个数差t2,不妨设t1<t2

则左边最多往中间跳(t2-1)/t1次

然后只能右边往中间跳,是一个辗转相除的过程,即在logK的时间内我们可以用这种方法得到某个结点它向上K次后的结点,或者根节点,同时还可以顺便算下深度

那么只要求始终两个状态的深度d1,d2,将较深的调整到同一深度

然后二分/倍增求与lca的深度差x

ans=2*x+abs(d1-d2)

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define ll long long 
using namespace std;
const int MAXN = 10000 + 10;
const int INF = 1000000003;
int read();
struct data
{
    long long a[4];
    bool operator != (const data& rs)const
    {
        for(int i=1;i<=3;i++) if(a[i] != rs.a[i]) return 1;
        return 0;
    }
};
long long a[4], b[4];
long long dep;
data work(long long  *a, long long k)
{
    data ans;
    long long t1 = a[2] - a[1], t2 = a[3] - a[2];
    for(int i=1;i<=4;i++) ans.a[i] = a[i];
    if(t1 == t2) return ans;
    if(t1 < t2)
    {
        long long t = min(k, (t2 - 1) / t1);
        k -= t; dep += t;
        ans.a[2] += t * t1; ans.a[1] += t * t1;
    }
    else
    {
        long long t = min(k, (t1 - 1) / t2);
        k -= t; dep += t;
        ans.a[2] -= t * t2; ans.a[3] -= t * t2;
    }
    if(k) return work(ans.a, k);
    else return ans;
}
int main()
{
    while(scanf("%I64d%I64d%I64d", &a[1], &a[2], &a[3])!=EOF)
    {
        for(int i=1;i<=3;i++) scanf("%I64d", &b[i]);
        sort(a + 1, a + 4); sort(b + 1, b + 4); dep = 0;
        data t1 = work(a, INF); ll d1 = dep; dep = 0;
        data t2 = work(b, INF); ll d2 = dep; dep = 0;
        if(t1 != t2){puts("NO"); continue;}
        if(d1 > d2)
        {
            swap(d1, d2);
            for(int i=1;i<=3;i++) swap(a[i], b[i]);
        }
        int ans = d2 - d1;
        t1 = work(b, ans);for(int i=1;i<=3;i++) b[i] = t1.a[i];
        int l = 0, r = d1;
        int rs;
        while(l <= r)
        {
            int mid = (l + r) >> 1;
            if(!(work(a, mid) != work(b, mid)))
            {
                rs = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        puts("YES");
        printf("%d\n", ans + 2 * rs);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值