【PAT】1117 Eddington Number (25 分)

British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, he has even defined an "Eddington number", E -- that is, the maximum integer E such that it is for E days that one rides more than E miles. Eddington's own E was 87.

Now given everyday's distances that one rides for N days, you are supposed to find the corresponding E (≤N).

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​), the days of continuous riding. Then N non-negative integers are given in the next line, being the riding distances of everyday.

Output Specification:

For each case, print in a line the Eddington number for these N days.

Sample Input:

10
6 7 6 9 3 10 8 2 7 8

Sample Output:

6

题意: Eddington number 就是一个人能够达到的最大E(有E天骑行大于E miles,E天不一定要连续),给出一个人N天的骑行距离,求他的Eddington number。

分析:把N天的骑行距离从大到小排列,满足a[i] > i 的最大 i 就是他的Eddington number。

反面教材:一开始的做法是按距离给出的顺序累加得到序列 6 13 19 28 31 41 49 51 58 66,然后从右向左a[i] / i > i ,发现 i = 6满足样例输出。。。然而这种做法当然是错的 :)   for E days that one rides more than E miles——对这句话的理解是关键,for E days是有E天,不是连续E天,而且这E天每一天都要骑大于E,不是平均值大于E,发现自己经常因为不理解题目掉进坑里。。。

#include <iostream> 
#include <algorithm>
int n,seq[100002];
using namespace std;
bool Comp(int a, int b) { return a > b; }
int main() {
	scanf("%d",&n );
	for(int i = 1; i <= n; i++) 
		scanf("%d",&seq[i] );
	sort(seq,seq + n + 1,Comp);
	for(int i = n - 1; i >= 0; i--) {
		if(seq[i] > i + 1) {
			printf("%d",i + 1);
			return 0;
		}
	}
	printf("0");
	return 0;
}
发布了30 篇原创文章 · 获赞 26 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36032963/article/details/88929582