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 ...
随机推荐
- 线程池的应用及Callable接口的使用
public interface Executor { /** * Executes the given command at some time in the future. The comman ...
- VIm vi 使用 汇总
x 删除当前光标下的字符dw 删除光标之后的单词剩余部分.d$ 删除光标之后的该行剩余部分.dd 删除当前行. c 功能和d相同,区别在于完成删除操作后进入INSERT MODEcc 也是删除当前行, ...
- Hbase之使用多Get实例返回数据
import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hado ...
- LinuxShell脚本攻略--第一章 小试牛刀
使用 shell 进行数学运算: #!/bin/bash no1=; no2=; let result=no1+no2 echo $result result=$[ $no1 + no2 ] resu ...
- xcode6 AsynchronousTesting 异步任务测试
xcode集成了非常方便的测试框架,XCTest 在xcode6之后,提供了 <XCTest/XCTestCase+AsynchronousTesting.h> 利用此我们可以直接在XCT ...
- python 练习 10
#!/usr/bin/python # -*- coding: UTF-8 -*- i = int(raw_input('净利润:')) arr = [1000000,600000,400000,20 ...
- ubuntu下安装基本配置
安装ubuntu更新: sudo apt-get update sudo apt-get upgrade 安装Docky: sudo add-apt-repository ppa:ricotz/doc ...
- python实现微信打飞机游戏
环境:Ubuntu 16.04 LTS Python 2.7.11 + Pygame + Pycharm 代码: # -*- coding: UTF-8 -*- import pygame, ran ...
- hdu-----(3746)Cyclic Nacklace(kmp)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- AppSettings和ConnectionStrings的区别
AppSettings是ASP.NET1.1时期用的,在.NET Framework 2.0中,新增了ConnectionStrings. 1.<connectionStrings> &l ...