A1117

题意

爱丁顿喜欢骑车,并且记录下自己n天的每天的骑行路程,定义了一个“爱丁顿数"E,为满足刚好有E天骑行路程超过E的最大整数。

思路分析

此题不应使用Hash[]做。

  • 对数组从大到小排序,而后枚举天数e从1到n,当a[e] <= e时,ans = e-1即为最大的e。ans需要初始化为n。

参考代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int a[maxn];

bool cmp(const int &a,const int &b) {
    return a > b;
}

int main(void){
    int n;cin>>n;
    for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1,cmp);        //从大到小排序 
    
    int ans = n;
    for(int e = 1;e <= n;e++) {         //枚举E从1到n。 
        if(a[e] <= e) {     //如果a[e]小于当前天数,说明最大天数为e-1。 
            ans = e-1;
            break;
        } 
    }
    cout<<ans;
    return 0;
}  

总结

题目代码不难理解,但是逻辑性不好理解,刚开始第一次做用的Hash[]发现怎么都只能过第一个测试点甚是疑惑。之后使用sort从大到小发现了其中的规律就能解出该题了

猜你喜欢

转载自www.cnblogs.com/Western-Trail/p/10351959.html