【LCA】bzoj 2144:跳跳棋
2144: 跳跳棋
Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 248 Solved: 121
[Submit][Status][Discuss]
Description
跳跳棋是在一条数轴上进行的。棋子只能摆在整点上。每个点不能摆超过一个棋子。我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置。我们要通过最少的跳动把他们的位置移动成x,y,z。(棋子是没有区别的)跳动的规则很简单,任意选一颗棋子,对一颗中轴棋子跳动。跳动后两颗棋子距离不变。一次只允许跳过1颗棋子。 写一个程序,首先判断是否可以完成任务。如果可以,输出最少需要的跳动次数。
Input
第一行包含三个整数,表示当前棋子的位置a b c。(互不相同)第二行包含三个整数,表示目标位置x y z。(互不相同)
Output
如果无解,输出一行NO。如果可以到达,第一行输出YES,第二行输出最少步数。
Sample Input
0 3 5
Sample Output
2
【范围】
100% 绝对值不超过10^9
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> using namespace std; inline int in()
{
int x=,f=;char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')f=-,ch=getchar();
while(ch<=''&&ch>='')x=x*+ch-'',ch=getchar();
return x*f;
} struct data{
int a[];
}; int a[],b[],tem=; data up(int c[],int k)
{
data t;
int t1=c[]-c[],t2=c[]-c[];
for(int i=;i<=;i++)t.a[i]=c[i];
if(t1==t2)return t;
else if(t1<t2)
{
int ans=min(k,(t2-)/t1);
tem+=ans;k-=ans;
t.a[]+=ans*t1,t.a[]+=ans*t1;
}
else
{
int ans=min(k,(t1-)/t2);
tem+=ans,k-=ans;
t.a[]-=ans*t2,t.a[]-=ans*t2;
}
if(k)return up(t.a,k);
else return t;
} bool operator!=(data a,data b)
{
for(int i=;i<=;i++)if(a.a[i]!=b.a[i])return ;
return ;
} int main()
{
for(int i=;i<=;i++)a[i]=in();
for(int j=;j<=;j++)b[j]=in();
sort(+a,+a+);
sort(+b,+b+);
data t1=up(a,1e9);int d1=tem;tem=;
data t2=up(b,1e9);int d2=tem;tem=;
if(t1!=t2){
printf("NO");
return ;
}
if(d1>d2)
{
swap(d1,d2);
for(int i=;i<=;i++)swap(a[i],b[i]);
}
int ans=d2-d1;
t1=up(b,ans);
for(int i=;i<=;i++)b[i]=t1.a[i];
int l=,r=d1,res;
while(l<=r)
{
int mid=(l+r)>>;
if(up(a,mid)!=up(b,mid))l=mid+;
else r=mid-,res=mid;
}
printf("YES\n");
printf("%d",ans+*res);
return ;
}
【LCA】bzoj 2144:跳跳棋的更多相关文章
- [BZOJ 2144]跳跳棋
Description 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他 ...
- bzoj 2144: 跳跳棋——倍增/二分
Description 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他 ...
- 【BZOJ 2144】 2144: 跳跳棋 (倍增LCA)
2144: 跳跳棋 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 642 Solved: 307 Description 跳跳棋是在一条数轴上进行的 ...
- 【bzoj2144】跳跳棋
2144: 跳跳棋 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 492 Solved: 244[Submit][Status][Discuss] ...
- BZOJ2144跳跳棋——LCA+二分
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.我们用跳跳棋来做一个简单的 游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...
- 【洛谷】1852:[国家集训队]跳跳棋【LCA】【倍增?】
P1852 [国家集训队]跳跳棋 题目背景 原<奇怪的字符串>请前往 P2543 题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个 ...
- 跳跳棋——二分+建模LCA
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...
- 不一样的LCA——luoguP1852跳跳棋
洛谷端题目链接 loj端题目链接 题目大意: 在一条数轴上进行跳跳棋游戏.棋子只能摆在整点上.每个点不能摆超过一个棋子.用跳跳棋完成:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动 ...
- P1852 跳跳棋 [LCA思想+二分答案]
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有\(3\)颗棋子,分别在\(a,b,c\)这三个位置.我们要通过最少的跳动 ...
随机推荐
- Ehcache(2.9.x) - API Developer Guide, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
- Ehcache(2.9.x) - Configuration Guide, Configuring Cache
About Ehcache Configuration Ehcache supports declarative configuration via an XML configuration file ...
- Viewpager+Fragment出现空白页面的问题
写了三个Fragment,一次点击跳转显示正常,如果从第一个直接跳转到第三个,第三个页面会出现空白界面. 问题找到了:原来动态获取数据页面数据不显示,页面显示空白,就是onCreateView每次都调 ...
- iOS 网络 -- cocoaPods 安装和使用教程
Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage CocoaPods 是什么? 当你开发iOS应用时, ...
- C# WinForm TextBox添加水印效果
1.新建项目添加WatermarkTextBox类: using System; using System.Collections.Generic; using System.Text; using ...
- [java学习笔记]java语言基础概述之运算符&程序流程控制&for循环嵌套
一.运算符 算数运算符 +,-,*,/,%,++,-- 1.+,-,*,/ 整数除以整数的结果为舍弃了小数部分的整数. 2.%:取余 %左右都为正数,左边小于右边结果为左边值,如,4%5=4 %左边大 ...
- jquery——ajax加载后的内容,单击事件失效
使用delegate(),on()绑定事件,可以将事件绑定到其祖先元素上,这样以后加载出来的元素,单击事件仍然有效
- SQL正常工作日上班安排
alter proc [work] as declare @i int begin id into #restdate from dt_work where work_date in (select ...
- 【Winform】 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”。
问题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Window ...
- 学习W3SCHOOL 表单验证
//表单学习笔记 //建立一张表单的验证 <!DOCTYPE html> <html> <head> <meta http-equiv="Conte ...