繁体   English   中英

如何减少执行此问题的时间限制

[英]How can I decrease the time limit of execution of this problem

from collections import Counter
def main() :

    n = int(input())
    x1 =tuple(map(int,input().split(' ')))
    n2 = int(input())
    res = []
    memo1 = []
    memo2 = []
    for s1 in range(n2):
        c = 0
        p = list(map(int, input().split(' ')))
        if p in memo1:
            c = memo2[memo1.index(p)]
        else:
            x = x1[p[0]-1 : p[1]]
            y = x1[p[2]-1 : p[3]]
            x = Counter(x)
            y = Counter(y)
            f = x - y
            common = x - f
            g = y - common
            f = f + g
            for v1 in f:
                if v1 in common:
                    c = c + (f[v1]*common[v1])
            for v1 in common:
                c = c + common[v1]**2
            memo1.append(p)
            memo2.append(c)
        res.append(c)
    print(*res,sep='\n')
 main()

这是一个针对codeforces.com 问题的程序

请建议我如何解决这个问题...

一些通用指针:

  1. 在程序开始时只处理一次输入
  2. 问题陈述中的条件 2 和 3 告诉您哪些索引是有效的。 l1 <= i <= r1 和 l2 <= j <= r2。 不要浪费时间遍历输入列表中所有可能的 position。 只检查那些不会违反 2 和 3 的索引。
  3. 预处理。 条件 1 要求您验证 a_i 和 a_j 是否相等。 你会经常这样做。 因此,当您处理输入时,请跟踪(在 map / 字典中)哪些值是相同的。 这样以后跟踪就更容易了。

特定问题的指针:

查看不相交的集合数据结构。 它通常用于快速评估对象之间的等效性。 在您的情况下,您可以对其进行设置,以便它评估诸如“如果 a_i 等于 a_j,则 i 等于 j”之类的东西。

然后您可以查询数据结构以获取所有等价类(所有指向相等值的索引)。

一旦你有了一组数字(索引),你只需要找到匹配条件 2 和 3 的所有可能的索引对。

我很确定Segment Tree可以轻松解决这个问题。 我试图解决但仍然无法通过所有测试用例。
不过,我正在发布我的解决方案,如果您可以进一步优化它

from collections import Counter

' create segment tree in which each node will contain Counter of values'
def create(current,s,e):
    if s==e:
        tree[current]=Counter([array[s]])
        return tree[current]
    else:
        mid=(s+e)//2
        tree[current]=create(2*current+1,s,mid)+create(2*current+2,mid+1,e)
        return tree[current]

' query elements from l to r and return their counter'
def query(current,s,e,l,r):
    if s>=l and e<=r:
        return tree[current]
    if (r<s and r<e) or (l>s and l>e):
        return Counter()
    mid=(s+e)//2
    return query(2*current+1,s,mid,l,r)+query(2*current+2,mid+1,e,l,r)

n=int(input())
array=list(map(int,input().split( )))
tree=[0]*(4*n+1)
create(0,0,n-1)
#print(tree)
for _ in range(int(input())):
    l1,r1,l2,r2=map(int,input().split( ))
    ans1=query(0,0,n-1,l1-1,r1-1)
    ans2=query(0,0,n-1,l2-1,r2-1)
    ans=0
    for v in ans1:
        ans+=ans1[v]*ans2[v]
    print(ans)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM