-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLookBack.cpp
46 lines (43 loc) · 873 Bytes
/
LookBack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ull unsigned long long
ull solve (){
int n;
cin >> n;
ull a[n];
forn(i, n){
cin >> a[i];
}
ull ops,x;
ops = x = 0;
ull num;
for (int i = 1; i < int(n); i++){
if(a[i] < a[i - 1]){
num = a[i];
while(num < a[i - 1]){
num *= 2;
++x;
}
} else{
num = a[i-1];
while(num*2 <= a[i] && x > 0){
num *= 2;
--x;
}
}
ops += x;
}
return ops;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t, n;
cin >> t;
while(t--) {
cout << solve() << "\n";
}
}