CodeForce#1625B Elementary Particles解题笔记

原题链接

B. Elementary Particles

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

解题思路:

        遍历元素,寻找相同元素,其序号为i,j,则由含该元素的最长和谐子串长度为(i-0)+(n-1-j)+1,即第i元素左侧元素数+第j元素右侧元素数+元素本身。直接两层循环的话O(N2)的时间复杂度会TLE,所以可以排个序将相同元素放到一起,将时间复杂度优化至O(NlnN)

AC代码

#include
#include
#include
#include
using namespace std;

struct L3TG
{
    int num;
    int sn;
};

bool cmp1(L3TG a, L3TG b){
    if(a.num == b.num) return a.sn < b.sn;
    return a.num < b.num;
}

inline int read(){
    int num = 0;
    char c;
    bool flag = false;
    while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
        if (c == '-') flag = true;
    else
        num = c - '0';
    while (isdigit(c = getchar()))
    num = num * 10 + c - '0';
    return (flag ? -1 : 1) * num;
}

L3TG a[150001];
int main(){
    int loop, n, max, tmp;
    scanf("%d", &loop);
    while (loop--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++){
            a[i].num = read();
            a[i].sn = i;
        }
        sort(a, a + n, cmp1);
        max = 0;
        for(int i = 0; i < n - 1; i++){
            if(a[i].num == a[i + 1].num){
                tmp = a[i].sn + n - a[i + 1].sn;
                max = tmp > max ? tmp : max;
            }
        }
        if(max){
            printf("%d\n", max);
        }else printf("-1\n");
    }
    // system("pause");
    return 0;
}

你可能感兴趣的:(贪心算法,排序算法)