B. Jumping Jack
time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

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.

Sample test(s)
input
2
output
3
input
6
output
3
input
0
output
0

题意:

Jack在数轴上跳。他第i次跳能够往左或往右跳i个单位。如今问你他最少花多少步跳到点x。

思路:

能够无论x正负把x变成正数。这样是等效的。然后他最快的跳法当然是往一个方向一直跳。假设这样恰好能到x。这样肯定是步数最小的解。假设不能恰好跳到。我们设按这样的方法跳第一个比x大的位置为y。

假设y-x是偶数的话。我们还是能够达到最优步数。

就是先将第(y-x)/2步往左跳。其他不变。那么恰好也能到x。假设为奇数。变成下一个偶数即可了。为什么能够这样呢。由于无论如何。x=(±1)+(±2)+(±3)+.......(±n)。所以要想跳到必须奇偶性一致。比y小一定不行、所以仅仅有在y右边找了。

具体见代码:

#include<bits/stdc++.h>
int main()
{
int tp,x,ans;
while(~scanf("%d",&x))
{
x=x<0?-x:x,ans=ceil((-1+sqrt(1+8.0*x))/2),tp=ans*(ans+1)/2-x;
while(tp&1) ans++,tp+=ans;
printf("%d\n",ans);
}
return 0;
}

Codeforces 11B Jumping Jack(数学)的更多相关文章

  1. codeforces 11B Jumping Jack

    Jack is working on his jumping skills recently. Currently he's located at point zero of the number l ...

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

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

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

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

  4. Jumping Jack CodeForces - 11B

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

  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. CodeForces 534C Polycarpus' Dice (数学)

    题意:第一行给两个数,n 和 A,n 表示有n 个骰子,A表示 n 个骰子掷出的数的和.第二行给出n个数,表示第n个骰子所能掷出的最大的数,这些骰子都有问题, 可能或多或少的掷不出几个数,输出n个骰子 ...

  8. codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)

    题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) ——数学相关知识: 首先:我们知道一些事情,对于k,假设有ci%k==0,那么一定能确定x%k的值,比如k=5和ci=20,知道x% ...

  9. Codeforces Gym 100269G Garage 数学

    Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...

随机推荐

  1. 51nod 1526 分配笔名(Trie树+贪心)

    建出Trie树然后求出一个点子树中有多少笔名和真名.然后贪心匹配即可. #include<iostream> #include<cstring> #include<cst ...

  2. [SCOI2012]喵星球上的点名(树状数组+后缀数组)

    我们把所有的名,姓,询问都拼起来构成一个新的长串,然后跑一边SA.排完序后对于每一个询问,我们可以二分求出它所对应的区间(即满足这个区间的前缀都是这个询问串).然后问题就转化为很多区间问区间出现过的不 ...

  3. spring中IOC的简单使用

    spring的ioc主要就是依赖注入,有基于构造器的依赖注入还有通过设值注入,这里我只简单的实现设值注入的方法,通过spring的依赖管理,我们可以很方便的了解各层之间的依赖关系,降低了各层之间的耦合 ...

  4. pandas 3 设置值

    from __future__ import print_function import pandas as pd import numpy as np np.random.seed(1) dates ...

  5. 让div垂直居中

    参考链接:https://www.cnblogs.com/softwarefang/p/6095806.html 以前我的方法总是比较粗暴,纯粹通过margin来实现,这个方法的缺点不仅在于需要多次微 ...

  6. Adobe Flex迷你教程 —Flex圆角容器

    在Flex3时代可以设置borderSides属性达到圆角效果,如:borderSides="top left right" ,在Flex4中borderSides属性貌似已经没有 ...

  7. WPF 内部的5个窗口之 MediaContextNotificationWindow

    原文:WPF 内部的5个窗口之 MediaContextNotificationWindow 本文告诉大家在 WPF 内部的5个窗口的 MediaContextNotificationWindow 是 ...

  8. ie11 .pac代理脚本无法使用的问题

    参考: http://blogs.msdn.com/b/ieinternals/archive/2013/10/11/web-proxy-configuration-and-ie11-changes. ...

  9. C++primer书店程序

    #include <iostream> #include <string> #include <cassert> #include <algorithm> ...

  10. leetcode 刷题之路 66 Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...