Pearls in a Row
There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type ai.
Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type.
Split the row of the pearls to the maximal number of good segments. Note that each pearl should appear in exactly one segment of the partition.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
The first line contains integer n (1 ≤ n ≤ 3·105) — the number of pearls in a row.
The second line contains n integers ai (1 ≤ ai ≤ 109) – the type of the i-th pearl.
On the first line print integer k — the maximal number of segments in a partition of the row.
Each of the next k lines should contain two integers lj, rj (1 ≤ lj ≤ rj ≤ n) — the number of the leftmost and the rightmost pearls in the j-th segment.
Note you should print the correct partition of the row of the pearls, so each pearl should be in exactly one segment and all segments should contain two pearls of the same type.
If there are several optimal solutions print any of them. You can print the segments in any order.
If there are no correct partitions of the row print the number "-1".
5 1 2 3 4 1
1 1 5
5 1 2 3 4 5
-1
7 1 2 1 3 1 2 1
2 1 3 4 7
......emmmmm昨晚想歪了 比赛结束几分钟才改过来........我代码.....离散化动态规划....
大佬的代码.....set STL
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
const int maxn=3*1e5+100;
int dp[maxn];
int digit[maxn];
map<int ,int>mp;
int last[maxn];
int beginn[maxn];
vector<int>same[maxn];
int gg[maxn];
int main()
{
int n;
scanf("%d",&n);
int now=0;
for(int i=1;i<=n;i++){
scanf("%d",&digit[i]);
if(mp[digit[i]]==0)
{
mp[digit[i]]=++now;
last[i]=now;
}else
last[i]=mp[digit[i]];
}
//离散化成功 然后接下来就是寻找它的上一个是谁
//现在的数组的元素变成了last里面的值
for(int i=1;i<=n;i++)
same[last[i]].push_back(i);//把属于他们的都找出来了
for(int i=1;i<=n;i++){
if(same[last[i]].size()==1)
beginn[i]=-1;
else
{
if(beginn[same[last[i]][0]]==-1)continue;
beginn[same[last[i]][0]]=-1;
int kk=same[last[i]].size();
for(int j=1;j<kk;j++)
beginn[same[last[i]][j]]=same[last[i]][j-1];
}
}
for(int i=1;i<=n;i++){
if(beginn[i]==-1)
dp[i]=dp[i-1];
else
dp[i]=max(dp[i-1],dp[beginn[i]-1]+1);
}
if(dp[n]==0)
{
printf("-1\n");
return 0;
}else
{
printf("%d\n",dp[n]);
int t=0;
for(int i=1;i<=n;i++){
if(dp[i]==t)
{
gg[i]=t;
t++;
}
if(gg[i]==dp[n])
{
if(i==n)break;
else
{
gg[i]=0;
gg[n]=n;
}
}
}
int flag=0;
for(int i=1;i<=n;i++){
if(gg[i]==0&&flag==0){
printf("%d ",i);
flag=1;
}else if(flag==1&&gg[i]!=0)
{
printf("%d\n",i);
flag=0;
}
}
}
return 0;
}
大佬:copy来自 http://blog.csdn.net/gkhack/article/details/50558935
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
set<int>s;
const int maxn=3e5+10;
int pos[maxn];
int main()
{
int n,k=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
if(s.count(x)==0)s.insert(x);
else
{
pos[k++] = i;
s.clear();
}
}
if(k==0){
printf("-1\n");
return 0;
}
pos[k-1]=n;
printf("%d\n",k);
printf("1 %d\n",pos[0]);
for(int i=0;i<k-1;i++)
printf("%d %d\n",pos[i]+1,pos[i+1]);
return 0;
}