公式

B - Nineball 解説 by en_translator


Original proposer: physics0523

It is easy to use bucket sort.
In bucket sort, we construct a table \(bk[x] = \{\) the number of occurrences of \(x\) \(\}\), which we regard as the result of sorting.
This can be done by iteratively adding \(1\) to \(bk[A_i]\) for each element of \(A\).

Whether the number-\(9\) ball remains can be checked by whether \(bk[9]\) equals \(0\).
To find the minimum ball number, inspect for each \(i=1,2,\dots,9\) whether \(bk[i]\) is \(1\). The minimum \(i\) with \(bk[i]=1\) is the sought value.

This procedure can be implemented by combining if and for statements.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  int K;
  cin >> K;
  vector<int> bk(10,0);
  for(int i=0;i<K;i++){
    int A;
    cin >> A;
    bk[A]=1;
  }
  if(bk[9]==0){cout << "-1\n"; return 0;}
  for(int i=1;i<=9;i++){
    if(bk[i]==1){
      cout << i << "\n";
      return 0;
    }
  }
  return 0;
}