Greedy, find the farthest Delete
# Include <iostream>
# Include <vector>
# Include <algorithm>
# Include <cstdio>
# Include <queue>
# Include <stack>
# Include <string>
# Include <map>
# Include <set>
# Include <cmath>
# Include <cassert>
# Include <cstring>
# Include <iomanip>
Using namespace std;
# Ifdef _ WIN32
# Define i64 _ int64
# Define out64 "% I64d \ n"
# Define in64 "% I64d"
# Else
# Define i64 long
# Define out64 "% lld \ n"
# Define in64 "% lld"
# Endif
/*********** For topcoder by zz1215 *******************/
# Define FOR (I, a, B) for (int I = (a); I <= (B); I ++)
# Define FFF (I, a) for (int I = 0; I <(a); I ++)
# Define FFD (I, a, B) for (int I = (a); I >= (B); I --)
# Define S64 (a) scanf (in64, &)
# Define SS (a) scanf ("% d", &)
# Define LL (a) <1)
# Define RR (a) <1) + 1)
# Define pb push_back
# Define MAX (a, B) (a)> (B )? (A) (B ))
# Define MIN (a, B) (a) <(B )? (A) (B ))
# Define CL (Q) while (! Q. empty () Q. pop ()
# Define MM (name, what) memset (name, what, sizeof (name ))
# Define read freopen ("in.txt", "r", stdin)
# Define write freopen ("out.txt", "w", stdout)
Int inf = 0x3f3f3ff;
Const i64 inf64 = 0x3f3f3f3f3f3f3fll;
Const double oo = 10e9;
Const double eps = 10e-9;
Const double pi = acos (-1.0 );
Const int maxn = 100111;
Struct zs
{
Int x;
Int id;
Bool operator <(const zs & a) const
{
Return x <a. x;
}
Bool operator = (const zs & a) const
{
Return x = a. x;
}
} Zx;
Struct zz
{
Int x;
Int id;
Bool operator <(const zz & a) const
{
Return id> a. id;
}
Bool operator = (const zz & a) const
{
Return id = a. id;
}
} Za;
Int n, m;
Int a [maxn];
Map <int, set <int> mp;
Set <zs> s;
Set <zz> sa;
Set <int>: iterator si;
Set <zs >:: iterator ti;
Set <zz >:: iterator zi;
Int main ()
{
While (cin> n> m)
{
Mp. clear ();
For (int I = 1; I <= m; I ++)
{
Mp [I]. insert (inf ++ );
}
For (int I = 1; I <= n; I ++)
{
Cin> a [I];
Mp [a [I]. insert (I );
Mp [a [I]. insert (inf ++ );
}
S. clear ();
Sa. clear ();
For (int I = 1; I <= m; I ++)
{
Zx. x = za. x = I;
Zx. id = za. id = * mp [I]. begin ();
S. insert (zx );
Sa. insert (za );
}
Int ans = 0;
For (int I = 1; I <= n; I ++)
{
Zx. x = a [I];
If (s. find (zx) = s. end ())
{
Ans ++;
Zx. id = * mp [a [I]. upper_bound (I );
Za. x = zx. x;
Za. id = zx. id;
Sa. insert (za );
S. insert (zx );
Zx. x = sa. begin ()-> x;
Sa. erase (sa. begin ());
S. erase (zx );
}
Else
{
Zx. x = a [I];
Zx. id = * mp [a [I]. upper_bound (I );
Za. id = s. find (zx)-> id;
Sa. erase (za );
S. erase (zx );
S. insert (zx );
Za. x = zx. x;
Za. id = zx. id;
Sa. insert (za );
}
}
Cout <ans <endl;
}
Return 0;
}