Skip to content

Commit 58e5d0f

Browse files
committed
[2000] CF777E 栈+贪心
1 parent df2c740 commit 58e5d0f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

main/700-799/777E.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+
"bufio"
5+
. "fmt"
6+
"io"
7+
"sort"
8+
)
9+
10+
// github.com/EndlessCheng/codeforces-go
11+
func CF777E(_r io.Reader, out io.Writer) {
12+
in := bufio.NewReader(_r)
13+
var n int
14+
Fscan(in, &n)
15+
a := make([]struct{ r, R, h int }, n)
16+
for i := range a {
17+
Fscan(in, &a[i].r, &a[i].R, &a[i].h)
18+
}
19+
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 })
20+
21+
sum := int64(a[0].h)
22+
ans := sum
23+
s := []int{0}
24+
for i := 1; i < n; i++ {
25+
for len(s) > 0 && a[s[len(s)-1]].r >= a[i].R {
26+
sum -= int64(a[s[len(s)-1]].h)
27+
s = s[:len(s)-1]
28+
}
29+
s = append(s, i)
30+
sum += int64(a[i].h)
31+
if sum > ans {
32+
ans = sum
33+
}
34+
}
35+
Fprint(out, ans)
36+
}
37+
38+
//func main() { CF777E(os.Stdin, os.Stdout) }

main/700-799/777E_test.go

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

0 commit comments

Comments
 (0)