LA 7158. ACM-ICPC World Finals 2015 I. Ship Traffic

在区间首尾分别+1 -1的方法叫扫描线??

#include <iostream>
#include <algorithm>
using std::cin;
struct Moment {
  double value; short weight;
  bool operator < (const Moment &mo2) {
    return value == mo2.value ? weight < mo2.weight : value < mo2.value;
  }
};

Moment mo[200002];
int cnt;
void add_time(double v, int w) {
  mo[cnt].value = v;
  mo[cnt].weight = w;
  cnt++;
}

int main() {
  std::ios::sync_with_stdio(false);

  int n; double w, u, v, t1, t2;
  cin >> n >> w >> u >> v >> t1 >> t2;
  cnt = 0;
  add_time(t1, 0);
  add_time(t2, 0);

  for (int i = 0; i < n; ++i) {
    char ch_dir; int m;
    cin >> ch_dir >> m;
    int dir = (ch_dir == 'W') ? 1 : -1;
    while (m--) {
      double len, pos;
      cin >> len >> pos;
      double start_time = std::max(dir * pos / u - w * (i + 1) / v, t1),
             end_time = std::min((dir * pos + len) / u - w * i / v, t2);
      if (start_time >= t2 || end_time <= t1)
        continue;
      add_time(start_time, 1);
      add_time(end_time, -1);
    }
  }

  std::sort(mo, mo + cnt);

  double ans = 0;
  for (int i = 0, cur = 0; i + 1 < cnt; ++i) {
    cur += mo[i].weight;
    if (cur == 0) {
      ans = std::max(ans, mo[i + 1].value - mo[i].value);
    }
  }

  std::cout << ans << std::endl;
  return 0;
}
posted @ 2018-01-11 13:53  Planet6174  阅读(270)  评论(0编辑  收藏  举报