Acowdemia I#11231

Acowdemia I (Editorial)

Let us consider how to determine the -index for a given array of numbers. First, sort the array in descending order. Then find the largest value such that the elements in positions from to are at least equal to . In other words, we look for the maximum for which the condition a_h \ge h holds.

For example, in the following case the -index is equal to , since a_3 ≥ 3, but a_4 < 4.

After adding citations, the -index cannot increase by more than . Indeed, since a_{h+1} < h + 1, and each paper can be cited at most once, the maximum we can achieve is to increase a_{h + 1} by (if initially a_{h + 1} = h).

Next, starting from position h + 1 and moving towards smaller indices, increase the array elements by . In other words, increase by all elements in the segment a[max(1, h - l + 2); h + 1].

After that, we sort the array in descending order again and recalculate the -index. The resulting value is the answer.

Example

In the first example, Bessie cannot cite her own papers, so her -index for the set (1, 100, 2, 3) is equal to .

In the second example, if Bessie cites the third paper, the number of citations becomes (1, 100, 3, 3). In this case, the -index increases to .

Consider the following example. The numbers are already arranged in descending order. The -index of this array is equal to .

Algorithm implementation

The hIndex function computes the -index for an array that has already been sorted in descending order. Indexing starts from . The function returns the maximum value of for which the condition v_i \ge i holds. If such an index does not exist (that is, the -index of the array is ), then the function returns the value v.size() - 1, which is equal to .

int hIndex(vector<int> &v)
{
  for (int i = 1; i < v.size(); i++)
    if (v[i] < i) return i - 1;
  return v.size() - 1;
}

The main part of the program. Read the input data.

scanf("%d %d", &n, &l);
v.resize(n + 1);
for (i = 1; i <= n; i++)
  scanf("%d", &v[i]);

Sort the array in descending order, starting from the first element.

sort(v.begin() + 1, v.end(), greater<int>());

Compute the -index of the original array.

h = hIndex(v);

Starting from position h + 1 and moving towards smaller indices, increase the corresponding values v_i by .

for (i = h + 1; i > max(h - l + 1, 0); i--)
  v[i]++;

Sort the array in descending order and compute its new -index.

sort(v.begin() + 1, v.end(), greater<int>());
h = hIndex(v);

Print the answer.

printf("%d\n", h);