当前位置:   article > 正文

Codeforces Round 887 (Div. 2) 题解||A+B

Codeforces Round 887 (Div. 2) 题解||A+B

A. Desorting

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 a1a2an1an.

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):

  • Choose an index i i i ( 1 ≤ i ≤ n − 1 1 \leq i \leq n-1 1in1).
  • Add 1 1 1 to a 1 , a 2 , … , a i a_1, a_2, \ldots, a_i a1,a2,,ai.
  • Subtract 1 1 1 from a i + 1 , a i + 2 , … , a n a_{i+1}, a_{i+2}, \ldots, a_n ai+1,ai+2,,an.

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 1t100). 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 2n500) — 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 1ai109) — 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:

  • Pick i = 1 i = 1 i=1. The array a a a then becomes [ 2 , 0 ] [2, 0] [2,0], which is not sorted.

In the second case, we can perform 2 2 2 operations to make the array not sorted:

  • Pick i = 3 i = 3 i=3. The array a a a then becomes [ 2 , 9 , 11 , 12 ] [2, 9, 11, 12] [2,9,11,12].
  • Pick i = 3 i = 3 i=3. The array a a a then becomes [ 3 , 10 , 12 , 11 ] [3, 10, 12, 11] [3,10,12,11], which is 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.

题解

1.题意

首先是题目大致的意思是给你一个长度为N的整数序列,他去使用一个操作去使整个序列不是排序的,整个操作可以概括为选取一个下标为i 的让前 i 项的值都加上 1 后面的项都减去一。

2.思路

寻找一段两点之间的区间,让他们的值保证是最小的,(简化),对这个值进行操作。

3.代码
#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 ; 
}
  • 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

B. Fibonaccharsis

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=fi1+fi2 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=fi1+fi2, 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 1t2105), 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 1n2105, 3 ≤ k ≤ 1 0 9 3 \leq k \leq 10^9 3k109).

It is guaranteed the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

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:

  • [ 6 , 8 , 14 , 22 ] [6,8,14,22] [6,8,14,22],
  • [ 4 , 9 , 13 , 22 ] [4,9,13,22] [4,9,13,22],
  • [ 2 , 10 , 12 , 22 ] [2,10,12,22] [2,10,12,22],
  • [ 0 , 11 , 11 , 22 ] [0,11,11,22] [0,11,11,22].

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.

题解

1,思路

在作者自身的题解中使用了二分搜素,但是这个题暴力也能过,斐波那契数列,他给定我们一个 n 也就是最后一项,这个时候我们要反向的思考数学性质,毕竟这是一道B 题不可能太难。

因为斐波那契数列的S1 +S2 =S3 这个性质,根据递归的思想我们只需要再确定一项就能确定完整的数列,也就是一层递归,我们再用一层k的遍历来验证答案的正确性即可(数学简化)
在这里插入图片描述

3.代码

#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 ;
}
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/519156?site
推荐阅读
相关标签
  

闽ICP备14008679号