Submission #551775

# Submission time Handle Problem Language Result Execution time Memory
551775 2022-04-21T14:08:29 Z cadmiumsky Rope (JOI17_rope) C++14
100 / 100
1300 ms 92324 KB
#include <bits/stdc++.h>

using namespace std;
const int nmax = 1e6 + 5, mmax = 1e6 + 5;
int N, m, rez[mmax];
vector<int> g[mmax];

namespace MO {
  int freq[mmax], ffreq[nmax];
  int ptr = 0;
  void init() {memset(freq, 0, sizeof(freq)), memset(ffreq, 0, sizeof(ffreq)), ptr = 0;}
  void ignore(int x) {
    ffreq[freq[x]]--;
    while(ptr > 0 && ffreq[ptr] == 0)
      ptr--;
  }
  void reinsert(int x) {
    ffreq[freq[x]]++;
    ptr = max(freq[x], ptr);
  }
  void add(int x, int aggr) {
    ffreq[freq[x]]--;
    freq[x] += aggr;
    ffreq[freq[x]]++;
    while(ffreq[ptr + 1] > 0)
      ptr++;
    while(ptr > 0 && ffreq[ptr] == 0)
      ptr--;
  }
}

static void solve(vector<int> v) {
  int n = v.size();
  assert(n % 2 == 0);
  for(int i = 0; i < m; i++) g[i].clear();
  for(int i = 0; i < n; i += 2) {
    if(v[i] != v[i + 1])
      g[v[i]].push_back(v[i + 1]),
      g[v[i + 1]].push_back(v[i]);
    MO::add(v[i], 1);
    MO::add(v[i + 1], 1);
  }
  for(int i = 0, collat; i < m; i++) {
    MO::ignore(i);
    collat = g[i].size();
    for(auto x : g[i])
      MO::add(x, -1);
    rez[i] = min(rez[i], collat + (N - (MO::freq[i] + collat)) - MO::ptr);
    for(auto x : g[i])
      MO::add(x, 1);
    MO::reinsert(i);
  }
}

int main() {
  int n;
  cin >> n >> m;
  N = n;
  vector<int> v(n), temp;
  for(auto &x : v)
    cin >> x, --x;
  for(int i = 0; i < m; i++)
    rez[i] = N * 2;
  MO::init();
  temp = v;
  if(n % 2 == 1)
    MO::add(v.back(), 1), v.pop_back();
  solve(v);
  v = temp;
  MO::init();
  MO::add(v[0], 1);
  if(n % 2 == 0)  
    MO::add(v.back(), 1), v.pop_back();
  reverse(v.begin(), v.end()); v.pop_back();
  reverse(v.begin(), v.end()); 
  solve(v);
  for(int i = 0; i < m; i++)
    cout << rez[i] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 16 ms 31572 KB Output is correct
3 Correct 16 ms 31572 KB Output is correct
4 Correct 17 ms 31612 KB Output is correct
5 Correct 17 ms 31608 KB Output is correct
6 Correct 18 ms 31572 KB Output is correct
7 Correct 17 ms 31568 KB Output is correct
8 Correct 15 ms 31560 KB Output is correct
9 Correct 16 ms 31584 KB Output is correct
10 Correct 17 ms 31740 KB Output is correct
11 Correct 16 ms 31572 KB Output is correct
12 Correct 16 ms 31572 KB Output is correct
13 Correct 17 ms 31560 KB Output is correct
14 Correct 16 ms 31572 KB Output is correct
15 Correct 19 ms 31600 KB Output is correct
16 Correct 17 ms 31572 KB Output is correct
17 Correct 16 ms 31608 KB Output is correct
18 Correct 16 ms 31572 KB Output is correct
19 Correct 17 ms 31572 KB Output is correct
20 Correct 16 ms 31620 KB Output is correct
21 Correct 15 ms 31612 KB Output is correct
22 Correct 16 ms 31616 KB Output is correct
23 Correct 16 ms 31572 KB Output is correct
24 Correct 16 ms 31616 KB Output is correct
25 Correct 16 ms 31564 KB Output is correct
26 Correct 15 ms 31616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 16 ms 31572 KB Output is correct
3 Correct 16 ms 31572 KB Output is correct
4 Correct 17 ms 31612 KB Output is correct
5 Correct 17 ms 31608 KB Output is correct
6 Correct 18 ms 31572 KB Output is correct
7 Correct 17 ms 31568 KB Output is correct
8 Correct 15 ms 31560 KB Output is correct
9 Correct 16 ms 31584 KB Output is correct
10 Correct 17 ms 31740 KB Output is correct
11 Correct 16 ms 31572 KB Output is correct
12 Correct 16 ms 31572 KB Output is correct
13 Correct 17 ms 31560 KB Output is correct
14 Correct 16 ms 31572 KB Output is correct
15 Correct 19 ms 31600 KB Output is correct
16 Correct 17 ms 31572 KB Output is correct
17 Correct 16 ms 31608 KB Output is correct
18 Correct 16 ms 31572 KB Output is correct
19 Correct 17 ms 31572 KB Output is correct
20 Correct 16 ms 31620 KB Output is correct
21 Correct 15 ms 31612 KB Output is correct
22 Correct 16 ms 31616 KB Output is correct
23 Correct 16 ms 31572 KB Output is correct
24 Correct 16 ms 31616 KB Output is correct
25 Correct 16 ms 31564 KB Output is correct
26 Correct 15 ms 31616 KB Output is correct
27 Correct 36 ms 33612 KB Output is correct
28 Correct 36 ms 33532 KB Output is correct
29 Correct 34 ms 33524 KB Output is correct
30 Correct 39 ms 33396 KB Output is correct
31 Correct 37 ms 33500 KB Output is correct
32 Correct 40 ms 33640 KB Output is correct
33 Correct 35 ms 33484 KB Output is correct
34 Correct 37 ms 33264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 16 ms 31572 KB Output is correct
3 Correct 16 ms 31572 KB Output is correct
4 Correct 17 ms 31612 KB Output is correct
5 Correct 17 ms 31608 KB Output is correct
6 Correct 18 ms 31572 KB Output is correct
7 Correct 17 ms 31568 KB Output is correct
8 Correct 15 ms 31560 KB Output is correct
9 Correct 16 ms 31584 KB Output is correct
10 Correct 17 ms 31740 KB Output is correct
11 Correct 16 ms 31572 KB Output is correct
12 Correct 16 ms 31572 KB Output is correct
13 Correct 17 ms 31560 KB Output is correct
14 Correct 16 ms 31572 KB Output is correct
15 Correct 19 ms 31600 KB Output is correct
16 Correct 17 ms 31572 KB Output is correct
17 Correct 16 ms 31608 KB Output is correct
18 Correct 16 ms 31572 KB Output is correct
19 Correct 17 ms 31572 KB Output is correct
20 Correct 16 ms 31620 KB Output is correct
21 Correct 15 ms 31612 KB Output is correct
22 Correct 16 ms 31616 KB Output is correct
23 Correct 16 ms 31572 KB Output is correct
24 Correct 16 ms 31616 KB Output is correct
25 Correct 16 ms 31564 KB Output is correct
26 Correct 15 ms 31616 KB Output is correct
27 Correct 36 ms 33612 KB Output is correct
28 Correct 36 ms 33532 KB Output is correct
29 Correct 34 ms 33524 KB Output is correct
30 Correct 39 ms 33396 KB Output is correct
31 Correct 37 ms 33500 KB Output is correct
32 Correct 40 ms 33640 KB Output is correct
33 Correct 35 ms 33484 KB Output is correct
34 Correct 37 ms 33264 KB Output is correct
35 Correct 39 ms 33720 KB Output is correct
36 Correct 45 ms 33740 KB Output is correct
37 Correct 42 ms 33740 KB Output is correct
38 Correct 41 ms 33652 KB Output is correct
39 Correct 44 ms 33644 KB Output is correct
40 Correct 40 ms 33784 KB Output is correct
41 Correct 40 ms 33784 KB Output is correct
42 Correct 43 ms 33772 KB Output is correct
43 Correct 39 ms 33756 KB Output is correct
44 Correct 41 ms 33772 KB Output is correct
45 Correct 43 ms 33820 KB Output is correct
46 Correct 41 ms 33860 KB Output is correct
47 Correct 40 ms 33768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 16 ms 31572 KB Output is correct
3 Correct 16 ms 31572 KB Output is correct
4 Correct 17 ms 31612 KB Output is correct
5 Correct 17 ms 31608 KB Output is correct
6 Correct 18 ms 31572 KB Output is correct
7 Correct 17 ms 31568 KB Output is correct
8 Correct 15 ms 31560 KB Output is correct
9 Correct 16 ms 31584 KB Output is correct
10 Correct 17 ms 31740 KB Output is correct
11 Correct 16 ms 31572 KB Output is correct
12 Correct 16 ms 31572 KB Output is correct
13 Correct 17 ms 31560 KB Output is correct
14 Correct 16 ms 31572 KB Output is correct
15 Correct 19 ms 31600 KB Output is correct
16 Correct 17 ms 31572 KB Output is correct
17 Correct 16 ms 31608 KB Output is correct
18 Correct 16 ms 31572 KB Output is correct
19 Correct 17 ms 31572 KB Output is correct
20 Correct 16 ms 31620 KB Output is correct
21 Correct 15 ms 31612 KB Output is correct
22 Correct 16 ms 31616 KB Output is correct
23 Correct 16 ms 31572 KB Output is correct
24 Correct 16 ms 31616 KB Output is correct
25 Correct 16 ms 31564 KB Output is correct
26 Correct 15 ms 31616 KB Output is correct
27 Correct 36 ms 33612 KB Output is correct
28 Correct 36 ms 33532 KB Output is correct
29 Correct 34 ms 33524 KB Output is correct
30 Correct 39 ms 33396 KB Output is correct
31 Correct 37 ms 33500 KB Output is correct
32 Correct 40 ms 33640 KB Output is correct
33 Correct 35 ms 33484 KB Output is correct
34 Correct 37 ms 33264 KB Output is correct
35 Correct 39 ms 33720 KB Output is correct
36 Correct 45 ms 33740 KB Output is correct
37 Correct 42 ms 33740 KB Output is correct
38 Correct 41 ms 33652 KB Output is correct
39 Correct 44 ms 33644 KB Output is correct
40 Correct 40 ms 33784 KB Output is correct
41 Correct 40 ms 33784 KB Output is correct
42 Correct 43 ms 33772 KB Output is correct
43 Correct 39 ms 33756 KB Output is correct
44 Correct 41 ms 33772 KB Output is correct
45 Correct 43 ms 33820 KB Output is correct
46 Correct 41 ms 33860 KB Output is correct
47 Correct 40 ms 33768 KB Output is correct
48 Correct 306 ms 54576 KB Output is correct
49 Correct 328 ms 54536 KB Output is correct
50 Correct 309 ms 54512 KB Output is correct
51 Correct 285 ms 54252 KB Output is correct
52 Correct 273 ms 52852 KB Output is correct
53 Correct 308 ms 53752 KB Output is correct
54 Correct 315 ms 52836 KB Output is correct
55 Correct 305 ms 52488 KB Output is correct
56 Correct 297 ms 52256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 16 ms 31572 KB Output is correct
3 Correct 16 ms 31572 KB Output is correct
4 Correct 17 ms 31612 KB Output is correct
5 Correct 17 ms 31608 KB Output is correct
6 Correct 18 ms 31572 KB Output is correct
7 Correct 17 ms 31568 KB Output is correct
8 Correct 15 ms 31560 KB Output is correct
9 Correct 16 ms 31584 KB Output is correct
10 Correct 17 ms 31740 KB Output is correct
11 Correct 16 ms 31572 KB Output is correct
12 Correct 16 ms 31572 KB Output is correct
13 Correct 17 ms 31560 KB Output is correct
14 Correct 16 ms 31572 KB Output is correct
15 Correct 19 ms 31600 KB Output is correct
16 Correct 17 ms 31572 KB Output is correct
17 Correct 16 ms 31608 KB Output is correct
18 Correct 16 ms 31572 KB Output is correct
19 Correct 17 ms 31572 KB Output is correct
20 Correct 16 ms 31620 KB Output is correct
21 Correct 15 ms 31612 KB Output is correct
22 Correct 16 ms 31616 KB Output is correct
23 Correct 16 ms 31572 KB Output is correct
24 Correct 16 ms 31616 KB Output is correct
25 Correct 16 ms 31564 KB Output is correct
26 Correct 15 ms 31616 KB Output is correct
27 Correct 36 ms 33612 KB Output is correct
28 Correct 36 ms 33532 KB Output is correct
29 Correct 34 ms 33524 KB Output is correct
30 Correct 39 ms 33396 KB Output is correct
31 Correct 37 ms 33500 KB Output is correct
32 Correct 40 ms 33640 KB Output is correct
33 Correct 35 ms 33484 KB Output is correct
34 Correct 37 ms 33264 KB Output is correct
35 Correct 39 ms 33720 KB Output is correct
36 Correct 45 ms 33740 KB Output is correct
37 Correct 42 ms 33740 KB Output is correct
38 Correct 41 ms 33652 KB Output is correct
39 Correct 44 ms 33644 KB Output is correct
40 Correct 40 ms 33784 KB Output is correct
41 Correct 40 ms 33784 KB Output is correct
42 Correct 43 ms 33772 KB Output is correct
43 Correct 39 ms 33756 KB Output is correct
44 Correct 41 ms 33772 KB Output is correct
45 Correct 43 ms 33820 KB Output is correct
46 Correct 41 ms 33860 KB Output is correct
47 Correct 40 ms 33768 KB Output is correct
48 Correct 306 ms 54576 KB Output is correct
49 Correct 328 ms 54536 KB Output is correct
50 Correct 309 ms 54512 KB Output is correct
51 Correct 285 ms 54252 KB Output is correct
52 Correct 273 ms 52852 KB Output is correct
53 Correct 308 ms 53752 KB Output is correct
54 Correct 315 ms 52836 KB Output is correct
55 Correct 305 ms 52488 KB Output is correct
56 Correct 297 ms 52256 KB Output is correct
57 Correct 1300 ms 92324 KB Output is correct
58 Correct 999 ms 75344 KB Output is correct
59 Correct 967 ms 75260 KB Output is correct
60 Correct 1064 ms 79608 KB Output is correct
61 Correct 1026 ms 79580 KB Output is correct
62 Correct 576 ms 65536 KB Output is correct
63 Correct 753 ms 70248 KB Output is correct
64 Correct 648 ms 67296 KB Output is correct
65 Correct 429 ms 59740 KB Output is correct