当前位置:网站首页>Cf:c. the third problem

Cf:c. the third problem

2022-07-06 00:44:00 White speed Dragon King's review

 Insert picture description here
 Insert picture description here

analysis

This is a permutation problem
It looks fancy
Speaking of arrangement , We are going to find this arrangement 0 To n Corresponding subscript position
We have MET The definition is the smallest number except string , All strings must meet the same minimum number
therefore , We followed “ Minimum ”, From the smallest 0 Start to find the corresponding indexes one by one
First 0 I'm sure I can't change my position , secondly 1 Can't change ( Otherwise, we can always find an interval to make MET Unequal )
And then look 2 The number behind , use lr Maintain the left and right of the current index interval , And then if 2 stay lr In the words of , It has r - l + 1 - 2 A choice
If it is in the index range lr Outside , Then there must be a choice of interval that makes MET Unequal , For a better image , for instance :
+++2+++1+++0+(1)
++2++++1+++0+(2)
----+++++++++++
If we choose a line + The range of , That's obvious MET(1) > 2 and MET(2) = 2
So when we choose number k When we count , the previous lr The length of the index interval is r - l + 1
We have to id[i] It can be similar only in the index interval , At this time, the number of positions it can choose is r - l + 1 - i
use ans Just ride in tandem

ac code

import sys
input = sys.stdin.readline

MOD = 10 ** 9 + 7

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    id = [0] * n
    for i, v in enumerate(a):
        id[v] = i
    # we judge from v = 0,1,...
    ans = 1
    l = r = id[0]
    for i in range(1, n):
        # if id[i] outside [l, r], no other choice
        # cause there must be a choice MET not equal
        if id[i] < l or id[i] > r:
            l, r = min(id[i], l), max(id[i], r)
        else:
            ans = ans * (r - l + 1 - i) % MOD
    print(ans)

summary

array + Sort + Internal and external simulation
Guess a reasonable situation by excluding some situations

原网站

版权声明
本文为[White speed Dragon King's review]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060036450930.html