邓心一 6 years ago
parent
commit
074d959526

+ 19 - 8
easy/558.quad-tree-intersection.java

@@ -21,14 +21,25 @@ class Node {
 };
 */
 class Solution {
-    public Node intersect(Node quadTree1, Node quadTree2) {
-        
-    }
-
-    private Node helper(Node node1, Node node2) {
-        if (node1.isLeaf && node2.isLeaf) {
-            return new Node(node1.val || node2.val, true, null, null, null, null);
+    public Node intersect(Node q1, Node q2) {
+        if (q1.isLeaf) return (q1.val ? q1 : q2);
+        if (q2.isLeaf) return (q2.val ? q2 : q1);
+        q1.topLeft = intersect(q1.topLeft, q2.topLeft);
+        q1.topRight = intersect(q1.topRight, q2.topRight);
+        q1.bottomLeft = intersect(q1.bottomLeft, q2.bottomLeft);
+        q1.bottomRight = intersect(q1.bottomRight, q2.bottomRight);
+        if (q1.topLeft.isLeaf && q1.topRight.isLeaf && q1.bottomLeft.isLeaf && q1.bottomRight.isLeaf) {
+            if (q1.topLeft.val && q1.topRight.val && q1.bottomLeft.val && q1.bottomRight.val) {
+                q1.isLeaf = true;
+                q1.val = true;
+                return q1;
+            }
+            if (!(q1.topLeft.val || q1.topRight.val || q1.bottomLeft.val || q1.bottomRight.val)) {
+                q1.isLeaf = true;
+                q1.val = false;
+                return q1;
+            }
         }
-        
+        return q1;
     }
 }

+ 40 - 0
oj/360-2018-spring/1-snatch-gold.go

@@ -0,0 +1,40 @@
+// http://exercise.acmcoder.com/quesexcuse?paperId=213
+
+package main
+
+import "fmt"
+
+func main() {
+	var T int
+	fmt.Scan(&T)
+	for cid := 1; cid <= T; cid++ {
+		var n int
+		fmt.Scan(&n)
+		gold := make([]int, n+1)
+		for i := 1; i <= n; i++ {
+			fmt.Scan(&gold[i])
+			gold[i] += gold[i-1]
+		}
+		dp := make([][]int, n)
+		for i := range dp {
+			dp[i] = make([]int, n)
+		}
+		for i := n - 1; 0 <= i; i-- {
+			for j := i; j < n; j++ {
+				if i == j {
+					dp[i][j] = gold[i+1] - gold[i]
+					continue
+				}
+				dp[i][j] = gold[j+1] - gold[i] - minInt(dp[i+1][j], dp[i][j-1])
+			}
+		}
+		fmt.Printf("Case #%d: %d %d\n", cid, dp[0][n-1], gold[n]-gold[0]-dp[0][n-1])
+	}
+}
+
+func minInt(x, y int) int {
+	if x < y {
+		return x
+	}
+	return y
+}

+ 30 - 0
oj/360-2018-spring/2-cut-balloon.go

@@ -0,0 +1,30 @@
+package main
+
+import "fmt"
+
+const MOD int = 1000000007
+
+func main() {
+	var n int
+	fmt.Scan(&n)
+	color := make([]int, n)
+	for i := range color {
+		fmt.Scan(&color[i])
+	}
+	dp := make([]int, n+1)
+	dp[0] = 1
+	for i := 1; i <= n; i++ {
+		dp[i] = dp[i-1]
+		var set [10]bool
+		set[color[i-1]] = true
+		for j := 1; 0 < i-j; j++ {
+			if !set[color[i-j-1]] {
+				dp[i] = (dp[i] + dp[i-j-1]) % MOD
+				set[color[i-j-1]] = true
+			} else {
+				break
+			}
+		}
+	}
+	fmt.Println(dp[n])
+}

+ 14 - 0
oj/360-2018-spring/3-running.go

@@ -0,0 +1,14 @@
+package main
+
+import (
+	"fmt"
+	"math"
+)
+
+func main() {
+	var L, R int
+	fmt.Scan(&L, &R)
+	theta := float64(L) / float64(R)
+	fmt.Printf("%.3f %.3f\n", float64(R)*math.Cos(theta), float64(R)*math.Sin(-theta))
+	fmt.Printf("%.3f %.3f", float64(R)*math.Cos(theta), float64(R)*math.Sin(theta))
+}

+ 56 - 0
oj/tencent-2018-spring/Arrange.java

@@ -0,0 +1,56 @@
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Scanner;
+
+public class Main {
+    static class Pair {
+        int time;
+        int rank;
+
+        Pair(int time, int rank) {
+            this.time = time;
+            this.rank = rank;
+        }
+    }
+
+    public static void main(String[] args) { // 200*xi + 3*yi
+        Scanner scanner = new Scanner(System.in);
+        int n = scanner.nextInt(), m = scanner.nextInt();
+        Pair[] mach = new Pair[n];
+        Pair[] task = new Pair[m];
+        for (int i = 0; i < n; i++) {
+            mach[i] = new Pair(scanner.nextInt(), scanner.nextInt());
+        }
+        for (int i = 0; i < m; i++) {
+            task[i] = new Pair(scanner.nextInt(), scanner.nextInt());
+        }
+        Comparator cmp = new Comparator<Pair>() {
+            @Override
+            public int compare(Pair p1, Pair p2) {
+                if (p1.time != p2.time)
+                    return p2.time - p1.time;
+                return p2.rank - p1.rank;
+            }
+        };
+        Arrays.sort(task, cmp);
+        Arrays.sort(mach, cmp);
+        int cnt = 0, gain = 0, idx = 0;
+        int[] level = new int[101];
+        for (Pair t : task) {
+            while (idx < n && t.time <= mach[idx].time) {
+                level[mach[idx].rank]++;
+                idx++;
+            }
+            for (int i = t.rank; i < 101; i++) {
+                if (0 < level[i]) {
+                    level[i]--;
+                    cnt++;
+                    gain += 200 * t.time + 3 * t.rank;
+                    break;
+                }
+            }
+        }
+        System.out.printf("%d %d\n", cnt, gain);
+        scanner.close();
+    }
+}

+ 49 - 0
oj/tencent-2018-spring/Painter.java

@@ -0,0 +1,49 @@
+import java.util.Scanner;
+
+public class Painter {
+    private static int N = 0, M = 0;
+
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        N = scanner.nextInt();
+        M = scanner.nextInt();
+        scanner.nextLine(); // !!important
+        char[][] board = new char[N][M];
+        for (int i = 0; i < N; i++) {
+            board[i] = scanner.nextLine().toCharArray();
+        }
+        int cnt = 0;
+        for (int i = 0; i < N; i++) {
+            for (int j = 0; j < M; j++) {
+                switch (board[i][j]) {
+                    case 'G':
+                        cnt++;
+                        dfs(board, j, i, 1, 1, 'Y', 'B');
+                    case 'B':
+                        cnt++;
+                        dfs(board, j, i, 1, -1, 'B', 'Y');
+                        break;
+                    case 'Y':
+                        cnt++;
+                        dfs(board, j, i, 1, 1, 'Y', 'B');
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }
+        System.out.println(cnt);
+        scanner.close();
+    }
+
+    private static void dfs(char[][] board, int x, int y, int dx, int dy, char curr, char next) {
+        if (x < 0 || M <= x || y < 0 || N <= y || board[y][x] == 'X' || board[y][x] == next) return;
+        if (board[y][x] == curr) {
+            board[y][x] = 'X';
+        } else if (board[y][x] == 'G') {
+            board[y][x] = next;
+        }
+        dfs(board, x+dx, y+dy, dx, dy, curr, next);
+        dfs(board, x-dx, y-dy, dx, dy, curr, next);
+    }
+}

+ 23 - 0
oj/tencent-2018-spring/SongList.java

@@ -0,0 +1,23 @@
+import java.util.Scanner;
+
+public class SongList {
+    private static final int MOD = 1000000007;
+
+    public static void main(String[] args) {
+        try (Scanner scanner = new Scanner(System.in)) {
+            int K = scanner.nextInt();
+            int A = scanner.nextInt(), X = scanner.nextInt();
+            int B = scanner.nextInt(), Y = scanner.nextInt();
+            int[][] dp = new int[X + Y + 1][K + 1];
+            dp[0][0] = 1; // !!important
+            for (int i = 1; i <= X + Y; i++) {
+                for (int j = 0; j <= K; j++) {
+                    dp[i][j] = dp[i - 1][j]; // Do not pick current song.
+                    int len = X < i ? B : A;
+                    if (len <= j) dp[i][j] = (dp[i][j] + dp[i - 1][j - len]) % MOD; // Pick
+                }
+            }
+            System.out.println(dp[X + Y][K]);
+        }
+    }
+}