codeforces 11B Jumping Jack
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
2
3
6
3
0
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的更多相关文章
- Codeforces 11B Jumping Jack(数学)
B. Jumping Jack time limit per test 1 second memory limit per test 64 megabytes input standard input ...
- cf 11B Jumping Jack(贪心,数学证明一下,,)
题意: 给一个数X. 起始点为坐标0.第1步跳1格,第2步跳2格,第3步跳3格,.....以此类推. 每次可以向左跳或向右跳. 问最少跳几步可以到坐标X. 思路: 假设X是正数. 最快逼近X的方法是不 ...
- Jumping Jack CodeForces - 11B
Jumping Jack CodeForces - 11B 就是一个贪心. 基本思路: 正负没有关系,先取绝对值. 首先跳过头,然后考虑怎么回来. 设超过头的步数为kk.如果kk为偶数,那么直接在前面 ...
- Codeforces Beta Round #11 B. Jumping Jack 数学
B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...
- codeforces 11 B.Jumping Jack 想法题
B. Jumping Jack Jack is working on his jumping skills recently. Currently he's located at point zero ...
- [BFS,大水题] Codeforces 198B Jumping on Walls
题目:http://codeforces.com/problemset/problem/198/B Jumping on Walls time limit per test 2 seconds mem ...
- 苏州大学ICPC集训队新生赛第二场
A - Score UVA - 1585 水 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin> ...
- [Elasticsearch] 多字段搜索 (三) - multi_match查询和多数字段 <译>
multi_match查询 multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询. NOTE 存在几种类型的multi_match查询,其中的3种正好和在“了解你的数据”一节中提 ...
- Elasticsearch: 权威指南 » 深入搜索 » 多字段搜索 » 多数字段 good
跨字段实体搜索 » 多数字段编辑 全文搜索被称作是 召回率(Recall) 与 精确率(Precision) 的战场: 召回率 ——返回所有的相关文档:精确率 ——不返回无关文档.目的是在结果的 ...
随机推荐
- 入门OJ:photo
题目描述 有N个人,来自K个家族.他们排成一行准备照相,但是由于天生的排外性,每个人都希望和本家族的人站在一起,中间不要加入别的家族的人.问最少从队列中去掉多少个就可以达到这个目的. 输入格式 第一行 ...
- Mybatis总结(一)
Mybatis总结(一) 一.Mybatis启动流程(代码层面) 关于config.xml <?xml version="1.0" encoding="UTF-8& ...
- 深度漫谈数据系统架构——Lambda architecture
https://mp.weixin.qq.com/s/whmhm2yzug2WVdH3dTq8hg
- Why should I avoid blocking the Event Loop and the Worker Pool?
Don't Block the Event Loop (or the Worker Pool) | Node.js https://nodejs.org/en/docs/guides/dont-blo ...
- c++ 三五法则 自己理解
简介 三五法则规定了什么时候需要 1 拷贝构造函数 2 拷贝赋值函数 3 析构函数 1. 需要析构函数的类也需要拷贝构造函数和拷贝赋值函数. 通常,若一个类需要析构函数,则代表其合成的析构函数 ...
- 【转载】【网络安全】渗透中 PoC、Exp、Payload 与 Shellcode 的区别
原文地址 渗透中 PoC.Exp.Payload 与 Shellcode 的区别 概念 PoC,全称"Proof of Concept",中文"概念验证",常指 ...
- 正则re高级用法
search 需求:匹配出文章阅读的次数 #coding=utf-8 import re ret = re.search(r"\d+", "阅读次数为 9999" ...
- 分布式缓存 — kafka
Kafka是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就是可以实时的处理大量数据以满足各种需求场景:比如基于h ...
- mysql 用户 登陆 权限相关
1. CREATE USER 'dog2'@'localhost' IDENTIFIED BY ''; 将"localhost"改为"%",表示在任何一台电脑上 ...
- 获取 *.properties配置文件内容
package com.loan.modules.common.util; import java.util.ResourceBundle; /** * 获取 *.properties配置文件内容 * ...