PAT1117. Eddington Number

xiaoxiao2021-02-28  163

思路:搞懂题意是关键–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; }

如有不当之处欢迎指出!

转载请注明原文地址: https://www.6miu.com/read-18027.html

最新回复(0)