Ivan and Powers of Two

STM32超声波避障小车硬件设计与实时测距实现 超声波测距是嵌入式系统中经典的环境感知技术,其原理基于声波在空气中的传播时间与距离的线性关系,通过精确计时实现非接触式距离测量。该技术具备成本低、抗光干扰强、算法简洁等优势,广泛应用于智能小车、机器人避障、液位监测等场景。工程落地的关键在于硬件信号完整性(如5V/3.3V电平匹配)、微秒级定时精度(依赖高稳定外部晶振与输入捕获模式)以及强干扰环境下的数据鲁棒性(需软硬协同滤波)。本文以STM32F103C8T6为平台,深入解析HC-SR04模块的时序闭环控制、TIM3输入捕获双沿测量、L298N电机状态机驱 阅读详情

 Ivan and Powers of Two
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order.

Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0)need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0).

Help Ivan, find the required quantity of numbers.

Input

The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an.

Output

Print a single integer — the answer to the problem.

Sample test(s)
input
4
0 1 1 1
output
0
input
1
3
output
3
Note

In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7.

In the second sample you need to add numbers 20, 21, 22.


思路:这里写中文思路吧,下面用英文下思路是因为四级还没过关,囧,必须要练练,不然完蛋了有木有啊

贪心。

首先要准备构造等比序列吧

1、如果序列里面没有相同的很好办

2、如果非降序序列有相同的怎么办?

必须合并相同的两个数,直到当前的这个数在集合里面唯一。

理由:

(1)、a,a 合并成a+1是因为:2^a+2^a=2*2^a=2^(a+1),合并成一个和再加一个来和a进行组合是一样的花费

(2)、再说2^a已经是大于低位的所有数字之和,所以出现2个a绝对不会考虑把a拿到低位去和其他数拼凑成低位的其他缺的数。也就是说对于当前ai而言:

不用去管低位,低位有什么就是什么,没什么就得补上,ai只需要往上考虑即可


AC Program:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<set>
using namespace std;
//a,a exists,it must be combined cause 
//(1)a,a->a+1  can minimize the number
//(2)2^a has been bigger than the lower bit numbers,it's impossible to be used to make
//the sum of lower bit numbers.
int main()
{
set<int>ss;
int n;
int a;
cin>>n;
int maxn=0;
for(int i=0;i<n;i++){
   cin>>a;
   while(ss.count(a))ss.erase(a),a++;//2^a+2^a=2*2^a=2^(a+1)//change until 'a' are the only one.
   ss.insert(a);        
   maxn=max(a,maxn); 
}
cout<<maxn+1-ss.size()<<endl;
//system("Pause");
return 0;}





















别再怕非观测区了!手把手教你用移相法搞定PMSM单电阻FOC的电流重构难题 本文详细解析了永磁同步电机(PMSM)单电阻FOC系统中非观测区的电流重构难题,重点介绍了移相法的工程实践。通过STM32平台的具体实现和调试指南,帮助工程师有效解决相电流检测中的采样窗口不足问题,提升系统稳定性和性能。 阅读详情

相关推荐

手把手教你学Simulink--基于火电发电系统的场景实例:燃煤发电机组的锅炉-汽轮机协调控制仿真

本文介绍了使用Simulink构建燃煤发电机组锅炉-汽轮机协调控制系统的仿真方法。系统包含锅炉子系统、汽轮机子系统和协调控制器三大模块,通过传递函数建模和PID控制策略实现协调控制。详细说明了从模型创建、子系统设置到控制器设计的完整流程,包括燃料供给、汽轮机模型和数据处理等关键环节。最后通过性能评估验证系统响应速度、参数稳定性和效率等指标。该仿真方法为火电系统控制策略设计和优化提供了有效工具。

MHD0815的博客 1153

Powers of Two

You are givennintegersa1, a2, ..., an. Find the number of pairs of indexesi, j(i < j) thatai + ajis a power of2(i. e. some integerxexists so thatai + aj = 2x). 题目意思让你找有多少个2个组合的数字能是2的幂方 直接暴力搜 #include "bits/stdc++.h" #define ll long l...

weixin_53013914的博客 498

版图学习汇总十一(DRC、LVS注意事项)

esd。

weixin_56071389的博客 2262

Powers Of Two(二进制拆分)

题目链接:http://codeforces.com/contest/1095/problem/C Examples input 9 4 output YES 1 2 2 4 input 8 1 output YES 8 input 5 1 output NO input 3 7 output NO 题意:n能否拆成k个2的整数次方相加,如果能输出YES以及一种拆法,否则输出NO 思路:先找出n...

永夜莫明的博客 1285

Qwen2.5-7B大模型实战:从零部署到高效推理全流程解析

本文详细解析了Qwen2.5-7B大模型从零部署到高效推理的全流程。内容涵盖环境准备、模型下载、使用Transformers进行核心对话推理,并深入介绍了vLLM与TGI生产级服务部署、GPTQ/AWQ量化技术以降低显存消耗,以及llama.cpp、Ollama等本地推理方案。旨在帮助开发者快速掌握部署技巧,实现大模型在消费级硬件上的高效运行。

weixin_29061425的博客 490

别再手动算考勤了!教你用Python+企业微信API自动生成月度报表

本文介绍如何利用Python和企业微信API构建自动化考勤分析系统,实现从数据获取到可视化报表生成的全流程自动化。通过企业微信打卡接口获取数据,使用Pandas进行高效清洗和分析,并生成Excel和HTML格式的报表,大幅提升考勤统计效率。系统支持智能判定考勤状态,如迟到、早退等,并可通过定时任务实现月度自动化运行。

