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. UVA-10375 唯一分解定理

    #include<iostream> #include<string.h> #include<algorithm> #include<math.h> # ...

  2. iOS-拍照后裁剪,不可拖动照片的问题

    2016.07.08 15:04* 字数 1837 阅读 6066评论 6喜欢 26赞赏 1 问题 在项目中,选择照片或拍照的功能很长见,由于我之前采用系统自带的UIimagePickViewCont ...

  3. centos7下安装python3.6

    一.wget 官网下载到本地 进入家目录: cd ~ wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz下载到本地 解压移动到/ ...

  4. ntpd、ntpdate、hwclock的区别

    hwclock --systohc 使用ntpdate更新系统时间 - 潜龙勿用 - CSDN博客https://blog.csdn.net/suer0101/article/details/7868 ...

  5. react初入门

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Java Map 集合实现类

    Map 用于保存具有映射关系的数据,集合里会保存两组值,一组用于保存Map里的key,一组用于保存Map里的value,key与map可以是任何引用类型数据.Map的key不允许重复.key与valu ...

  7. Java 简单的登录验证码

    1 验证码的作用 验证码是为了区分人与机器,如果没有验证码机制,web网站或者应用会遇到很多问题,具体如下: ① 网站容易被暴力登录攻破密码,可以制作一个自动程序不断的尝试登录,密码很容易被破解,系统 ...

  8. js中的arguments

    了解这个对象之前先来认识一下javascript的一些功能: 其实Javascript并没有重载函数的功能,但是Arguments对象能够模拟重载.Javascrip中国每个函数都会有一个Argume ...

  9. python读文件指定行的数据

    import linecacheprint linecache.getline('url.txt',2) 读取url.txt文件的第2行内容

  10. Yii2总结

    1. Web访问流程(即在浏览器中输入一个网址至浏览器展现页面结果的过程) a. 将输入的网址提取出域名,在本地hosts文件中查找对应的IP地址(windows为C:/windows/system3 ...