• CodeTON Round 6 (Div. 1 + Div. 2, Rated, Prizes!)C - Colorful Table


    C. Colorful Table

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given two integers n� and k�. You are also given an array of integers a1,a2,…,an�1,�2,…,�� of size n�. It is known that for all 1≤i≤n1≤�≤�, 1≤ai≤k1≤��≤�.

    Define a two-dimensional array b� of size n×n�×� as follows: bi,j=min(ai,aj)��,�=min(��,��). Represent array b� as a square, where the upper left cell is b1,1�1,1, rows are numbered from top to bottom from 11 to n�, and columns are numbered from left to right from 11 to n�. Let the color of a cell be the number written in it (for a cell with coordinates (i,j)(�,�), this is bi,j��,�).

    For each color from 11 to k�, find the smallest rectangle in the array b� containing all cells of this color. Output the sum of width and height of this rectangle.

    Input

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

    The first line of each test case contains two integers n� and k� (1≤n,k≤1051≤�,�≤105) — the size of array a� and the number of colors.

    The second line of each test case contains n� integers a1,a2,…,an�1,�2,…,�� (1≤ai≤k1≤��≤�) — the array a�.

    It is guaranteed that the sum of the values of n� and k� over all test cases does not exceed 105105.

    Output

    For each test case, output k� numbers: the sums of width and height of the smallest rectangle containing all cells of a color, for each color from 11 to k�.

    Example

    input

    Copy

     
    

    5

    2 1

    1 1

    2 2

    1 2

    3 5

    3 2 4

    4 2

    1 2 1 2

    5 3

    1 2 3 2 1

    output

    Copy

    4 
    4 2 
    0 6 6 2 0 
    8 6 
    10 6 2 
    

    Note

    In the first test case, the entire array b� consists of color 11, so the smallest rectangle for color 11 has a size of 2×22×2, and the sum of its sides is 44.

    In the second test case, the array b� looks like this:

    11
    12

    One of the corner cells has color 22, and the other three cells have color 11. Therefore, the smallest rectangle for color 11 has a size of 2×22×2, and for color 22 it is 1×11×1.

    In the last test case, the array b� looks like this:

    11111
    12221
    12321
    12221
    11111

    1. #include
    2. using namespace std;
    3. using ll=long long;
    4. ll LEFT_l[1000010], RIGHT_l[1000010],LEFT_r[1000010], RIGHT_r[1000010];
    5. void findRectangles(ll n, ll k, vector &a)
    6. {
    7. for(int i=0; i<max(k,n)+100; i++)
    8. {
    9. LEFT_l[i]=1000000;
    10. // MIN[i]=1000000;
    11. RIGHT_r[i]=-1;
    12. // MAX[i]=-1;
    13. }
    14. ll MAX=-1,MIN=1000000;
    15. for (ll i = 0; i < n; i++)
    16. {
    17. ll x=a[i];
    18. LEFT_l[x] = min(LEFT_l[x], i);
    19. RIGHT_r[x] = max(RIGHT_r[x], i);
    20. }
    21. vectorans;
    22. ans.clear();
    23. for(ll i=k; i>0; i--)
    24. {
    25. if(LEFT_l[i]==1000000&&RIGHT_r[i]==-1)
    26. {
    27. ans.push_back(0);
    28. }
    29. else
    30. {
    31. MAX=max(MAX,RIGHT_r[i]);
    32. MIN=min(MIN,LEFT_l[i]);
    33. ans.push_back(2*(MAX-MIN+1));
    34. }
    35. }
    36. for(int i=0; i<max(n,k)+100; i++)
    37. {
    38. LEFT_l[i]=1000000;
    39. // MIN[i]=1000000;
    40. RIGHT_r[i]=-1;
    41. // MAX[i]=-1;
    42. }
    43. for (ll i = k-1; i >=0; i--)
    44. {
    45. cout<' ';
    46. }
    47. cout << endl;
    48. }
    49. int main()
    50. {
    51. ios::sync_with_stdio(false);
    52. ll t;
    53. cin >> t;
    54. while (t--)
    55. {
    56. ll n, k;
    57. cin >> n >> k;
    58. vector a(n);
    59. for (ll i = 0; i < n; i++)
    60. {
    61. cin >> a[i];
    62. }
    63. findRectangles(n, k, a);
    64. }
    65. return 0;
    66. }

    清空数组没有清空完全

     for(int i=0; i

  • 相关阅读:
    Java、泛型归并排序
    js--js实现基础排序算法
    车路协同 智能路侧决策系统总体架构及应用
    VirtualBox 安装UOS统信服务器操作系统
    Windows下文本生成图像AI画图尝鲜体验
    Kafka25道面试题总结(答案解析)
    蓝桥杯 第 1 场算法双周赛 第2题 数树数【算法赛】c++ 位运算巧解
    java计算机毕业设计vue基层社区管理服务网源码+数据库+系统+lw文档
    LeetCode-2485-找出中枢整数
    树--堆和优先权队列
  • 原文地址:https://blog.csdn.net/dongdongdong122/article/details/133021292