Title

CF279A Point on Spiral 题解

解题思路

按照题目中的规律画出来的图片如下:

那么,我们直接根据规律判断当前查询的节点在那一条线段上就可以了。易得,当前的基础转向次数为 max(|x|1,|y|1)×4,那么加上一个在当前周期内部的转向次数就可以了。

AC 代码

#include<bits/stdc++.h>
#define ll long long
#define ull usigned long long
using namespace std;
const string TypideName="c";
inline void readc(char &c){
    c=getchar();
    while(c==' '||c=='\n')
        c=getchar();
}inline void writec(char c){putchar(c);}
template<typename T>inline void read(T& x) {
    if(typeid(x).name()==TypideName){char ch;readc(ch);x=ch;return;}
    x = 0; bool f = false; char ch = getchar();
    while (ch < '0' || ch>'9') { if (ch == '-') f = !f; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
    x = (f ? -x : x); return;
}template<typename T>inline void put(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x > 9) put(x / 10);
    putchar(x % 10 + '0'); return;
}template<typename T>inline void write(T x) {
    if(typeid(x).name()==TypideName){writec(x);return;}
    put(x);
}
template<typename T,typename... Args>
inline void read(T& x,Args&...x_) {read(x),read(x_...);}
template<typename T,typename... Args>
inline void write(T x,Args...x_){write(x),write(x_...);}
int x,y;
inline void work(){
    read(x,y);
    if(x==0&&y==0) {write(0);return;}
    int ans=(max(abs(x),abs(y))-1)*4;
    if(y<0&&x>y&&x<=-y) ans+=4;
    else if(x==max(abs(x),abs(y))&&y>1-max(abs(x),abs(y))&&y<=max(abs(x),abs(y))) ans+=1;
    else if(y==max(abs(x),abs(y))&&x>=-max(abs(x),abs(y))&&x<=max(abs(x),abs(y))) ans+=2;
    else if(x==-max(abs(x),abs(y))&&y>=-max(abs(x),abs(y))&&y<=max(abs(x),abs(y))) ans+=3;
    write(ans,'\n');
}
signed main(){
    work();
    return 0;
}
posted @   UncleSam_Died  阅读(1)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示