转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Checkers

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

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

 

题意:

给出三个点数轴上的起始为止与终止位置,问能否通过不断的跳跃从起始位置到终止位置(ps:不需要一一对应),每次跳跃时不得跨过两个点,若能,则给出最少需要几次操作

分析:

不妨设a<b<c,x<y<z。

由于一次不能跨越两个点。若b-a>c-b,则只能进行(1)b->2a-b-a或者(2)b->2c-b或者(3)a->2b-a;

若b-a<c-b,则只能进行(1)b->2a-b-a或者(2)b->2c-b或者(3)c->2b-c;

若b-a=c-b,则只能进行(1)b->2a-b-a或者(2)b->2c-b;

我们可以发现,若每次选取(1)(2)操作,则三个数在数轴上的跨度会不断变大,我们可以视为其分别向左儿子和右儿子移动,

若选取(3)操作,则三个数在数轴上的跨度会不断变小,对此,我们可以将其视为向父亲结点移动(因为对于每种状态,其对应的(3)操作是唯一的,这恰好对应这树的父亲结点是唯一的)。

通过一定次数的向父亲结点移动,我们可以发现最终一定会变成第三种状态。也就是对于判断yes还是no,我们只需要判断其所对应的根节点是否相同,若不同,则必定不能从初始状态转移到最终状态。

那么,如何快速地求出某一状态进行若干操作之后的状态呢?

我们假设a<b<c,并且c-b>a-b。那么,一次(3)操作之后会变成b,2b-a,c。也就是相当于给a和b同时加上了b-a。那么对于这样需要移动几次呢?(c-b)/(b-a)?

这是不对的,例如1,2,4。若移动二次,则会变成3,4,4。也就是我们要避免b-a=c-b的发生。即对于(c-b)%(b-a)==0时,我们需要少移一次。

于是,利用向下取整的特性,我们可以发现只要移(c-b-1)/(b-a)次。

如此,不断往复,我们就可以快速地求出移动若干次的状态,然后二分搞一下就行了

 //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 10
ll a[MAXN],b[MAXN];
ll ra[MAXN],rb[MAXN];
void gcd(ll *x,ll &d){
ll l1=x[]-x[];
ll l2=x[]-x[];
ll t;
while(l1!=l2){
if(l1<l2){
t=(l2-)/l1;
ll tmp=t*l1;
x[]+=tmp;
x[]+=tmp;
}else{
t=(l1-)/l2;
ll tmp=t*l2;
x[]-=tmp;
x[]-=tmp;
}
d+=t;
l1=x[]-x[];
l2=x[]-x[];
//gcd(x,d);
}
}
void gao(ll *x,ll d){
ll l1=x[]-x[];
ll l2=x[]-x[];
ll t;
while(d>){
if(l1<l2){
t=(l2-)/l1;
if(t>d)t=d;
ll tmp=t*l1;
x[]+=tmp;
x[]+=tmp;
}else {
t=(l1-)/l2;
if(t>d)t=d;
ll tmp=t*l2;
x[]-=tmp;
x[]-=tmp;
}
d-=t;
l1=x[]-x[];
l2=x[]-x[];
}
} int main()
{
ios::sync_with_stdio(false);
while(scanf("%I64d%I64d%I64d",&a[],&a[],&a[])!=EOF){
for(int i=;i<;i++)scanf("%I64d",&b[i]);
sort(a,a+);
sort(b,b+);
for(int i=;i<;i++)ra[i]=a[i];
for(int i=;i<;i++)rb[i]=b[i];
ll d1=,d2=;
bool flag=;
for(int i=;i<;i++)if(ra[i]==ra[i+])flag=;
for(int i=;i<;i++)if(rb[i]==rb[i+])flag=;
if(flag){
printf("NO\n");
continue;
}
gcd(ra,d1);
gcd(rb,d2);
flag=;
for(int i=;i<;i++)
if(ra[i]!=rb[i])flag=;
if(flag){
printf("NO\n");
continue;
}
printf("YES\n");
ll ans = ;
ll tmp = ;
if(d1>d2)gao(a,d1-d2);
else gao(b,d2-d1);
ll l=,r=min(d1,d2);
while(l<=r){
ll mid=(l+r)>>;
for(int i=;i<;i++)ra[i]=a[i];
for(int i=;i<;i++)rb[i]=b[i];
gao(ra,mid);
gao(rb,mid);
bool flag=;
for(int i=;i<;i++)
if(ra[i]!=rb[i])flag=;
if(!flag){
tmp=mid;
r=mid-;
}else {
l=mid+;
}
}
printf("%I64d\n",tmp*+max(d1-d2,d2-d1));
}
return ;
}

