原题
题目大意
Doremy 有 n n n 桶油漆,用一个长度为 n n n 的序列 a a a 表示。第 i i i 个桶中的颜料编号为 a i a_i ai。定义 c ( l , r ) c(l,r) c(l,r) 为从 a l a_l al 到 a r a_r ar 的子串中颜料种类的数量(及相同的颜料只算一次)。请找出一对 l l l 和 r r r 使得 l ≤ r l \leq r l≤r 并且 r − l − c ( l , r ) r-l-c(l,r) r−l−c(l,r) 的值最大。
输入格式
The input consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1≤t≤104 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1≤n≤105 ) — the length of the array a a a .
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\ldots,a_n a1,a2,…,an ( 1 ≤ a i ≤ n 1 \le a_i \le n 1≤ai≤n ).
It is guaranteed that the sum of n n n does not exceed 1 0 5 10^5 105 .
输出格式
For each test case, output l l l and r r r such that l ≤ r l \leq r l≤r and r − l − c ( l , r ) r-l-c(l,r) r−l−c(l,r) is maximized.
If there are multiple solutions, you may output any.
样例 #1
样例输入 #1
7
5
1 3 2 2 4
5
1 2 3 4 5
4
2 1 2 1
3
2 3 3
2
2 2
1
1
9
9 8 5 2 1 1 2 3 3
样例输出 #1
2 4
1 5
1 4
2 3
1 2
1 1
3 9
个人思路总结
错误分析
只想到 l , r , c ( l , r ) l,r,c(l,r) l,r,c(l,r)的大小在最大时应为 l , c ( l , r ) l,c(l,r) l,c(l,r)尽量小, r r r尽量大,使用前缀和求区间和,未考虑到时间复杂度。
思路改进
遇到此类问题,应该多角度去思考,例如这个题的区间,不要盯死了 l l l和 r r r,可以从小的区间想到大的区间,也可以从大的区间想到小的区间,遵照的就是哪种角度方便、容易,就向哪里想。
题解
整体思路
假设我们已有了一个区间 c ( l , r ) c(l,r) c(l,r),现在要扩大这个区间,我们可以把 r r r向右挪
原来: a n s = r − l − c ( l , r ) ans=r-l-c(l,r) ans=r−l−c(l,r)
现在:
若 ar+1在原来的区间已出现过
a
n
s
=
(
r
+
1
)
−
l
−
c
(
l
,
r
)
ans=(r+1)-l-c(l,r)
ans=(r+1)−l−c(l,r)
a
n
s
ans
ans增加了
1
1
1
若 ar+1在原来的区间未出现过
a
n
s
=
(
r
+
1
)
−
l
−
(
c
(
l
,
r
)
+
1
)
ans=(r+1)-l-(c(l,r)+1)
ans=(r+1)−l−(c(l,r)+1)
a
n
s
ans
ans未改变
也就是说,向右扩大这个区间, a n s ans ans的值是只加不减的
每一位上最多有一次 a n s + 1 ans+1 ans+1的机会,所以我们只要让 r r r一步步扩大,经过每一个数就一定能得到最大的 a n s ans ans
要让 r r r经过每一位, l l l就要等于 1 1 1, r r r一直到最大也就是 n n n,所以 l = 1 , r = n l=1,r=n l=1,r=n时, a n s ans ans一定最大
对于每一组数据,直接输出 1 1 1 n n n即可
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,a[100010];
cin >> t ;
while(t--)//每组测试数据
{
cin >> n ;
for(int i=1;i<=n;i++)
cin >> a[i] ;
cout << 1 << " " << n << endl ;//直接输出 1 n
}
return 0 ;
}