题解 | #中位数#

中位数

https://ac.nowcoder.com/acm/contest/11177/A

中位数

(考时脑壳抽了

思路其实很简单
容易知道最终序列长度为
如果不操作,最小的中位数,即为位置为

我们把删去的数全都往中位数右边的数加即可,中位数不变 的序列中位数最小值 就是位置

如果,原序列和即为所求

记得排序(

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

int t;const int maxn=2e5+10;
int n,k;
int m[maxn];
int main(){
    cin>>t;
    while(t--){
        cin>>n>>k;long long ans=0;
        for(int i=1;i<=n;++i) cin>>m[i],ans+=m[i];
        sort(m+1,m+1+n);
        if(k==n-1) cout<<ans<<endl;
        else cout<<m[(n-k+1)/2]<<endl;
    }return 0;
}

(蒟蒻只能写这道题题解

全部评论

相关推荐

Java面试先知:我也是和你一样的情况,hr 说等开奖就行了
点赞 评论 收藏
分享
10-03 17:08
已编辑
西安电子科技大学 Java
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务