Mishka and Contest

本文解析了CodeForces竞赛中的A题,介绍了如何通过从两端遍历的方式解决难度过滤问题,给出了C++与Python的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://codeforces.com/contest/999/problem/A

问题描述:

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 (1n,k1001≤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 (1ai1001≤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.

Output

Print one integer — the maximum number of problems Mishka can solve.

问题分析:题目大意就是从n个值中从最左边找到第一个比k大的下标,或者可以从最右边找到第一个比k值大的数的下标。最后输出两个下标值分别到达边界的距离和。注意当输出与n值相等时,表示n个数都<=k.直接输出k值。

AC代码:

#include <iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
using namespace std;

const int N = 1005;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,k;
	cin>>n>>k;
	int a[105];
	for(int i = 0;i < n;i++){
		cin>>a[i];
	}
	int cnt = 0;
	//从左往右查找 
	for(int i = 0;i <n;i++){
		if(a[i] > k){
			break;
		}
		cnt++;
	}
	//从右往左查找 
	for(int i = n-1;i >= 0;i--){
		if(a[i] > k){
			break;
		}
		cnt++;
	}
	//所有值都比<=k 
	if(cnt > n)
	cnt /= 2;
	cout<<cnt<<endl;
	return 0;
}

当然还有更为简洁的代码,分别给出c++和python代码:

#include <bits/stdc++.h>
using namespace std;
int n,k,i,s,f,a[179000];;
int main(){
	for(cin>>n>>k;i<n;i++)
		cin>>a[i],f==0&&a[i]<=k?s++:f=1;
	if(s<n)for(i=n-1;a[i]<=k;i--)s++;
	cout<<s;
}
n,k = map(int, input().split(' '))
print(n - len(''.join('x '[int(v) <= k] for v in input().split()).strip()))


http://codeforces.com/contest/999/problem/A
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值