题目大意:给出一个数组,要求将数组分成若干段,每个数都必须属于一段,使得每一段都至少有两个数相同。
为何不贪心呢?从左往右扫描,只要满足形成一段的条件,立马形成一段,注意最后一段结尾必须是n。
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
typedef __int64 LL;
const int INF=1e9+7;
const double eps=1e-7;
const int maxn=3*100000;
int n,a[maxn+10];
map<int,int >mp;
vector<int >ve;
void pre()//离散化,其实这题不用map离散化,可用set代替。
{
mp.clear();
for(int i=1;i<=n;i++)
{
if(!mp.count(a[i]))
{
int k=mp.size();
mp[a[i]]=k;
a[i]=k;
}
else
{
int num=mp[a[i] ];
a[i]=num;
}
}
}
int vis[maxn+10];//可用set代替标记vis+离散化+map
void work()
{
memset(vis,-1,(n+1)*sizeof vis[0] );
ve.clear();
int ans=0;
for(int i=1;i<=n;i++)
{
int x=a[i];
if(vis[x]==ans)
{
ans++;
ve.push_back(i);
vis[x]=-1;
}
else
{
vis[x]=ans;
}
}
if(ve.size()==0) {puts("-1");return;}
printf("%d\n",ve.size());
int last=1;
for(int i=0;i<ve.size();i++)
{
int x=ve[i];
if(i!=ve.size()-1) printf("%d %d\n",last,x);
else printf("%d %d\n",last,n);
last=x+1;
}
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
pre();
work();
}
return 0;
}