Submission #21011

# Submission time Handle Problem Language Result Execution time Memory
21011 2017-03-29T09:37:50 Z model_code Long Distance Coach (JOI17_coach) C++11
100 / 100
419 ms 20716 KB
#include <cstdio>
#include <cstdlib>
#include <set>
#include <functional>
#include <algorithm>

using namespace std;

typedef long long Int;

const int MAXN = 500050;
const Int INFLL = 1001001001001001001LL;

template<typename T> void chmin(T& a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T& a, T b) { if (a < b) a = b; }
int in() { int x; scanf("%d", &x); return x; }
Int lin() { Int x; scanf("%lld", &x); return x; }

struct CHT {
  typedef pair<Int, Int> line;
  struct comp {
    bool operator() (const line& a, const line& b) {
      return a.first != b.first ? a.first > b.first : a.second < b.second;
    }
  };
  set<line, comp> lines;

  CHT() {
    lines.emplace(INFLL, INFLL);
    lines.emplace(0, INFLL + 1);
  }

  template<typename T> T prev(T it) { --it; return it; }
  template<typename T> T next(T it) { ++it; return it; }

  bool compare(Int a, Int b, Int c, Int d) {  // ab >= cd?
    bool neg1 = (a < 0) ^ (b < 0), neg2 = (c < 0) ^ (d < 0);
    if (neg1 ^ neg2) return neg2;
    if (neg1 && neg2) return compare(abs(c), abs(d), abs(a), abs(b));
    if (a < 0) return compare(-a, -b, -c, -d);
    if (b == 0 || d == 0) return d == 0;
    if (a / d != c / b) return a / d > c / b;
    if (c % b == 0 || a % d == 0) return c % b == 0;
    return compare(b, a % d, d, c % b);
  }

  bool check(line l1, line l2, line l3) {
    return compare(l2.first - l1.first, l3.second - l2.second, l2.second - l1.second, l3.first - l2.first);
  }

  void add(Int a, Int b) {
    line l(a, b);
    auto it = lines.insert(l).first;
    if (check(*prev(it), *it, *next(it))) {
      lines.erase(it);
      return;
    }

    for (--it; it != lines.begin() && check(*prev(it), *it, l); --it) { }
    lines.erase(++it, lines.find(l));

    if (next(lines.find(l)) == lines.end()) {
      return;
    }

    for (it = next(lines.find(l)); next(it) != lines.end() && check(l, *it, *next(it)); ++it) { }
    lines.erase(next(lines.find(l)), it);
  }

  Int value(const line& l, Int x) {
    return l.second >= INFLL ? INFLL : l.first * x + l.second;
  }

  Int query(Int x) {
    lines.erase(lines.begin());
    auto it = lines.begin();
    Int res = value(*it++, x);
    while (it != lines.end()) {
      Int v = value(*it, x);
      if (res > v) {
        res = v;
        lines.erase(lines.begin());
        it = next(lines.begin());
      } else {
        break;
      }
    }
    lines.emplace(INFLL, INFLL);
    return res;
  }
};

Int X, T, W, S[MAXN], dp[MAXN], C[MAXN];
int N, M;

Int off(Int s) {
  return s % T;
}

struct passenger {
  int d, c;
  Int cnt;
} P[MAXN];

int main() {
  X = lin();
  N = in();
  M = in();
  W = lin();
  T = lin();
  for (int i = 0; i < N; ++i) {
    S[i] = lin();
  }
  S[N++] = X;
  for (int i = 1; i <= M; ++i) {
    P[i].d = in();
    P[i].c = in();
    P[i].cnt = X / T + (P[i].d < X % T ? 1 : 0);
  }

  sort(P + 1, P + M + 1, [] (const passenger& a, const passenger& b) {
    return a.d > b.d;
  });
  sort(S, S + N, [] (const Int a, const Int b) {
    return off(a) < off(b);
  });

  C[0] = 0;
  for (int i = 1; i <= M; ++i) {
    C[i] = C[i - 1] + P[i].c;
  }

  CHT cht;
  int last = N;
  Int empty = 0;
  P[0].d = T + 1;
  for (int i = 1; i <= M; ++i) {
    Int min1 = min(empty, (i == 1 ? INFLL : cht.query(i - 1) + C[i - 1]));
    empty = min1 + W * P[i].cnt;
    for (int j = last - 1; j >= 0 && off(S[j]) >= P[i].d; --j) {
      Int b = min1;
      cht.add(W * (S[j] / T), b - W * (S[j] / T) * (i - 1) - C[i - 1]);
      last = j;
    }
  }

  printf("%lld\n", min(empty, cht.query(M) + C[M]) + W * (X / T + (X % T ? 1 : 0)));

  return 0;
}

Compilation message

coach.cpp: In function 'int in()':
coach.cpp:16:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 int in() { int x; scanf("%d", &x); return x; }
                                  ^
coach.cpp: In function 'Int lin()':
coach.cpp:17:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 Int lin() { Int x; scanf("%lld", &x); return x; }
                                     ^

