Skip to content

Commit

Permalink
[2000] CF777E 栈+贪心
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Dec 27, 2021
1 parent df2c740 commit 58e5d0f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
38 changes: 38 additions & 0 deletions main/700-799/777E.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"bufio"
. "fmt"
"io"
"sort"
)

// github.com/EndlessCheng/codeforces-go
func CF777E(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
var n int
Fscan(in, &n)
a := make([]struct{ r, R, h int }, n)
for i := range a {
Fscan(in, &a[i].r, &a[i].R, &a[i].h)
}
sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.R > b.R || a.R == b.R && a.r > b.r })

sum := int64(a[0].h)
ans := sum
s := []int{0}
for i := 1; i < n; i++ {
for len(s) > 0 && a[s[len(s)-1]].r >= a[i].R {
sum -= int64(a[s[len(s)-1]].h)
s = s[:len(s)-1]
}
s = append(s, i)
sum += int64(a[i].h)
if sum > ans {
ans = sum
}
}
Fprint(out, ans)
}

//func main() { CF777E(os.Stdin, os.Stdout) }
29 changes: 29 additions & 0 deletions main/700-799/777E_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/EndlessCheng/codeforces-go/main/testutil"
"testing"
)

// https://codeforces.com/problemset/problem/777/E
// https://codeforces.com/problemset/status/777/problem/E
func TestCF777E(t *testing.T) {
// just copy from website
rawText := `
inputCopy
3
1 5 1
2 6 2
3 7 3
outputCopy
6
inputCopy
4
1 2 1
1 3 3
4 6 2
5 7 1
outputCopy
4`
testutil.AssertEqualCase(t, rawText, 0, CF777E)
}

0 comments on commit 58e5d0f

Please sign in to comment.