Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.

Input

The input data consists of only one integer x ( - 109 ≤ x ≤ 109).

Output

Output the minimal number of jumps that Jack requires to reach x.

Example

Input
2
Output
3
Input
6
Output
3
Input
0
Output
0

这题想的很僵硬,想了一个小时才想出来。
题意:步数从1开始递增,只能选择向左走和向右走。问到达x最少要走几次。
解题思路:可以很轻易的(我推了半小时才反应过来)的想出 x=1±2±3±...n;
对吧,因为不是向左走就是向右走嘛,所以不是加就是减。
这样我们可以推出x肯定在1到1+2+3+...n之间(包括n。
所以我们找第一个大于等于x的 1~n和,如果刚好等于就直接输出步数。
如果大于x,就找第一个大于x且与x差为偶数的数。(这需要一点脑洞
因为x+n与x-n的差肯定是个偶数,而1~n的和包含了从2,4,6,8一直到n的所有偶数,所以只要找到离x最近的差为偶数的 1~n和,就是答案了。

说的比较乱,不懂的话可以自己写1到10的例子走一下就明白了。

附ac代码:
 1 #include <cstdio>
2 #include <iostream>
3 #include <cmath>
4 #include <string>
5 #include <cstring>
6 #include <algorithm>
7 #include <queue>
8 #include <map>
9 #include <vector>
10 using namespace std;
11 const int maxn = 1e6+10;
12 typedef long long ll;
13 const ll mod = 1e9+7;
14 const int inf = 0x3f3f3f3f;
15 const double eps=1e-6;
16 ll ans[maxn];
17 ll l[maxn];
18 ll r[maxn];
19 int main() {
20 ios::sync_with_stdio(false);
21 int n;
22 cin>>n;
23 n=abs(n);
24 if(n==0)
25 {
26 cout<<0<<endl;
27 return 0;
28 }
29 for(int i=1;i<=1e9;++i)
30 {
31 ll s=i*(i+1)/2;
32 if(s==n)
33 {
34 cout<<i<<endl;
35 break;
36 }
37 else if(s>n &&(s-n)%2==0)
38 {
39 cout<<i<<endl;
40 break;
41 }
42 }
43 return 0;
44 }


codeforces 11B Jumping Jack的更多相关文章

  1. Codeforces 11B Jumping Jack(数学)

    B. Jumping Jack time limit per test 1 second memory limit per test 64 megabytes input standard input ...

  2. cf 11B Jumping Jack(贪心,数学证明一下,,)

    题意: 给一个数X. 起始点为坐标0.第1步跳1格,第2步跳2格,第3步跳3格,.....以此类推. 每次可以向左跳或向右跳. 问最少跳几步可以到坐标X. 思路: 假设X是正数. 最快逼近X的方法是不 ...

  3. Jumping Jack CodeForces - 11B

    Jumping Jack CodeForces - 11B 就是一个贪心. 基本思路: 正负没有关系,先取绝对值. 首先跳过头,然后考虑怎么回来. 设超过头的步数为kk.如果kk为偶数,那么直接在前面 ...

  4. Codeforces Beta Round #11 B. Jumping Jack 数学

    B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...

  5. codeforces 11 B.Jumping Jack 想法题

    B. Jumping Jack Jack is working on his jumping skills recently. Currently he's located at point zero ...

  6. [BFS,大水题] Codeforces 198B Jumping on Walls

    题目:http://codeforces.com/problemset/problem/198/B Jumping on Walls time limit per test 2 seconds mem ...

  7. 苏州大学ICPC集训队新生赛第二场

    A - Score UVA - 1585 水 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin> ...

  8. [Elasticsearch] 多字段搜索 (三) - multi_match查询和多数字段 <译>

    multi_match查询 multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询. NOTE 存在几种类型的multi_match查询,其中的3种正好和在“了解你的数据”一节中提 ...

  9. Elasticsearch: 权威指南 » 深入搜索 » 多字段搜索 » 多数字段 good

      跨字段实体搜索  » 多数字段编辑 全文搜索被称作是 召回率(Recall) 与 精确率(Precision) 的战场: 召回率 ——返回所有的相关文档:精确率 ——不返回无关文档.目的是在结果的 ...

随机推荐

  1. 使用Spring的RestTemplate进行接口调用

    引自:http://www.zimug.com/ 1.常见的http服务的通信方式 经常使用的方式有HttpClient.OkHttp.RestTemplate.其中RestTemplate是一种更优 ...

  2. unity3D进阶

    前言 在之前的例子中,我们都没有用到unity的精髓,例如地形系统.物理系统.粒子系统等,本文记录unity3D的进阶简单应用 前期准备 https://unity.cn/releases/full/ ...

  3. CISCO 如何重置3850交换机密码

    SUMMARY STEPS: Connect a terminal or PC to the switch. Set the line speed on the emulation software ...

  4. REST 架构的替代方案 为什么说GraphQL是API的未来?

    Managing enterprise accounts - GitHub Docs https://docs.github.com/en/graphql/guides/managing-enterp ...

  5. Python爬虫学习笔记(三)

    Cookies: 以抓取https://www.yaozh.com/为例 Test1(不使用cookies): 代码: import urllib.request # 1.添加URL url = &q ...

  6. 硬核!八张图搞懂 Flink 端到端精准一次处理语义 Exactly-once(深入原理,建议收藏)

    Flink 在 Flink 中需要端到端精准一次处理的位置有三个: Source 端:数据从上一阶段进入到 Flink 时,需要保证消息精准一次消费. Flink 内部端:这个我们已经了解,利用 Ch ...

  7. linux下安装 zookeeper-3.4.9并搭建集群环境

    本文主要记录作者在实践过程中实现在centos7环境下安装zookeeper并搭建集群的详细步骤,关于zookeeper本文将不做详细介绍,安装步骤详情如下: 前提准备:3台linux服务器(因为zo ...

  8. 调用ajax 跨域调用接口

    //ajax 跨域请求数据 function ajaxType (){ $.ajax({ url: "http://127.0.0.1:9090/spring_mvc/HttpClient/ ...

  9. (27)Vim 3

    Vim移动光标快捷键汇总1.Vim快捷方向键 h 光标向左移动一位 j 光标向下移动一行(以回车为换行符),也就是光标向下移动 k 光标向上移动一行(也就是向上移动) l 光标向右移动一位2.Vim光 ...

  10. BPF CO-RE 示例代码解析

    BPF CO-RE 示例代码解析 在BPF的可移植性和CO-RE一文的末尾提到了一个名为runqslower的工具,该工具用于展示在CPU run队列中停留的时间大于某一值的任务.现在以该工具来展示如 ...