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 (≤).
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤), the days of continuous riding. Then N non-negative integers are given in the next line, being the riding distances of everyday.
Output Specification:
For each case, print in a line the Eddington number for these N days.
Sample Input:
10
6 7 6 9 3 10 8 2 7 8
Sample Output:
6
题意:
找出一个最大的数字E,使其满足有E个大于E的数字。
思路:
将数组从大到小进行排列,随着index的增加E的值在减小,当index与E相等的时候就是所要找的最大值。 将样例进行排序可得10, 9, 8,8, 7, 7, 6, 6, 3, 2.
大于等于v[i] 的有i+1个, 即大于v[i] - 1的有i + 1个,即大于 v[i] 的有 i + 2个。根据题意可知要使超过的英里数大于等于天数即:v[i] >= i + 2; 也即:v[i] > i + 1。(这部分推导确实有点……)
Code :
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int main() { 6 int n, t = 0; 7 cin >> n; 8 vector<int> v(n); 9 for (int i = 0; i < n; ++i) cin >> v[i]; 10 sort(v.begin(), v.end(), greater<int>()); 11 while (t < n && v[t] > t + 1) t++; 12 cout << t << endl; 13 return 0; 14 }
参考: