CodeForces 620C(STL_B题)解题报告
题目链接:http://codeforces.com/problemset/problem/620/C
---------------------------------------------------------------------------------
题意:给出一个长为n的序列,将其拆分成多段,使得每段中有两个相同的数。
思路:利用set,将每部分判断是否在set中,如果不在set中,将其insert。否则记录序号,清空set。
注意:
本题中有陷阱,在于最后一段。例如:
10 933677171 80672280 80672280 933677171 933677171 933677171 933677171 80672280 80672280 933677171
的输入,如果仅仅简单判断,会在最后末尾输出8 9,然而最后一段应该输出8 10作为结束。
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; set<int> s; int n=0; int main(void){ scanf("%d",&n); int a[n+5]={0}; int pos[300005]={0}; int j=0; for(int i=0;i<n;i++){ scanf("%d",&a[i]); } for(int i=0;i<n;i++){ if(s.count(a[i])==0) s.insert(a[i]); else{ pos[j]=i; j++; s.clear(); } } if(j==0) printf("-1\n"); else{ pos[j-1]=n-1;//重要 记得判断最后一位是不是末尾,否则设定为末尾 printf("%d\n",j); printf("1 %d\n",pos[0]+1); for(int i=0;i<j-1;i++){ printf("%d %d\n", pos[i]+2, pos[i + 1]+1); } } return 0; }

浙公网安备 33010602011771号