Mishka started participating in a programming contest. There are nn problems in the contest. Mishka's problem-solving skill is equal to kk.
Mishka arranges all problems from the contest into a list. Because of his weird principles, Mishka only solves problems from one of the ends of the list. Every time, he chooses which end (left or right) he will solve the next problem from. Thus, each problem Mishka solves is either the leftmost or the rightmost problem in the list.
Mishka cannot solve a problem with difficulty greater than kk. When Mishka solves the problem, it disappears from the list, so the length of the list decreases by 11. Mishka stops when he is unable to solve any problem from any end of the list.
How many problems can Mishka solve?
The first line of input contains two integers nn and kk (1≤n,k≤1001≤n,k≤100) — the number of problems in the contest and Mishka's problem-solving skill.
The second line of input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100), where aiai is the difficulty of the ii-th problem. The problems are given in order from the leftmost to the rightmost in the list.
Print one integer — the maximum number of problems Mishka can solve.
8 4 4 2 3 1 5 1 6 4
5
5 2 3 1 2 1 3
0
5 100 12 34 55 43 21
5
In the first example, Mishka can solve problems in the following order: [4,2,3,1,5,1,6,4]→[2,3,1,5,1,6,4]→[2,3,1,5,1,6]→[3,1,5,1,6]→[1,5,1,6]→[5,1,6][4,2,3,1,5,1,6,4]→[2,3,1,5,1,6,4]→[2,3,1,5,1,6]→[3,1,5,1,6]→[1,5,1,6]→[5,1,6], so the number of solved problems will be equal to 55.
In the second example, Mishka can't solve any problem because the difficulties of problems from both ends are greater than kk.
In the third example, Mishka's solving skill is so amazing that he can solve all the problems.
题目大意指给长度为 n 的数组,k为能做出题的“最大难度”,可以从开头做到末尾,反之亦然,也可以从开头做到某题卡住了,再从末尾开始做,求此种策略下能做出的题的数量,即 a[i] <= k 的数量。
代码如下:
#include <iostream>
using namespace std ;
int main(){
int n , k ;
while ( cin >> n >> k ){
int a[1005] ;
for ( int i = 0 ; i < n ; i ++ ){
cin >> a[i] ;
}
int num = 0 ;
for ( int i = 0 , j = n - 1 ; i < n && j >= 0 ; ){
if ( k >= a[i] ){
num ++ ;
i ++ ;
}
else if ( k >= a[j] ){
num ++ ;
j -- ;
}
else{
break ;
}
}
cout << num << endl ;
}
return 0 ;
}
Mishka参加了一个编程竞赛,面对一系列不同难度的问题。竞赛包含n个问题,Mishka的问题解决技能为k。Mishka从列表的一端开始解决问题,每次选择最左或最右的问题尝试解决。如果问题的难度超过Mishka的能力,则跳过并选择另一端的问题。该策略的目标是最大化解决的问题数量。
1323

被折叠的 条评论
为什么被折叠?



