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
  1. 2
Output
  1. 3
Input
  1. 6
Output
  1. 3
Input
  1. 0
Output
  1. 0

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

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

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

    今天发现一个视图,通过SM30居然无法维护,这个视图就是V_160M,表为T_160M,是采购相关的系统消息, 不过别着急,有办法维护的,呵呵,看下面: 试一试OMCQ这个事物代码吧! 分享出来,给需 ...

  2. Spring Boot(IDEA,Gradle)超详细用户管理项目(一)——Hello World

    1.构建工具的配置(Gradle):自定义-所有设置:构建.执行.部署-构建工具-Gradle: 设置Gradle用户主目录:(该目录相当于仓库,gradle将下载所需依赖到此目录下),此目录下可新建 ...

  3. uni-app 获取地址位置

    uni.getLocation 获取当前的地理位置.速度. 在微信小程序中,当用户离开应用后,此接口无法调用:当用户点击"显示在聊天顶部"时,此接口可继续调用 uni.getLoc ...

  4. 命名秘籍周获近五千星——GitHub 热点速览 v.21.04

    作者:HelloGitHub-小鱼干 命名一直是编程界的难点,这次 naming-cheatsheet 就能帮上你的忙.按照它的 SID(Short..Intuitive.Descriptive)原则 ...

  5. C++ unordered_map/unordered_set 自定义键类型

    1. unordered_map 和 unordered_set template < class Key, // unordered_map::key_type class T, // uno ...

  6. JavaScript中函数的调用!

    JavaScript中函数的调用! 1 普通函数 // 1 普通函数 function fn() { console.log(123); } // 函数名 + 一个小括号! 或者 函数名.call() ...

  7. 【转】使用ssh-keygen和ssh-copy-id三步实现SSH无密码登录

    [原]http://blog.chinaunix.net/uid-26284395-id-2949145.html ssh-keygen  产生公钥与私钥对. ssh-copy-id 将本机的公钥复制 ...

  8. Any race is a bug. When there is a race, the compiler is free to do whatever it wants.

    https://mp.weixin.qq.com/s/pVJiFdDDKVx707eKL19bjA 谈谈 Golang 中的 Data Race 原创 ms2008 poslua 2019-05-13 ...

  9. Python Data Structure and Algorithms Tutorial

    Python - Algorithm Design - Tutorialspoint https://www.tutorialspoint.com/python_data_structure/pyth ...

  10. 【练习】goroutine chan 通道 总结

    1. fatal error: all goroutines are asleep - deadlock! 所有的协程都休眠了 - 死锁! package mainimport("fmt&q ...