PAT 乙级 1060 爱丁顿数(25)

用力戳我直达原题~

简单的贪心题目。降序。i值从最大遍历到1,当满足a[i] > i 的i值则是结果。(不能是a[i] >= i,因为题目要求是超过)

你想啊,例如遍历到i = 7时,a[7] > 7,也就是说第7天的骑车距离大于7,由于是降序,那么前6天的骑车距离肯定也大于等于a[7],那么i就是结果。

开始我还想用二分结果的方法,但由于排序用了O(nlogn),再来一个O(logn)已经没有意义,所以直接线性解决。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;
bool cmp(const int &a,const int &b){return a>b;}
int main()
{
    int n, i, num[100010];
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", num + i);
    sort(num + 1, num + n + 1, cmp);
    for(i = n; i >= 1; i--)
    {
        if(num[i] > i)
        {
            cout << i << endl;
            break;
        }
    }
    if(i == 0)
        cout << 0 << endl;  
}
原文地址:https://www.cnblogs.com/bestwzh/p/6399789.html