CF寒假补题——#765-B. Elementary Particles

time limit per test :2 seconds

memory limit per test: 256 megabytes

Martians are actively engaged in interplanetary trade. Olymp City, the Martian city known for its spaceport, has become a place where goods from all the corners of our Galaxy come. To deliver even more freight from faraway planets, Martians need fast spaceships.

A group of scientists conducts experiments to build a fast engine for the new spaceship. In the current experiment, there are nn elementary particles, the ii-th of them has type aiai.

Denote a subsegment of the particle sequence (a1,a2,…,ana1,a2,…,an) as a sequence (al,al+1,…,aral,al+1,…,ar) for some left bound ll and right bound rr (1≤l≤r≤n1≤l≤r≤n). For instance, the sequence (1 4 2 8 5 7)(1 4 2 8 5 7) for l=2l=2 and r=4r=4 has the sequence (4 2 8)(4 2 8) as a subsegment. Two subsegments are considered different if at least one bound of those subsegments differs.

Note that the subsegments can be equal as sequences but still considered different. For example, consider the sequence (1 1 1 1 1)(1 1 1 1 1) and two of its subsegments: one with l=1l=1 and r=3r=3 and another with l=2l=2 and r=4r=4. Both subsegments are equal to (1 1 1)(1 1 1), but still considered different, as their left and right bounds differ.

The scientists want to conduct a reaction to get two different subsegments of the same length. Denote this length kk. The resulting pair of subsegments must be harmonious, i. e. for some ii (1≤i≤k1≤i≤k) it must be true that the types of particles on the ii-th position are the same for these two subsegments. For example, the pair (1 7 3)(1 7 3) and (4 7 8)(4 7 8) is harmonious, as both subsegments have 77 on the second position. The pair (1 2 3)(1 2 3) and (3 1 2)(3 1 2) is not harmonious.

The longer are harmonious subsegments, the more chances for the scientists to design a fast engine. So, they asked you to calculate the maximal possible length of harmonious pair made of different subsegments.

Input

The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases. The following are descriptions of the test cases.

The first line contains an integer nn (2≤n≤1500002≤n≤150000) — the amount of elementary particles in the sequence.

The second line contains nn integers aiai (1≤ai≤1500001≤ai≤150000) — types of elementary particles.

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053⋅105.

Output

For each test, print a single integer, maximal possible length of harmonious pair made of different subsegments. If such pair does not exist, print −1−1 instead.

Example

input

Copy

4
7
3 1 5 2 1 3 4
6
1 1 1 1 1 1
6
1 4 2 8 5 7
2
15 15

output

Copy

4
5
-1
1

Note

The first test case is shown on the picture below:

As you can see from it, you may choose the subsegments (2 1 3 4)(2 1 3 4) and (3 1 5 2)(3 1 5 2), which are a harmonious pair. Their length is equal to 44, so the answer is 44.

In the second test case, you need to take two subsegments: one with l=1l=1 and r=5r=5, and one with l=2l=2 and r=6r=6. It's not hard to observe that these segments are a harmonious pair and considered different even though they are both equal to (1 1 1 1 1)(1 1 1 1 1).

In the third test case, you cannot make a harmonious pair, so the answer is −1−1.

 

题目类型:(我一开始感觉像双指针QAQ)

解题目标:找出最长和谐子序列的长度并输出。

解题思路:把每一位及该位的下标存入vector<PII>,并排序,用set也可,顺序遍历遇到相邻的且存放数值相等的元素进行运算并更新答案最大长度。

运算的推导:以3 1 5 2 1 3 4为例, 

                        3 向后找到了第二个3,第二个3最长可以继续向后扩展到序列末端即4,同时第一3向前最多可以扩展到初始端即3,(和谐子序列要求 存在一个在同一个位置上的数相同,且子序列左边界与右边界不同)可以得出3的最长子序列长度为:2(3 1 与 3 4);

同理找到了第一个1 ,向前扩展到3,第二个1向后扩展到4; 可以得到最长子序列长度为:4(3 1 5 2 与 2  1 3 4)即 end - i2 + (i1 - begin),在本题中begin(0/1) 与end(length) 已知;

错误代码:

#include<bits/stdc++.h>

using namespace std;
const int N = 150010;
int num[N];
int main()
{
    int t ;
    cin>>t;
    while(t -- )
    {
        int n;
        cin>>n;
        for(int i = 1; i <= n; i++)
            cin>>num[i];
        int ans = -1;
        for(int l = 1; l <= n-1; l++)
        {
            for(int r = l+1; r <= n; r++)
            {
                if(num[l] == num[r])
                {
                    if(n - r + l > ans ) ans = n - r + l;
                }
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 150010;
int main()
{
    int t ;
    cin>>t;
    while(t -- )
    {
        int n;
        cin>>n;
        vector< pair<int, int> > v;
        for(int i = 0 ; i < n; i++)
        {
            int num;
            cin>>num;
            v.push_back(make_pair(num, i));
        }
        sort(v.begin(), v.end());
        int ans = -1;
        for(int i = 0; i<n-1; i++)// i 不能取到n-1!!!
        {
            //cout<<i<<' '<<v[i].first<<' '<<v[i].second<<endl;
            if(v[i].first == v[i+1].first)
            {
                ans = max(ans , n - v[i+1].second + v[i].second);//公式
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值