Codeforces Round 961 (Div. 2) C. Squaring

Codeforces Round 961 (Div. 2) C. Squaring

Ikrpprpp找到了一个由整数组成的数组 a a a 。他喜欢正义,所以他想要公平,也就是说,让它不递减。要做到这一点,他可以对数组的索引 1 ≤ i ≤ n 1 \le i \le n 1≤i≤n 执行一个公正的行为,它将用 a i 2 a_i ^ 2 ai2 替换 a i a_i ai (位置 i i i 的元素及其平方)。例如,如果 a = [ 2 , 4 , 3 , 3 , 5 , 3 ] a = [2,4,3,3,5,3] a=[2,4,3,3,5,3] 和ikrpprpp选择对 i = 4 i = 4 i=4 执行正义行为,则 a a a 变为 [ 2 , 4 , 3 , 9 , 5 , 3 ] [2,4,3,9,5,3] [2,4,3,9,5,3] 。

使数组不递减所需的最少正义行为是多少?

Input

First line contains an integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1≤t≤1000) --- the number of test cases. It is followed by the description of test cases.

For each test case, the first line contains an integer n n n --- size of the array a a a. The second line contains n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10 ^5 1≤n≤2⋅105) integers a 1 , a 2 , ... , a n a_1, a_2,\ldots, a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 6 1 \le a_i \le 10 ^ 6 1≤ai≤106).

The sum of n n n over all test cases does not exceed 2 ⋅ 10 5 2 \cdot {10}^5 2⋅105.

Output

For each testcase, print an integer --- minimum number of acts of justice required to make the array a a a non-decreasing. If it is impossible to do that, print − 1 -1 −1.

Example
cpp 复制代码
7
3
1 2 3
2
3 2
3
3 1 5
4
1 1 2 3
3
4 3 2
9
16 2 4 2 256 2 4 2 8
11
10010 10009 10008 10007 10006 10005 10004 10003 10002 10001 10000
output
cpp 复制代码
0
1
-1
0
3
15
55

Note

In the first test case, there's no need to perform acts of justice. The array is fair on its own!

In the third test case, it can be proven that the array cannot become non-decreasing.

In the fifth test case, ikrpprppp can perform an act of justice on index 3, then an act of justice on index 2, and finally yet another act of justice on index 3. After that, a a a will become [ 4 , 9 , 16 ] [4, 9, 16] [4,9,16].

cpp 复制代码
#include<bits/stdc++.h>  
using namespace std;  

typedef pair<int,int> PII;
typedef long long LL;

inline void solve()
{
	int n;cin>>n;
	vector<LL> a(n);
	for(int i=0;i<n;i++) cin>>a[i];
	
	int cnt=0;
	LL res=0;
	for(int i=1;i<n;i++)
	{
		if(a[i]==1 && a[i-1]>1) 
		{
			cout<<"-1\n";
			return ;
		}
		LL t=a[i];
		while(a[i-1]>t) 
		{
			t=t*t;
			cnt++;
		}
		while(cnt>0 && a[i-1]*a[i-1]<=t)
		{
			t=sqrtl(t);
			cnt--;
		}
		res+=cnt;
	}
	cout<<res<<"\n";
}

signed main() 
{  
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T=1;cin>>T;
	while(T--) solve();
    
    return 0;
}
相关推荐
乐之者v5 分钟前
leetCode43.字符串相乘
java·数据结构·算法
A懿轩A1 小时前
C/C++ 数据结构与算法【数组】 数组详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·数组
古希腊掌管学习的神1 小时前
[搜广推]王树森推荐系统——矩阵补充&最近邻查找
python·算法·机器学习·矩阵
云边有个稻草人1 小时前
【优选算法】—复写零(双指针算法)
笔记·算法·双指针算法
半盏茶香1 小时前
在21世纪的我用C语言探寻世界本质 ——编译和链接(编译环境和运行环境)
c语言·开发语言·c++·算法
忘梓.2 小时前
解锁动态规划的奥秘:从零到精通的创新思维解析(3)
算法·动态规划
️南城丶北离2 小时前
[数据结构]图——C++描述
数据结构··最小生成树·最短路径·aov网络·aoe网络
✿ ༺ ོIT技术༻2 小时前
C++11:新特性&右值引用&移动语义
linux·数据结构·c++
字节高级特工2 小时前
【C++】深入剖析默认成员函数3:拷贝构造函数
c语言·c++
计算机学长大白3 小时前
C中设计不允许继承的类的实现方法是什么?
c语言·开发语言