PAT1117. Eddington Number

来源:互联网 发布:lena藤井莉娜的淘宝店 编辑:程序博客网 时间:2024/06/06 02:33

思路:搞懂题意是关键–E满足有共有E天骑车的距离超过E米,求最大的E!
将数组排序,我们假设最大的E是e,e满足条件有e天骑车超过e米,并且e+1不满足有e+1天骑车超过e+1米。那么我们可以逆序统计所有满足a[i]>ni的就是答案。


AC代码

#include <stdio.h>#include <algorithm> using namespace std;const int maxn = 1e5 + 5;int a[maxn];int main() {    int n;    scanf("%d", &n);    for(int i = 0; i < n; i++) {        scanf("%d", &a[i]);    }    sort(a, a+n);    int ans = 1;    for(int i = n-1; i >= 0 && ans < a[i]; i--) {        ans++;    }     printf("%d\n", ans-1);    return 0;}

如有不当之处欢迎指出!

原创粉丝点击