这道题是直尺法的模板题:
#include<iostream> using namespace std; const int maxn = 1e6 + 5; const int M = 2e3 + 5; int n, m, a[maxn], vis[M]; int main(){ cin >> n >> m; for (int i = 0; i < n; ++i)cin >> a[i]; int st = 0, en = 0, num = 0, res = n, l = 1, r = n; while (1){ while (en < n&&num < m){ if (vis[a[en++]]++ == 0)num++; } if (num < m)break; if (res>en - st){ res = en - st; l = st + 1; r = en; } if (--vis[a[st++]] ==0 )num--; } cout << l << " " << r << endl; }