weixin_27875131的博客 290

Additive Powers-of-Two (APoT) Quantization:硬件友好的非均匀量化方法

Additive Powers-of-Two Quantization:硬件友好的非均匀量化方法摘要方法Additive Powers-of-Two量化 (APoT)量化表示均匀量化表示Powers-of-Two (PoT) 量化表示Additive Powers-of-Two(APoT)量化表示参数化Clipping函数 (RCF)权重归一化APoT量化伪代码实验结果CIFAR-10ImageNet 本文是电子科大&哈佛大学&新加坡国立联合发表在 ICLR2020 上的一篇非均匀量化(A

u012347027的博客 3970

华为OD技术面八股文_C++_01

汇总华为OD技术面C++方向八股文真题,并提供详细解答。

qq_45776114的博客 1887

codeforces 题目 Powers Of Two

思维,二进制

2301_76575887的博客 568

ACC自适应巡航系统

ACC自适应巡航系统可在高速驾驶情况下,自动控制纵向跟车距离,减轻驾驶员的工作量。该功能主要依靠安装在车辆前挡风玻璃上的摄像头和布置在格栅附近的雷达进行融合实现。

未知艺术家的博客 204

不同IP网段的PLC进行S7通信(PUT_GET)的具体步骤

不同IP网段的PLC进行S7通信(PUT_GET)的具体步骤 一般情况下,我们使用西门子的S7通信时都是将网络中的PLC的IP设置在同一网段,那么如果涉及到旧项目改造时,不方便修改IP地址,只能进行2个网段通信时,可以参考以下步骤: 准备条件: 2个路由器 CPU 1212C 主动连接 CPU 314C-2PN/DP 被动连接 网络搭建如下图所示: S7-1212C一侧 建立S7主动连接 ,IP地址:192.168.5.40,编写get/put 通信程序 S7-300 IP地址:192.168.0

Robot_PLC_HMI_Automation 2万+

Power of Two 判断是否为2的次方

Power of Two   Given an integer, write a function to determine if it is a power of two. Credits: Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

小人物的草稿本 599

【网络安全】浅谈数据库攻击复现及相关安全优化

4、准备数据库服务器为了安全正式环境应该把数据库单独放一台服务器,只允许让应用服务器 IP 来访问数据库服务器,通过独立两者,就算其中一台发生问题,另一台也是好的,进一步降低了危险性。5、定时备份数据库开发定时备份功能,可以使用 Crontab,或者其他框架,设定周期备份数据,哪怕是数据丢失或被删除也可以快速恢复。

2401_88545587的博客 221

CF305C Ivan and Powers of Two 题解

【洛谷】CF305C Ivan and Powers of Two 题解

gly2023的博客 413

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Ivan and Powers of Two(杂)

integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0)need to be added to the piece of

u014135990的专栏 153

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

codeforces 305 C. Ivan and Powers of Two

题目链接 给出一个非减序的数组a[n], 然后得到s=2^a1+.……+2^an, 要使s为2^v -1,需要在数组中添加几个数。 我的思路是这样的,由2^a+2^a = 2^(a+1)可知,如果有两个连续的数a,我们可以把他们合并为a+1放入集合中,使集合中没有重复的数,我可以用stl里的set。如果想要满足题目中的要求,集合中必须有最大那个数个元素...

145

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

CodeForces 305C Ivan and Powers of Two【set】

这个题啊,纠结了本公举一中午,知道如果出现了重复的,就要往后移,差点就把自己想的恶心的bool 数组付诸实现了,== 既然是思维的题,借助了STL的特性是再正常不过的了,既然想写数组,先应该想想Set这货啊,vector这货虽是不定长数组,但是不排序,不去重。 这个题还用到的一个特性是2*(2^n+2^n)=2^(n+1)所以如果遍历的时候,当前位存在了,那么很自然的他俩就没了,组合成大一倍的

MissZhou要努力 572

CodeForces 404C Ivan and Powers of Two

Ivan and Powers of Two Time Limit:1000MSMemory Limit:262144KB64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 404C Description Valera had an undirected conne...

weixin_30752699的博客 132

【CodeForces - 305C】Ivan and Powers of Two(思维)

C. Ivan and Powers of Two time limit per test 0.5 seconds memory limit per test 256 megabytes input standard input output standard output Ivan has got an array of n non-negative integers a1, ...

__似水流年__的博客 354

Ivan and Powers of Two CodeForces - 305C(set)

Ivan has got an array of n non-negative integers a1, a2, …, an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, …, 2an on a piece of paper. Now he wo...

starlet_kiss的博客 319

CF_305_C. Ivan and Powers of Two

Sample test(s) input 40 1 1 1 output 0 input 13 output 3 题目大致意思: 输入a[1],a[2]....a[n]n个非负整数,再添加最少数目的这样整数使得所有的整数以2为底的整数之后满足2^p(p>=0)的形式。 解题思路: 将输入的各个整数a[i]看看成整数的第a[i]个非零二

rootial的专栏 488

CodeForces - 305C Ivan and Powers of Two 优先队列

Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now ...

还我笑颜 251
上一篇: Eugeny and Play List
下一篇: Pythagorean Theorem II
muximuxi525
博客等级 码龄15年 134粉丝 294原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值