Mishka and Contest(模拟水题)
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?
Input
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 aiaiis the difficulty of the ii-th problem. The problems are given in order from the leftmost to the rightmost in the list.
Output
Print one integer — the maximum number of problems Mishka can solve.
Examples
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
Note
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个问题需要去解决,他的解题能力为kk,只能解决难度比kk小的题目,解题的过程是从左边或者从右边开始解题,当他遇到解不出来的题的时候就会停止解题。
解题思路:使用一个双指针,一个从头到尾,一个从尾到头,分别遍历,记录比kk小的数的个数即可。
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int main() 5 { 6 int n,m,flag,count; 7 int a[110],i; 8 scanf("%d%d",&n,&m); 9 for(i=0;i<n;i++) 10 { 11 scanf("%d",&a[i]); 12 } 13 flag=0; 14 count=0; 15 for(i=0;i<n;i++) 16 { 17 if(a[i]<=m) 18 { 19 count++; 20 } 21 else 22 { 23 break; 24 } 25 } 26 if(i>=n) 27 { 28 flag=1; 29 } 30 if(!flag) 31 { 32 for(i=n-1;i>=0;i--) 33 { 34 if(a[i]<=m) 35 { 36 count++; 37 } 38 else 39 { 40 break; 41 } 42 } 43 } 44 printf("%d\n",count); 45 return 0; 46 }