Submission #581930

# Submission time Handle Problem Language Result Execution time Memory
581930 2022-06-23T08:14:44 Z AlexLuchianov Sparklers (JOI17_sparklers) C++14
100 / 100
198 ms 9452 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>

using ll = long long;

int const nmax = 200000;
int const inf = 1000000000;
int v[5 + nmax];
ll arr[2][5 + nmax];

ll prefmin[5 + nmax], suffmin[5 + nmax];
ll prefmax[5 + nmax], suffmax[5 + nmax];

bool solve(int n, int k, int t, int speed) {
  ll base = speed * t * 2LL;
  int ptr = 0, ptr2 = 0;
  arr[0][++ptr] = 0;
  arr[1][++ptr2] = 0;
  for(int i = k - 1; 1 <= i; i--) {
    arr[0][++ptr] = -(v[i + 1] - v[i]);
    arr[0][++ptr] = base;
  }
  
  for(int i = k + 1; i <= n; i++) {
    arr[1][++ptr2] = -(v[i] - v[i - 1]);
    arr[1][++ptr2] = base;
  }
  
  for(int i = 1; i <= ptr; i++)
    arr[0][i] += arr[0][i - 1];
  for(int i = 1; i <= ptr2; i++)
    arr[1][i] += arr[1][i - 1];
  
  /*
  std::cout << "Visual Map:\n";
  for(int i = 1; i <= ptr; i++) {
    for(int j = 1; j <= ptr2; j++)
      std::cout << (0 <= (base + arr[0][i] + arr[1][j]));
    std::cout << '\n';
  }
  */

  int smax = 1;
  for(int i = 1;i <= ptr; i++)
    if(arr[0][smax] < arr[0][i])
      smax = i;
  
  std::vector<int> stLeft, stRight;
  stLeft.push_back(1);
  for(int i = 2; i <= smax; i++)
    if(arr[0][stLeft.back()] <= arr[0][i])
      stLeft.push_back(i);
  stRight.push_back(ptr);
  for(int i = ptr - 1; smax <= i; i--)
    if(arr[0][stRight.back()] <= arr[0][i])
      stRight.push_back(i);
  
  for(int i = 1; i <= ptr2; i++) { 
    suffmin[i] = prefmin[i] = arr[1][i];
    suffmax[i] = prefmax[i] = arr[1][i];
  }

  for(int i = 2;i <= ptr2; i++) {
    prefmin[i] = std::min(prefmin[i], prefmin[i - 1]);
    prefmax[i] = std::max(prefmax[i], prefmax[i - 1]);
  }

  for(int i = ptr2 - 1; 1 <= i; i--) {
    suffmin[i] = std::min(suffmin[i], suffmin[i + 1]);
    suffmax[i] = std::max(suffmax[i], suffmax[i + 1]);
  }
  
  if(prefmin[ptr2] + arr[0][smax] + base < 0)
    return 0;
  
  auto findPointUpper = [&](int id) {
    int x = 0;
    for(int jump = (1 << 20); 0 < jump; jump /= 2)
      if(x + jump <= ptr2 && 0 <= prefmin[x + jump] + arr[0][id] + base)
        x += jump;
    return x;
  };
  
  auto findPointLower = [&](int id) {
    int x = ptr2 + 1;
    for(int jump = (1 << 20); 0 < jump; jump /= 2)
      if(1 <= x - jump && 0 <= suffmin[x - jump] + arr[0][id] + base)
        x -= jump;
    return x;
  };
  
  auto rangeMin = [&](int x, int y) {
    ll valmin = arr[0][x];
    for(int i = x;i <= y; i++)
      valmin = std::min(valmin, arr[0][i]);
    return valmin;
  };

  for(int i = 1; i < stLeft.size(); i++) {
    int lim = findPointUpper(stLeft[i - 1]);
    if(lim < 1)
      return 0;
    if(rangeMin(stLeft[i - 1], stLeft[i]) + base + prefmax[lim] < 0)
      return 0;
  }
  
  for(int i = 1; i < stRight.size(); i++) {
    int lim = findPointLower(stRight[i - 1]);
    if(ptr2 < lim)
      return 0;
    if(rangeMin(stRight[i], stRight[i - 1]) + base + suffmax[lim] < 0)
      return 0;
  }
  return 1;
}

int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);

  int n, k, t;
  std::cin >> n >> k >> t;
  

  for(int i = 1; i <= n; i++)
    std::cin >> v[i];
  int speed = -1;
  for(int jump = (1 << 30); 0 < jump; jump /= 2)
    if((speed + jump) <= inf / t && solve(n, k, t, speed + jump) == 0)
      speed += jump;
  std::cout << speed + 1 << '\n';
  return 0;
}

Compilation message

sparklers.cpp: In function 'bool solve(int, int, int, int)':
sparklers.cpp:101:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |   for(int i = 1; i < stLeft.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~
sparklers.cpp:109:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |   for(int i = 1; i < stRight.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 344 KB Output is correct
28 Correct 2 ms 344 KB Output is correct
29 Correct 2 ms 344 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 344 KB Output is correct
28 Correct 2 ms 344 KB Output is correct
29 Correct 2 ms 344 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 2 ms 340 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 126 ms 3476 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 26 ms 1108 KB Output is correct
44 Correct 111 ms 8052 KB Output is correct
45 Correct 129 ms 6268 KB Output is correct
46 Correct 114 ms 8260 KB Output is correct
47 Correct 110 ms 7904 KB Output is correct
48 Correct 113 ms 7340 KB Output is correct
49 Correct 101 ms 9452 KB Output is correct
50 Correct 110 ms 6588 KB Output is correct
51 Correct 104 ms 8336 KB Output is correct
52 Correct 123 ms 5396 KB Output is correct
53 Correct 113 ms 9144 KB Output is correct
54 Correct 147 ms 5572 KB Output is correct
55 Correct 113 ms 6388 KB Output is correct
56 Correct 102 ms 8624 KB Output is correct
57 Correct 129 ms 4728 KB Output is correct
58 Correct 114 ms 7304 KB Output is correct
59 Correct 198 ms 6028 KB Output is correct
60 Correct 119 ms 5276 KB Output is correct
61 Correct 124 ms 5860 KB Output is correct
62 Correct 100 ms 8592 KB Output is correct
63 Correct 115 ms 5920 KB Output is correct
64 Correct 106 ms 5992 KB Output is correct
65 Correct 102 ms 7368 KB Output is correct
66 Correct 109 ms 6660 KB Output is correct
67 Correct 97 ms 7764 KB Output is correct
68 Correct 137 ms 5516 KB Output is correct
69 Correct 120 ms 5320 KB Output is correct