hdu3830 (二分+LCA)的更多相关文章

  1. NOIP2015 运输计划(二分+LCA+差分)

    4326: NOIP2015 运输计划 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 308  Solved: 208[Submit][Status] ...

  2. HDU 3830 Checkers(二分+lca)

    Description Little X, Little Y and Little Z are playing checkers when Little Y is annoyed. So he wan ...

  3. lightoj-1128-Greatest Parent(二分+LCA)

    传送门 首先我要实力吐槽这个lightoj 它给我的注册密码藏在不为人所见的地方 注册注册了10多分钟 qwq -------------------------------------------- ...

  4. bzoj4326 树链剖分 + 线段树 // 二分 lca + 树上差分

    https://www.lydsy.com/JudgeOnline/problem.php?id=4326 题意:N个点的树上给M条树链,问去掉一条边的权值之后所有树链长度和的最大值最小是多少. 首先 ...

  5. P2680 运输计划[二分+LCA+树上差分]

    题目描述 公元20442044 年,人类进入了宇宙纪元. L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条航道建立在两个星球之间,这 n-1n−1 条航道连通了 LL 国的所有星球. 小 ...

  6. bzoj4326: NOIP2015 运输计划(二分+LCA+树上差分)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4326 题目大意:有一颗含有n个顶点的树,每两个点之间有一个边权,现在有m个运输计划,每个 ...

  7. LOJ2425 NOIP2015 运输计划 【二分+LCA+树上差分】*

    LOJ2425 NOIP2015 运输计划 LINK 题意:给你一颗树,可以将任意一条边的权值变成0,然后求m条路径的长度的最小值 思路: 先二分最后的距离ans,然后我们把路程大于ans的所有路径拿 ...

  8. 跳跳棋[LCA+二分查找]-洛谷1852

    传送门 这真是一道神仙题 虽然我猜到了这是一道LCA的题 但是... 第一遍看题,我是怎么也没想到能和树形图扯上关系 并且用上LCA 但其实其实和上一道lightoj上的那道题很类似 只不过那时一道很 ...

  9. 【转】Tarjan&LCA题集

    转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...

随机推荐

  1. windbg vmware win7联机调试环境搭建

    接下来设置虚拟机启动模式,可以直接设置现在的虚拟机启动项为debug模式 或者直接新建一个启动项目 bcdedit /dbgsettings {serial [baudrate:value][debu ...

  2. xml解析,练习

    <collection shelf="New Arrivals"><movie title="Enemy Behind">   < ...

  3. Injector Job深入分析

    Injector Job的主要功能是根据crawlId在hbase中创建一个表,将将文本中的seed注入表中. (一)命令执行 1.运行命令 [jediael@master local]$ bin/n ...

  4. http_load压力测试

    http_load是基于linux平台的性能测试工具,它体积非常小,仅100KB.它以并行复用的方式运行,可以测试web服务器的吞吐量与负载. 一.安装http_load A.进入/usr/local ...

  5. Linux - How To Set Up an NFS Mount on CentOS 6

    About NFS (Network File System) Mounts NFS mounts work to share a directory between several servers. ...

  6. 《Programming WPF》翻译 第8章 4.关键帧动画

    原文:<Programming WPF>翻译 第8章 4.关键帧动画 到目前为止,我们只看到简单的点到点的动画.我们使用了To和From属性或者By属性来设计动画--相对于当前的属性值.这 ...

  7. cmake编译Debug和Release

    CMake 中有一个变量 CMAKE_BUILD_TYPE ,可以的取值是 Debug Release Rel WithDebInfo 和 MinSizeRel.当这个变量值为 Debug 的时候,C ...

  8. 图论专题训练1-D(K步最短路,矩阵连乘)

    题目链接 /* *题目大意: *求出从i到j,刚好经过k条边的最短路; * *矩阵乘法的应用之一(国家队论文): *矩阵乘法不满足交换律,矩阵乘法满足结合律; *给定一个有向图,问从A点恰好走k步(允 ...

  9. VS 2012 显示Link的参数

    VC 通过Link将cl编译出来的.obj文件链接到一起.不过默认设置还是看不到究竟是怎么做的.需要如下设置: 右键点击工程,选择Properties菜单,然后选择左边的Linker->Gene ...

  10. HtmlHelper扩展 及 页面编码化

    Html.Encode 新建Action,命名为HtmlEncodeDemo 将含有特殊字符的值传递给View视图 <%= ViewData["script"] %> ...