题解 | #淘汰分数#

淘汰分数

http://www.nowcoder.com/questionTerminal/9c4a4e879b4f49939dfaebea8948f976

主要是要考虑重复分数的情况,主要思路:排序后,依次遍历各个分数

#include <algorithm>
#include <vector>
  
 using namespace std;

void scoreBoundry() {
    int n, x, y;
    cin >> n >> x >> y;
    vector<int> scores(n);
    for (int i = 0; i < n; i++) {
        cin >> scores[i];
    }

    if (x > y || 2 * x > n || 2 * y < n) {
        cout << -1 << endl;
        exit(0);
    }

    sort(scores.begin(), scores.end());
    int i = 0;  
  // 前i个未通过,剩下未n-i个通过, i既代表前i个元素,也代表前i个元素的下一个索引位置        
    while (i < n) { 
   // 二分查找到scores[i]的起始索引范围[i, next), next为下个元素即scores[i+1]的起点位置
        int next = upper_bound(scores.begin() + i, scores.end(), scores[i])  - scores.begin();
        
        if (x <= next && next <= y && x <= n - next && n - next <= y) {     // 首次满足条件,输出最小的m值,并结束
            cout << scores[i]  << endl;
            exit(0);
        }

        if (next > y || n - next < x) {       // i往后遍历,再也不会得到结果,结束
            cout << -1 << endl;
            exit(0);
        }
        i = next;
    }
    cout << -1 << endl;        
}

int main() {

    scoreBoundry();

    return 0;
}
全部评论

相关推荐

Tom哥981:这份简历是“大一新生硬凹资深后端”的典型反面教材,槽点离谱到能让面试官直接笑出声: ### 1. 「年龄+入学时间」和项目复杂度完全脱节,可信度直接归0 你2024年7月才入学(现在刚读了1年多),19岁的大一新生,能把Vue3+Spring Boot+ShardingSphere+K8s+AI这些技术全塞进两个项目里?别说实际开发,光把这些技术的文档看完都得半年——这不是“能力强”,是“把招聘JD里的技术词全抄过来造假”,明摆着没碰过实际代码
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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