Codeforces Round #809 (Div. 2)B - Making Towers

题目描述:

You have a sequence of nn colored blocks. The color of the ii-th block is cici, an integer between 11 and nn.

You will place the blocks down in sequence on an infinite coordinate grid in the following way.

  1. Initially, you place block 11 at (0,0)(0,0).
  2. For 2≤i≤n2≤i≤n, if the (i−1)(i−1)-th block is placed at position (x,y)(x,y), then the ii-th block can be placed at one of positions (x+1,y)(x+1,y), (x−1,y)(x−1,y), (x,y+1)(x,y+1) (but not at position (x,y−1)(x,y−1)), as long no previous block was placed at that position.

A tower is formed by ss blocks such that they are placed at positions (x,y),(x,y+1),…,(x,y+s−1)(x,y),(x,y+1),…,(x,y+s−1) for some position (x,y)(x,y) and integer ss. The size of the tower is ss, the number of blocks in it. A tower of color rr is a tower such that all blocks in it have the color rr.

For each color rr from 11 to nn, solve the following problem independently:

  • Find the maximum size of a tower of color rr that you can form by placing down the blocks according to the rules.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105).

The second line of each test case contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤n1≤ci≤n).

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

Output

For each test case, output nn integers. The rr-th of them should be the maximum size of an tower of color rr you can form by following the given rules. If you cannot form any tower of color rr, the rr-th integer should be 00.

Example

input

Copy

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

output

Copy

3 2 2 0 0 0 0 
0 3 0 2 0 0 
1 
0 0 1 1 1 
1 0 4 0 0 0 
2 2 2 2 0 0 0 0 

Note

In the first test case, one of the possible ways to form a tower of color 11 and size 33 is:

  • place block 11 at position (0,0)(0,0);
  • place block 22 to the right of block 11, at position (1,0)(1,0);
  • place block 33 above block 22, at position (1,1)(1,1);
  • place block 44 to the left of block 33, at position (0,1)(0,1);
  • place block 55 to the left of block 44, at position (−1,1)(−1,1);
  • place block 66 above block 55, at position (−1,2)(−1,2);
  • place block 77 to the right of block 66, at position (0,2)(0,2).

The blocks at positions (0,0)(0,0), (0,1)(0,1), and (0,2)(0,2) all have color 11, forming an tower of size 33.

In the second test case, note that the following placement is not valid, since you are not allowed to place block 66 under block 55:

It can be shown that it is impossible to form a tower of color 44 and size 33.

思路:

大致得出一个数能出现的最高塔,与该颜色方块每次出现的位置的差的奇偶有关。map存储一下每个数最后出现的位置,当这个数再次出现的时候判一下两者下标差的奇偶,为奇数则可以更新高度,为偶数时不必更新map。

在这里插入图片描述

图片来自:http://t.csdn.cn/HbE8k

(题目思路也是参考了这篇文章的)

代码(CPP):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int n, a[maxn], flag[maxn], Max[maxn];

/*
    大致得出一个数能出现的最高塔,与该颜色方块每次出现的位置的差的奇偶有关
    思路:map存储一下每个数最后出现的位置,当这个数再次出现的时候判一下两者下标差的奇偶,为奇数则可以更新高度
    为偶数时不必更新map
*/

int main()
{
    freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        map<int, int> mp;
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++)
        {
            if(mp[a[i]])
            {
                int x = i - mp[a[i]];  // 当前最后一个数字最后出现的位置和目前这个位置的差值
                if(x & 1)  // 为奇数则可以更新高度,为偶数时不必更新map
                {
                    mp[a[i]] = i;
                    flag[a[i]]++;
                }
            }
            else  //这个数如果没出现过,用mp记录一下下标,flag记录塔的高度
            {
                mp[a[i]] = i;
                flag[a[i]] = 1;
            }
            Max[a[i]] = max(Max[a[i]], flag[a[i]]); // 维护最大值
        }
        for (int i = 1; i <= n; i++)
        {
            if(i != 1)
                cout << " ";
            cout << Max[i];
            Max[i] = 0;  // 用memset似乎会超时,所以选择在输出的同时初始化
            flag[i] = 0;
        }
        cout << "\n";
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值