Submission #408820

# Submission time Handle Problem Language Result Execution time Memory
408820 2021-05-19T17:12:19 Z Mamnoon_Siam Sparklers (JOI17_sparklers) C++17
100 / 100
185 ms 9488 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
#define all(v) begin(v), end(v)
#define sz(v) (int)(v).size()
#define fi first
#define se second
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif

int main(int argc, char const *argv[])
{
#ifdef LOCAL
  freopen("in", "r", stdin);
#endif
  int n, k; ll t;
  scanf("%d %d %lld", &n, &k, &t);
  vector<ll> X(n+1);
  for(int i = 1; i <= n; ++i) {
    scanf("%lld", &X[i]);
  }
  if(X[n] == 0) {
    puts("0");
    return 0;
  }
  function<bool(ll)> possible = [&](ll s) {
    auto x = X;
    for(int i = 1; i <= n; ++i) {
      x[i] -= 2LL * s * t * i;
    }
    x[0] = LLONG_MAX;
    x.push_back(LLONG_MIN);
    vi pg(n+1), ns(n+1), ng(n+1), ps(n+1);
    vector<ll> pg_min = x, ns_max = x;
    vector<ll> ng_min = x, ps_max = x;
    vector<int> stk = {0};
    for(int i = 1; i <= n; ++i) {
      while(x[stk.back()] < x[i]) {
        pg_min[i] = min(pg_min[i], pg_min[stk.back()]);
        stk.pop_back();
      }
      pg[i] = stk.back();
      stk.push_back(i);
    }
    stk.clear();
    stk = {n+1};
    for(int i = n; i >= 1; --i) {
      while(x[stk.back()] > x[i]) {
        ns_max[i] = max(ns_max[i], ns_max[stk.back()]);
        stk.pop_back();
      }
      ns[i] = stk.back();
      stk.push_back(i);
    }
    stk.clear();
    x[0] = LLONG_MIN, x[n+1] = LLONG_MAX;
    stk = {0};
    for(int i = 1; i <= n; ++i) {
      while(x[stk.back()] >= x[i]) {
        ps_max[i] = max(ps_max[i], ps_max[stk.back()]);
        stk.pop_back();
      }
      ps[i] = stk.back();
      stk.push_back(i);
    }
    stk.clear();
    stk = {n+1};
    for(int i = n; i >= 1; --i) {
      while(x[stk.back()] <= x[i]) {
        ng_min[i] = min(ng_min[i], ng_min[stk.back()]);
        stk.pop_back();
      }
      ng[i] = stk.back();
      stk.push_back(i);
    }
    int l1 = k, r1 = k;
    while(pg[l1] >= 1 or ns[r1] <= n) {
      if(pg[l1] >= 1 and pg_min[l1] >= x[r1])
        l1 = pg[l1];
      else if(ns[r1] <= n and ns_max[r1] <= x[l1])
        r1 = ns[r1];
      else
        return false;
    }
    int l2 = 1, r2 = n;
    while(ng[l2] <= k or ps[r2] >= k) {
      if(ng[l2] <= k and ng_min[l2] >= x[r2])
        l2 = ng[l2];
      else if(ps[r2] >= k and ps_max[r2] <= x[l2])
        r2 = ps[r2];
      else
        return false;
    }
    return l1 == l2 and r1 == r2;
  };
  int lo = 1, hi = 1e9, opt = -1;
  while(lo <= hi) {
    int mid = (lo + hi) >> 1;
    if(possible(mid)) {
      opt = mid;
      hi = mid-1;
    } else {
      lo = mid+1;
    }
  }
  printf("%d\n", opt);
  return 0;
}
/*
* use std::array instead of std::vector, if u can
* overflow?
* array bounds
*/

Compilation message

sparklers.cpp: In function 'int main(int, const char**)':
sparklers.cpp:23:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |   scanf("%d %d %lld", &n, &k, &t);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
sparklers.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     scanf("%lld", &X[i]);
      |     ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 292 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 300 KB Output is correct
28 Correct 2 ms 296 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 3 ms 332 KB Output is correct
34 Correct 3 ms 332 KB Output is correct
35 Correct 3 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 2 ms 332 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 3 ms 332 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 292 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 2 ms 300 KB Output is correct
28 Correct 2 ms 296 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
33 Correct 3 ms 332 KB Output is correct
34 Correct 3 ms 332 KB Output is correct
35 Correct 3 ms 332 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 2 ms 332 KB Output is correct
38 Correct 2 ms 332 KB Output is correct
39 Correct 3 ms 332 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 86 ms 6304 KB Output is correct
42 Correct 6 ms 620 KB Output is correct
43 Correct 25 ms 1888 KB Output is correct
44 Correct 167 ms 9484 KB Output is correct
45 Correct 175 ms 9476 KB Output is correct
46 Correct 174 ms 9404 KB Output is correct
47 Correct 157 ms 9388 KB Output is correct
48 Correct 164 ms 9488 KB Output is correct
49 Correct 185 ms 9448 KB Output is correct
50 Correct 165 ms 9432 KB Output is correct
51 Correct 172 ms 9384 KB Output is correct
52 Correct 169 ms 9488 KB Output is correct
53 Correct 170 ms 9380 KB Output is correct
54 Correct 128 ms 9468 KB Output is correct
55 Correct 168 ms 9388 KB Output is correct
56 Correct 165 ms 9436 KB Output is correct
57 Correct 164 ms 9488 KB Output is correct
58 Correct 159 ms 9420 KB Output is correct
59 Correct 10 ms 1228 KB Output is correct
60 Correct 131 ms 9456 KB Output is correct
61 Correct 123 ms 9380 KB Output is correct
62 Correct 118 ms 9480 KB Output is correct
63 Correct 112 ms 9436 KB Output is correct
64 Correct 116 ms 9352 KB Output is correct
65 Correct 120 ms 9488 KB Output is correct
66 Correct 115 ms 9460 KB Output is correct
67 Correct 113 ms 9448 KB Output is correct
68 Correct 114 ms 9488 KB Output is correct
69 Correct 119 ms 9344 KB Output is correct