Elementary Particles(1100)Codeforces Round #765 (Div. 2)

B. Elementary Particles
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 𝑛 elementary particles, the 𝑖-th of them has type 𝑎𝑖.

Denote a subsegment of the particle sequence (𝑎1,𝑎2,…,𝑎𝑛) as a sequence (𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟) for some left bound 𝑙 and right bound 𝑟 (1≤𝑙≤𝑟≤𝑛). For instance, the sequence (1 4 2 8 5 7) for 𝑙=2 and 𝑟=4 has the sequence (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) and two of its subsegments: one with 𝑙=1 and 𝑟=3 and another with 𝑙=2 and 𝑟=4. Both subsegments are equal to (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 𝑘. The resulting pair of subsegments must be harmonious, i. e. for some 𝑖 (1≤𝑖≤𝑘) it must be true that the types of particles on the 𝑖-th position are the same for these two subsegments. For example, the pair (1 7 3) and (4 7 8) is harmonious, as both subsegments have 7 on the second position. The pair (1 2 3) and (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 𝑡 (1≤𝑡≤100) — the number of test cases. The following are descriptions of the test cases.

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

The second line contains 𝑛 integers 𝑎𝑖 (1≤𝑎𝑖≤150000) — types of elementary particles.

It is guaranteed that the sum of 𝑛 over all test cases does not exceed 3⋅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 instead.

Example
inputCopy
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
outputCopy
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) and (3 1 5 2), which are a harmonious pair. Their length is equal to 4, so the answer is 4.

In the second test case, you need to take two subsegments: one with 𝑙=1 and 𝑟=5, and one with 𝑙=2 and 𝑟=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).

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

题意 :

  • 给一字符串,找两个等长的连续子串(不能起始下标一致),满足至少有一个位置上值相同,如果无法找到则输出-1,能则输出最大长度

思路 :

  • 考虑每两个值相同的元素,最大长度为第一个元素前数量加上第二个元素后数量加一(本身)
  • 要使长度最大,因此,枚举所有相邻的值相同的元素
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <unordered_map>
#define endl '\n'
#define pb push_back
using namespace std;
 
inline void solve()
{
    int n; cin >> n;
    unordered_map<int, vector<int>> ma;
    for (int i = 0, x; i < n && cin >> x; i ++ )
        ma[x].pb(i + 1);
    
    int mx = -1;
    for (auto item : ma)
    {
        vector<int> ve = item.second;
        if (ve.size() < 2) continue;
        for (int i = 0; i + 1 < ve.size(); i ++ )
        {
            mx = max(mx, ve[i] - 1 + n - ve[i + 1] + 1);
        }
    }
    
    cout << mx << endl;
 
}
 
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int _; cin >> _;
    
    while (_ -- )
        solve();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值