Problem:
Score : 1200 points

Problem Statement

There are N arrays.
The length of each array is M and initially each array contains integers (1，2，...，M) in this order.

Mr. Takahashi has decided to perform Q operations on those N arrays.
For the i-th (1≤i≤Q) time, he performs the following operation.

Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5，4，3，2，1), this array becomes (2，5，4，3，1).

Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations.
Determine if it is possible or not.

Constraints

2≤N≤10^5

2≤M≤10^5

1≤Q≤10^5

1≤a_i≤M

Input

The input is given from Standard Input in the following format:

N M
Q
a_1 a_2 ... a_Q

Output

Print Yes if it is possible to make N arrays exactly the same after performing the Q operations.
Otherwise, print No.

Sample Input 1

2 2
3
2 1 2

Sample Output 1

Yes

You can perform the operations as follows.

Sample Input 2

3 2
3
2 1 2

Sample Output 2

No

Sample Input 3

2 3
3
3 2 1

Sample Output 3

Yes

You can perform the operations as follows.

Sample Input 4

3 3
6
1 2 2 3 3 3

Sample Output 4

No

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;
const int DEBUG = 0;

const int N = 100010;
bool done[N];

bool match(int n, int m, const VI &target, const VI &op, int tol) {
  assert (0 <= tol && tol < m);
  VI inv_t(m);
  REP(i, 0, m) {
    inv_t[target[i]] = i;
  }
  VI prog(m + 1);
  VI pass(m + 1);
  prog[0] = n;
  REP(i, 0, op.size()) {
    int idx = inv_t[op[i]];
    if (prog[idx] == 0 && !pass[idx]) {
      return false;
    }
    if (prog[idx] == 0) {
      continue;
    }
    prog[idx]--;
    prog[idx + 1]++;
    pass[idx] = 1;
  }
  if (DEBUG) {
    REP(i, 0, m + 1) {
      cout << "prog[" << i << "]=" << prog[i] << endl;
    }
  }
  REP(i, 0, tol) {
    if (prog[i] > 0) {
      return 0;
    }
  }
  return 1;
}

int main(void){
  int n, m, q;
  cin >> n >> m >> q;
  VI a(q);
  REP(i, 0, q) {
    cin >> a[i];
    a[i]--;
  }
  reverse(a.begin(), a.end());
  vector<bool> used(m);
  int cur = 0;
  VI target(m);
  REP(i, 0, q) {
    if (used[a[i]]) {
      continue;
    }
    used[a[i]] = 1;
    target[cur] = a[i];
    cur++;
  }
  REP(i, 0, m) {
    if (!used[i]) {
      target[cur++] = i;
    }
  }
  assert (cur == m);
  int oldcur = m - 1;
  while (oldcur > 0) {
    if (target[oldcur - 1] < target[oldcur]) {
      oldcur--;
    } else {
      break;
    }
  }
  if (DEBUG) {
    cerr << "target:";
    REP(i, 0, m) {
      cerr << " " << target[i];
    }
    cerr << endl << "tol = " << oldcur << endl;
  }
  cout << (match(n, m, target, a, oldcur) ? "Yes" : "No") << endl;
}