codeforces 158A Next Round (水题)

本文介绍了一种计算竞赛晋级人数的方法,参与者总数为n,已知各参赛者的得分,需找出分数等于或超过第k名选手的所有选手数量。示例包括输入输出样例及简单代码实现。

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

题目:

A. Next Round
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

"Contestant who earns a score equal to or greater than the k-th place finisher's score will advance to the next round, as long as the contestant earns a positive score..." — an excerpt from contest rules.

A total of n participants took part in the contest (n ≥ k), and you already know their scores. Calculate how many participants will advance to the next round.

Input

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 50) separated by a single space.

The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 100), where ai is the score earned by the participant who got the i-th place. The given sequence is non-increasing (that is, for all i from 1 to n - 1 the following condition is fulfilled: ai ≥ ai + 1).

Output

Output the number of participants who advance to the next round.

Sample test(s)
input
8 5
10 9 8 7 7 7 5 5
output
6
input
4 2
0 0 0 0
output
0
Note

In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.

In the second example nobody got a positive score.

题意分析:

有n个数字,计算大于等于第k个数的个数。水题,不解释


代码:

#include<iostream>
int n,k,i,j,a[51];
using namespace std;
int main()
{
    cin>>n>>k;
    while(n>i)  cin>>a[i++];
    while(a[j]&&a[j]>=a[k-1])++j;
    cout<<j;
}


### 解决方案概述 对于Codeforces竞赛中的目解答,通常涉及多种算法和技术的应用。具体到所提及的不同问: #### 目一:最小字典序数组交换 针对给定条件下的整数序列 \(a_1, a_2,\ldots,a_n\) ,目标是在满足特定条件下通过交换两个元素来获得按字典顺序排列尽可能小的结果[^2]。 为了实现这一目的,可以采取如下策略: - 找出所有可能的奇偶组合对 \((i,j)\),其中 \(a_i + a_j\) 的和为奇数。 - 对这些符合条件的位置执行交换操作。 - 经过一系列合法的操作之后,最终得到的就是所需的最小子典序数组。 ```python def min_lexicographical_array(a): n = len(a) even_index = next((i for i in range(n) if a[i] % 2 == 0), None) odd_index = next((i for i in range(n) if a[i] % 2 != 0), None) # 如果存在这样的索引,则尝试交换使结果更优 if even_index is not None and odd_index is not None: a[even_index], a[odd_index] = a[odd_index], a[even_index] return sorted(a) ``` 此方法确保了每次都能找到最优解并返回经过调整后的有序列表。 #### 目二:字符计数比较 另一个例子涉及到简单的字符串处理逻辑,在这里只需要统计输入串中不同类型的字符数量,并依据简单规则判断输出哪一个更多[^3]。 解决方案非常直观: - 根据所得数据直接决定应该打印“A”还是“B”。 ```python from collections import Counter for _ in range(int(input())): mp = Counter(input().strip()) print("A" if mp['A'] > mp['B'] else "B") ``` 这段代码片段能够高效完成指定的任务需求。 #### 性能优化建议 考虑到时间复杂度的要求较为严格,推荐采用编译型静态类型语言如C++编写程序;如果坚持使用解释性脚本语言比如Python的话,考虑利用PyPy等JIT编译器提高运行效率[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值