BUPT 2017 summer training (for 16) #1C

题意

A new computer scientist is trying to develop a new memory management system called "spiral memory management".

The basic idea of this system is that it represents the memory as a 2D grid with a predefined origin point, and the address of each memory location is represented as its (x,y) coordinates, and it tries to store frequently used data as close to the origin point as possible.

This system needs a search algorithm. Our computer scientist is currently developing an algorithm called "square spiral search".

The algorithm searches the memory locations in the following order (also shown in the figure):

(0,0), (1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1,-1), (2,-1), (2,0), (2,1), (2,2), (1,2), (0,2), (-1,2), (-2,2), (-2,1), (-2,0), (-2,-1), (-2,-2,) ,(-1,-2) ... and so on.



Now he is wondering: how many steps would it take to reach a memory location with the address (x, y) using the square spiral search. Can you help him find the answer?

题解

代码

#include <cstdio>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int x,y;
scanf("%d%d",&x,&y);
if(!x&&!y){
puts("0");continue;
}
ll s=max(abs(x),abs(y)),ans=(s*2+1)*(s*2+1)-1;
if(y==-s)
ans-=(s-x);
else if(x==-s)
ans-=s*2+(s+y);
else if(y==s)
ans-=s*4+(s+x);
else
ans-=s*6+(s-y);
printf("%lld\n",ans);
}
return 0;
}

【Gym - 100947G】Square Spiral Search的更多相关文章

  1. 【POJ 1084】 Square Destroyer

    [题目链接] http://poj.org/problem?id=1084 [算法] 迭代加深 [代码] #include <algorithm> #include <bitset& ...

  2. 【DFS+剪枝】Square

    https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/J [题意] 给定n个木棍,问这些木棍能否围成一个正方形 [Accepted] # ...

  3. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  4. 【LeetCode 99】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. 【LeetCode练习题】Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  6. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  7. 【 Gym 101116K 】Mixing Bowls(dfs)

    BUPT2017 wintertraining(15) #4H Gym - 101116K 题意 给定一个菜谱,大写的单词代表混合物,小写的代表基础原料.每个混合物由其它混合物或基础原料组成,不会间接 ...

  8. 【 Gym - 101124E 】Dance Party (数学)

    BUPT2017 wintertraining(15) #4G Gym - 101124 E.Dance Party 题意 有c种颜色,每个颜色最多分配给两个人,有M个男士,F个女士,求至少一对男士同 ...

  9. 【Gym - 101124A】The Baguette Master (数学,几何)

    BUPT2017 wintertraining(15) #4F Gym - 101124A 题意 给定画框宽度,画的四边和一个对角线长度,求画框外沿周长. 题解 过顶点做画框的垂线,每个角都得到两个全 ...

随机推荐

  1. mybatis的mapper注入失败

    因为处在两个不同的资源文件夹下: 导致classpath无法加载其中一些文件,所以修改为classpath*后顺利进行. <!-- 加载spring容器 --> <!-- neede ...

  2. AQS解析(未完成)

    参考:Java并发之AQS详解 同步队列和condition等待队列.获取到锁的线程则处于可运行状态,而未获取到锁的线程则被添加到同步队列中,等待获取到锁的线程释放锁. 一.数据结构 Node sta ...

  3. 【kindle笔记】之 《恶意》-2018-4-20

    [kindle笔记]读书记录-总 在答辩和考试和各种大作业的重压以及两天后全校停电的巨大挤压中,一口气读完了恶意这本书. 这本书是我读的东野圭吾的第二本书.第一本是心心念念的<解忧杂货店> ...

  4. PAT L2-016 愿天下有情人都是失散多年的兄妹

    https://pintia.cn/problem-sets/994805046380707840/problems/994805061769609216 呵呵.大家都知道五服以内不得通婚,即两个人最 ...

  5. a标签中的onclick和href的使用

    onclick和href 链接的 onclick 事件被先执行,其次是 href 属性下的动作(页面跳转,或 javascript 伪链接):  假设链接中同时存在 href 与 onclick,如果 ...

  6. [转帖]SSH 的 三种代理功能.

    https://www.zhihu.com/people/skywind3000/activities 之前用过frp 知道 ssh 有一 反向代理的功能 没想到 还有这么多高级功能 收藏一下. ss ...

  7. spring AOP源码分析(一)

    对于springAOP的源码分析,我打算分三部分来讲解:1.配置文件的解析,解析为BeanDefination和其他信息然后注册到BeanFactory中:2.为目标对象配置增强行为以及代理对象的生成 ...

  8. input type=date时,时间数据回填,报错The specified value "2019-0404-18" does not conform to the required format, "yyyy-MM-dd".

    <input autocomplete id="start-time" name="start_time" type="date" c ...

  9. C# Note17: 使用Ionic.Zip.dll实现解压缩文件

    首先下载ionic.Zip.dll,然后在项目中添加该引用,之后就可以在cs中使用了: using Ionic.Zip; #region Ionic.Zip压缩文件 private readonly ...

  10. 关于golang.org/x包问题

    关于golang.org/x包问题 由于谷歌被墙,跟谷歌相关的模块无法通过go get来下载,解决方法: git clone https://github.com/golang/net.git $GO ...