您的位置:首页 > 其它

Educational Codeforces Round 6-C. Pearls in a Row(贪心)

2016-01-22 01:16 633 查看
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
5
1 2 3 4 1


output
1
1 5


input
5
1 2 3 4 5


output
-1


input
7
1 2 1 3 1 2 1


output
2
1 3
4 7


思路:
这题就是一道简单的贪心题目,只要一找到是有两个相同的就立即变为一段,再特判一下最后的区间是否n值就行了。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
typedef unsigned __int64 LL;
typedef  __int64 ll;
const int T = 3000100;
const int mod = 1000000007;

set<int> se;
int u[T],v[T];

int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif

int n,m,i,j,k;
int cnt;

while(~scanf("%d",&n))
{
cnt = 0;se.clear();
scanf("%d",&k);
u[cnt]=1;
se.insert(k);
for(i=1;i<n;++i){
scanf("%d",&k);

if(se.find(k)!=se.end()){
se.clear();
v[cnt++] = i+1;
u[cnt] = i+2;
}
else
se.insert(k);
}
if(cnt==0){
printf("-1\n");
continue;
}
printf("%d\n",cnt);
if(v[cnt-1]!=n){
v[cnt-1]=n;
}
for(i=0;i<cnt;++i){
printf("%d %d\n",u[i],v[i]);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces