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

    http://www.oschina.net/code/snippet_109648_2229

  2. azure git 托管

    azure git上传部署步骤:(首次提交)cd 至本地代码路径git initgit add .git commit –m "initial commit"git remote ...

  3. centos7 最小化安装 无 ifconfig,netstat 的安装

    centos7 最小化安装 无 ifconfig,netstat 的安装 centos7 最小化安装之后,默认是没有 ifconfig,netstat命令的: 我们可以直接使用 yum -y inst ...

  4. Unity4.3 遮挡剔除:基本知识

    http://blogs.unity3d.com/2013/12/02/occlusion-culling-in-unity-4-3-the-basics/ 这篇博文由Umbra Software的J ...

  5. CFX客户端调用报错

    Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Unmarshalling Error: unex ...

  6. 各种浏览器css hack

    转载 http://www.cnblogs.com/jikey/archive/2010/06/21/1761924.html IE都能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识 ...

  7. 使用Eclipse构建Maven的SpringMVC项目

    一.直接建立Maven项目方法1.建立Maven项目 接下来使用Eclipse的maven构建一个web项目,以构建SpringMVC项目为例: 1.1 选择建立Maven Project 选择Fil ...

  8. org.springframework.web.servlet.PageNotFound No mapping found for HTTP request with URI [/AssetRepair/assetRepairController/test.do] in DispatcherServlet with name 'assetrepair'

    web.xml文件配置: xxx-servlet.xml 我们可以发现DispatcherServlet会处理"jsp"后缀的请求;而模型视图后缀也是jsp的 如果这样配置会报以下 ...

  9. sqlserver 修改替换text,ntext类型字段的两种方案

    方案一 用Update和Replace --替换语句(因为varchar(max)最大值是8000,所以大于8000的部分会被截掉) UPDATE dbo.SNS_UserBlog SET [Desc ...

  10. MVC之超链接的寻址

    传统式 href直接跟链接地址URL <a href="@Model.Base.BdtUrl" target="_blank">首页</a&g ...