Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Codechef-猴子电源(问题代码:MPOW)_Python_Algorithm_Data Structures - Fatal编程技术网

Python Codechef-猴子电源(问题代码:MPOW)

Python Codechef-猴子电源(问题代码:MPOW),python,algorithm,data-structures,Python,Algorithm,Data Structures,我正在关注以下方面的挑战: 一排有n棵树,你已经得到了这些树的高度 树。每棵树上都有一只猴子,每只猴子都有一只(原文如此) 爬树的特殊能力。每只猴子的力量都是可以控制的 计算为π(x)。其中x是最大连续时间的总数 树(即当前树的左侧,也包括其自身) 其高度小于或等于当前树的高度。 打印n只猴子可获得的最大功率。在这里 π(x)可以表示为:-π(x)=(x)∗(十)−1)∗(十)−2) 第1条 你必须回答t个独立的测试用例 以10^9+7的模输出答案 [……] 样本输入:- 输出:- 说明:- 对

我正在关注以下方面的挑战:

一排有n棵树,你已经得到了这些树的高度 树。每棵树上都有一只猴子,每只猴子都有一只(原文如此) 爬树的特殊能力。每只猴子的力量都是可以控制的 计算为π(x)。其中x是最大连续时间的总数 树(即当前树的左侧,也包括其自身) 其高度小于或等于当前树的高度。 打印n只猴子可获得的最大功率。在这里 π(x)可以表示为:-π(x)=(x)∗(十)−1)∗(十)−2) 第1条

你必须回答t个独立的测试用例

以10^9+7的模输出答案

[……]

样本输入:- 输出:- 说明:- 对于第一棵小于或等于自身的树 只剩下一个了。第一个猴子的幂是π(1)=1。对于 第二棵小于或等于其左边的树为none,因此π(1)=1 第三棵树:比左边小或相等的树是 π(3)=6.5,对于其余的树也是如此

我的代码:
def电源(x):
如果x==1:
返回1
其他:
返回x*功率(x-1)
对于范围内的(int(input()):
mod=10**9+7
number=int(输入())
trees=list(映射(int,input().split())
堆栈=[]
列表1=[]
对于范围内的i(编号):
如果len(stack)==0:
列表1.追加(-1)
elif堆栈[-1]>树[i]:
list1.append(trees.index(堆栈[-1]))

elif堆栈[-1]0和堆栈[-1]出现运行时错误的主要原因是递归。查看问题中提供的元素数约束:
1为什么没有在
power()
中添加模数?这是正确的方法。你能解释一下你的算法是如何工作的吗?我将使用二叉树来查找到目前为止看到的较小元素的数量,并插入该数量,然后得到阶乘。另外,您是否总是会遇到运行时错误(以及哪一个错误),或者只是在特定的输入上?请看我的答案@Arnav Luhadiya。
1
6
6 4 12 3 6 7
6
def power(x):
    if x==1:
        return 1
    else:
        return x*power(x-1)
for _ in range(int(input())):
    mod = 10**9 + 7
    number = int(input())
    trees = list(map(int,input().split()))
    stack = []
    list1 = []
    for i in range(number):
        if len(stack)==0:
            list1.append(-1)
        elif stack[-1]>trees[i]:
            list1.append(trees.index(stack[-1]))
        elif stack[-1]<=trees[i]:
            while len(stack)>0 and stack[-1]<=trees[i]:
                stack.pop()
            if len(stack)==0:
                list1.append(-1)
            else:
                list1.append(trees.index(stack[-1]))
        stack.append(trees[i])
    list2 = [0]*number
    for i in range(number):
        list2[i]= i- list1[i]
    x = max(list2)
    print(power(x)%mod)
def power(x, mod):
    if x<=1:
        return 1
    else:
        ans = 1
        for i in range(1, x+1):
            ans = (ans * i)%mod
            
        return ans
        
for _ in range(int(input())):
    mod = 10**9 + 7
    number = int(input())
    trees = list(map(int,input().split()))
    stack = []
    list1 = []
    for i in range(number):
        if len(stack)==0:
            list1.append(-1)
        elif stack[-1]>trees[i]:
            list1.append(trees.index(stack[-1]))
        elif stack[-1]<=trees[i]:
            while len(stack)>0 and stack[-1]<=trees[i]:
                stack.pop()
            if len(stack)==0:
                list1.append(-1)
            else:
                list1.append(trees.index(stack[-1]))
        stack.append(trees[i])
    list2 = [0]*number
    for i in range(number):
        list2[i]= i- list1[i]
    x = max(list2)
    print(power(x, mod)%mod)