dengxinyi vor 6 Jahren
Ursprung
Commit
8a240870dc

+ 37 - 0
bailian/4110.santa-claus-gifts/main.cc

@@ -0,0 +1,37 @@
+#include <algorithm>
+#include <cstdio>
+
+using namespace std;
+
+const double eps = 1e-6;
+
+struct Candy {
+  int v;
+  int w;
+  bool operator<(const Candy that) const {
+    return double(this->v) / this->w - double(that.v) / that.w > eps;
+  }
+};
+
+int main() {
+  int n, w;
+  scanf("%d %d", &n, &w);
+  Candy c[100];
+  for (int i = 0; i < n; i++) {
+    scanf("%d %d", &c[i].v, &c[i].w);
+  }
+  sort(c, c + n);
+  int curr_w = 0;
+  double curr_v = 0.0;
+  for (int i = 0; i < n; i++) {
+    if (curr_w + c[i].w <= w) {
+      curr_w += c[i].w;
+      curr_v += c[i].v;
+    } else {
+      curr_v += double(w - curr_w) / c[i].w * c[i].v;
+      break;
+    }
+  }
+  printf("%.1f\n", curr_v);
+  return 0;
+}

+ 30 - 0
bailian/4151.movie-festival/main.cc

@@ -0,0 +1,30 @@
+#include <algorithm>
+#include <cstdio>
+
+using namespace std;
+
+struct Movie {
+  int s;
+  int e;
+  bool operator<(const Movie that) const { return this->e < that.e; }
+};
+
+int main() {
+  int n;
+  Movie m[100];
+  for (;;) {
+    scanf("%d", &n);
+    if (n == 0) break;
+    for (int i = 0; i < n; i++) scanf("%d %d", &m[i].s, &m[i].e);
+    sort(m, m + n);
+    int pre = -1, cnt = 0;
+    for (int i = 0; i < n; i++) {
+      if (pre <= m[i].s) {
+        cnt++;
+        pre = m[i].e;
+      }
+    }
+    printf("%d\n", cnt);
+  }
+  return 0;
+}

+ 55 - 0
poj/3190.stall-reservations/main.cc

@@ -0,0 +1,55 @@
+#include <algorithm>
+#include <cstdio>
+#include <queue>
+
+using namespace std;
+
+struct Cow {
+  int i;
+  int s;
+  int e;
+  bool operator<(const Cow that) const { return this->s < that.s; }
+};
+
+struct Stall {
+  int i;
+  int e;
+  // The < op here means the rank of this is LESS than the rank of that.
+  bool operator<(const Stall that) const { return this->e > that.e; }
+};
+
+const int N = 50000;
+
+int main() {
+  int n, res[N];
+  Cow c[N];
+  while (scanf("%d", &n) != EOF) { // Multi group of input
+    for (int i = 0; i < n; i++) {
+      scanf("%d %d", &c[i].s, &c[i].e);
+      c[i].i = i;
+    }
+    sort(c, c + n);
+
+    priority_queue<Stall> q;
+    int id = 0;
+    res[c[0].i] = ++id;
+    q.push({id, c[0].e});
+
+    for (int i = 1; i < n; i++) {
+      Stall s = q.top();
+      if (s.e < c[i].s) { // Is NOT <=, but < only
+        res[c[i].i] = s.i;
+        s.e = c[i].e;
+        q.pop();
+      } else {
+        res[c[i].i] = ++id;
+        s = {id, c[i].e};
+      }
+      q.push(s);
+    }
+
+    printf("%d\n", id);
+    for (int i = 0; i < n; i++) printf("%d\n", res[i]);
+  }
+  return 0;
+}