hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏
problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030
#include <cstdio>
#include <cmath>
#include <algorithm>
int calPathLength(int x, int y) {
//path length from 1 (1st line, lower) to a number in ith line is only differ by -1,
// 1 to 17 - 25 (5th line) is 2*(5-1)+(0 or -1)
//path length from 3 (3rd line, upper) to a number in ith line (except first, last one)
// is only differ by 1, 3 to 18 - 24 (5th line) is 2*(5-2)+(0 or 1)
// if y is out of valid triangle starting from x, then plus distance with the border
// left_border=cx, right_border=cx+(ry-rx)*2
// if on the left, plus left_border-cy,
// if on the right, plus cy-right_border,
// if in the valid range, plus by 0, 1, or -1, namely (cy&1)-(cx&1)
int rx,ry,cx,cy, tmp,res; // x is the cx-th number in row rx
if(x>y) std::swap(x,y);
rx=sqrt(x-1), ry=sqrt(y-1);
cx=x-rx*rx;
cy=y-ry*ry;
res=(ry-rx)<<1;
if((tmp=cx-cy)>=0) return res+tmp;
if((tmp=cy-cx-res)>=0) return res+tmp;
else return res+(cy&1)-(cx&1);
}
int main() {
//freopen("input.txt","r",stdin);
int x,y;
while(scanf("%d%d",&x,&y)!=EOF) {
printf("%d\n",calPathLength(x,y));
}
return 0;
}
thanks to http://www.acmerblog.com/hdu-1030-delta-wave-1282.html
below is an excerpt with a little modification.
求三角形内两点的最短路径,很容易证明最短的路径就是两点在三个方向的距离之和。
#include <cstdio>
#include <cmath>
int main() {
int m,n,ai,aj,bi,bj,ak,bk;
while (scanf("%d%d",&m,&n)!=EOF) {
ai = sqrt(m-1);
bi = sqrt(n-1);
aj = (m-ai*ai-1)>>1;
bj = (n-bi*bi-1)>>1;
ak = ((ai+1)*(ai+1)-m)>>1;
bk = ((bi+1)*(bi+1)-n)>>1;
printf("%d\n",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.
hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏的更多相关文章
- hdu 1051 (greedy algorithm, how a little modification turn 15ms to 0ms) 分类: hdoj 2015-06-18 12:54 29人阅读 评论(0) 收藏
the 2 version are essentially the same, except version 2 search from the larger end, which reduce th ...
- hdu 1047 (big integer sum, fgets or scanf, make you func return useful infos) 分类: hdoj 2015-06-18 08:21 39人阅读 评论(0) 收藏
errors made, boundary conditions, <= vs < , decreasing vs increasing , ++, –, '0'/'1' vs 0/1 p ...
- hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat) 分类: hdoj 2015-06-16 12:33 28人阅读 评论(0) 收藏
as Scott Meyers said in his book Effective STL, "My advice on choosing among the sorting algori ...
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- The Zen Programmer (zhuan)
http://blog.csdn.NET/marksinoberg/article/details/52460725 ***************************************** ...
- OpenGL的glClearColor和glClear改变背景颜色
OpenGL的glClearColor和glClear改变背景颜色 结合以下两个函数void glClearColor(GLclampf red, GLclampf green, GLclamp ...
- 启动eclipse说在sdk目录下的platforma-tools下面找不到adb.exe
adb是什么?adb有什么用?adb工具如何用? 是用来管理模拟器和真机的通用调试工具,该工具功能强大,直接打开cmd即可使用adb命令,adb的全称为Android Debug Bridge,是 ...
- Python语言及其应用 - 知识点遍历
遍历python语法 2.基本元素:数字.字符串和变量 2.1 python有哪些基本类型? 布尔型(True, False),整型(42,100000),浮点型(3.14159,1.0e8) ...
- 一个比较全面的java随机数据生成工具包
最近,由于一个项目的原因需要使用一些随机数据做测试,于是写了一个随机数据生成工具,ExtraRanom.可以看成是Java官方Random类的扩展,主要用于主要用于测试程序.生成密码.设计抽奖程序等情 ...
- location.pathname;outline:medium;undefined不能加引号
1. location.pathname -- 返回URL的域名后的部分.例如 http://www.dreamdu.com/xhtml/ 返回/xhtml/ 2. 判断某个名称为undefined时 ...
- sql server 导出数据到 Azure Hbase / Hive 详细步骤
The Hadoop on Azure Sqoop Import Sample Tutorial Table of Contents Overview Goals Key technologi ...
- Excel中的表单控件和active控件
EXCEL中有两种控件:表单控件和active控件 表单控件是excel5和excel95开始使用的,从excel97开始,active控件开始出现 关于表单控件和active控件的区别和使用范围,网 ...
- WPF:行列显示
新建显示病人信息控件PatientElement Add-->NewItem-->WPF-->UserControl(WPF),名称:PatientElement.xmal < ...
- 如何使用java调用DLL运行C++(初篇)
JNI:Java Native Interface,简称JNI,是Java平台的一部分,可用于让Java和其他语言编写的代码进行交互. 下面是从网上摘取的JNI工作示意图: