Submission #949579

# Submission time Handle Problem Language Result Execution time Memory
949579 2024-03-19T11:23:11 Z ace5 Rope (JOI17_rope) C++17
80 / 100
2500 ms 120844 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1000001;

vector<int> cc[maxn];
set<pair<int,int>> ck;
int a[maxn];
int u[maxn];
int ans[maxn];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for(int j = 0;j < n;++j)
    {
        cin >> a[j];
        a[j]--;
        cc[a[j]].push_back(j);
    }
    for(int j = 0;j < m;++j)
    {
        u[j] = cc[j].size();
        ck.insert({-u[j],j});
        ans[j] = n+1;
    }
    if(m == 1)
    {
        cout << 0;
        return 0;
    }
    for(int j = 0;j < m;++j)
    {
        int op = u[j];
        int xx = 0;
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 1)
                break;
            int pr = a[cc[j][k]^1];
            ck.erase({-u[pr],pr});
        }
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 1)
                break;
            int pr = a[cc[j][k]^1];
            u[pr]--;
        }
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 1)
                break;
            int pr = a[cc[j][k]^1];
            if(pr != j)
                xx = max(xx,u[pr]);
        }
        auto it = ck.begin();
        if(ck.size() && (*it).second == j)
        {
            it++;
        }
        int tk = min(-xx,(it == ck.end() ? 0 : (*it).first));
        ans[j] = min(ans[j],n-op+tk);
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 1)
                break;
            int pr = a[cc[j][k]^1];
            u[pr]++;
        }
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 1)
                break;
            int pr = a[cc[j][k]^1];
            ck.insert({-u[pr],pr});
        }
    }
    for(int j = 0;j < m;++j)
    {
        int op = u[j];
        int xx = 0;
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 0)
                break;
            if(cc[j][k] == 0)
                continue;
            int pr = a[((cc[j][k]-1)^1)+1];
            ck.erase({-u[pr],pr});
        }
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 0)
                break;
            if(cc[j][k] == 0)
                continue;
            int pr = a[((cc[j][k]-1)^1)+1];
            u[pr]--;
        }
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 0)
                break;
            if(cc[j][k] == 0)
                continue;
            int pr = a[((cc[j][k]-1)^1)+1];
            if(pr != j)
                xx = max(xx,u[pr]);
        }
        auto it = ck.begin();
        if(ck.size() && (*it).second == j)
        {
            it++;
        }
        int tk = min(-xx,(it == ck.end() ? 0 : (*it).first));
        ans[j] = min(ans[j],n-op+tk);
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 0)
                break;
            if(cc[j][k] == 0)
                continue;
            int pr = a[((cc[j][k]-1)^1)+1];
            u[pr]++;
        }
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 0)
                break;
            if(cc[j][k] == 0)
                continue;
            int pr = a[((cc[j][k]-1)^1)+1];
            ck.insert({-u[pr],pr});
        }
        cout << ans[j] << "\n";
    }
}

Compilation message

rope.cpp: In function 'int main()':
rope.cpp:40:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:47:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:54:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:69:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:76:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:88:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:97:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:106:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:123:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:132:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  132 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 7 ms 29276 KB Output is correct
3 Correct 7 ms 29276 KB Output is correct
4 Correct 6 ms 29348 KB Output is correct
5 Correct 7 ms 29152 KB Output is correct
6 Correct 6 ms 29276 KB Output is correct
7 Correct 7 ms 29116 KB Output is correct
8 Correct 6 ms 29108 KB Output is correct
9 Correct 6 ms 29276 KB Output is correct
10 Correct 7 ms 29276 KB Output is correct
11 Correct 6 ms 29356 KB Output is correct
12 Correct 6 ms 29276 KB Output is correct
13 Correct 6 ms 29276 KB Output is correct
14 Correct 7 ms 29348 KB Output is correct
15 Correct 6 ms 29348 KB Output is correct
16 Correct 6 ms 29276 KB Output is correct
17 Correct 6 ms 29276 KB Output is correct
18 Correct 6 ms 29352 KB Output is correct
19 Correct 6 ms 29276 KB Output is correct
20 Correct 6 ms 29344 KB Output is correct
21 Correct 7 ms 29276 KB Output is correct
22 Correct 6 ms 29276 KB Output is correct
23 Correct 6 ms 29348 KB Output is correct
24 Correct 6 ms 29276 KB Output is correct
25 Correct 6 ms 29272 KB Output is correct
26 Correct 6 ms 29276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 7 ms 29276 KB Output is correct
3 Correct 7 ms 29276 KB Output is correct
4 Correct 6 ms 29348 KB Output is correct
5 Correct 7 ms 29152 KB Output is correct
6 Correct 6 ms 29276 KB Output is correct
7 Correct 7 ms 29116 KB Output is correct
8 Correct 6 ms 29108 KB Output is correct
9 Correct 6 ms 29276 KB Output is correct
10 Correct 7 ms 29276 KB Output is correct
11 Correct 6 ms 29356 KB Output is correct
12 Correct 6 ms 29276 KB Output is correct
13 Correct 6 ms 29276 KB Output is correct
14 Correct 7 ms 29348 KB Output is correct
15 Correct 6 ms 29348 KB Output is correct
16 Correct 6 ms 29276 KB Output is correct
17 Correct 6 ms 29276 KB Output is correct
18 Correct 6 ms 29352 KB Output is correct
19 Correct 6 ms 29276 KB Output is correct
20 Correct 6 ms 29344 KB Output is correct
21 Correct 7 ms 29276 KB Output is correct
22 Correct 6 ms 29276 KB Output is correct
23 Correct 6 ms 29348 KB Output is correct
24 Correct 6 ms 29276 KB Output is correct
25 Correct 6 ms 29272 KB Output is correct
26 Correct 6 ms 29276 KB Output is correct
27 Correct 18 ms 30144 KB Output is correct
28 Correct 17 ms 30040 KB Output is correct
29 Correct 18 ms 30044 KB Output is correct
30 Correct 18 ms 30044 KB Output is correct
31 Correct 19 ms 30044 KB Output is correct
32 Correct 16 ms 30192 KB Output is correct
33 Correct 18 ms 30044 KB Output is correct
34 Correct 17 ms 30044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 7 ms 29276 KB Output is correct
3 Correct 7 ms 29276 KB Output is correct
4 Correct 6 ms 29348 KB Output is correct
5 Correct 7 ms 29152 KB Output is correct
6 Correct 6 ms 29276 KB Output is correct
7 Correct 7 ms 29116 KB Output is correct
8 Correct 6 ms 29108 KB Output is correct
9 Correct 6 ms 29276 KB Output is correct
10 Correct 7 ms 29276 KB Output is correct
11 Correct 6 ms 29356 KB Output is correct
12 Correct 6 ms 29276 KB Output is correct
13 Correct 6 ms 29276 KB Output is correct
14 Correct 7 ms 29348 KB Output is correct
15 Correct 6 ms 29348 KB Output is correct
16 Correct 6 ms 29276 KB Output is correct
17 Correct 6 ms 29276 KB Output is correct
18 Correct 6 ms 29352 KB Output is correct
19 Correct 6 ms 29276 KB Output is correct
20 Correct 6 ms 29344 KB Output is correct
21 Correct 7 ms 29276 KB Output is correct
22 Correct 6 ms 29276 KB Output is correct
23 Correct 6 ms 29348 KB Output is correct
24 Correct 6 ms 29276 KB Output is correct
25 Correct 6 ms 29272 KB Output is correct
26 Correct 6 ms 29276 KB Output is correct
27 Correct 18 ms 30144 KB Output is correct
28 Correct 17 ms 30040 KB Output is correct
29 Correct 18 ms 30044 KB Output is correct
30 Correct 18 ms 30044 KB Output is correct
31 Correct 19 ms 30044 KB Output is correct
32 Correct 16 ms 30192 KB Output is correct
33 Correct 18 ms 30044 KB Output is correct
34 Correct 17 ms 30044 KB Output is correct
35 Correct 57 ms 30300 KB Output is correct
36 Correct 61 ms 30300 KB Output is correct
37 Correct 55 ms 30044 KB Output is correct
38 Correct 53 ms 30272 KB Output is correct
39 Correct 57 ms 30296 KB Output is correct
40 Correct 37 ms 30300 KB Output is correct
41 Correct 38 ms 30300 KB Output is correct
42 Correct 37 ms 30552 KB Output is correct
43 Correct 34 ms 30372 KB Output is correct
44 Correct 39 ms 30520 KB Output is correct
45 Correct 38 ms 30300 KB Output is correct
46 Correct 33 ms 30300 KB Output is correct
47 Correct 34 ms 30300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 7 ms 29276 KB Output is correct
3 Correct 7 ms 29276 KB Output is correct
4 Correct 6 ms 29348 KB Output is correct
5 Correct 7 ms 29152 KB Output is correct
6 Correct 6 ms 29276 KB Output is correct
7 Correct 7 ms 29116 KB Output is correct
8 Correct 6 ms 29108 KB Output is correct
9 Correct 6 ms 29276 KB Output is correct
10 Correct 7 ms 29276 KB Output is correct
11 Correct 6 ms 29356 KB Output is correct
12 Correct 6 ms 29276 KB Output is correct
13 Correct 6 ms 29276 KB Output is correct
14 Correct 7 ms 29348 KB Output is correct
15 Correct 6 ms 29348 KB Output is correct
16 Correct 6 ms 29276 KB Output is correct
17 Correct 6 ms 29276 KB Output is correct
18 Correct 6 ms 29352 KB Output is correct
19 Correct 6 ms 29276 KB Output is correct
20 Correct 6 ms 29344 KB Output is correct
21 Correct 7 ms 29276 KB Output is correct
22 Correct 6 ms 29276 KB Output is correct
23 Correct 6 ms 29348 KB Output is correct
24 Correct 6 ms 29276 KB Output is correct
25 Correct 6 ms 29272 KB Output is correct
26 Correct 6 ms 29276 KB Output is correct
27 Correct 18 ms 30144 KB Output is correct
28 Correct 17 ms 30040 KB Output is correct
29 Correct 18 ms 30044 KB Output is correct
30 Correct 18 ms 30044 KB Output is correct
31 Correct 19 ms 30044 KB Output is correct
32 Correct 16 ms 30192 KB Output is correct
33 Correct 18 ms 30044 KB Output is correct
34 Correct 17 ms 30044 KB Output is correct
35 Correct 57 ms 30300 KB Output is correct
36 Correct 61 ms 30300 KB Output is correct
37 Correct 55 ms 30044 KB Output is correct
38 Correct 53 ms 30272 KB Output is correct
39 Correct 57 ms 30296 KB Output is correct
40 Correct 37 ms 30300 KB Output is correct
41 Correct 38 ms 30300 KB Output is correct
42 Correct 37 ms 30552 KB Output is correct
43 Correct 34 ms 30372 KB Output is correct
44 Correct 39 ms 30520 KB Output is correct
45 Correct 38 ms 30300 KB Output is correct
46 Correct 33 ms 30300 KB Output is correct
47 Correct 34 ms 30300 KB Output is correct
48 Correct 665 ms 40792 KB Output is correct
49 Correct 666 ms 40532 KB Output is correct
50 Correct 674 ms 40808 KB Output is correct
51 Correct 601 ms 40384 KB Output is correct
52 Correct 536 ms 39176 KB Output is correct
53 Correct 445 ms 39804 KB Output is correct
54 Correct 437 ms 38820 KB Output is correct
55 Correct 371 ms 38652 KB Output is correct
56 Correct 360 ms 38436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 7 ms 29276 KB Output is correct
3 Correct 7 ms 29276 KB Output is correct
4 Correct 6 ms 29348 KB Output is correct
5 Correct 7 ms 29152 KB Output is correct
6 Correct 6 ms 29276 KB Output is correct
7 Correct 7 ms 29116 KB Output is correct
8 Correct 6 ms 29108 KB Output is correct
9 Correct 6 ms 29276 KB Output is correct
10 Correct 7 ms 29276 KB Output is correct
11 Correct 6 ms 29356 KB Output is correct
12 Correct 6 ms 29276 KB Output is correct
13 Correct 6 ms 29276 KB Output is correct
14 Correct 7 ms 29348 KB Output is correct
15 Correct 6 ms 29348 KB Output is correct
16 Correct 6 ms 29276 KB Output is correct
17 Correct 6 ms 29276 KB Output is correct
18 Correct 6 ms 29352 KB Output is correct
19 Correct 6 ms 29276 KB Output is correct
20 Correct 6 ms 29344 KB Output is correct
21 Correct 7 ms 29276 KB Output is correct
22 Correct 6 ms 29276 KB Output is correct
23 Correct 6 ms 29348 KB Output is correct
24 Correct 6 ms 29276 KB Output is correct
25 Correct 6 ms 29272 KB Output is correct
26 Correct 6 ms 29276 KB Output is correct
27 Correct 18 ms 30144 KB Output is correct
28 Correct 17 ms 30040 KB Output is correct
29 Correct 18 ms 30044 KB Output is correct
30 Correct 18 ms 30044 KB Output is correct
31 Correct 19 ms 30044 KB Output is correct
32 Correct 16 ms 30192 KB Output is correct
33 Correct 18 ms 30044 KB Output is correct
34 Correct 17 ms 30044 KB Output is correct
35 Correct 57 ms 30300 KB Output is correct
36 Correct 61 ms 30300 KB Output is correct
37 Correct 55 ms 30044 KB Output is correct
38 Correct 53 ms 30272 KB Output is correct
39 Correct 57 ms 30296 KB Output is correct
40 Correct 37 ms 30300 KB Output is correct
41 Correct 38 ms 30300 KB Output is correct
42 Correct 37 ms 30552 KB Output is correct
43 Correct 34 ms 30372 KB Output is correct
44 Correct 39 ms 30520 KB Output is correct
45 Correct 38 ms 30300 KB Output is correct
46 Correct 33 ms 30300 KB Output is correct
47 Correct 34 ms 30300 KB Output is correct
48 Correct 665 ms 40792 KB Output is correct
49 Correct 666 ms 40532 KB Output is correct
50 Correct 674 ms 40808 KB Output is correct
51 Correct 601 ms 40384 KB Output is correct
52 Correct 536 ms 39176 KB Output is correct
53 Correct 445 ms 39804 KB Output is correct
54 Correct 437 ms 38820 KB Output is correct
55 Correct 371 ms 38652 KB Output is correct
56 Correct 360 ms 38436 KB Output is correct
57 Execution timed out 2525 ms 120844 KB Time limit exceeded
58 Halted 0 ms 0 KB -