Skip to content

Commit

Permalink
CF1168A 二分 贪心
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Sep 2, 2022
1 parent 4c27fe0 commit 8d11c4e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
35 changes: 14 additions & 21 deletions main/1100-1199/1168A.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,27 @@ import (
)

// github.com/EndlessCheng/codeforces-go
func Sol1168A(reader io.Reader, writer io.Writer) {
in := bufio.NewReader(reader)
out := bufio.NewWriter(writer)
defer out.Flush()

func CF1168A(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
var n, m int
Fscan(in, &n, &m)
arr := make([]int, n)
for i := range arr {
Fscan(in, &arr[i])
a := make([]int, n)
for i := range a {
Fscan(in, &a[i])
}
ans := sort.Search(m, func(op int) bool {
min := 0
for _, a := range arr {
if a+op < m+min {
if a+op < min {
Fprint(out, sort.Search(m, func(lim int) bool {
pre := 0
for _, v := range a {
// (pre-v+m)%m 表示把 v 改成 pre,需要的操作次数
if (pre-v+m)%m > lim { // 无法修改成 pre
if v < pre { // 无法保证单调非降
return false
}
if a > min {
min = a
}
pre = v // 只能 v 不变了
}
}
return true
})
Fprintln(out, ans)
}))
}

//func main() {
// Sol1168A(os.Stdin, os.Stdout)
//}
//func main() { CF1168A(os.Stdin, os.Stdout) }
4 changes: 2 additions & 2 deletions main/1100-1199/1168A_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestSol1168A(t *testing.T) {
func TestCF1168A(t *testing.T) {
// just copy from website
rawText := `
10 10
Expand All @@ -22,5 +22,5 @@ inputCopy
0 6 1 3 2
outputCopy
1`
testutil.AssertEqualCase(t, rawText, -1, Sol1168A)
testutil.AssertEqualCase(t, rawText, -1, CF1168A)
}

0 comments on commit 8d11c4e

Please sign in to comment.