codeforces 620C

题意:给你n个数,一段子序列拥有两个相同的数就称为happy segment,求最多的happy segment,没有的话就输出-1,否则第一行输出happy segment的个数a,接下来a行输出每个happy segment的起始位置和终止位置.

思路:用stl的set做,只需记录每个happy segment的终止位置就好了,

 

 1 #include<iostream>
 2 #include<set>
 3 const int qq=3e5+10;
 4 using namespace std;
 5 set<int>p;
 6 int x[qq];
 7 int main()
 8 {
 9     int n,m,c;    cin >> n;
10     c=0;
11     for(int i=1;i<=n;++i){
12         cin >> m;
13         if(p.count(m)==0)    p.insert(m);
14         else{
15             x[c++]=i;
16             p.clear();
17         }
18     }
19     if(!c)    cout << "-1" << endl;
20     else{
21         x[c-1]=n;
22         cout << c << endl;
23         cout << "1" << " " << x[0] << endl;
24         for(int i=0;i<c-1;++i)
25             cout << x[i]+1 << " " << x[i+1] << endl; 
26     }
27     return 0;
28 }

 

posted @ 2016-01-22 21:58  我不萌、我要高冷  阅读(314)  评论(0编辑  收藏  举报