问题描述:某国选美大赛,总共有n个选手(编号从1到n),m个评委。每个评委只能拿到一张选票,每张选票可以为编号L到R的选手加上一分。得分最高的选手就可以原地出道,走向人生巅峰。现在让你找出得分最高的选手。
输入格式: 第一行两个整数n,m(1<=n,m<=100000) 接下来m行,每行输入两个整数L和R(1<=L<=R<=n)
输出格式: 按递增顺序输出每个选手的编号(注意不要有行末空格)
代码:
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, m;
printf("Enter the number of contestants and judges: ");
scanf("%d %d", &n, &m);
int *scores = (int *)calloc(n, sizeof(int));
int L, R, i;
printf("Enter the votes:\n");
for (i = 0; i < m; i++) {
scanf("%d %d", &L, &R);
for (int j = L - 1; j < R; j++) {
scores[j]++;
}
}
int maxScore = 0;
for (i = 0; i < n; i++) {
if (scores[i] > maxScore) {
maxScore = scores[i];
}
}
printf("Contestants with the highest score (%d): ", maxScore);
for (i = 0; i < n; i++) {
if (scores[i] == maxScore) {
printf("%d ", i + 1);
}
}
free(scores);
return 0;
}
例如: