【Look Back】【cf 905E】

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

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

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

(1)a[i-1]=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
#include
#include
#include
#include
#include
#include
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>a[i];
		
		LL ans=0;
		LL t=0;
		for(int i=1;ir) cnt++,r*=2;
			t=max((LL)0,t+cnt);
			ans+=t;
		}
		cout<

你可能感兴趣的:(算法,c++,数据结构)