HDU 3830 - Checkers (LCA模型)

本文介绍了一种在无限大小的棋盘上进行状态转移的方法,通过定义特定的跳动规则来实现从一种状态到另一种状态的转换。文章提供了一个详细的算法实现过程,包括如何将任意给定的状态规范化并计算最小步骤数。

Checkers

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


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.
 


很详细的题解



#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define LL long long
struct node
{
    LL x,y,z;
    LL dis;
};

LL check(node a,node b)
{
    if(a.x==b.x&&a.y==b.y&&a.z==b.z) return 1;
    return 0;
}

node bong(node &b)
{
    node a=b;
    LL dis=0;
    while(a.z-a.y!=a.y-a.x){
        LL len1 = a.y-a.x;
        LL len2 = a.z-a.y;
        if(len1<len2){
            LL c=(len2-1)/len1;
            a.x+=c*len1;
            a.y+=c*len1;
            dis+=c;
        }else{
            LL c=(len1-1)/len2;
            a.z-=c*len2;
            a.y-=c*len2;
            dis+=c;
        }
    }
    b.dis=dis;
    return a;
}


void sort_(node &a)
{
    if(a.x>a.y) swap(a.x, a.y);
    if(a.y>a.z) swap(a.y, a.z);
    if(a.x>a.y) swap(a.y, a.x);
}


node update(node b,LL k)
{
    node a=b;
    while(k){
        LL len1 = a.y-a.x;
        LL len2 = a.z-a.y;
        if(len1<len2){
            LL c=(len2-1)/len1;
            c=min(c,k);
            a.x+=c*len1;
            a.y+=c*len1;
            k-=c;
            a.dis-=c;
        }else{
            LL c=(len1-1)/len2;
            c=min(c,k);
            a.z-=c*len2;
            a.y-=c*len2;
            k-=c;
            a.dis-=c;
        }
    }
    return a;
}

int main()
{
    node a;
    node b;
    while(~scanf("%lld %lld %lld",&a.x,&a.y,&a.z)){
        scanf("%lld %lld %lld",&b.x,&b.y,&b.z);
        sort_(a);sort_(b);
        node aa=bong(a);
        node bb=bong(b);
        if(!check(aa,bb)){
            printf("NO\n");
            continue;
        }
        if(a.dis<b.dis){
            swap(a, b);
        }
        LL ans=a.dis-b.dis;
        a=update(a, a.dis-b.dis);
        LL l=0,r=a.dis;
        while(l<r){
            LL mid = (l+r)>>1;
            aa=update(a, mid);
            bb=update(b, mid);
            if(check(aa, bb)){
                r=mid;
            }else{
                l=mid+1;
            }
        }
        printf("YES\n");
        printf("%lld\n",2*r+ans);
    }
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值