题目描述:
British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, he has even defined an “Eddington number”, E – that is, the maximum integer E such that it is for E days that one rides more than E miles. Eddington’s own E was 87.
Now given everyday’s distances that one rides for N days, you are supposed to find the corresponding E (≤N).
输入格式:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10^5), the days of continuous riding. Then N non-negative integers are given in the next line, being the riding distances of everyday.
输出格式:
For each case, print in a line the Eddington number for these N days.
输入样例:
10
6 7 6 9 3 10 8 2 7 8
输出样例:
6
题意:
给出N个数字,求最大的E,可以使其中有E个值大于E(大于E个值大于E也可以)。
思路:
1.设定cnt=0,如果有大于N的数字出现,cnt++(因为这个数字肯定大于E)
2.使用数组Edd把每个数字(小于等于E)出现的次数存起来,大于等于E的数字第1步已经统计了
3.将数组从后往前循环,每次先判断cnt是否大于等于i,如果是,则求得想要的最大值E,后将Edd[i]累加到cnt上,作为下一步判断的依据(之所以要后累加是因为E个值是要大于E而不是大于等于E)。
参考代码:
#include <stdio.h>
const int maxn = 100010;
int Edd[maxn] = {0};
int main(){
int n;
scanf("%d", &n);
int cnt = 0, maxE = 0;
for(int i = 0; i < n; i++){
int mile;
scanf("%d", &mile);
if(mile <= n) Edd[mile]++;
else cnt++;
}
for(int i = n; i >= 0; i--){
if(i <= cnt){
maxE = i;
break;
}
cnt += Edd[i];
}
printf("%d\n", maxE);
return 0;
}