Official

B - Dentist Aoki Editorial by en_translator


Maintain an array that represents the state as follows:

  • \(1\) for a hole with a teeth;
  • \(0\) for a hole without a teeth.

When an operation is performed for a hole, flip \(0\) and \(1\) appropriately; then the final answer can be obtained as the sum of the value in the array.

Tips: we introduce concise ways to flip between \(a\) and \(b\) in a variable \(x\) (i.e. if \(x\) is \(a\), make it \(b\), and vice versa) (without an if statement).

  • Set \(x = (a+b)-x\).
  • Set \(x = a \ \rm{xor} \ \)\(b\)\( \ \rm{xor} \ \)\(x\).

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  int n,q;
  cin >> n >> q;
  vector<int> a(n,1);
  for(int i=0;i<q;i++){
    int t;
    cin >> t;
    a[t-1]^=1;
  }
  int s=0;
  for(auto &nx : a){s+=nx;}
  cout << s << "\n";
  return 0;
}

posted:
last update: