赞
踩
Call an array a a a of length n n n sorted if a 1 ≤ a 2 ≤ … ≤ a n − 1 ≤ a n a_1 \leq a_2 \leq \ldots \leq a_{n-1} \leq a_n a1≤a2≤…≤an−1≤an.
Ntarsis has an array a a a of length n n n.
He is allowed to perform one type of operation on it (zero or more times):
The values of a a a can be negative after an operation.
Determine the minimum operations needed to make
a
a
a not sorted.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1≤t≤100). The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 500 2 \leq n \leq 500 2≤n≤500) — the length of the array a a a.
The next line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1≤ai≤109) — the values of array a a a.
It is guaranteed that the sum of
n
n
n across all test cases does not exceed
500
500
500.
Output
Output the minimum number of operations needed to make the array not sorted.
Example
input
4
2
1 1
4
1 8 10 13
3
1 3 2
3
1 9 14
output
1
2
0
3
Note
In the first case, we can perform 1 1 1 operation to make the array not sorted:
In the second case, we can perform 2 2 2 operations to make the array not sorted:
It can be proven that 1 1 1 and 2 2 2 operations are the minimal numbers of operations in the first and second test cases, respectively.
In the third case, the array is already not sorted, so we perform 0 0 0 operations.
首先是题目大致的意思是给你一个长度为N的整数序列,他去使用一个操作去使整个序列不是排序的,整个操作可以概括为选取一个下标为i 的让前 i 项的值都加上 1 后面的项都减去一。
寻找一段两点之间的区间,让他们的值保证是最小的,(简化),对这个值进行操作。
#include <iostream> #include <cmath> using namespace std ; const int N = 510 ; int q[N] ; int ans ; int main () { int t ; cin >> t ; while ( t -- ) { ans = 1e9;//赋值方式: 1, 1en + m int n ; cin >> n ; for(int i = 0 ; i < n ; i ++ ) { cin >> q[i] ; if(i != 0 ) { ans = min(ans , q[i] - q[i - 1]); } } if(ans < 0 ) cout << 0 << endl ; else cout << ( ans / 2 )+ 1 << endl ; } return 0 ; }
Ntarsis has received two integers n n n and k k k for his birthday. He wonders how many fibonacci-like sequences of length k k k can be formed with n n n as the k k k-th element of the sequence.
A sequence of non-decreasing non-negative integers is considered fibonacci-like if f i = f i − 1 + f i − 2 f_i = f_{i-1} + f_{i-2} fi=fi−1+fi−2 for all i > 2 i > 2 i>2, where f i f_i fi denotes the i i i-th element in the sequence. Note that f 1 f_1 f1 and f 2 f_2 f2 can be arbitrary.
For example, sequences such as [ 4 , 5 , 9 , 14 ] [4,5,9,14] [4,5,9,14] and [ 0 , 1 , 1 ] [0,1,1] [0,1,1] are considered fibonacci-like sequences, while [ 0 , 0 , 0 , 1 , 1 ] [0,0,0,1,1] [0,0,0,1,1], [ 1 , 2 , 1 , 3 ] [1, 2, 1, 3] [1,2,1,3], and [ − 1 , − 1 , − 2 ] [-1,-1,-2] [−1,−1,−2] are not: the first two do not always satisfy f i = f i − 1 + f i − 2 f_i = f_{i-1} + f_{i-2} fi=fi−1+fi−2, the latter does not satisfy that the elements are non-negative.
Impress Ntarsis by helping him with this task.
Input
The first line contains an integer t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 5 1 \leq t \leq 2 \cdot 10^5 1≤t≤2⋅105), the number of test cases. The description of each test case is as follows.
Each test case contains two integers, n n n and k k k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1≤n≤2⋅105, 3 ≤ k ≤ 1 0 9 3 \leq k \leq 10^9 3≤k≤109).
It is guaranteed the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.
Output
For each test case output an integer, the number of fibonacci-like sequences of length k k k such that the k k k-th element in the sequence is n n n. That is, output the number of sequences f f f of length k k k so f f f is a fibonacci-like sequence and f k = n f_k = n fk=n. It can be shown this number is finite.
Example
input
8
22 4
3 9
55 11
42069 6
69420 4
69 1434
1 3
1 4
output
4
0
1
1052
11571
0
1
0
Note
There are 4 4 4 valid fibonacci-like sequences for n = 22 n = 22 n=22, k = 4 k = 4 k=4:
For n = 3 n = 3 n=3, k = 9 k = 9 k=9, it can be shown that there are no fibonacci-like sequences satisfying the given conditions.
For n = 55 n = 55 n=55, k = 11 k = 11 k=11, [ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ] [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] [0,1,1,2,3,5,8,13,21,34,55] is the only fibonacci-like sequence.
在作者自身的题解中使用了二分搜素,但是这个题暴力也能过,斐波那契数列,他给定我们一个 n 也就是最后一项,这个时候我们要反向的思考数学性质,毕竟这是一道B 题不可能太难。
因为斐波那契数列的S1 +S2 =S3 这个性质,根据递归的思想我们只需要再确定一项就能确定完整的数列,也就是一层递归,我们再用一层k的遍历来验证答案的正确性即可(数学简化)
#include <iostream> using namespace std ; bool cnt ; int main () { int t ; cin >> t ; while (t --) { int n , k ; int ans = 0 ; cin >> n >> k ; for(int i = n / 2 ; i <= n ; i ++) { cnt = false ; int ans1 = n ; int ans2 = i ; int kk = k - 2 ; for(int j = n - i ; kk > 0; j = ans1 - ans2 ,kk -- ) { ans1 = ans2 ; ans2 = j ; if(ans1 < ans2 || ans1 < 0 || ans2 < 0) { cnt = true; break; } } if(!cnt) ans ++ ; } cout << ans << endl ; } return 0 ; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。