D - KAIBUNsyo Editorial by en_translator

An important observation for the problem is as follows.

  • For every integer \(i\) (\(1 \le i \le N\)), if \(A_i \neq A_{N+1-i}\), then integers \(A_i\) and \(A_{N+1-i}\) should both be substituted by the same integer at last.

Let us regard this as an edge connecting between the vertices \(A_i,A_{N+1-i}\) in an undirected graph.

Then, we can see the following fact:

  • For every integer pair \((i,j)\), if \(i\) and \(j\) belongs to the same connected component of the graph, then \(i\) and \(j\) should both be substituted by the same integer.

Now, how can we minimize the number of operations to make all the integers in a connected component same?

If a connected component has \(k\) integers, obviously we need \(k-1\) or more operations to make them the same integer. Conversely, the objective can be achieved in \(k-1\) substitutions described as follows:

Fix an integer in the connected component, and change all other integers in the connected component to the fixed integer.

Therefore, the solution of this problem is (the number of distinct integers in \(A\)) - (the number of connected component of the graph).

There are several ways of finding this value; two typical ways are:

  • to perform DFS (Depth-First Search) or BFS (Breadth-First Search) for each connected component.
  • to use disjoint-set data structure (Union-Find).

The time complexity is around \(O(N)\) or \(O(N \log N)\), which is fast enough to solve this problem.

Sample code in C++


using namespace std;
using Graph=vector<vector<int>>;

void dfs(int v,vector<bool> &flag,Graph &g){
  for(auto &nx : g[v]){dfs(nx,flag,g);}

int main(){
  int n,res=0;
  cin >> n;
  vector<int> a(n);
  vector<bool> flag(200005,false);
  Graph g(200005);
  for(auto &nx : a){
    cin >> nx;
  int p=0,q=n-1;
  for(int i=1;i<=200000;i++){
  cout << res << '\n';

last update: