hdu3830 (二分+LCA)
转载请注明出处: 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
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.
The second line is x,y,z.
They are all integers in range (-10^9, 10^9) and the two status are valid.
If it is YES, then output the least steps in the second line.
0 3 5
2
The middle checker jumps to position 0, and the status is 0 1 3
Then , the middle one jumps to 5.
题意:
给出三个点数轴上的起始为止与终止位置,问能否通过不断的跳跃从起始位置到终止位置(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)的更多相关文章
- NOIP2015 运输计划(二分+LCA+差分)
4326: NOIP2015 运输计划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 308 Solved: 208[Submit][Status] ...
- HDU 3830 Checkers(二分+lca)
Description Little X, Little Y and Little Z are playing checkers when Little Y is annoyed. So he wan ...
- lightoj-1128-Greatest Parent(二分+LCA)
传送门 首先我要实力吐槽这个lightoj 它给我的注册密码藏在不为人所见的地方 注册注册了10多分钟 qwq -------------------------------------------- ...
- bzoj4326 树链剖分 + 线段树 // 二分 lca + 树上差分
https://www.lydsy.com/JudgeOnline/problem.php?id=4326 题意:N个点的树上给M条树链,问去掉一条边的权值之后所有树链长度和的最大值最小是多少. 首先 ...
- P2680 运输计划[二分+LCA+树上差分]
题目描述 公元20442044 年,人类进入了宇宙纪元. L 国有 nn 个星球,还有 n-1n−1 条双向航道,每条航道建立在两个星球之间,这 n-1n−1 条航道连通了 LL 国的所有星球. 小 ...
- bzoj4326: NOIP2015 运输计划(二分+LCA+树上差分)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4326 题目大意:有一颗含有n个顶点的树,每两个点之间有一个边权,现在有m个运输计划,每个 ...
- LOJ2425 NOIP2015 运输计划 【二分+LCA+树上差分】*
LOJ2425 NOIP2015 运输计划 LINK 题意:给你一颗树,可以将任意一条边的权值变成0,然后求m条路径的长度的最小值 思路: 先二分最后的距离ans,然后我们把路程大于ans的所有路径拿 ...
- 跳跳棋[LCA+二分查找]-洛谷1852
传送门 这真是一道神仙题 虽然我猜到了这是一道LCA的题 但是... 第一遍看题,我是怎么也没想到能和树形图扯上关系 并且用上LCA 但其实其实和上一道lightoj上的那道题很类似 只不过那时一道很 ...
- 【转】Tarjan&LCA题集
转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...
随机推荐
- 洛谷 P1656 炸铁路
P1656 炸铁路 题目提供者kkksc03 标签图论搜索/枚举洛谷原创 难度普及/提高- 题目描述 因为某国被某红色政权残酷的高压暴力统治.美国派出将军uim,对该国进行战略性措施,以解救涂炭的生灵 ...
- CentOS和Ubuntu的区别
CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代 ...
- Java 中字符串的子串搜索
基友前两天参加了阿里的实习生面试,问了个问题,就是关于字符串的子串搜索的问题.想想实现方式无非就是两层循环,但是 java 中是有现成实现的,于是我就去查查源码,看看 java 语言怎么实现这个的,发 ...
- Python爬虫学习:一、相关概念与基础知识
爬虫: 网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成.传统爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL,在抓取网页的过程中,不断从当前页面上抽 ...
- Unicode的解救方案 - Windows程序设计(SDK)002
Unicode的解救方案 让编程改变世界 Change the world by program 内容节选: 早期的Windows也是使用ASCII字符集,ASCII好处多多,但ASCII的第一个字母 ...
- async 异步流程控制规则
github 学习async网址 : https://github.com/caolan/async/ 1.Async 函数介绍 async 主要实现了三个部分的流程控制功能 1.集合:Collect ...
- AQS详解
一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQueuedSynchronized(AQS)! 类如其名,抽象的队列式的同步器,AQ ...
- error Infos
- SQL生成随机字符串
1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')
- Angular学习笔记(2)——TODO小应用
Angular学习笔记(2)--TODO小应用 1. 写在前面 之前我们跑了Angular的Hello World,你是不是对它有点感觉了呢?这一篇将结合一个TODO程序来继续学习Angular的用法 ...