UVALive
来源:互联网 发布:小米 网络诊断怎么关闭 编辑:程序博客网 时间:2024/06/07 13:23
题目链接
分析:
我们可以建一个坐标系
竖线右边+X,左边-X,一列为一;纵方向半个六边形高度为一;
假设坐标(-2, 2)到(3,4),那么让(-2, 2)的横坐标先变成3这样至少三步;如果纵坐标的高度差小于等于横坐标高度差,那么高度差可以在横坐标平移的的过程中弥补,否则还需要纵坐标平移。
代码:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cstdlib>#include <vector>#include <queue>#include <deque>#include <stack>#include <set>#include <cmath>using namespace std;const int d[] = {4, 2};int sum[200], num[200];int init() { sum[1] = 0; int t = 1, cnt = 2; while (true) { num[cnt - 1] = sum[cnt] = t; cnt ++; t += d[cnt & 1]; if (cnt > 100) break; } for (int i = 1; i < cnt; i ++) sum[i] += sum[i - 1]; return cnt;}void work(int x, int len, int &xx, int &yy) { int f = (int)(lower_bound(sum + 1, sum + len, x) - sum - 1); int k = x - sum[f]; int d = (f + 1) / 2; if (k <= d || k >= num[f] - d + 1) { if (k <= d) { xx = 1 - f; yy = (k - 1) * 2 + 1; if (f % 2 == 0) yy ++; } else { xx = f - 1; int tmpk = num[f] - k; yy = tmpk * 2 + 1; if (f % 2 == 0) yy ++; } } else { if (k > num[f] / 2) { int tmpk = k - num[f] / 2; xx = tmpk - 1; int rnk = num[f] - k - d; yy = rnk + d * 2; if (f % 2 == 0) yy ++; } else { xx = k - num[f] / 2 - 1; int rnk = k - d - 1; yy = rnk + d * 2; if (f % 2 == 0) yy ++; } }}int main() { int cnt = init(); int a, b; while (~scanf("%d%d", &a, &b)) { if (a == 0 && b == 0) break; int x1, x2, y1, y2; work(a, cnt, x1, y1); work(b, cnt, x2, y2); int dx = abs(x1 - x2); int dy = abs(y1 - y2); if (dy > dx) printf("%d\n", (dy - dx) / 2 + dx); else printf("%d\n", dx); } return 0;}
0 0
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- 队列
- ThreadPoolExecutor 并发调用
- SCOI2012 Blinker的仰慕者 BZOJ 2757
- android contentprovider 插件化之二
- 心有灵犀一点通——从『灵犀语音助手』漫谈人工智能的未来
- UVALive
- css应用
- 第29课:彻底解密Spark 1.6.X以前Shuffle中JVM内存使用及配置内幕详情:Spark到底能够缓存多少数据、Shuffle到底占用了多少数据、磁盘的数据远远比内存小却还是报告内存不足?
- Bootstrap 轮播效果
- codeforces 258D Little Elephant and Broken Sorting
- 指针练习
- 51 nod 1082 与7无关的数
- mini版串口通信
- 撸代码记录篇2.1-奔跑的皮皮熊