-
Notifications
You must be signed in to change notification settings - Fork 0
/
1307E.go
150 lines (143 loc) · 2.32 KB
/
1307E.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"sync"
)
var cnt [5005][5005] int
var a [5005] int
var tot [5005] int
var l [5005] int
var maxx, ans, n, m int
var mod int64 = 1e9+7
func main() {
n = ReadInt()
m = ReadInt()
for i := 1; i <= n; i++ {
a[i] = ReadInt()
}
for i := 1; i <= m; i++ {
u := ReadInt()
v := ReadInt()
cnt[u][v]++
}
for i := 1; i <= n; i++ {
tot[a[i]]++
}
for i := 1; i <= n; i++ {
for j := 1; j <= n; j++ {
cnt[i][j] += cnt[i][j-1]
}
}
res:=0
var sum int64 =1
for i:=1;i<=n;i++{
u:=cnt[i][tot[i]]
if u>0 {
res++
sum=sum*int64(u)%mod
}
}
maxx=res
ans=int(sum)
for i:=1;i<=n;i++ {
l[a[i]]++
if cnt[a[i]][l[a[i]]]==cnt[a[i]][l[a[i]]-1] {
continue
}
res:=1
var sum int64=1
u:=cnt[a[i]][tot[a[i]]-l[a[i]]]
if tot[a[i]]-l[a[i]]>=l[a[i]] {
u--
}
if u>0 {
res++
sum = sum * int64(u) % mod
}
for j:=1;j<=n;j++ {
if j==a[i] {
continue
}
p:=l[j]
q:=tot[j]-l[j]
u:=cnt[j][p]*cnt[j][q]-cnt[j][min(p,q)]
if u>0 {
res+=2
sum=sum*int64(u)%mod
} else {
u=cnt[j][p]+cnt[j][q]
if u>0 {
res++
sum=sum*int64(u)%mod
}
}
}
if res>maxx {
maxx=res
ans=int(sum)
} else if res==maxx {
ans=(ans+int(sum))%int(mod)
}
}
fmt.Println(maxx,ans)
}
////////////////////////////////////
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup
func ReadString() string {
in.Scan()
return in.Text()
}
func ReadStringSlice(n int) []string {
s := make([]string, n)
for i := 0; i < n; i++ {
s[i] = ReadString()
}
return s
}
func ReadInt() int {
intStr := ReadString()
i, _ := strconv.ParseInt(intStr, 10, 32)
return int(i)
}
func ReadIntSlice(n int) []int {
arr := make([]int, n)
for i := 0; i < n; i++ {
arr[i] = ReadInt()
}
return arr
}
func init() {
//set input
in = bufio.NewScanner(os.Stdin)
in.Buffer(make([]byte, 1024), int(2e+5))
in.Split(bufio.ScanWords)
//set output
out = make(chan string, 16)
outWg = &sync.WaitGroup{}
outWg.Add(1)
writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
go func(write *bufio.Writer) {
defer outWg.Done()
defer write.Flush()
for line := range out {
write.WriteString(line + "\n")
}
}(writer)
}