# Verdict Execution time Memory Grader output
1 Correct 0 ms 20716 KB Output is correct
2 Correct 0 ms 20716 KB Output is correct
3 Correct 0 ms 20716 KB Output is correct
4 Correct 0 ms 20716 KB Output is correct
5 Correct 0 ms 20716 KB Output is correct
6 Correct 0 ms 20716 KB Output is correct
7 Correct 0 ms 20716 KB Output is correct
8 Correct 0 ms 20716 KB Output is correct
9 Correct 0 ms 20716 KB Output is correct
10 Correct 0 ms 20716 KB Output is correct
11 Correct 0 ms 20716 KB Output is correct
12 Correct 0 ms 20716 KB Output is correct
13 Correct 0 ms 20716 KB Output is correct
14 Correct 0 ms 20716 KB Output is correct
15 Correct 0 ms 20716 KB Output is correct
16 Correct 0 ms 20716 KB Output is correct
17 Correct 0 ms 20716 KB Output is correct
18 Correct 0 ms 20716 KB Output is correct
19 Correct 0 ms 20716 KB Output is correct
20 Correct 0 ms 20716 KB Output is correct
21 Correct 0 ms 20716 KB Output is correct
22 Correct 0 ms 20716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 20716 KB Output is correct
2 Correct 0 ms 20716 KB Output is correct
3 Correct 0 ms 20716 KB Output is correct
4 Correct 0 ms 20716 KB Output is correct
5 Correct 0 ms 20716 KB Output is correct
6 Correct 0 ms 20716 KB Output is correct
7 Correct 0 ms 20716 KB Output is correct
8 Correct 0 ms 20716 KB Output is correct
9 Correct 0 ms 20716 KB Output is correct
10 Correct 0 ms 20716 KB Output is correct
11 Correct 0 ms 20716 KB Output is correct
12 Correct 0 ms 20716 KB Output is correct
13 Correct 0 ms 20716 KB Output is correct
14 Correct 0 ms 20716 KB Output is correct
15 Correct 0 ms 20716 KB Output is correct
16 Correct 0 ms 20716 KB Output is correct
17 Correct 0 ms 20716 KB Output is correct
18 Correct 0 ms 20716 KB Output is correct
19 Correct 0 ms 20716 KB Output is correct
20 Correct 0 ms 20716 KB Output is correct
21 Correct 0 ms 20716 KB Output is correct
22 Correct 0 ms 20716 KB Output is correct
23 Correct 0 ms 20716 KB Output is correct
24 Correct 0 ms 20716 KB Output is correct
25 Correct 0 ms 20716 KB Output is correct
26 Correct 0 ms 20716 KB Output is correct
27 Correct 0 ms 20716 KB Output is correct
28 Correct 0 ms 20716 KB Output is correct
29 Correct 0 ms 20716 KB Output is correct
30 Correct 0 ms 20716 KB Output is correct
31 Correct 0 ms 20716 KB Output is correct
32 Correct 0 ms 20716 KB Output is correct
33 Correct 0 ms 20716 KB Output is correct
34 Correct 0 ms 20716 KB Output is correct
35 Correct 0 ms 20716 KB Output is correct
36 Correct 0 ms 20716 KB Output is correct
37 Correct 0 ms 20716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 20716 KB Output is correct
2 Correct 0 ms 20716 KB Output is correct
3 Correct 0 ms 20716 KB Output is correct
4 Correct 0 ms 20716 KB Output is correct
5 Correct 0 ms 20716 KB Output is correct
6 Correct 0 ms 20716 KB Output is correct
7 Correct 0 ms 20716 KB Output is correct
8 Correct 0 ms 20716 KB Output is correct
9 Correct 0 ms 20716 KB Output is correct
10 Correct 0 ms 20716 KB Output is correct
11 Correct 0 ms 20716 KB Output is correct
12 Correct 0 ms 20716 KB Output is correct
13 Correct 0 ms 20716 KB Output is correct
14 Correct 0 ms 20716 KB Output is correct
15 Correct 0 ms 20716 KB Output is correct
16 Correct 0 ms 20716 KB Output is correct
17 Correct 0 ms 20716 KB Output is correct
18 Correct 0 ms 20716 KB Output is correct
19 Correct 0 ms 20716 KB Output is correct
20 Correct 0 ms 20716 KB Output is correct
21 Correct 0 ms 20716 KB Output is correct
22 Correct 0 ms 20716 KB Output is correct
23 Correct 0 ms 20716 KB Output is correct
24 Correct 0 ms 20716 KB Output is correct
25 Correct 0 ms 20716 KB Output is correct
26 Correct 0 ms 20716 KB Output is correct
27 Correct 0 ms 20716 KB Output is correct
28 Correct 0 ms 20716 KB Output is correct
29 Correct 0 ms 20716 KB Output is correct
30 Correct 0 ms 20716 KB Output is correct
31 Correct 0 ms 20716 KB Output is correct
32 Correct 0 ms 20716 KB Output is correct
33 Correct 0 ms 20716 KB Output is correct
34 Correct 0 ms 20716 KB Output is correct
35 Correct 0 ms 20716 KB Output is correct
36 Correct 0 ms 20716 KB Output is correct
37 Correct 0 ms 20716 KB Output is correct
38 Correct 3 ms 20716 KB Output is correct
39 Correct 0 ms 20716 KB Output is correct
40 Correct 3 ms 20716 KB Output is correct
41 Correct 3 ms 20716 KB Output is correct
42 Correct 3 ms 20716 KB Output is correct
43 Correct 3 ms 20716 KB Output is correct
44 Correct 3 ms 20716 KB Output is correct
45 Correct 3 ms 20716 KB Output is correct
46 Correct 3 ms 20716 KB Output is correct
47 Correct 3 ms 20716 KB Output is correct
48 Correct 3 ms 20716 KB Output is correct
49 Correct 0 ms 20716 KB Output is correct
50 Correct 3 ms 20716 KB Output is correct
51 Correct 0 ms 20716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 20716 KB Output is correct
2 Correct 0 ms 20716 KB Output is correct
3 Correct 0 ms 20716 KB Output is correct
4 Correct 0 ms 20716 KB Output is correct
5 Correct 0 ms 20716 KB Output is correct
6 Correct 0 ms 20716 KB Output is correct
7 Correct 0 ms 20716 KB Output is correct
8 Correct 0 ms 20716 KB Output is correct
9 Correct 0 ms 20716 KB Output is correct
10 Correct 0 ms 20716 KB Output is correct
11 Correct 0 ms 20716 KB Output is correct
12 Correct 0 ms 20716 KB Output is correct
13 Correct 0 ms 20716 KB Output is correct
14 Correct 0 ms 20716 KB Output is correct
15 Correct 0 ms 20716 KB Output is correct
16 Correct 0 ms 20716 KB Output is correct
17 Correct 0 ms 20716 KB Output is correct
18 Correct 0 ms 20716 KB Output is correct
19 Correct 0 ms 20716 KB Output is correct
20 Correct 0 ms 20716 KB Output is correct
21 Correct 0 ms 20716 KB Output is correct
22 Correct 0 ms 20716 KB Output is correct
23 Correct 0 ms 20716 KB Output is correct
24 Correct 0 ms 20716 KB Output is correct
25 Correct 0 ms 20716 KB Output is correct
26 Correct 0 ms 20716 KB Output is correct
27 Correct 0 ms 20716 KB Output is correct
28 Correct 0 ms 20716 KB Output is correct
29 Correct 0 ms 20716 KB Output is correct
30 Correct 0 ms 20716 KB Output is correct
31 Correct 0 ms 20716 KB Output is correct
32 Correct 0 ms 20716 KB Output is correct
33 Correct 0 ms 20716 KB Output is correct
34 Correct 0 ms 20716 KB Output is correct
35 Correct 0 ms 20716 KB Output is correct
36 Correct 0 ms 20716 KB Output is correct
37 Correct 0 ms 20716 KB Output is correct
38 Correct 3 ms 20716 KB Output is correct
39 Correct 0 ms 20716 KB Output is correct
40 Correct 3 ms 20716 KB Output is correct
41 Correct 3 ms 20716 KB Output is correct
42 Correct 3 ms 20716 KB Output is correct
43 Correct 3 ms 20716 KB Output is correct
44 Correct 3 ms 20716 KB Output is correct
45 Correct 3 ms 20716 KB Output is correct
46 Correct 3 ms 20716 KB Output is correct
47 Correct 3 ms 20716 KB Output is correct
48 Correct 3 ms 20716 KB Output is correct
49 Correct 0 ms 20716 KB Output is correct
50 Correct 3 ms 20716 KB Output is correct
51 Correct 0 ms 20716 KB Output is correct
52 Correct 326 ms 20716 KB Output is correct
53 Correct 359 ms 20716 KB Output is correct
54 Correct 346 ms 20716 KB Output is correct
55 Correct 313 ms 20716 KB Output is correct
56 Correct 319 ms 20716 KB Output is correct
57 Correct 319 ms 20716 KB Output is correct
58 Correct 339 ms 20716 KB Output is correct
59 Correct 326 ms 20716 KB Output is correct
60 Correct 296 ms 20716 KB Output is correct
61 Correct 319 ms 20716 KB Output is correct
62 Correct 313 ms 20716 KB Output is correct
63 Correct 293 ms 20716 KB Output is correct
64 Correct 203 ms 20716 KB Output is correct
65 Correct 396 ms 20716 KB Output is correct
66 Correct 419 ms 20716 KB Output is correct
67 Correct 373 ms 20716 KB Output is correct
68 Correct 393 ms 20716 KB Output is correct
69 Correct 326 ms 20716 KB Output is correct
70 Correct 329 ms 20716 KB Output is correct
71 Correct 319 ms 20716 KB Output is correct
72 Correct 329 ms 20716 KB Output is correct
73 Correct 343 ms 20716 KB Output is correct
74 Correct 353 ms 20716 KB Output is correct
75 Correct 366 ms 20716 KB Output is correct
76 Correct 356 ms 20716 KB Output is correct
77 Correct 283 ms 20716 KB Output is correct
78 Correct 279 ms 20716 KB Output is correct
79 Correct 323 ms 20716 KB Output is correct
80 Correct 326 ms 20716 KB Output is correct