CodeForces 166C - Median(思路)

本文介绍了一种算法,用于解决给定一组数字和目标值k的情况下,至少需要添加多少个数才能使这组数字的中位数等于k的问题。通过排序和使用lower_bound与upper_bound函数来确定添加k的位置。

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

题意:给定 n 个数字,给定一个数 k ,求至少需要添加几个数才能使这 n 个数的中位数等于 k (与数学上的中位数概念不同的是,有 n 个数,下标为 1 ~ n,则中位数为排序后的第 (n + 1) / 2 个数)。

由小到大排序后,先找到最后一个小于 k 的位置,再找到第一个大于 k 的位置,那么中间这些位置都为 k ,看这些位置是否落在中位数的位置上,若没有,则就在这些位置中加 k ,直到满足条件。

 

#include<cstdio>  
#include<cstring>  
#include<cctype>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<deque>  
#include<queue>  
#include<stack>  
#include<list>  
typedef long long ll;  
typedef unsigned long long llu;  
const int MAXN = 500 + 10;  
const int MAXT = 10000 + 10;  
const int INF = 0x7f7f7f7f;  
const double pi = acos(-1.0);  
const double EPS = 1e-6;  
using namespace std;  
  
int n, k, a[MAXN];  
  
  
  
int main(){  
    scanf("%d%d", &n, &k);  
    int tmp, ans = 0;  
    for(int i = 0; i < n; ++i)  scanf("%d", a + i);  
    sort(a, a + n);  
    int left = lower_bound(a, a + n, k) - a;  
    int right = upper_bound(a, a + n, k) - a;  
    int mid = (n - 1) / 2;  
    while(mid < left || mid >= right){  
        ++ans;  
        ++right;               //中间加了一个 k 之后,第一个大于 k 的数的下标需要加 1  
        mid = (++n - 1) / 2;   //重新取中位数  
    }  
    printf("%d\n", ans);  
    return 0;  
}  

 

转载于:https://www.cnblogs.com/tyty-TianTengtt/p/5995991.html

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值