英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数” E ,即满足有 E 天骑车超过 E 英里的最大整数 E。据说爱丁顿自己的 E 等于87。
现给定某人 N 天的骑车距离,请你算出对应的爱丁顿数 E(≤N)。
输入格式:
输入第一行给出一个正整数 N (≤105),即连续骑车的天数;第二行给出 N 个非负整数,代表每天的骑车距离。
输出格式:
在一行中给出 N 天的爱丁顿数。
输入样例:
10
6 7 6 9 3 10 8 2 7 8
输出样例:
6
个人理解
这题看似简单,但是如果方法没选好真的要耗很久。一开始我是从小到大排序,结果发现不同类型的处理太复杂了,没法很好地处理好每种情况,解决了一个测试点可能又会影响另一个测试点,所以在参考了https://www.liuchuo.net/archives/2480这位学姐的博客后,采用了她的方法,主要思路如下
从下标1开始存储n天的公里数在数组a中,对n个数据从大到小排序,i表示了骑车的天数,那么满足a[i] > i的最大值即为所求
代码实现
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define ep 1e-5
#define INF 0x7FFFFFFF
int const maxn = 100005;
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int solve(int nums[], int n) {
int ans = 0, pos = 1;
while (nums[pos] > pos && ans <= n) {
pos ++;
ans ++;
}
return ans;
}
int main() {
//初始化
int n, nums[maxn];
//输入
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> nums[i];
}
//从大到小排序
sort(nums+1, nums+1+n, cmp);
//Calulate
int ans = solve(nums, n);
//输出
cout << ans << endl;
return 0;
}
总结
学习不息,继续加油
越发觉得自己辣鸡,但还是要继续努力啊!
最后的最后,放一份从小到大排序的代码,尚未AC
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <iostream>
#define ll long long
#define ep 1e-5
#define INF 0x7FFFFFFF
int const maxn = 100005;
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int solve(int nums[], int n) {
int ret = 0;
if (nums[n-1] == 1) {
return ret;
}
else if (n == 1) {
ret = 1;
return ret;
}
else {
for (int i = 0; i < n; i ++) {
int bigger_days = n - i - 1;
int tmp_ret = 0;
if (bigger_days < nums[i]) {
tmp_ret = bigger_days;
}
if (tmp_ret > ret)
ret = tmp_ret;
}
return ret;
}
}
int main() {
//初始化
int n, nums[maxn];
//输入
cin >> n;
for (int i = 0; i < n; i ++) {
cin >> nums[i];
}
//从小到大排序
sort(nums, nums+n);
//Calulate
int ans = solve(nums, n);
//输出
cout << ans << endl;
return 0;
}