1060. Eddington number (25)

Eddington, a British astronomer, likes to ride bicycles. It is said that in order to show off his cycling skills, he also defined an "Edington number" E, that is, the largest integer E that satisfies E days of cycling for more than E miles. Eddington's own E is said to be equal to 87.

Now, given someone's cycling distance in N days, please calculate the corresponding Eddington number E (<=N).

Input format:

输入第一行给出一个正整数N(<=105),即连续骑车的天数;第二行给出N个非负整数,代表每天的骑车距离。

Output format:

在一行中给出N天的爱丁顿数。

Input sample:

10
6 7 6 9 3 10 8 2 7 8

Sample output:

6

Problem- solving idea: first sort the array from large to small, and regard someone's cycling distance as decreasing every day. a[i] is the riding distance on the i-th day. If a[i]>i, it means that there are i days where the riding distance is greater than a[i] until a[i]

#include <algorithm>
using namespace std;
int a[1000000];
bool cmp(int a, int b) {
    return a > b;
}
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a+1, a+n+1, cmp);
    int ans = 0, p = 1;
    while(ans <= n && a[p] > p) {
        ans++;
        p++;
    }
    printf("%d", ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325618608&siteId=291194637