QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#780406#1191. Reordering the DocumentsvwxyzTL 1188ms11840kbPython34.8kb2024-11-25 10:46:122024-11-25 10:46:20

Judging History

你现在查看的是最新测评结果

  • [2024-11-25 10:46:20]
  • 评测
  • 测评结果:TL
  • 用时:1188ms
  • 内存:11840kb
  • [2024-11-25 10:46:12]
  • 提交

answer

from collections import defaultdict

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False,rollback=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.weight=[0]*self.N
        self.rollback=rollback
        if self.rollback:
            self.operate_list=[]
            self.operate_set=[]

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if not self.rollback:
            if self.weighted:
                w=0
                for y in stack[::-1]:
                    self.parents[y]=x
                    w+=self.weight[y]
                    self.weight[y]=w
            else:
                for y in stack[::-1]:
                    self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if self.rollback:
            self.operate_list.append([])
            self.operate_set.append([])
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]-self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
                if self.weighted:
                    w=-w
            if self.rollback:
                self.operate_list[-1].append((self.parents,root_y,self.parents[root_y]))
                self.operate_list[-1].append((self.size,root_x,self.size[root_x]))
                self.operate_set[-1].append(root_y)
                if self.label!=None:
                    self.operate_list[-1]((self.label,root_x,self.label[root_x]))
                if self.weighted:
                    self.operate_list[-1].append((self.weight,root_y,self.weight[root_y]))
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w+self.weight[x]-self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]-self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components
    
    def Rollback(self):
        assert self.rollback
        if self.operate_list:
            for lst,x,v in self.operate_list.pop():
                lst[x]=v
            for x in self.operate_set.pop():
                self.roots.add(x)            
            return True
        else:
            return False

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys())))

N,M=map(int,input().split())
S=list(map(int,input().split()))
mod=10**9+7
UF=UnionFind(2*N)
for i in range(N):
    for j in range(i+1,N):
        if S[i]>S[j]:
            UF.Union(i*2,j*2+1)
            UF.Union(i*2+1,j*2)
for i in range(N):
    if UF.Same(i*2,i*2+1):
        ans=0
        break
