滑动窗口,维护[l,r]。mp记录前面出现过的数的种数和每个数出现的次数。
对于a[r]而言,能加入到当前的序列中的条件就是出现过或者是前面的种数小于k。
否则就是l右移同时该数的数目减一,为0的话就种类减一。
/***************************************** Author :Crazy_AC(JamesQi) Time :2015 File Name :取尺法 *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <climits> using namespace std; #define MEM(x,y) memset(x, y,sizeof x) #define pk push_back typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> ii; typedef pair<ii,int> iii; const double eps = 1e-10; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; /********************************************************/ /**********************Point*****************************/ /********************************************************/ struct Point{ double x,y; Point(double x=0,double y=0):x(x),y(y){} }; typedef Point Vector; Vector operator + (Vector A,Vector B){ return Vector(A.x + B.x,A.y + B.y); } Vector operator - (Vector A,Vector B){//向量减法 return Vector(A.x - B.x,A.y - B.y); } Vector operator * (Vector A,double p){//向量数乘 return Vector(A.x * p,A.y * p); } Vector operator / (Vector A,double p){//向量除实数 return Vector(A.x / p,A.y / p); } int dcmp(double x){//精度正负、0的判断 if (fabs(x) < eps) return 0; return x < 0?-1:1; } bool operator < (const Point& A,const Point& B){//小于符号的重载 return A.x < B.x || (A.x == B.x && A.y < B.y); } bool operator == (const Point& A,const Point& B){//点重的判断 return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0; } double Dot(Vector A,Vector B){//向量的点乘 return A.x * B.x + A.y * B.y; } double Length(Vector A){//向量的模 return sqrt(Dot(A,A)); } double Angle(Vector A,Vector B){//向量的夹角 return acos(Dot(A,B) / Length(A) / Length(B)); } double Cross(Vector A,Vector B){//向量的叉积 return A.x * B.y - A.y * B.x; } double Area2(Point A,Point B,Point C){//三角形面积 return Cross(B - A,C - A); } Vector Rotate(Vector A,double rad){//向量的旋转 return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad)); } Vector Normal(Vector A){//法向量 int L = Length(A); return Vector(-A.y / L,A.x / L); } double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离 Vector v1 = B - A,v2 = p - A; return fabs(Cross(v1,v2)) / Length(v1); } double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离 if (A == B) return Length(p - A); Vector v1 = B - A, v2 = p - A,v3 = p - B; if (dcmp(Dot(v1,v2) < 0)) return Length(v2); else if (dcmp(Dot(v1,v3)) > 0) return Length(v3); else return DistanceToLine(p,A,B); } Point GetLineProjection(Point p,Point A,Point B){//投影 Vector v = B - A; return A + v * (Dot(v, p - A) / Dot(v, v)); } bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交 double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1); double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1); return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0; } bool OnSegment(Point p,Point A,Point B){//点在线段上 return dcmp(Cross(A - p,B - p)) == 0 && dcmp(Dot(A - p,B - p)) < 0; } double PolygonArea(Point* p,int n){//多边形面积 double area = 0; for (int i = 1;i < n - 1;++i){ area += Cross(p[i] - p[0],p[i + 1] - p[0]); } return area / 2; } double PolygonArea(vector<Point> p){//多边形面积 Point O(0,0); double ret = 0; p.push_back(p[0]); for (int i = 0;i < p.size() - 1;++i){ ret += Cross(p[i] - O,p[i + 1] - O); } return ret / 2.0; } /********************************************************/ /**********************Line******************************/ /********************************************************/ struct Line{ Point p;//直线上任意一点 Vector v;//方向向量,它的左边就是半平面 double ang;//极角 Line(){} // Line(Point p,Vector v):p(p),v(v){} Line(Point A,Point B):p(A){ v = B - A; ang = atan2(v.y,v.x); } bool operator < (const Line& rhs)const{ return ang < rhs.ang; } }; int n, k; int a[500010]; map<int,int> mp; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); cin >> n >> k; for (int i = 1;i <= n;++i) scanf("%d",a + i); int r = 2,l = 1; mp.clear(); mp[a[1]]++; int ansr = 1,ansl = 1; while(r <= n){ if (mp.count(a[r]) || mp.size() < k){ mp[a[r]]++; if (r - l > ansr - ansl){ ansl = l; ansr = r; } r++; }else{ if (mp[a[l]] == 1) mp.erase(a[l]); else mp[a[l]]--; l++; } } cout << ansl << ' ' << ansr << endl; return 0; }