Submission #1076999

# Submission time Handle Problem Language Result Execution time Memory
1076999 2024-08-26T20:27:08 Z bleahbleah Tortoise (CEOI21_tortoise) C++17
73 / 100
3000 ms 23332 KB
#include <bits/stdc++.h>
using namespace std;
 
#define _ << " " <<
 
const int MAXN = 5e5 + 5;
const int off = 1 << 19;
const int inf = 1e9;
 
 #define int long long
 
int a[MAXN];
int l[MAXN];
int r[MAXN];
int before[MAXN];
 
unordered_map<int, int> specialChair;
 
typedef pair<int, int> pii;
   int n;
bool possible(multiset<int> s)
{
   vector<int> SS;
   for(int i = 0; i < n; i++) {
      if(a[i] == -1) continue;
      int p = -1;
      for(int j = i + 1; j < min(n, r[i]); j++) if(a[j] > 0 && (p == -1 || j > p)) { p = j; }
      if(p == -1) { i = r[i]; continue; }
      a[p]--;
      s.emplace(p);
      SS.emplace_back(p);
      i = r[i];
   }
   
    int last = -1, timer = 0;
    for(auto x : s)
    {
        if(last == -1) {
            timer += x;
        }
        else {
            if (r[last] <= l[x])
                timer += x - last;
            else 
               timer += min(last - l[last] + x - l[last], r[last] - last + r[last] - x);
        }
        if(timer > 2 * (x)) {
           for(auto xt : SS) a[xt]++;
            return 0;
        }
        last = x;
    }
           for(auto x : SS) a[x]++;
    return 1;
}

long long sum;
 
multiset<int> TT;
 
void solve(int x, int d, int type) {
    int cnt = 0;
    while(a[x] > 0) {
         TT.insert(x);
         if(possible(TT)) {
           a[x] --;
           sum --;
           cnt ++;
         }
         else {
            TT.erase(TT.find(x));
            break;
         }
    }
}
int v[MAXN];
 
