HDU 3830 Checkers(二分+lca)
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 Format
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 Format
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
一题思维好题
自己在草稿纸上手模一下;
设 a<b<c
则下一步为 2a-b a c或a c 2c-b;
若 b-a<c-b; 上一步为 b 2b-a c
若 b-a>c-b; 上一步为 a 2b-c b;
可以发现 中间点往外扩 a b c 距离增加;
反之减小;
将图画出 图是一颗二叉树!!!!;
下一步就是扩展儿子;上一步访问父亲;
那么再思考根节点问题 ;
对每个状态 它不停往上走 a b c 距离会减小;
直到 b-a=c-b;那快速找根节点只要用辗转相除就好;
那么 题目询问的第一个问题就好解决了;只要找根节点就行了;
第二个问题 ;
由于此题建图+lca麻烦而且数据也较大 空间不允许 ;那我们可以采用二分+lca
先把起始和终止状态调整到同一高度;
然后二分上升高度 往上走 判断是否相同就行了;(也要用辗转相除)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
struct state{int x,y,z,d;};
state st,ed;
int n,i,j,l,k,m,r;
void sor(state &now)
{
if(now.x>now.y)swap(now.x,now.y);
if(now.y>now.z)swap(now.y,now.z);
if(now.x>now.y)swap(now.x,now.y);
}
state root(state &now)
{
int q=-1,p=1,r;
state pre=now;
while(q!=p)
{
q=pre.y-pre.x;p=pre.z-pre.y;
if(p<q)
{
r=(q-1)/p;
pre.y-=r*p;
pre.z-=r*p;
}
else
{
r=(p-1)/q;
pre.x+=r*q;
pre.y+=r*q;
}
now.d+=r;
sor(pre);
}
pre.d=0;
return pre;
}
bool equr(state a,state b)
{return a.x==b.x&&a.y==b.y&&a.z==b.z ? true : false;};
state up(state now,int num)
{
int p=-1,q=1,r;
while(num&&q!=p)
{
p=now.y-now.x;q=now.z-now.y;
if(q<p)
{
r=min(num,(p-1)/q);
now.y-=q*r;
now.z-=q*r;
}
else
{
r=min(num,(q-1)/p);
now.x+=p*r;
now.y+=p*r;
}
num-=r;
sor(now);
}
return now;
}
bool check(int mid)
{
state nex1=st,nex2=ed;
nex1=up(nex1,mid+st.d-ed.d);nex2=up(nex2,mid);
if(equr(nex1,nex2))return true;
return false;
}
int main()
{
// freopen("xx.in","r",stdin);
while(scanf("%d%d%d%d%d%d",&st.x,&st.y,&st.z,&ed.x,&ed.y,&ed.z)==6)
{
sor(st);sor(ed);st.d=ed.d=0;
if(!equr(root(st),root(ed)))
printf("NO\n");
else
{
int mid,ans;
l=0;r=max(st.d,ed.d);
if(st.d<ed.d)swap(st,ed);
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid))r=mid-1,ans=mid;
else l=mid+1;
}
printf("YES\n%d\n",st.d-ed.d+ans*2);
}
}
}
HDU 3830 Checkers(二分+lca)的更多相关文章
- HDU 3830 Checkers
意甲冠军: 有三件 所有其他棋子可以跳 不能分开的两个跳跃 当被问及状态u为了国家v最低短跳转 思路: 对于一个状态三个棋子的位置能够设为 x y z (小到大) 仅仅有当y-x=z-y的时候 ...
- NOIP2015 运输计划(二分+LCA+差分)
4326: NOIP2015 运输计划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 308 Solved: 208[Submit][Status] ...
- UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)
UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...
- HDU 4822 Tri-war(LCA树上倍增)(2013 Asia Regional Changchun)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow ...
- hdu 2413(最大匹配+二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2413 思路:由于要求最少的时间,可以考虑二分,然后就是满足在limit时间下,如果地球战舰数目比外星战 ...
- HDU 5884 Sort (二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...
- hdu 1281棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...
- HDU 3966 dfs序+LCA+树状数组
题目意思很明白: 给你一棵有n个节点的树,对树有下列操作: I c1 c2 k 意思是把从c1节点到c2节点路径上的点权值加上k D c1 c2 k 意思是把从c1节点到c2节点路径上的点权值减去k ...
- hdu3830 (二分+LCA)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Checkers Time Limit: 2000/1000 MS (Java/O ...
随机推荐
- 杭电 2095 find your present (2)【位运算 异或】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2095 解题思路:因为只有我们要求的那个数出现的次数为奇数,所以可以用位运算来做,两次异或同一个数最后结 ...
- ZBrush细说3D海盗角色的创建艺术
一提到海盗,就不由自主想到了<加勒比海盗>,那个帅得一塌糊涂的杰克船长更是让人夜不能寐寝难安,但在艺术的世界里,角色无美丑,今天我们要讲的这位海盗,就与“帅气”八竿子打不着了,它甚至有点古 ...
- img标签过滤加fs模块实现图片文件缓存
方法一:function iCache(selector) { selector.each(function(data) { //msg(data); ! function(data) { var u ...
- pc页面滚动的时候,背景图不动只是页面滚动
代码如下,直接拷贝出去就能看效果: 用到的方法 background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动. <!DOCTYPE html> <h ...
- 自定义View实现圆角化
目的: 1.实现自定义ReleativeLayout圆角化 实现: 1.在res目录中新建attrs.xml文件,自定义属性如下. <?xml version="1.0" e ...
- FastFDS常用命令
1.启停fastdfs相关服务 #start fastdfs 启动服务 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart /usr/loca ...
- php 流
php:// — 访问各个输入/输出流(I/O streams) 说明 PHP 提供了一些杂项输入/输出(IO)流,允许访问 PHP 的输入输出流.标准输入输出和错误描述符, 内存中.磁盘备份的临时文 ...
- P1892 团伙
题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...
- 编写 Node.js Rest API 的 10 个最佳实践
Node.js 除了用来编写 WEB 应用之外,还可以用来编写 API 服务,我们在本文中会介绍编写 Node.js Rest API 的最佳实践,包括如何命名路由.进行认证和测试等话题,内容摘要如下 ...
- 【ACM-ICPC 2018 沈阳赛区网络预赛 K】Supreme Number
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然每个数字只可能是1,3,5,7 然后如果3,5,7这些数字出现两次以上.显然两个3||5||7都能被11整除. 然后1的话最多能 ...