Codeforces Round #633 (Div. 2)C Powered Addition (贪心,二进制)
Powered Addition
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You have an array aa of length nn. For every positive integer xx you are going to perform the following operation during the xx-th second:
- Select some distinct indices i1,i2,…,iki1,i2,…,ik which are between 11 and nn inclusive, and add 2x−12x−1 to each corresponding position of aa. Formally, aij:=aij+2x−1aij:=aij+2x−1 for j=1,2,…,kj=1,2,…,k. Note that you are allowed to not select any indices at all.
You have to make aa nondecreasing as fast as possible. Find the smallest number TT such that you can make the array nondecreasing after at most TT seconds.
Array aa is nondecreasing if and only if a1≤a2≤…≤ana1≤a2≤…≤an.
You have to answer tt independent test cases.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains single integer nn (1≤n≤1051≤n≤105) — the length of array aa. It is guaranteed that the sum of values of nn over all test cases in the input does not exceed 105105.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109).
Output
For each test case, print the minimum number of seconds in which you can make aa nondecreasing.
Example
input
3
4
1 7 6 5
5
1 2 3 4 5
2
0 -4
output
2
0
3
Note
In the first test case, if you select indices 3,43,4 at the 11-st second and 44 at the 22-nd second, then aa will become [1,7,7,8][1,7,7,8]. There are some other possible ways to make aa nondecreasing in 22 seconds, but you can't do it faster.
In the second test case, aa is already nondecreasing, so answer is 00.
In the third test case, if you do nothing at first 22 seconds and select index 22 at the 33-rd second, aa will become [0,0][0,0].
思路:找每一个非递增子序列的最大峰值差,比如是1 2 3 4 5 4 1 5 6 4,有两个峰一个是 5 4 1 还有一个是 6 4,取max(5-4,5-1,6-4)就好,然后模拟求出答案~
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
ll sz[200005],n;
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}x*=f;
}
inline ll query(ll x){ll res=0;while(x){res+=sz[x];x-=lb(x);}return res;}
inline void add(ll x,ll val){while(x<=n){sz[x]+=val;x+=lb(x);}}//第x个加上val
ll t,a[100005];
int main()
{
cin>>t;
for(int i=1;i<=t;i++)
{
ll x;
cin>>x;
for(int i=1;i<=x;i++)cin>>a[i];
ll maxx=-1,ans=0;
for(int i=1;i<x;i++)
{
if(a[i]>a[i+1])
{
int j;
for(j=i+1;a[i]>=a[j]&&j<=x;j++)
{
maxx=max(a[i]-a[j],maxx);
}
i=j-1;
}
}
//cout<<maxx<<endl;
if(maxx<=0)
{
cout<<0<<endl;
continue;
}
int k=1;
while(maxx>0)
{
maxx-=k;
ans++;
k*=2;
}
cout<<ans<<endl;
}
return 0;
}
- (原创)详解KMP算法
- Flash/Flex学习笔记(30):不用startDrag和stopDrag的对象拖动
- JS代码和网页的混用
- Flash/Flex学习笔记(29):MovieClip帧/时间轴的控制
- Oracle日常运维操作总结-数据库的启动和关闭
- Python中Json解析的坑
- Flash/Flex学习笔记(28):动态文本的滚动控制
- CentOS7下Elasticsearch集群部署记录
- Flash/Flex学习笔记(27):摄像头/麦克风的视频/音量指示器
- Flash/Flex学习笔记(26):AS3自定义右键菜单
- Python27中Json对中文的处理
- Python在VSCode中进入交互界面调试
- CentOS7下单机部署RabbltMQ环境的操作记录
- Flash/Flex学习笔记(24):粒子效果
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android项目实战之百度地图地点签到功能
- PHP Redis扩展无法加载的问题解决方法
- PHP 文件上传限制问题
- Flutter permission_handler 权限插件的使用详解
- Flutter使用JsBridge方式处理Webview与H5通信的方法
- flutter 怎么实现app整体灰色效果
- 解决android studio中使用monitor工具无法打开data文件夹问题
- Android安装应用 INSTALL_FAILED_DEXOPT 问题及解决办法
- php简单检测404页面的方法示例
- thinkPHP3.2使用RBAC实现权限管理的实现
- Flutter以两种方式实现App主题切换的代码
- PHP中非常有用却鲜有人知的函数集锦
- PHP针对redis常用操作实例详解
- thinkPHP5使用Rabc实现权限管理
- PHP实现cookie跨域session共享的方法分析