else:
    seen=[False]*N
    dp=[0]*(M+1)
    dp[0]=1
    for lst in UF.Linked_Components().values():
        if seen[lst[0]//2]:
            continue
        cnt0,cnt1=0,0
        for x in lst:
            seen[x//2]=True
            if x%2==0:
                cnt0+=1
            else:
                cnt1+=1
        prev=dp
        dp=[0]*(M+1)
        for m in range(M+1):
            if cnt0+m<=M:
                dp[cnt0+m]+=prev[m]
                dp[cnt0+m]%=mod
            if cnt1+m<=M:
                dp[cnt1+m]+=prev[m]
                dp[cnt1+m]%=mod
    ans=0
    for m in range(M+1):
        if N-m<=M:
            ans+=dp[m]
            ans%=mod
print(ans)

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 10684kb

input:

6 3
1 3 4 2 6 5

output:

4

result:

ok answer is '4'

Test #2:

score: 0
Accepted
time: 11ms
memory: 10768kb

input:

6 6
1 3 4 2 6 5

output:

8

result:

ok answer is '8'

Test #3:

score: 0
Accepted
time: 12ms
memory: 10652kb

input:

4 4
4 3 1 2

output:

0

result:

ok answer is '0'

Test #4:

score: 0
Accepted
time: 2ms
memory: 10664kb

input:

6 3
1 3 4 2 6 5

output:

4

result:

ok answer is '4'

Test #5:

score: 0
Accepted
time: 6ms
memory: 10664kb

input:

6 6
1 3 4 2 6 5

output:

8

result:

ok answer is '8'

Test #6:

score: 0
Accepted
time: 14ms
memory: 10584kb

input:

4 4
4 3 1 2

output:

0

result:

ok answer is '0'

Test #7:

score: 0
Accepted
time: 69ms
memory: 10912kb

input:

1000 500
4 5 6 8 10 11 12 13 14 15 20 23 25 26 28 29 33 35 1 2 36 38 3 7 41 9 16 44 48 17 18 51 52 53 19 21 54 22 24 59 61 62 27 67 30 31 32 34 68 69 37 39 73 40 76 77 42 81 83 43 85 45 87 46 89 94 47 95 49 50 97 101 55 103 105 56 57 58 106 60 108 110 63 111 114 64 115 65 119 66 70 71 120 121 72 124...

output:

11363968

result:

ok answer is '11363968'

Test #8:

score: 0
Accepted
time: 67ms
memory: 10912kb

input:

1000 500
1 3 5 7 8 11 12 13 15 18 19 21 25 26 28 30 32 2 4 6 34 9 36 37 10 38 39 14 41 43 44 16 45 17 20 22 46 50 52 23 53 54 24 55 57 27 29 58 61 31 63 64 33 35 66 40 69 42 72 73 47 48 49 51 76 77 80 56 81 59 83 60 62 65 84 67 68 85 70 71 74 87 75 78 79 88 82 86 93 95 89 96 90 97 91 92 94 103 106 9...

output:

809753703

result:

ok answer is '809753703'

Test #9:

score: 0
Accepted
time: 58ms
memory: 10980kb

input:

1000 500
1 2 4 5 6 7 8 12 13 14 17 22 23 24 27 30 31 33 34 37 38 42 45 46 47 48 49 50 51 52 54 57 58 61 63 64 66 67 68 69 76 78 79 81 82 83 84 90 91 92 93 95 97 98 3 9 10 11 15 16 99 105 18 106 108 109 19 20 21 25 26 28 113 118 123 29 32 129 133 35 134 36 39 40 135 41 137 43 142 44 143 145 147 53 55...

output:

292864

result:

ok answer is '292864'

Test #10:

score: 0
Accepted
time: 62ms
memory: 10992kb

input:

1000 500
2 3 6 8 10 12 13 16 17 19 1 4 5 21 7 9 11 14 15 18 20 22 25 26 23 27 24 32 35 28 29 30 31 33 36 42 34 37 43 38 39 45 40 48 41 51 44 53 55 56 46 47 57 49 50 58 59 52 54 63 60 64 61 62 65 68 66 67 69 70 73 71 72 74 75 77 81 84 86 76 91 92 95 96 99 101 78 79 80 102 103 82 104 83 85 87 109 113 ...

output:

957884317

result:

ok answer is '957884317'

Test #11:

score: 0
Accepted
time: 68ms
memory: 10908kb

input:

1000 500
2 3 4 5 7 8 9 11 14 18 21 22 23 26 28 33 35 37 38 40 41 47 49 50 51 53 54 55 59 60 61 63 64 65 66 70 73 75 77 79 80 87 88 90 95 96 99 100 101 102 103 104 108 109 110 111 114 115 116 117 121 122 126 128 1 6 10 129 130 12 132 133 13 134 135 15 141 16 142 143 17 19 20 24 145 25 27 29 30 147 14...

output:

20979531

result:

ok answer is '20979531'

Test #12:

score: 0
Accepted
time: 186ms
memory: 11064kb

input:

2000 1000
1 2 3 7 9 16 17 18 19 21 22 24 27 30 31 32 34 37 39 40 42 43 45 46 47 49 50 55 56 57 58 60 63 64 65 66 4 70 71 72 74 5 75 6 78 79 8 80 82 10 11 12 13 14 15 84 86 20 23 25 88 90 91 92 26 93 96 28 29 98 33 99 100 102 35 104 107 36 38 41 44 48 109 112 51 52 113 116 117 53 118 122 123 125 54 5...

output:

290071346

result:

ok answer is '290071346'

Test #13:

score: 0
Accepted
time: 209ms
memory: 11056kb

input:

2000 1000
2 4 6 9 10 14 16 17 20 22 23 24 27 29 31 33 35 40 42 47 48 49 51 56 59 60 62 65 67 68 70 71 74 75 78 79 82 86 93 95 96 97 98 99 100 101 102 104 105 109 111 113 115 117 118 122 123 124 125 127 130 132 133 134 135 136 137 141 142 145 147 148 154 156 158 159 160 161 169 173 175 176 178 179 18...

output:

64

result:

ok answer is '64'

Test #14:

score: 0
Accepted
time: 174ms
memory: 11012kb

input:

2000 1000
3 9 10 11 1 2 4 15 16 5 18 19 20 21 6 25 26 7 28 8 12 29 13 14 32 35 36 39 17 40 41 22 23 43 44 24 53 27 55 56 30 31 59 33 60 34 37 62 38 65 70 71 42 72 73 45 74 46 75 47 77 48 80 49 85 86 50 51 87 88 52 54 57 58 61 63 64 66 89 67 94 68 69 96 76 100 101 78 102 79 103 104 107 81 110 82 111 ...

output:

235072

result:

ok answer is '235072'

Test #15:

score: 0
Accepted
time: 228ms
memory: 11156kb

input:

2000 1000
1 4 5 7 9 11 14 15 18 20 22 23 24 28 29 30 31 33 34 37 38 39 40 41 44 46 47 49 50 52 54 60 61 62 68 69 70 71 72 77 81 84 2 86 3 91 92 6 8 10 95 96 12 99 101 102 13 103 104 109 113 115 116 16 117 17 119 120 121 122 125 126 127 132 19 21 134 25 135 26 27 32 35 36 42 43 45 137 48 138 141 51 5...

output:

166912791

result:

ok answer is '166912791'

Test #16:

score: 0
Accepted
time: 200ms
memory: 10984kb

input:

2000 1000
2 3 4 5 7 8 9 11 14 18 21 22 23 26 28 33 35 37 38 40 41 47 49 50 51 53 54 55 59 60 61 63 64 65 66 70 73 75 77 79 80 87 1 6 10 12 88 13 15 16 90 17 19 95 20 96 99 24 100 101 102 25 103 104 108 109 27 29 30 110 31 32 111 114 115 116 34 117 121 36 122 126 39 42 43 128 44 129 130 45 132 133 46...

output:

418696764

result:

ok answer is '418696764'

Test #17:

score: 0
Accepted
time: 442ms
memory: 11468kb

input:

3000 1500
1 2 3 7 9 16 17 18 19 21 22 24 27 30 31 32 34 37 39 40 42 43 45 46 47 49 50 55 56 57 58 60 63 64 65 66 70 71 72 74 75 78 79 80 82 84 86 88 90 91 92 93 96 98 99 100 102 104 107 109 112 113 116 117 118 122 123 125 127 131 132 133 136 138 139 140 141 144 146 147 149 150 153 154 157 158 161 4 ...

output:

505570241

result:

ok answer is '505570241'

Test #18:

score: 0
Accepted
time: 462ms
memory: 11440kb

input:

3000 1500
2 4 6 9 10 14 16 17 20 22 23 24 27 29 31 33 35 40 42 47 48 49 51 56 59 60 62 65 67 68 70 71 74 75 78 79 82 86 93 95 96 97 98 99 100 101 102 104 105 109 111 113 115 117 118 122 123 124 125 127 130 132 133 134 135 136 137 141 142 145 147 148 154 156 158 159 160 161 169 173 175 176 178 179 18...

output:

2

result:

ok answer is '2'

Test #19:

score: 0
Accepted
time: 388ms
memory: 11492kb

input:

3000 1500
3 9 10 11 15 16 18 19 20 21 25 26 28 29 32 35 36 39 40 41 43 44 53 55 56 59 60 62 65 70 71 1 72 73 2 74 4 75 77 80 85 86 5 87 6 88 7 8 89 94 96 12 13 100 101 14 17 102 103 104 22 107 23 24 110 27 30 31 111 112 114 115 33 34 37 116 117 38 119 120 121 122 42 45 124 46 47 48 49 50 125 126 127...

output:

2024192

result:

ok answer is '2024192'

Test #20:

score: 0
Accepted
time: 363ms
memory: 11492kb

input:

3000 1500
1 4 5 7 9 11 14 15 2 3 6 18 20 8 10 12 22 13 16 17 23 24 28 29 30 31 19 21 25 26 33 27 32 34 35 37 38 39 36 40 41 44 46 42 43 47 49 50 52 45 48 51 53 54 60 55 61 62 68 69 56 57 58 59 70 63 71 64 65 66 72 77 67 73 81 84 74 75 86 91 76 78 79 92 80 82 95 96 99 101 102 103 104 83 109 113 85 87...

output:

697783719

result:

ok answer is '697783719'

Test #21:

score: 0
Accepted
time: 438ms
memory: 11464kb

input:

3000 1500
2 3 4 5 7 8 9 11 14 18 21 22 23 26 28 33 35 37 38 40 41 47 49 50 51 53 54 55 59 60 1 6 61 63 64 10 12 65 13 66 15 70 16 73 75 77 17 19 79 80 20 87 24 88 90 25 27 29 30 31 32 34 95 36 96 99 100 39 101 42 102 43 44 45 103 46 48 52 104 56 57 108 58 62 67 68 109 110 111 114 115 116 69 117 71 7...

output:

721286793

result:

ok answer is '721286793'

Test #22:

score: 0
Accepted
time: 737ms
memory: 11680kb

input:

4000 2000
1 2 3 7 9 16 17 18 19 21 22 24 27 30 31 32 34 37 39 40 42 43 45 46 47 49 50 55 56 57 58 60 63 64 65 66 70 71 72 74 75 78 79 80 82 84 86 88 90 91 92 93 96 98 99 100 102 104 107 109 112 113 116 117 118 122 123 125 127 131 132 133 136 138 139 140 141 144 146 147 149 150 153 154 157 158 161 16...

output:

474181687

result:

ok answer is '474181687'

Test #23:

score: 0
Accepted
time: 648ms
memory: 11616kb

input:

4000 2000
1 3 5 7 8 11 12 13 15 18 19 2 21 25 4 26 28 30 6 9 10 14 16 17 32 34 20 36 37 38 39 41 43 22 23 44 24 45 27 46 50 52 29 53 54 31 55 33 57 35 58 40 61 63 64 42 47 66 69 48 72 49 51 73 56 76 77 59 80 60 81 62 65 83 67 68 84 85 70 87 71 74 75 78 88 79 82 86 89 90 91 93 95 96 97 98 99 100 92 9...

output:

12368

result:

ok answer is '12368'

Test #24:

score: 0
Accepted
time: 608ms
memory: 11644kb

input:

4000 2000
3 9 10 11 15 16 18 19 20 21 25 26 28 29 32 35 36 39 40 41 43 44 53 55 56 59 60 62 65 70 71 72 73 74 1 2 4 75 77 80 85 5 6 7 86 87 8 88 12 89 94 13 96 14 100 101 17 102 22 103 23 24 104 107 27 110 111 30 31 33 112 34 37 38 42 45 46 47 48 49 50 114 51 52 115 54 116 117 119 57 120 121 122 124...

output:

318291867

result:

ok answer is '318291867'

Test #25:

score: 0
Accepted
time: 603ms
memory: 11744kb

input:

4000 2000
2 3 6 8 10 12 13 16 17 19 21 25 26 27 32 35 36 42 43 45 48 51 53 55 56 57 58 59 63 64 65 66 67 73 74 75 76 78 79 80 82 83 85 87 88 89 90 93 94 97 98 100 105 106 107 108 110 111 112 114 118 123 124 128 129 130 1 131 4 133 136 5 7 9 11 14 139 140 15 18 20 22 23 24 28 29 30 31 33 34 142 37 38...

output:

85518659

result:

ok answer is '85518659'

Test #26:

score: 0
Accepted
time: 582ms
memory: 11636kb

input:

4000 2000
1 6 10 12 13 15 16 17 19 20 24 25 27 29 30 31 32 2 3 34 4 36 5 7 39 42 43 44 8 9 45 46 11 48 52 56 14 18 21 22 23 57 26 28 58 33 35 62 37 38 40 41 47 49 50 51 53 67 68 69 71 72 74 76 78 81 54 82 83 84 85 55 59 86 89 60 91 61 92 63 64 93 65 66 94 97 70 98 105 73 75 106 77 107 79 80 87 112 8...

output:

29597070

result:

ok answer is '29597070'

Test #27:

score: 0
Accepted
time: 1188ms
memory: 11824kb

input:

5000 2500
1 2 3 7 9 16 17 18 19 21 22 24 27 30 31 32 34 37 39 40 42 43 45 46 47 49 50 55 56 57 58 60 63 64 65 66 70 71 72 74 75 78 79 80 82 84 86 88 90 91 92 93 96 98 99 100 102 104 107 109 112 113 116 117 118 122 123 125 127 131 132 133 136 138 139 140 141 144 146 147 149 150 153 154 157 158 161 16...

output:

398080

result:

ok answer is '398080'

Test #28:

score: 0
Accepted
time: 1013ms
memory: 11732kb

input:

5000 2500
1 3 5 7 8 11 12 13 15 18 19 21 25 26 28 30 32 34 36 37 38 39 41 43 44 45 46 50 52 53 54 55 57 58 61 63 64 66 69 72 73 76 77 80 81 83 84 85 87 88 89 90 91 92 94 103 106 107 108 110 112 114 116 119 120 121 126 128 129 131 138 139 140 143 144 146 149 150 151 152 153 155 157 162 163 164 165 16...

output:

108654396

result:

ok answer is '108654396'

Test #29:

score: 0
Accepted
time: 904ms
memory: 11840kb

input:

5000 2500
1 2 4 5 6 7 8 12 13 14 17 22 3 23 9 24 27 10 11 30 15 16 18 31 33 19 20 21 34 37 38 25 26 28 42 29 45 46 47 48 49 50 32 51 52 54 57 35 58 61 36 39 40 63 41 64 43 66 44 53 55 56 67 68 69 76 78 79 59 81 60 82 62 65 83 84 90 70 91 92 71 93 95 97 72 98 99 105 73 106 108 74 109 113 75 118 123 1...

output:

293975703

result:

ok answer is '293975703'

Test #30:

score: 0
Accepted
time: 1121ms
memory: 11832kb

input:

5000 2500
2 3 6 8 10 12 13 16 17 19 21 25 26 27 32 35 36 42 43 45 48 51 53 55 56 57 58 59 63 64 65 66 67 73 1 4 74 75 76 78 79 80 82 5 83 85 87 88 7 9 11 14 89 90 93 15 18 94 20 22 97 98 23 100 105 24 28 29 30 31 106 33 107 108 110 34 37 38 39 40 41 44 111 112 46 114 118 47 123 49 124 128 50 129 130...

output:

961199856

result:

ok answer is '961199856'

Test #31:

score: 0
Accepted
time: 970ms
memory: 11764kb

input:

5000 2500
2 3 4 5 7 8 9 11 14 18 21 22 23 26 28 33 35 37 38 40 41 1 6 10 47 49 50 51 53 54 12 13 55 15 16 17 19 20 59 60 24 61 63 64 25 65 27 66 29 70 73 30 31 75 32 34 36 77 39 79 80 87 42 88 90 95 43 96 44 45 99 100 46 48 52 56 57 58 101 102 62 67 68 103 69 71 104 108 109 110 72 74 111 76 78 81 11...

output:

297034581

result:

ok answer is '297034581'

Test #32:

score: -100
Time Limit Exceeded

input:

5000 2500
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:


result: