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) 收藏的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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) ...

  8. 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) ...

  9. 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 ...

随机推荐

  1. 【ubuntu java】java: error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory

    先检查了环境变量PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/loca ...

  2. centos下vsftpd安装与配置

    1.已经配置好可以上网了,所以即可通过yum install vsftpd安装啦.如果不能上网则可以通过dvd2.iso工具集rpm方式进行安装. 2.安装好后,默认本地可以通过匿名用户登录,但是其它 ...

  3. location.pathname;outline:medium;undefined不能加引号

    1. location.pathname -- 返回URL的域名后的部分.例如 http://www.dreamdu.com/xhtml/ 返回/xhtml/ 2. 判断某个名称为undefined时 ...

  4. 日期操作类--SimpleDateFormat类

    使用SimpleDateFormat格式化日期 SimpleDateFormat是一个以语言环境敏感的方式来格式化和分析日期的类.SimpleDateFormat允许你选择任何用户自定义日期时间格式来 ...

  5. javascript正则表达式介绍

    正则表达式就是一个用来描述字符模式的对象.它被用来在文本中执行模式匹配(pattern-matching)以及”查找-替换”(search-and-replace)的任务.javascript中正则的 ...

  6. eclipse导入git项目(转)

    1.首先在github.com上申请一个账号2.Eclipse需要安装egit插件,在Eclipse中选择help->Marketplace,在search中输入egit,找到后安装即可 3.从 ...

  7. 32-HTML辅助方法

    顾名思义,HTML辅助方法(HTML Helper)就是用来辅助产生HTML之用,在开发View的时候一定会面对许多HTML标签,处理这些HTML的工作非常繁琐,为了降低View的复杂度,可以使用HT ...

  8. 《Java程序设计》学期总结

    <Java程序设计> 学期总结 课程设计小组 -迦瓦栈队 团队博客 读书笔记汇总 第一周 第二周 第三周 第四周 第五周 第六周 第七周 第八周 第九周 第十周 实验报告汇总 实验一 实验 ...

  9. C/C++ 关于生成静态库(lib)/动态库(dll)文件如何使用(基于windows基础篇)

    1. 首先,如何制作一个静态库(lib)? 额, 对于静态库,我们知道,里头是不应该有Main函数,它只是一个配合文件.之所以称之为lib静态库,其实就是指,我们需要用到lib里头的函数时,我们才会去 ...

  10. cf------(round)#1 B. Spreadsheets(模拟)

    B. Spreadsheets time limit per test 10 seconds memory limit per test 64 megabytes input standard inp ...