AT_arc221_c.Two Deques Sorting

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a positive integer NN and a length-NN sequence of positive integers A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N).

There is also a length-00 sequence of positive integers BB.

You can perform the following operation between 00 and NN times, inclusive.

  • Remove the first or last element of AA, and append that element to the beginning or end of BB.

What is the maximum number of operations you can perform, given that BB must be a strictly increasing sequence after the operations?

You are given TT test cases; solve each of them.

给你一个正整数 NN 和一个长度为 NN 的正整数序列 A=(A1,A2,…,AN)A=(A_1,A_2,\dots,A_N)。

此外,还有一个长度为 00 的正整数序列 BB。

你可以执行以下操作 00 至 NN 次(含端点):

  • 从 AA 的开头或末尾移除一个元素,并将该元素添加到 BB 的开头或末尾。

在所有操作结束后,要求 BB 是一个严格递增的序列。在此约束下,最多能执行多少次操作?

你将得到 TT 组测试用例;请分别求解每组测试用例。

输入格式

The input is given from Standard Input in the following format:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

Each test case is given in the following format:

NN
A1A_1 A2A_2 …\ldots ANA_N

输入从标准输入中按以下格式给出:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

每个测试用例按以下格式给出:

NN
A1A_1 A2A_2 …\ldots ANA_N

输出格式

Output the answers for the test cases in order, separated by newlines.

按顺序输出测试用例的答案,答案之间用换行符分隔。

输入输出样例

  • 输入#1

    3
    5
    3 1 4 1 5
    3
    2 2 1
    16
    3 4 5 7 12 15 1 15 16 8 14 2 16 14 9 2

    输出#1

    4
    2
    11

说明/提示

Sample 1 Explanation:
For the first test case, consider performing the operations as follows.

  • Remove the first element of AA and append it to the end of BB. Now A=(1,4,1,5),B=(3)A=(1,4,1,5),B=(3).
  • Remove the first element of AA and append it to the beginning of BB. Now A=(4,1,5),B=(1,3)A=(4,1,5),B=(1,3).
  • Remove the first element of AA and append it to the end of BB. Now A=(1,5),B=(1,3,4)A=(1,5),B=(1,3,4).
  • Remove the last element of AA and append it to the end of BB. Now A=(1),B=(1,3,4,5)A=(1),B=(1,3,4,5).

The final B=(1,3,4,5)B=(1,3,4,5) is a strictly increasing sequence, and the number of operations is 44. It is impossible to perform five operations while satisfying the condition, so the answer is 44.

For the second test case, note that B=(1,2,2)B=(1,2,2) is not a strictly increasing sequence.

Constraints

  • 1≤T≤3×1051\le T\le 3\times 10^5
  • 1≤N≤3×1051\le N\le 3\times 10^5
  • 1≤Ai≤N1\le A_i \le N (1≤i≤N)(1\leq i\leq N)
  • The sum of NN over all test cases is at most 3×1053\times 10^5.
  • All input values are integers.

样例 1 解释:
对于第一个测试用例,考虑按如下方式执行操作:

  • 移除 AA 的第一个元素,并将其追加到 BB 的末尾。此时 A=(1,4,1,5)A=(1,4,1,5),B=(3)B=(3)。
  • 移除 AA 的第一个元素,并将其插入到 BB 的开头。此时 A=(4,1,5)A=(4,1,5),B=(1,3)B=(1,3)。
  • 移除 AA 的第一个元素,并将其追加到 BB 的末尾。此时 A=(1,5)A=(1,5),B=(1,3,4)B=(1,3,4)。
  • 移除 AA 的最后一个元素,并将其追加到 BB 的末尾。此时 A=(1)A=(1),B=(1,3,4,5)B=(1,3,4,5)。

最终得到的 B=(1,3,4,5)B=(1,3,4,5) 是一个严格递增序列,且操作次数为 44。在满足条件的前提下无法执行五次操作,因此答案为 44。

对于第二个测试用例,注意 B=(1,2,2)B=(1,2,2) 并非严格递增序列。

约束条件

  • 1≤T≤3×1051\le T\le 3\times 10^5
  • 1≤N≤3×1051\le N\le 3\times 10^5
  • 1≤Ai≤N1\le A_i \le N (1≤i≤N)(1\leq i\leq N)
  • 所有测试用例的 NN 之和不超过 3×1053\times 10^5。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页