From 58e5d0f0418329a32cfdbfcb5b8e82e75c9be0a4 Mon Sep 17 00:00:00 2001 From: Endless Date: Mon, 27 Dec 2021 14:20:46 +0800 Subject: [PATCH] =?UTF-8?q?[2000]=20CF777E=20=E6=A0=88+=E8=B4=AA=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/700-799/777E.go | 38 ++++++++++++++++++++++++++++++++++++++ main/700-799/777E_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 main/700-799/777E.go create mode 100644 main/700-799/777E_test.go diff --git a/main/700-799/777E.go b/main/700-799/777E.go new file mode 100644 index 0000000000..c6ef61af5c --- /dev/null +++ b/main/700-799/777E.go @@ -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) } diff --git a/main/700-799/777E_test.go b/main/700-799/777E_test.go new file mode 100644 index 0000000000..c4f2f2441c --- /dev/null +++ b/main/700-799/777E_test.go @@ -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) +}