Educational Codeforces Round 6 C.Pearls in a Row(欲張り)


Educational Codeforces Round 6 C:http://codeforces.com/contest/620/problem/C
C.Pearls in a Row
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
out put
スタンダードアウト
The re are n pearls in a row.Let's enumerate them with integers from 1 ト n from the left to the right.The pearl number i has the type ai.
Let's call a sequence of consecutive pearlsa 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 shoruld appar in exactly one segment of the partition.
As input/output can reach huge size it is recommanded to use fast input/out put methods:for example、prefer to use scanf/printfinstead of cin/cout in C++で、prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
Input
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.
Output
On the first line print integer k — the maximal number of segments in partition of the row.
Each of the next k LINE shoul d contain two integers lj,̵rj (1̵≦̵lj𔎅≦rj𔎅n)—the number of the leftmost and the right mols in the j-th segment.
Note you shound print the corect partition of the row of the pearls、so each pearl shound be in exactly one segment and all segments shound contan 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のcorect partitions of the row print the number"-1.
Sample test(s)
input
5
1 2 3 4 1
out put
1
1 5
input
5
1 2 3 4 5
out put
-1
input
7
1 2 1 3 1 2 1
out put
2
1 3
4 7
一つのネックレスを、いくつかのセグメントに分けて、それぞれのセグメントに少なくとも二つの同じタイプの真珠があります。
現在の真珠のタイプが現れたかどうかは、mapで記録します。もし現在の真珠のタイプがすでに現れたら、前の真珠と分けます。注意する必要があります。最後の真珠を処理して、ネックレスの最後まで締めくくります。
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int n,a[300005],i,pre,num;
vector<pair<int,int> > ans;
map<int,bool> vis;
bool flag=false;

int main() {
    scanf("%d",&n);
    for(i=1;i<=n;++i)
        scanf("%d",a+i);
    pre=1;
    for(i=1;i<=n;++i) {
        if(vis[a[i]]) {
            ans.push_back(make_pair(pre,i));
            vis.clear();
            pre=i+1;
        }
        else
            vis[a[i]]=true;
    }
    if(pre==1)
        printf("-1");
    else {
        printf("%d
",num=ans.size()); --num; for(i=0;i<num;++i) printf("%d %d
",ans[i].first,ans[i].second); printf("%d %d
",ans[num].first,n); } return 0; }