• 1117 Eddington Number (25分)


    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;
    }


  • 相关阅读:
    个性化推荐系统(二)---构建推荐引擎
    个性化推荐系统(一)---今日头条等的内容划分、分类
    双11线上压测netty内存泄露
    数据、信息、知识、智慧
    ReentrantLock的相关方法使用
    公平锁和非公平锁
    Lock中使用Condition实现等待通知
    使用IO流将数据库中数据生成一个文件,结果使用Notepad++打开部分数据结尾出现NUL
    ThreadLocal的使用
    join方法
  • 原文地址:https://www.cnblogs.com/kalicener/p/13525179.html
Copyright © 2020-2023  润新知