跳到正文

题解

P7072 [CSP-J 2020] 直播获奖

Earth2023约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟
题目
P7072

版本

  1. v1当前

题目传送门

题目&目的

阅读题目可知:

  • 我们需要在每一次输入中求出分数线lowScorelowScore,其中lowScorelowScore的计算需要获奖百分比ww与计划获奖人数targettarget
  • 计划获奖人数为计算为max⁡(1,⌊p×w%⌋)\max(1, \lfloor p \times w \%\rfloor),其中:
    • 使用max⁡(a,b)\max(a, b)以获取aa与bb的较大值
    • pp为当前已评出的选手成绩个数
    • ww为获奖百分比
    • ⌊a⌋\lfloor a \rfloor为aa向下取整的值
  • 在targettarget不满足lowScorelowScore的w%w\%个人时,我们以targettarget为当前获奖人数来计算。

思路1(WA,65ptspts)

第5次评测记录

阅读此题后,容易想到使用for循环并对每次循环进行降序排序,以获取分数较高的同学,并且对分数线进行计算。

65ptspts代码如下:

C++
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n, w, p = 0;
    cin >> n >> w;
    int arr[n + 5];
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
        p++;
        sort(arr, arr + p, greater<int>());
        int goodPeopleSum = max(1, int(p * (w / 100.0)));
        int scoreLineIdx = int(p * (w / 100.0)) - 1;
        if (scoreLineIdx < 0) scoreLineIdx = 0;
        cout << arr[scoreLineIdx] << ' ';
    }
    return 0;
}

本蒟蒻在提交评测后发现,65ptspts!

这是为什么呢?(我不知道)

思路2(AC,100ptspts)

第11次评测记录

在阅读程序后发现,sortsort函数的时间复杂度为O(nlogn)\mathcal O(n log n),明显不符合此题的排序 (nn=100000:你找我?)

本懒到不想多想其他方法的蒟蒻自然想到桶排序,毕竟scorei≤600score_i \leq 600。(啊?有吗?)

首先在此题代码中声明一个int类型数组,并将所有索引位置的元素全部赋值为0,大小为605(别问)。

C++
int arr[605] = {0};

接着计算计划获奖人数targettarget,然后在获得每一个得分时对索引为获得分数的元素执行自加操作,并且使用一个for循环,从600进行倒序循环,条件为j≤0j \leq 0,每个循环过后执行自减操作,以从大到小遍历整个arrarr。

C++
int score, target = max(1, int(i * w / 100.0)), sum = 0;
scanf("%d", &score);
arr[score]++;
for (int j = 600; j <= 0; j--) {

}

其中当前获取的分数为第ii个。

上文声明了一个名称为sumsum的intint类型变量,接下来,我们在for循环中对sumsum进行操作,每次循环使sumsum加上arrjarr_j,表示当前发现的符合计划的分数。

C++
sum += arr[j];

如何停止发现呢?

用一个if语句,对sum≥targetsum \geq target进行判断。

C++
if (sum >= target) {
    printf("%d ", j);
    break;
}

AC Code

C++
#include <bits/stdc++.h>
using namespace std;
int arr[610] = {0}, n, w;
int main() {
    scanf("%d %d", &n, &w);
    for (int i = 1; i <= n; i++) {
        int score;
        scanf("%d", &score);
        arr[score]++;
        int sum = 0;
        for (int j = 600; j >= 0; j--) {
            sum += arr[j];
            if (sum >= max(1, int(i * w / 100.0))) { // 不想声明target,直接加入判断条件中
                cout << j << ' ';
                break;
            }
        }
    }
    return 0;
}

写在最后

第一次写笔记(不是题解),如有某些文字表述错误,请立刻告诉我!!!

Edit time:2024/09/28 15:29:30

原文