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
别的题目我都很少写blog,大部分总结都是写在notebook上,但我觉得这个题目代码思路我写的很好,复杂度比系统自带的sort函数应该要低,能达到o(n),可以和大家分享一下。
#include <iostream> using namespace std; const int maxsize=100001; int a[maxsize],n; bool is_ok(int s){//遍历数组判断是否满足大于s的天数大于等于s int counts=0; for(int i=0;i<n;i++){ if(a[i]>s)counts++; } if(counts>=s)return true; else return false; } void find_ee(int low,int high,int& en){ if(low>high)return; int mid=(low+high)/2; if(is_ok(mid)){ en=mid;//放在前面递归结束时不会修改en find_ee(mid+1,high,en); }else{ find_ee(low,mid-1,en); } } int main(){ cin>>n; int en=0; for(int i=0;i<n;i++)cin>>a[i]; find_ee(1,n,en); cout<<en; return 0; }