跳到正文

个人记录

P7072 [CSP-J2020] 直播获奖 题解

hlb44约 2 分钟
原文

这是 2025年10月23日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月4日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

首先我们先把题目中 max⁡(1,⌊p×w%⌋)\max(1, \lfloor p \times w \%\rfloor) 这个公式用代码表示:max(1,p*w/100)。

注意:不需要使用 double,使用可能会导致精度溢出输出错误答案。

第一种方案:

使用 sort 每次排序,输出第 max⁡(1,⌊p×w%⌋)\max(1, \lfloor p \times w \%\rfloor) 个人的成绩,由于 w≤100w \le 100,不会越界。

注意:这种方式时间复杂度达到 O(n×nlog⁡n)O(n \times n \log n),由于 n≤105n \le 10 ^ 5,会超时。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;//定义数组大小最大值常量
int n,w,a[N];
bool cmp(int x,int y) {
	return x>y;
}//从大到小排序 
int main() {
	cin>>n>>w;
	for(int i=1; i<=n; i++) {
		scanf("%d",&a[i]);//使用scanf提高速度 
		sort(a+1,a+i+1,cmp);//排序 
		printf("%d ",a[max(1,i*w/100)]);//使用printf提高速度 
	}
	return 0;
}

正确解法:

使用 tit_i 表示得到 ii 分的人的个数。

那么如果当前人的成绩为 xx,代码应该为 t[x]++。

我们现在要求分数排名为 max⁡(1,⌊p×w%⌋)\max(1, \lfloor p \times w \%\rfloor) 的人的成绩,可以按照分数从高(600600)到低(00)枚举每一个分数的人的个数,每枚举一个,就让变量 sum 加上这个分数的人数,如果发现 sum≥max⁡(1,⌊p×w%⌋)sum \ge \max(1, \lfloor p \times w \%\rfloor),那么就说明当前这个分数即为答案,输出并返回,防止反复输出。

C++
#include<bits/stdc++.h>
using namespace std;
int n,w,x,t[605],sum;
int main() {
	cin>>n>>w;
	for(int i=1;i<=n;i++){
		cin>>x,t[x]++;
		for(int j=600,sum=0;j>=0;j--){
			sum+=t[j];
			if(sum>=max(1,i*w/100)){
				cout<<j<<" ";
				break;
			}
		}
	}
	return 0;
}