Educational Codeforces Round 6 C. Pearls in a Row set

C. 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/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 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".

input
5
1 2 3 4 1
output
1
1 5
 
题意:
  给你n个小珠子,每个珠子有不同的颜色,现在告诉你一段区间内有2个相同颜色的珠子那么这段区间可以独立成一段,现在问你最多能形成几段?
题解:
  set乱搞一通,出现了就截取,每个珠子都要属于最后的答案中一段里面,注意最后一段就好了
 

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000001;

vector<pair<int,int > > ans;
map<int ,int > mp;
set<int > s;
int main() {
    int x,n,l;
    scanf("%d",&n);
    l = 1;
    for(int i = 1; i <= n; i++) {
        scanf("%d",&x);
        if(s.count(x)) {
            ans.push_back(make_pair(l,i));
            s.clear();mp.clear();
            l = i+1;
        }
        else {
            mp[x] = i;
            s.insert(x);
        }
    }
    if(!ans.size()) puts("-1");
    else {
          printf("%d\n",ans.size());
          if(ans[ans.size()-1].second != n) ans[ans.size()-1].second = n;
    for(int i = 0; i < ans.size(); i++) printf("%d %d\n",ans[i].first,ans[i].second);
    }

    return 0;
}

 

你可能感兴趣的:(Educational Codeforces Round 6 C. Pearls in a Row set)