邓心一 6 年之前
父節點
當前提交
37b62a03ca
共有 5 個文件被更改,包括 229 次插入0 次删除
  1. 25 0
      oj/pdd-2019/no1/mult.go
  2. 29 0
      oj/pdd-2019/no2/card.go
  3. 52 0
      oj/pdd-2019/no3/money.go
  4. 61 0
      oj/pdd-2019/no4/Main.java
  5. 62 0
      oj/pdd-2019/no4/exp.go

+ 25 - 0
oj/pdd-2019/no1/mult.go

@@ -0,0 +1,25 @@
+package main
+
+import (
+	"fmt"
+	"sort"
+)
+
+func main() {
+	var n int
+	fmt.Scan(&n)
+	a, b := make([]int, n), make([]int, n)
+	for i := range a {
+		fmt.Scan(&a[i])
+	}
+	for i := range b {
+		fmt.Scan(&b[i])
+	}
+	sort.Ints(a)
+	sort.Sort(sort.Reverse(sort.IntSlice(b)))
+	s := 0
+	for i := 0; i < n; i++ {
+		s += a[i] * b[i]
+	}
+	fmt.Println(s)
+}

+ 29 - 0
oj/pdd-2019/no2/card.go

@@ -0,0 +1,29 @@
+package main
+
+import (
+	"fmt"
+	"strings"
+)
+
+func main() {
+	var s string
+	fmt.Scanln(&s)
+	s = strings.ToLower(s)
+	var freq [26]int
+	for _, r := range s {
+		freq[r-'a']++
+	}
+	first := int(s[0] - 'a')
+	freq[first]--
+	for i := 1; i < len(s); i++ {
+		ch := int(s[i] - 'a')
+		if first < ch && freq[ch] == 1 {
+			break
+		}
+		if ch < first && 0 < freq[first] {
+			first = ch
+		}
+		freq[ch]--
+	}
+	fmt.Println(string(first + 'a'))
+}

+ 52 - 0
oj/pdd-2019/no3/money.go

@@ -0,0 +1,52 @@
+package main
+
+import (
+	"fmt"
+	"sort"
+)
+
+type ints [][2]int
+
+func (is ints) Len() int           { return len(is) }
+func (is ints) Less(i, j int) bool { return is[i][0] < is[j][0] }
+func (is ints) Swap(i, j int)      { is[i], is[j] = is[j], is[i] }
+
+func main() {
+	var n, d int
+	fmt.Scan(&n, &d)
+	var is ints = make([][2]int, n)
+	for i := range is {
+		fmt.Scan(&is[i][0], &is[i][1])
+	}
+	sort.Sort(is)
+	dp := make([]int, n)
+	idx := 0
+	dp[0] = -1
+	for i := 1; i < n; i++ {
+		if is[i][0]-is[idx][0] < d {
+			dp[i] = dp[i-1]
+			continue
+		}
+		if pre := dp[i-1]; pre == -1 || is[pre][1] < is[idx][1] {
+			dp[i] = idx
+		} else {
+			dp[i] = dp[i-1]
+		}
+		idx++
+	}
+	max := 0
+	for i := range dp {
+		if dp[i] == -1 {
+			continue
+		}
+		max = maxInt(max, is[i][1]+is[dp[i]][1])
+	}
+	fmt.Println(max)
+}
+
+func maxInt(x, y int) int {
+	if x < y {
+		return y
+	}
+	return x
+}

+ 61 - 0
oj/pdd-2019/no4/Main.java

@@ -0,0 +1,61 @@
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Scanner;
+
+public class Main {
+    private static final int MOD = 1000000007;
+
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        String s1 = scanner.nextLine();
+        String s2 = scanner.nextLine();
+        scanner.close();
+        int lb = 0;
+        char[] ch1 = s1.toCharArray();
+        char[] ch2 = s2.toCharArray();
+        for (char c : ch1) {
+            lb += c == '(' ? 1 : -1;
+        }
+        for (char c : ch2) {
+            lb += c == '(' ? 1 : -1;
+        }
+        if (lb != 0) {
+            System.out.println(0);
+            return;
+        }
+        HashMap<List<Integer>, Integer> map = new HashMap<>();
+        int res = dfs(ch1, ch2, 0, 0, 0, map);
+        System.out.println(res);
+    }
+
+    private static int dfs(char[] ch1, char[] ch2, int i1, int i2, int lb, HashMap<List<Integer>, Integer> map) {
+        if (i1 == ch1.length && i2 == ch2.length) {
+            if (lb == 0) return 1;
+            return 0;
+        }
+        Integer res;
+        List<Integer> key = new ArrayList<>();
+        key.add(i1);
+        key.add(i2);
+        key.add(lb);
+        if ((res = map.get(key)) != null) return res;
+        res = 0;
+        if (i1 < ch1.length) {
+            if (ch1[i1] == '(') {
+                res = (res + dfs(ch1, ch2, i1+1, i2, lb+1, map)) % MOD;
+            } else if (lb != 0) {
+                res = (res + dfs(ch1, ch2, i1+1, i2, lb-1, map)) % MOD;
+            }
+        }
+        if (i2 < ch2.length) {
+            if (ch2[i2] == '(') {
+                res = (res + dfs(ch1, ch2, i1, i2+1, lb+1, map)) % MOD;
+            } else if (lb != 0) {
+                res = (res + dfs(ch1, ch2, i1, i2+1, lb-1, map)) % MOD;
+            } 
+        }
+        map.put(key, res);
+        return res;
+    }
+}

+ 62 - 0
oj/pdd-2019/no4/exp.go

@@ -0,0 +1,62 @@
+package main
+
+import "fmt"
+
+const MOD int = 1000000007
+
+func main() {
+	var s1, s2 string
+	fmt.Scanln(&s1)
+	fmt.Scanln(&s2)
+	lb := 0
+	for _, r := range s1 {
+		if r == '(' {
+			lb++
+		} else {
+			lb--
+		}
+	}
+	for _, r := range s2 {
+		if r == '(' {
+			lb++
+		} else {
+			lb--
+		}
+	}
+	if lb != 0 {
+		fmt.Println(0)
+		return
+	}
+	m := make(map[[3]int]int)
+	res := dfs([]rune(s1), []rune(s2), len(s1), len(s2), 0, 0, 0, m)
+	fmt.Println(res)
+}
+
+func dfs(s1, s2 []rune, l1, l2, i1, i2, lb int, m map[[3]int]int) int {
+	if i1 == l1 && i2 == l2 {
+		if lb == 0 {
+			return 1
+		}
+		return 0
+	}
+	if v, ok := m[[3]int{i1, i2, lb}]; ok {
+		return v
+	}
+	res := 0
+	if i1 < l1 {
+		if s1[i1] == '(' {
+			res = (res + dfs(s1, s2, l1, l2, i1+1, i2, lb+1, m)) % MOD
+		} else if lb != 0 {
+			res = (res + dfs(s1, s2, l1, l2, i1+1, i2, lb-1, m)) % MOD
+		}
+	}
+	if i2 < l2 {
+		if s2[i2] == '(' {
+			res = (res + dfs(s1, s2, l1, l2, i1, i2+1, lb+1, m)) % MOD
+		} else if lb != 0 {
+			res = (res + dfs(s1, s2, l1, l2, i1, i2+1, lb-1, m)) % MOD
+		}
+	}
+	m[[3]int{i1, i2, lb}] = res
+	return res
+}