158A - Next Round

本文介绍了一个简单的算法,用于计算竞赛中晋级下一轮的参赛者数量。输入包括参赛者的总人数、晋级名额及按非递增顺序排列的成绩列表。文章通过示例解释了如何筛选出符合条件的参赛者。

摘要生成于 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.
Examples
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个人的分数且成绩为正的人的数目。
code:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;

const int maxn=50;

int main()
{
    int n,k;
    int a[maxn+10];
    int ans=0;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",a+i);
        }
        for(int i=0;i<n;i++)
        {
            if(a[i]>=a[k-1]&&a[i]>0)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
### 解决方案概述 对于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、付费专栏及课程。

余额充值