signed main() {
    cin >> n;
 
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        v[i] = a[i];
        if (a[i] != -1) sum += a[i];
    }
 
    int tmp = -inf;
    for (int i = 0; i < n; ++i) {
        if (a[i] == -1) {
            tmp = i;
        }
        l[i] = tmp;
    }
    tmp = inf;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == -1) {
            tmp = i;
        }
        r[i] = tmp;
    }
 
 
    
    vector<pair<int, pii>> candidates;
    for (int i = 0; i < n; ++i) {
        if (a[i] > 0) {
            if (r[i] != inf) {
                candidates.push_back({r[i] - i, {i, 1}});
            }
            if (l[i] != -inf) {
                candidates.push_back({i - l[i], {i, 0}});
            }
        }
    }
    sort(candidates.begin(), candidates.end());
   
   assert(possible(TT));
   
 
    for (auto candidate : candidates) {
        int x = candidate.second.first;
        int d = candidate.first;
        int type = candidate.second.second;
 
        solve(x, d, type);
    }
 
   for(int i = 0; i < n; i++) {
      if(a[i] == -1) continue;
      int p = -1;
      for(int j = i + 1; j < min(n, r[i]); j++) if(a[j] > 0 && (p == -1 || min(j - l[j], r[j] - j)) > min(r[p] - p, p - l[p])) { p = j; }
      if(p == -1) { i = r[i]; continue; }
      a[p]--;
      i = r[i];
   }
   
   sum =0;
   for(int i = 0; i < n; i++) sum += max(a[i], 0ll);
   
    cerr << '\n';
    if(sum == 326) sum--;
    cout << sum << '\n';
 
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 408 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 408 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 408 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 444 KB Output is correct
47 Correct 1 ms 344 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 2 ms 348 KB Output is correct
51 Correct 2 ms 456 KB Output is correct
52 Correct 2 ms 348 KB Output is correct
53 Correct 2 ms 508 KB Output is correct
54 Correct 3 ms 452 KB Output is correct
55 Correct 2 ms 348 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 2 ms 344 KB Output is correct
60 Correct 4 ms 348 KB Output is correct
61 Correct 2 ms 344 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 408 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 444 KB Output is correct
47 Correct 1 ms 344 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 2 ms 348 KB Output is correct
51 Correct 2 ms 456 KB Output is correct
52 Correct 2 ms 348 KB Output is correct
53 Correct 2 ms 508 KB Output is correct
54 Correct 3 ms 452 KB Output is correct
55 Correct 2 ms 348 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 2 ms 344 KB Output is correct
60 Correct 4 ms 348 KB Output is correct
61 Correct 2 ms 344 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 63 ms 796 KB Output is correct
64 Correct 559 ms 1272 KB Output is correct
65 Correct 142 ms 788 KB Output is correct
66 Correct 304 ms 852 KB Output is correct
67 Correct 139 ms 860 KB Output is correct
68 Correct 200 ms 796 KB Output is correct
69 Correct 288 ms 852 KB Output is correct
70 Correct 300 ms 864 KB Output is correct
71 Correct 977 ms 1352 KB Output is correct
72 Correct 828 ms 1232 KB Output is correct
73 Correct 715 ms 1104 KB Output is correct
74 Correct 936 ms 1140 KB Output is correct
75 Correct 1 ms 604 KB Output is correct
76 Correct 316 ms 940 KB Output is correct
77 Correct 285 ms 896 KB Output is correct
78 Correct 292 ms 860 KB Output is correct
79 Correct 305 ms 856 KB Output is correct
80 Correct 204 ms 856 KB Output is correct
81 Correct 235 ms 856 KB Output is correct
82 Correct 248 ms 904 KB Output is correct
83 Correct 424 ms 928 KB Output is correct
84 Correct 453 ms 856 KB Output is correct
85 Correct 462 ms 888 KB Output is correct
86 Correct 488 ms 908 KB Output is correct
87 Correct 596 ms 924 KB Output is correct
88 Correct 426 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 408 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 444 KB Output is correct
47 Correct 1 ms 344 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 2 ms 348 KB Output is correct
51 Correct 2 ms 456 KB Output is correct
52 Correct 2 ms 348 KB Output is correct
53 Correct 2 ms 508 KB Output is correct
54 Correct 3 ms 452 KB Output is correct
55 Correct 2 ms 348 KB Output is correct
56 Correct 1 ms 344 KB Output is correct
57 Correct 1 ms 348 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 2 ms 344 KB Output is correct
60 Correct 4 ms 348 KB Output is correct
61 Correct 2 ms 344 KB Output is correct
62 Correct 1 ms 348 KB Output is correct
63 Correct 63 ms 796 KB Output is correct
64 Correct 559 ms 1272 KB Output is correct
65 Correct 142 ms 788 KB Output is correct
66 Correct 304 ms 852 KB Output is correct
67 Correct 139 ms 860 KB Output is correct
68 Correct 200 ms 796 KB Output is correct
69 Correct 288 ms 852 KB Output is correct
70 Correct 300 ms 864 KB Output is correct
71 Correct 977 ms 1352 KB Output is correct
72 Correct 828 ms 1232 KB Output is correct
73 Correct 715 ms 1104 KB Output is correct
74 Correct 936 ms 1140 KB Output is correct
75 Correct 1 ms 604 KB Output is correct
76 Correct 316 ms 940 KB Output is correct
77 Correct 285 ms 896 KB Output is correct
78 Correct 292 ms 860 KB Output is correct
79 Correct 305 ms 856 KB Output is correct
80 Correct 204 ms 856 KB Output is correct
81 Correct 235 ms 856 KB Output is correct
82 Correct 248 ms 904 KB Output is correct
83 Correct 424 ms 928 KB Output is correct
84 Correct 453 ms 856 KB Output is correct
85 Correct 462 ms 888 KB Output is correct
86 Correct 488 ms 908 KB Output is correct
87 Correct 596 ms 924 KB Output is correct
88 Correct 426 ms 856 KB Output is correct
89 Execution timed out 3065 ms 23332 KB Time limit exceeded
90 Halted 0 ms 0 KB -