【Look Back】【cf 905E】

给一个数组a,ai=ai*2,得到数组b,使得b[i]<b[i+1];

采用前缀和的思想,计算啊 a[i-1]到a[i]需要乘多少个2;

假设a[i-1]乘 t 个2,使得a[1]<a[2]<、、、<a[i-1],

(1)a[i-1]<a[i],需要看a[i-1]需要乘多少个2(假设需要乘cnt个2),使得a[i-1]*cnt*2>=a[i]

&&a[i-1]*cnt<=a[i];那么要使a[i]满足 a[i-1]*(2^t)<=a[i], 那么a[i]需要乘 t-cnt 个2;

例如:a[i-1]=2,a[i]=4,t=3;

易得cnt=1。a[i-1]*2*2*2=16,要使a[i]>=a[i-1]=16,那么a[i]需要乘 t-cnt 个2,也就是2个2就可以满足a[i]>=16。

(2)a[i-1]>a[i],需要看a[i]至少需要乘多少个2(假设至少需要乘cnt个2),使得a[i]*cnt>=a[i-1];

那么要使a[i]满足 a[i-1]*(2^t)<=a[i] 那么a[i]需要乘 t+cnt 个2。

例如:a[i-1]=4,a[i]=2,t=2;

易得cnt=1。a[i-1]*2*2=16,要使a[i]>=a[i-1]=16,那么a[i]需要乘 t+cnt 个2,也就是3个2就可以满足a[i]>=16。

输入样例:

9
1
1
2
2 1
3
3 2 1
4
7 1 5 3
5
11 2 15 7 10
6
1 8 2 16 8 16
2
624323799 708290323
12
2 1 1 3 3 11 12 22 45 777 777 1500
12
12 11 10 9 8 7 6 5 4 3 2 1

输出样例:

0
1
3
6
10
3
0
2
66 

#include<iostream>
#include<cmath>
#include<map>
#include<queue>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int N=2e5+10;
int a[N];
typedef long long LL;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		for(int i=0;i<n;i++) cin>>a[i];
		
		LL ans=0;
		LL t=0;
		for(int i=1;i<n;i++)
		{
			int l=a[i-1],r=a[i];
			LL cnt=0;
			while(l<r) cnt--,l*=2;
			while(l>r) cnt++,r*=2;
			t=max((LL)0,t+cnt);
			ans+=t;
		}
		cout<<ans<<endl;
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值