貪心,每次找最遠的刪
#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 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,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((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 = 0x3f3f3f3f3f3f3f3fLL;
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;
}