Codeforces - Educational Codeforces Round 6C - Pearls in a Row(练习)

来源:互联网 发布:aes解密 java 编辑:程序博客网 时间:2024/05/16 23:39

C. Pearls in a Row
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Sample test(s)
input
51 2 3 4 1
output
11 5
input
51 2 3 4 5
output
-1
input
71 2 1 3 1 2 1
output
21 34 7

题意:good的含义是某一段里有两个数相同,给你一串数组,尽可能地多分成good段,问最多能分成多少,并输出每一段的开头结尾,若没有则输出-1

这题没什么难度,用map存扫一遍直接就过了,写出来主要是想记录一下自己纱布的RE。。。。。。

看对了范围,是的,没看错,范围没看错!手抖少打了一个0。。。也是醉醉的,写出来长长记性


#include <iostream>#include <algorithm>#include <cstdio>#include <stdlib.h>#include <cmath>#include <cstring>#include <string.h>#include <cstdlib>#include <iostream>#include <set>#include <map>#include <vector>using namespace std;int a[300005]; // 范围!!!!!没看错但是敲错!!!!!!int b[300005];int main(){  int n;  while(scanf("%d", &n) != EOF){    for(int i = 1; i <= n; i++)      scanf("%d", &a[i]);    memset(b, 0, sizeof(b));    int k = 0, cnt = 1;    map<int, int> sa;    sa.clear();    for(int i = 1; i <= n; i++){      if(sa[a[i]] > 0){        b[k++] = cnt;        b[k++] = i;        cnt = i + 1;        sa.clear();      }      else        sa[a[i]]++;    }    if(!k)      printf("-1\n");    else{      if(cnt <= n){        b[k - 1] = n;      }      printf("%d\n", k / 2);      for(int i = 0; i < k; i++){        if(i % 2 == 0) printf("%d", b[i]);        else printf(" %d\n", b[i]);      }    }  }  return 0;}


0 0
原创粉丝点击