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 ...
随机推荐
- 转:C++语言的15个晦涩特性
转自 http://blog.jobbole.com/54140/ 操作符重载和检查顺序 重载,(逗号),||或者&&操作符会引起混乱,因为它打破了正常的检查规则.通常情况下,逗号操作 ...
- Pexpect模块的安装
Pexpect模块的安装 下载地址:https://pypi.python.org/pypi/pexpect/ 解压后在目录下运行:python ./setup.py install (必须是root ...
- 20160815_Redis安装
OS: CentOS6.4(x64) 参考网址: http://www.cnblogs.com/haoxinyue/p/3620648.html http://www.codeceo.com/arti ...
- div滚动条演示
<!DOCTYPE html> <html> <head> <meta charset="gb2312" /> <title& ...
- Unity5.3官方VR教程重磅登场-系列2
作者:王寒链接:https://zhuanlan.zhihu.com/p/20485529来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 欢迎继续我们的学习. 北京时间 ...
- 【转】 Linux下目录结构
装完Linux,首先需要弄清Linux 标准目录结构 / root — 启动Linux时使用的一些核心文件.如操作系统内核.引导程序Grub等. home — 存储普通用户的个人文件 ftp — 用户 ...
- jmeter 构建一个数据库测试计划
添加用户 第一步你想做的每一个JMeter测试计划是添加一个 线程组 元素. 的线程组 告诉JMeter的用户数量你想模拟,用户应该多长时间 发送请求,他们应该发送的请求的数量. 继续添加Thread ...
- Android 反编译工具简介
Android 反编译工具: 所需工具:1 apktool : 用于获取资源文件 2 dex2Jar : 用于将classes.dex转化成jar文件 2 jd-gui: 将jar文件转化成java文 ...
- 张艾迪Eidyzhang:解码天才Eidyzhang的诞生
AOA解码:天才Eidyzhang的诞生AOA深度解读:世界第一天才Eidyzhang: (TheWorldNo.1Girl+TheWorldNo.1InterentGirl+世界第一女孩+世界第一互 ...
- 固定定位fixed(IE6)
position: fixed; left:200px; top:100px; _left:200px; _top:100px ...