转载请注明出处: 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. js迭代器模式

    在迭代器模式中,通常有一个包含某种数据的集合的对象.该数据可能储存在一个复杂数据结构内部,而要提供一种简单 的方法能够访问数据结构中的每个元素. 实现如下: //迭代器模式 var agg = (fu ...

  2. linux 下配置mysql区分大小写(不区分可能出现找不到表的情况)怎么样使用yum来安装mysql

    Linux 默认情况下,数据库是区分大小写的:因此,要将mysql设置成不区分大小写 在my.cof 设置 lower_case_table_names=1(1忽略大小写,0区分大小写) 检查方式:在 ...

  3. [Mugeda HTML5技术教程之13]链接的添加方式

    在广告主的需求中,有很多情况下需要在动画中添加一些外部链接.这份文档就在Mugeda动画中添加外部链接的方式,做一下梳理. 1.通过点击触发的链接 就是要用户点击屏幕来触发链接的情况,这是推荐使用的方 ...

  4. Win7中,取消共享文件夹后有个小锁

    用过windows7的朋友都知道,Windows 7 中设置某一个文件夹属性为共享后,文件夹的图标上就增加一个小锁图案.起到了一个标记作用,挺好的.但是即使你将该文件夹的共享功能取消后,该小锁图案还是 ...

  5. asp编程中获取上下两个月第一天和最后一天的代码

    经常在asp编程遇到要获取上个月第一天和最后一天的日期,获取下个月第一天和最后一天的日期.这里总结了一下,将这些asp代码全部列出来了,以便以后遇到的时候使用.    上个月第一天:<%=dat ...

  6. 关于android:focusable属性

    http://www.cnblogs.com/Gaojiecai/archive/2013/06/18/3142783.html Android属性 android:focusableInTouchM ...

  7. hdu 1142 A Walk Through the Forest

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...

  8. WiEngine+Eclipse+CDT+Sequoyah实现c++编程智能感知提示

    经过一段时间的摸索,我初步肯定自己基于WiEngine平台和C++开发跨Android/iPhone游戏的最佳(至少目前)环境为: Eclipse+CDT+Sequoyah 第一,JAVA代码调试技术 ...

  9. 关于Yeoman使用的总结

    Yeoman由三部分组成 Yo 用于项目构建. Grunt 用于项目管理,任务制定. Bower 用于项目依赖管理. 经过一段时间的使用,对这些东西有了一些个人总结: 总体上说这些内容学习曲线略高,不 ...

  10. What’s the difference between an interface and an abstract class in Java?

    原文 What’s the difference between an interface and an abstract class in Java? It’s best to start answ ...