B. Making Towers

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

You have a sequence of n colored blocks. The color of the i-th block is ci, an integer between 1 and n.

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

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

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

For each color r from 1 to n, solve the following problem independently:

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

Input

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

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

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

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

Output

For each test case, output n integers. The r-th of them should be the maximum size of an tower of color r you can form by following the given rules. If you cannot form any tower of color r, the r-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 1 and size 3 is:

  • place block 1 at position (0,0);
  • place block 2 to the right of block 1, at position (1,0);
  • place block 3 above block 2, at position (1,1);
  • place block 4 to the left of block 3, at position (0,1);
  • place block 5 to the left of block 4, at position (−1,1);
  • place block 6 above block 5, at position (−1,2);
  • place block 7 to the right of block 6, at position (0,2).

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

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

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

时间是1s,数据量很大,这题我的算法是吧它压缩到了(O(t*n))这么多时间,写这题需要一点一点动态规划的思想。

首先解释一下一个色块距离下一个相同色块的距离必须为奇数,才能是色块加1。

以为实在不好手绘,请自己在草稿纸上,按照题目要求画就行了。

(可以参照第一个样例图)

#include <iostream>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t > 0)                                           /*times:O(t*n)*/
    {
        int n;
        cin >> n;
        int r[100001];                                     /*色块大小*/
        int data1[100001];
        int dp[100001];                                    /*上次状态的位置*/
        int vis[100001];                                   /*有没有访问过*/
        for(int i=1;i<=n;i++)
        {
            r[i]=0;
            dp[i]=0;
            vis[i]=0;
            cin>>data1[i];
        }
        for(int i=1;i<=n;i++)
        {
            if(vis[data1[i]]==0)                               /*第一次访问,我们就把这次的状态存进来,留到下次访问时候用*/
            {
                dp[data1[i]]=i;
                r[data1[i]]=1;                                 /*只要第一次访问,我们先把色块置成1,免得后面再加*/
            }
            else
            {
                int temp=i;                                    /*temp表示这次的位置在哪*/
                if((temp-dp[data1[i]])==1)                     /*这次位置减上次位置的距离为1,r[data1[i]]+1*/
                {
                    r[data1[i]]++;
                    dp[data1[i]]=temp;                        /*dp[data1[i]]表示data1[i]的上次位置*/
                }
                else
                {
                    if((temp-dp[data1[i]])%2!=0)             /*距离为奇数,r[data1[i]]++*/
                    {
                        r[data1[i]]++;
                        dp[data1[i]]=temp;
                    }
                }
            }
            vis[data1[i]]++;                                 /*访问过一次,加1*/
        }
        for (int i = 1; i <= n; i++)
        {
            cout << r[i] << " ";
        }
        cout << endl;
        t--;
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值