Skip to content

Commit 3b501b0

Browse files
committed
[1700][1A] CF1148C 分类讨论 交换至有序
1 parent a3e5454 commit 3b501b0

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

main/1100-1199/1148C.go

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

main/1100-1199/1148C_test.go

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

0 commit comments

Comments
 (0)