Skip to content

Commit 2831703

Browse files
committed
[1900][1A] CF1585D 结论题 三置换 排序
1 parent efb303d commit 2831703

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

main/1500-1599/1585D.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF1585D(_r io.Reader, _w io.Writer) {
11+
in := bufio.NewReader(_r)
12+
out := bufio.NewWriter(_w)
13+
defer out.Flush()
14+
15+
var T, n int
16+
for Fscan(in, &T); T > 0; T-- {
17+
Fscan(in, &n)
18+
a := make([]int, n+1)
19+
p := make([]int, n+1)
20+
eq := false
21+
for i := 1; i <= n; i++ { // 注意 a 从 1 开始
22+
Fscan(in, &a[i])
23+
if p[a[i]] > 0 {
24+
eq = true
25+
}
26+
p[a[i]] = i
27+
}
28+
if eq || n < 2 {
29+
Fprintln(out, "YES")
30+
continue
31+
}
32+
for i := 1; i < n-1; i++ {
33+
if p[i] == i {
34+
continue
35+
}
36+
x, y := p[i], i
37+
z := n
38+
if x == n {
39+
z--
40+
}
41+
p[a[z]] = x
42+
p[a[y]] = z
43+
a[x] = a[z]
44+
a[z] = a[y]
45+
//a[x], a[y], a[z] = a[z], a[x], a[y]
46+
}
47+
if a[n-1] < a[n] { // 注意 a 从 1 开始
48+
Fprintln(out, "YES")
49+
} else {
50+
Fprintln(out, "NO")
51+
}
52+
}
53+
}
54+
55+
//func main() { CF1585D(os.Stdin, os.Stdout) }

main/1500-1599/1585D_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1585/D
9+
// https://codeforces.com/problemset/status/1585/problem/D
10+
func TestCF1585D(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
7
15+
1
16+
1
17+
2
18+
2 2
19+
2
20+
2 1
21+
3
22+
1 2 3
23+
3
24+
2 1 3
25+
3
26+
3 1 2
27+
4
28+
2 1 4 3
29+
outputCopy
30+
YES
31+
YES
32+
NO
33+
YES
34+
NO
35+
YES
36+
YES`
37+
testutil.AssertEqualCase(t, rawText, 0, CF1585D)
38+
}

0 commit comments

Comments
 (0)