当前位置: 首页 > 工具软件 > hindex > 使用案例 >

A - Easy h-index(阅读理解)

公冶伟
2023-12-01

The h-index of an author is the largest h where he has at least h papers with citations not less than h.
h-index 指数类似于打游戏时的称号等级,只是这里的这个等级的评判有两个方面:1.发布的论文数量h1。

2.这h1份论文的的总引用次数h2。要求h2 >= h1。
Bobo has published many papers.
Given a0,a1,a2,…,an which means Bobo has published ai papers with citations exactly i, find the h

-index of Bobo.
a0 -->  ai  --> an 代表ai份论文的引用次数分别为0-->i-->n。

Input

The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains an integer n

.
The second line contains (n+1) integers a0,a1,…,an

.

Output

For each test case, print an integer which denotes the result.

## Constraint

* 1≤n≤2⋅105

 

* 0≤ai≤109
* The sum of n does not exceed 250,000

.

Sample Input

1
1 2
2
1 2 3
3
0 0 0 0

Sample Output

1
2
0

题解:这道题就是从后往前累加,如果大于了i,则输出i。

例如:

   2

   1   2   3

i: 0   1   2

令sum = 0,从后往前累加:

1.sum += a[2]  --------------a[2]=3  ,  i=2

sum = 3,i = 2;

sum > i;

输出  i

(至于为什么输出的是i,其实可以这么想:评判的标准不是有两项吗,我们完全可以用“引用数量”来求h值)

这个样例的解释是,有h1 = 2份论文,它们的引用次数大于等于h2 = 2(这里要求h1 <= h2)

 

贴代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

int a[1000005];
int main()
{
    int n;
    while(cin>>n){
        for(int i = 0;i < n+1;i++){
            cin>>a[i];
        }
        int sum = 0;
        bool flag = true;
        for(int i = n;i >= 0;i--){
            sum += a[i];
            if(sum >= i) {
                flag = false;
                cout<<i<<endl;
                break;
            }
        }
        if(flag){
            cout<<0<<endl;
        }
    }
    return 0;
}
 类似资料: