dengxinyi 5 tahun lalu
induk
melakukan
2f504141e5

+ 81 - 0
poj/4001.xiangqi/main.cc

@@ -0,0 +1,81 @@
+#include <cstdio>
+#include <cstring>
+
+#define X 10
+#define Y 9
+
+int dx[4] = {1, 0, -1, 0};
+int dy[4] = {0, 1, 0, -1};
+
+int hx[8] = {2, 2, 1, -1, -2, -2, 1, -1};
+int hy[8] = {1, -1, 2, 2, 1, -1, -2, -2};
+
+char board[X + 1][Y + 1];
+
+bool is_out(int x, int y) {
+    return x < 1 || X < x || y < 1 || Y < y;
+}
+
+bool solve(int x, int y) {
+    if (x < 1 || 3 < x || y < 4 || 6 < y) return true;
+    for (int i = 0; i < 4; i++) {
+        int nx = x + dx[i];
+        int ny = y + dy[i];
+        char c = 0;
+        int gap = 0;
+        while (gap < 2 && !is_out(nx, ny)) {
+            c = board[nx][ny];
+            nx += dx[i];
+            ny += dy[i];
+            if (c == 0) continue;
+            switch (c) {
+                case 'G': case 'R':
+                    if (gap == 0) return true;
+                    break;
+                case 'C':
+                    if (gap == 1) return true;
+                    break;
+            }
+            gap++;
+        }
+    }
+    for (int i = 0; i < 8; i++) {
+        int nx = x + hx[i];
+        int ny = y + hy[i];
+        if (is_out(nx, ny)) continue;
+        if (board[nx][ny] == 'H') {
+            nx = x + dx[i / 2];
+            ny = y + dy[i / 2];
+            if (board[nx][ny] == 0) return true;
+        }
+    }
+    return false;
+}
+
+int main() {
+    int N, gx, gy;
+    for (;;) {
+        scanf("%d %d %d", &N, &gx, &gy);
+        if (N == 0 && gx == 0 && gy == 0) break;
+        memset(board, 0, sizeof(board));
+        char s[2];
+        int x, y;
+        for (int n = 0; n < N; n++) {
+            scanf("%s %d %d", &s, &x, &y);
+            board[x][y] = s[0];
+        }
+        bool checkmate = true;
+        for (int nx = gx + 1, ny = gy; !is_out(nx, ny); nx++) {
+            char c = board[nx][ny];
+            if (c == 0) continue;
+            if (c == 'G') checkmate = false;
+            break;
+        }
+        for (int i = 0; i < 4; i++) {
+            int nx = gx + dx[i];
+            int ny = gy + dy[i];
+            checkmate &= solve(nx, ny);
+        }
+        printf(checkmate ? "YES\n" : "NO\n");
+    }
+}

+ 26 - 0
written-exam/ebay-2019-autumn/no1/main.cc

@@ -0,0 +1,26 @@
+#include <algorithm>
+#include <cstdio>
+
+using std::sort;
+
+#define N 10000
+
+int w[N];
+
+int main() {
+  int n, W;
+  scanf("%d %d", &n, &W);
+  for (int i = 0; i < n; i++) scanf("%d", &w[i]);
+  sort(w, w + n);
+  int l = 0, r = n - 1;
+  int cnt = 0;
+  while (l <= r) {
+    if (w[l] + w[r] <= W) {
+      l++;
+    }
+    r--;
+    cnt++;
+  }
+  printf("%d\n", cnt);
+  return 0;
+}

+ 65 - 0
written-exam/ebay-2019-autumn/no2/Main.java

@@ -0,0 +1,65 @@
+import java.util.*;
+
+public class Main {
+    public static void main(String[] args) {
+        Main main = new Main();
+        Scanner in = new Scanner(System.in);
+        int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
+        int[] a = new int[k];
+        for (int i = 0; i < k; i++) {
+            a[i] = in.nextInt();
+        }
+        in.close();
+        System.out.println(main.solve(n, m, a));
+    }
+
+    public int solve(int n, int m, int[] a) {
+        int[] prev = new int[n + 1];
+        int[] next = new int[a.length + 1];
+        Arrays.fill(prev, a.length);
+        for (int i = 0; i < a.length; i++) {
+            next[i] = a.length;
+            next[prev[a[i]]] = i;
+            prev[a[i]] = i;
+        }
+        PriorityQueue<Node> pq = new PriorityQueue<>();
+        boolean[] rem = new boolean[n + 1];
+        int size = 0, ans = 0;
+        for (int i = 0; i < a.length; i++) {
+            if (rem[a[i]]) {
+                pq.add(new Node(a[i], next[i]));
+                continue;
+            }
+            if (size == m) {
+                size--;
+                while (!pq.isEmpty()) {
+                    Node top = pq.poll();
+                    if (rem[top.x]) {
+                        rem[top.x] = false;
+                        break;
+                    }
+                }
+            }
+            size++;
+            ans++;
+            rem[a[i]] = true;
+            pq.add(new Node(a[i], next[i]));
+        }
+        return ans;
+    }
+}
+
+class Node implements Comparable<Node> {
+    int x;
+    int y;
+
+    public Node(int x, int y) {
+        this.x = x;
+        this.y = y;
+    }
+
+    @Override
+    public int compareTo(Node that) {
+        return that.y - this.y;
+    }
+}

+ 63 - 0
written-exam/ebay-2019-autumn/no2/main.cc

@@ -0,0 +1,63 @@
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <queue>
+#include <set>
+
+using std::priority_queue; using std::set;
+
+const int MAX = 100000;
+
+int a[MAX];
+int prev[MAX + 1];
+int next[MAX + 1];
+
+bool rem[MAX + 1];
+
+struct node {
+  int x;
+  int y;
+  node(int x, int y) : x(x), y(y) {}
+  int operator<(const node &that) const {
+    return that.y - this->y;
+  }
+};
+
+int main() {
+  int N, M, K;
+  scanf("%d %d %d", &N, &M, &K);
+  for (int i = 1; i <= N; i++) {
+    prev[i] = K;
+  }
+  for (int i = 0; i < K; i++) {
+    scanf("%d", &a[i]);
+    next[i] = K;
+    next[prev[a[i]]] = i;
+    prev[a[i]] = i;
+  }
+  int m = 0, cnt = 0;
+  memset(rem, 0, sizeof(rem));
+  priority_queue<node> pq;
+  for (int i = 0; i < K; i++) {
+    if (rem[a[i]]) {
+      pq.push(node(a[i], next[i]));
+      continue; // Hit the cache
+    }
+    if (m == M) {
+      m--;
+      while (!pq.empty()) {
+        node top = pq.top();
+        pq.pop();
+        if (rem[top.x]) {
+          rem[top.x] = false;
+          break;
+        }
+      }
+    }
+    m++;
+    cnt++;
+    rem[a[i]] = true;
+    pq.push(node(a[i], next[i]));
+  }
+  printf("%d\n", cnt);
+}

+ 8 - 0
written-exam/kickstart-2019-D/no1/Main.java

@@ -0,0 +1,8 @@
+import java.util.*;
+
+public class Main {
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        scanner.close();
+    }
+}

+ 58 - 0
written-exam/kickstart-2019-D/no1/main.cc

@@ -0,0 +1,58 @@
+#include <algorithm>
+#include <cstring>
+#include <cstdio>
+
+using std::max;
+
+#define N 100000
+#define Q 100000
+
+bool A[N];
+int P[Q];
+bool V[Q];
+int dp[2][N + 1];
+
+bool int2bool(int i) {
+    int b = 0;
+    while (0 < i) {
+        b += i & 1;
+        i >>= 1;
+    }
+    return b & 1;
+}
+
+int main() {
+    int T;
+    scanf("%d", &T);
+    for (int t = 1; t <= T; t++) {
+        int n, q;
+        scanf("%d %d", &n, &q);
+        for (int i = 0, j; i < n; i++) {
+            scanf("%d", &j);
+            A[i] = int2bool(j);
+        }
+        for (int i = 0, j; i < q; i++) {
+            scanf("%d %d", &P[i], &j);
+            V[i] = int2bool(j);
+        }
+        printf("Case #%d:", t);
+        for (int i = 0; i < q; i++) {
+            A[P[i]] = V[i];
+            memset(dp, 0, sizeof(dp));
+            for (int j = 1; j <= n; j++) {
+                if (A[j - 1]) { // Odd
+                    dp[1][j] = dp[0][j - 1] + 1;
+                    dp[0][j] = dp[1][j - 1] != 0 ? dp[1][j - 1] + 1 : 0;
+                } else { // Even
+                    dp[1][j] = max(0, dp[1][j - 1] + 1);
+                    dp[0][j] = max(1, dp[0][j - 1] + 1);
+                }
+            }
+            int l = 0;
+            for (int j = 1; j <= n; j++) l = max(l, dp[0][j]);
+            printf(" %d", l);
+        }
+        printf("\n");
+    }
+    return 0;
+}

+ 8 - 0
written-exam/kickstart-2019-D/no2/Main.java

@@ -0,0 +1,8 @@
+import java.util.*;
+
+public class Main {
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        scanner.close();
+    }
+}

+ 5 - 0
written-exam/kickstart-2019-D/no2/main.cc

@@ -0,0 +1,5 @@
+#include <cstdio>
+
+int main() {
+    return 0;
+}

+ 8 - 0
written-exam/kickstart-2019-D/no3/Main.java

@@ -0,0 +1,8 @@
+import java.util.*;
+
+public class Main {
+    public static void main(String[] args) {
+        Scanner scanner = new Scanner(System.in);
+        scanner.close();
+    }
+}

+ 19 - 0
written-exam/kickstart-2019-D/no3/main.cc

@@ -0,0 +1,19 @@
+#include <cstdio>
+
+#define N 100000
+
+int X[N];
+int C[N];
+
+int main() {
+    int T;
+    scanf("%d", &T);
+    for (int t = 1; t <= T; t++) {
+        int k, n;
+        scanf("%d %d", &k, &n);
+        for (int i = 0; i < n; i++) scanf("%d", &X[i]);
+        for (int i = 0; i < n; i++) scanf("%d", &C[i]);
+        printf("Case #%d: \n", t);
+    }
+    return 0;
+}

+ 75 - 0
written-exam/kickstart-2019-E/no1/main.cc

@@ -0,0 +1,75 @@
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <set>
+
+using namespace std;
+
+#define N_MAX 100000
+
+struct UF {
+  int id[N_MAX + 1];
+  int sz[N_MAX + 1];
+  int n;
+
+  void Init(int n) {
+    memset(sz, 0, sizeof(sz));
+    for (int i = 0; i <= n; i++) id[i] = i;
+  }
+
+  int Find(int i) {
+    int j = id[i];
+    if (i == j) return i;
+    j = Find(j);
+    id[i] = j;
+    return j;
+  }
+
+  bool Union(int i, int j) {
+    i = Find(i);
+    j = Find(j);
+    if (i == j) return false;
+    if (sz[i] < sz[j]) {
+      id[i] = j;
+    } else {
+      if (sz[i] == sz[j]) sz[i]++;
+      id[j] = i;
+    }
+    return true;
+  };
+} uf;
+
+struct Pair {
+  int x;
+  int y;
+
+  bool operator<(const Pair &that) const {
+    if (this->x != that.x) return this->x < that.x;
+    return this->y < that.y;
+  };
+};
+
+int main() {
+  int T;
+  scanf("%d", &T);
+  for (int t = 1; t <= T; t++) {
+    int N, M;
+    scanf("%d %d", &N, &M);
+    uf.Init(N);
+    set<Pair> black;
+    for (int i = 0, C, D; i < M; i++) {
+      scanf("%d %d", &C, &D);
+      if (C < D) black.insert(Pair{C, D});
+      else black.insert(Pair{D, C});
+    }
+    int sugar = 0;
+    set<Pair>::iterator it;
+    for (it = black.begin(); it != black.end(); ++it)
+      if (uf.Union(it->x, it->y)) sugar++;
+    for (int i = 1; i < N; i++)
+      for (int j = i + 1; j <= N; j++)
+        if (uf.Union(i, j)) sugar += 2;
+    printf("Case #%d: %d\n", t, sugar);
+  }
+  return 0;
+}

+ 89 - 0
written-exam/kickstart-2019-E/no2/main.cc

@@ -0,0 +1,89 @@
+#include <cstdio>
+#include <cstring>
+
+#define D_MAX 100000
+#define S_MAX 100000
+
+char ans[D_MAX + 1];
+int C[S_MAX];
+int E[S_MAX];
+
+int gcd(int a, int b) {
+  if (b == 0) return a;
+  return gcd(b, a % b);
+}
+
+struct Frac {
+  int a;
+  int b;
+
+  bool operator==(const Frac &that) const { 
+    return this->a == that.a && this->b == that.b;
+  }
+
+  void Norm() {
+    int c = gcd(a, b);
+    a /= c;
+    b /= c;
+  }
+} frac;
+
+int main() {
+  int T;
+  scanf("%d", &T);
+  for (int t = 1; t <= T; t++) {
+    int D, S, C_sum = 0, E_sum = 0;
+    scanf("%d %d", &D, &S);
+    bool parallel = true;
+    for (int i = 0; i < S; i++) {
+      scanf("%d %d", &C[i], &E[i]);
+      C_sum += C[i];
+      E_sum += E[i];
+      if (i == 0) {
+        frac = Frac{C[i], E[i]};
+        frac.Norm();
+        continue;
+      }
+      Frac curr = Frac{C[i], E[i]};
+      curr.Norm();
+      parallel |= frac == curr;
+    }
+    memset(ans, 0, sizeof(ans));
+    for (int i = 0, A, B; i < D; i++) {
+      scanf("%d %d", &A, &B);
+      if (C_sum < A || E_sum < B) {
+        ans[i] = 'N';
+        continue;
+      }
+      if (parallel) {
+        if (A * frac.b > (E_sum - B) * frac.a) ans[i] = 'N';
+        else ans[i] = 'Y';
+        continue;
+      }
+      int a = C[0] * (E_sum - B) - E[0] * A;
+      int b = C[0] * E[1] - C[1] * E[0];
+      Frac y = Frac{a, b};
+      y.Norm();
+      a = y.a;
+      b = y.b;
+      Frac x = Frac{A * b - C[1] * a, C[0] * b};
+      x.Norm();
+      if (y.a * y.b >= 0 && x.a * x.b >= 0 && y.a <= y.b && x.a <= x.b) {
+        ans[i] = 'Y';
+        continue;
+      }
+      Frac x0 = Frac{A, C[0]};
+      x0.Norm();
+      Frac x1 = Frac{E_sum - B, E[0]};
+      x1.Norm();
+      if (x0.a * x1.b > x0.b * x1.a) {
+        if (x0.a > x0.b) ans[i] = 'Y';
+        else ans[i] = 'N';
+      } else {
+        ans[i] = 'Y';
+      }
+    }
+    printf("Case #%d: %s\n", t, ans);
+  }
+  return 0;
+}

+ 15 - 0
written-exam/kickstart-2019-E/no3/main.cc

@@ -0,0 +1,15 @@
+#include <cstdio>
+
+#define X 1000000000
+#define P 30
+
+int main() {
+  int T;
+  scanf("%d", &T);
+  for (int t = 1; t <= T; t++) {
+    long L, R;
+    scanf("%d %d", &L, &R);
+    printf("Case #%d: %d\n", t, 0);
+  }
+  return 0;
+}