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. JAVA编程中button按钮,actionlistener和mouseClicked区别

    在java的编程中,对于按钮button 有两个事件: 1.actionPerformed 2.mouseClicked 区别: actionPerformed:一般事件,仅侦听鼠标左键的单击事件,右 ...

  2. 词嵌入之FastText

    什么是FastText FastText是Facebook于2016年开源的一个词向量计算和文本分类工具,它提出了子词嵌入的方法,试图在词嵌入向量中引入构词信息.一般情况下,使用fastText进行文 ...

  3. 用CSS制做一个三角形!

    用CSS制做一个三角形! <style> .outer { width: 0; height: 0; border-left: 10px solid transparent; border ...

  4. 【Windows】Win10家庭版启用组策略gpedit.msc

    [前言] 大家都认为,Windows 10家庭版中并不包含组策略,其实不然,它是有相关文件的,只是不让你使用而已.那么我们让系统允许你使用就好了. [操作步骤] 1.首先你需要在桌面上新建一个txt文 ...

  5. C指针的这些使用技巧,掌握后立刻提升一个Level

    这是道哥的第016篇原创 关注+星标公众号,不错过最新文章 目录 一.前言 二.八个示例 1. 开胃菜:修改主调函数中的数据 2. 在被调用函数中,分配系统资源 2.1 错误用法 2.2 正确用法 3 ...

  6. 2.kafka架构深入——生产者

    一个topic有多个partition,每个partition又有多个副本,在这些副本中又有一个leader和多个follower. 1)分区的原因 (1)方便在集群中扩展,每个Partition可以 ...

  7. Enabling Session Persistence 粘性会话

    NGINX Docs | HTTP Load Balancing https://docs.nginx.com/nginx/admin-guide/load-balancer/http-load-ba ...

  8. VMware vSphere (EXSI) 安装使用

    VMware vSphere 镜像下载 VMware vSphere Hypervisor (ESXi) 6.7 https://my.vmware.com/cn/web/vmware/downloa ...

  9. 「笔记」AC 自动机

    目录 写在前面 定义 引入 构造 暴力 字典图优化 匹配 在线 离线 复杂度 完整代码 例题 P3796 [模板]AC 自动机(加强版) P3808 [模板]AC 自动机(简单版) 「JSOI2007 ...

  10. loj10161 叶子的颜色

    CQOI 2009 给一棵有 mm 个节点的无根树,你可以选择一个度数大于 11 的节点作为根,然后给一些节点(根.内部节点.叶子均可)着以黑色或白色.你的着色方案应保证根节点到各叶子节点的简单路径上 ...