1455B.Jumps
链接:
https://codeforces.com/problemset/problem/1455/B
题意
有一个点,值为x,为类似数组a1,a2,a3,…ai的和,规定从0开始,从a1开始,每次可以选择加ai中的i,或者选择-1。
问最少为几个a的和?(有多组案例)
Example
input
5
1
2
3
4
5
output
1
3
2
3
4
解析
可以发现如果都是选择+i的话,和相当于为公差为1的等差数列的前n项和:1 + 2 + 3 + 4 + 5 +…+ n = n * (n + 1) / 2;将这个值与x进行对比,如果正好等于,则此刻的n即为正确答案。
如果大于再进行特别判断;设前n项和为t,判断t - 1和x的关系,如果t - 1 = x,说明再原有的n个数上还要再来一次-1的操作,所以答案就为n + 1;
如果t - 1 > x;用t - 1减去x得到值w = t - 1 - x;会发现该值一定是小于n的,所以都能在前面中找到该值,该值说明的是x与前n项和t相差w + 1,所以如果把前面中出现的w值那一步替换成-1,即t - w - 1 = t - (w + 1);此时结果就会得到x的值。
举个例子如下:
#include <iostream>using namespace std;int main()
{int _;cin >> _;while (_--){int x;cin >> x;int n = 0;while ((n + 1) * n / 2 < x){n++;}int t = n * (n + 1) / 2 - 1;if (t == x) n++;cout << n << endl;}return 0;
}
1455B.Jumps相关推荐
- codeforces 有意思的思维题 1 ~ 15
codeforces 思维题 1.给定数组,求满足i < j and ai * aj = i + j的数对数量 2.第 i 步向前跳 i 步或后退 1 步 3.给两个点,求正方形的另两个点 4. ...
- 1. 青蛙跳跳FrogJmp Count minimal number of jumps from position X to Y.
青蛙跳跳: package com.code;public class Test03_1 {public int solution(int X, int Y, int D) {int res = (Y ...
- codeforces 480B B. Long Jumps(贪心)
题目链接: B. Long Jumps time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [CF791D]Bear and Tree Jumps
题目描述 A tree is an undirected connected graph without cycles. The distance between two vertices is th ...
- Frog Jumps CodeForces - 1324C(二分)
There is a frog staying to the left of the string s=s1s2-sn consisting of n characters (to be more p ...
- [codeforces 1324C] Frog Jumps 一直向右+边界处理
Codeforces Round #627 (Div. 3) 比赛人数6434 [codeforces 1324C] Frog Jumps 一直向右+边界处理 总目录详见https://bl ...
- Frog Jumps
There is a frog staying to the left of the string s=s1s2-sn consisting of n characters (to be more p ...
- 木瓜移动每周跨境资讯:TikTok测试Jumps,店小秘收购17TRACK,Facebook推出新闻通讯平台Bulletin
出海相关 TikTok要在海外推"小程序"了.近日,TikTok测试了一项叫做 Jumps 的新功能,该功能下创作者可以将一些第三方 App 以类似"微信小程序" ...
- LeetCode——1824. 最少侧跳次数(Minimum Sideway Jumps)[中等]——分析及代码(Java)
LeetCode--1824. 最少侧跳次数[Minimum Sideway Jumps][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划(二维数组) (1)思路 (2)代 ...
最新文章
- 086_访问html元素
- 开源:Sagit.Framework For IOS 开发框架
- 电商仓储管理水平的五大标准
- Spring boot属性松散绑定
- Qt修炼手册8_常用的容器类QVector和QList
- ngnix之rewrite
- 一文教你使用java开发一款坦克大战游戏
- [ZJOI2009]取石子游戏
- 用mobiscroll.js的treelist实现弹出下拉效果
- LDA︱基于LDA的Topic Model变形+一些NLP开源项目
- python 灰度图像转彩色_彩色图像--伪彩处理 灰度图转伪彩色图像
- python3.10下载安装(附python学习教程)
- windows桌面图标全部不见了怎么办
- 如何使用 Xcode8 进行开发调试
- 开源RPC性能比拼测试 : 一不小心我们进了第一梯队
- 物联网已经发展多年,为什么一直不温不火
- Android手机Home键/Back回退键事件
- 服务器环境-安装基础环境
- html5 实现游戏直播,网页制作HTML5播放实现rtmp流直播
- java读取微信证书_Java中的微信支付(2):API V3 微信平台证书的获取与刷新