답안 #949558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949558 2024-03-19T10:49:21 Z ace5 Rope (JOI17_rope) C++17
80 / 100
2500 ms 121716 KB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    int a[n];
    vector<vector<int>> cc(m);
    set<pair<int,int>> ck;
    for(int j = 0;j < n;++j)
    {
        cin >> a[j];
        a[j]--;
        cc[a[j]].push_back(j);
    }
    vector<int> u(m);
    for(int j = 0;j < m;++j)
    {
        u[j] = cc[j].size();
    }
    for(int j = 0;j < m;++j)
        ck.insert({-u[j],j});
    vector<int> ans(m,n+1);
    for(int j = 0;j < m;++j)
    {
        int op = u[j];
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 1)
                break;
            int pr = cc[j][k]^1;
            ck.erase({-u[a[pr]],a[pr]});
            u[a[pr]]--;
            ck.insert({-u[a[pr]],a[pr]});
        }
        ck.erase({-u[j],j});
        ans[j] = min(ans[j],(ck.size() == 0 ? 0 : n-op+(*ck.begin()).first));
        ck.insert({-u[j],j});
        for(int k = 0;k < cc[j].size();++k)
        {
            if(cc[j][k] == n-1 && n%2 == 1)
                break;
            int pr = cc[j][k]^1;
            ck.erase({-u[a[pr]],a[pr]});
            u[a[pr]]++;
            ck.insert({-u[a[pr]],a[pr]});
        }
    }
    for(int j = 0;j < m;++j)
    {
        int op = u[j];
        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 = ((cc[j][k]-1)^1)+1;
            ck.erase({-u[a[pr]],a[pr]});
            u[a[pr]]--;
            ck.insert({-u[a[pr]],a[pr]});
        }
        ck.erase({-u[j],j});
        ans[j] = min(ans[j],(ck.size() == 0 ? 0 : n-op+(*ck.begin()).first));
        ck.insert({-u[j],j});
        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 = ((cc[j][k]-1)^1)+1;
            ck.erase({-u[a[pr]],a[pr]});
            u[a[pr]]++;
            ck.insert({-u[a[pr]],a[pr]});
        }
        cout << ans[j] << "\n";
    }
}

Compilation message

rope.cpp: In function 'int main()':
rope.cpp:31:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:43:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:56:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
rope.cpp:70:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         for(int k = 0;k < cc[j].size();++k)
      |                       ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 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 0 ms 460 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 344 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 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 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 0 ms 460 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 344 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 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 38 ms 1628 KB Output is correct
28 Correct 29 ms 1372 KB Output is correct
29 Correct 36 ms 1680 KB Output is correct
30 Correct 30 ms 1628 KB Output is correct
31 Correct 37 ms 1628 KB Output is correct
32 Correct 28 ms 1624 KB Output is correct
33 Correct 37 ms 1628 KB Output is correct
34 Correct 30 ms 1624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 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 0 ms 460 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 344 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 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 38 ms 1628 KB Output is correct
28 Correct 29 ms 1372 KB Output is correct
29 Correct 36 ms 1680 KB Output is correct
30 Correct 30 ms 1628 KB Output is correct
31 Correct 37 ms 1628 KB Output is correct
32 Correct 28 ms 1624 KB Output is correct
33 Correct 37 ms 1628 KB Output is correct
34 Correct 30 ms 1624 KB Output is correct
35 Correct 91 ms 1628 KB Output is correct
36 Correct 90 ms 1796 KB Output is correct
37 Correct 104 ms 1780 KB Output is correct
38 Correct 91 ms 1628 KB Output is correct
39 Correct 90 ms 1792 KB Output is correct
40 Correct 78 ms 1880 KB Output is correct
41 Correct 87 ms 2136 KB Output is correct
42 Correct 82 ms 1836 KB Output is correct
43 Correct 74 ms 1868 KB Output is correct
44 Correct 81 ms 1880 KB Output is correct
45 Correct 82 ms 1880 KB Output is correct
46 Correct 79 ms 1628 KB Output is correct
47 Correct 80 ms 1880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 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 0 ms 460 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 344 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 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 38 ms 1628 KB Output is correct
28 Correct 29 ms 1372 KB Output is correct
29 Correct 36 ms 1680 KB Output is correct
30 Correct 30 ms 1628 KB Output is correct
31 Correct 37 ms 1628 KB Output is correct
32 Correct 28 ms 1624 KB Output is correct
33 Correct 37 ms 1628 KB Output is correct
34 Correct 30 ms 1624 KB Output is correct
35 Correct 91 ms 1628 KB Output is correct
36 Correct 90 ms 1796 KB Output is correct
37 Correct 104 ms 1780 KB Output is correct
38 Correct 91 ms 1628 KB Output is correct
39 Correct 90 ms 1792 KB Output is correct
40 Correct 78 ms 1880 KB Output is correct
41 Correct 87 ms 2136 KB Output is correct
42 Correct 82 ms 1836 KB Output is correct
43 Correct 74 ms 1868 KB Output is correct
44 Correct 81 ms 1880 KB Output is correct
45 Correct 82 ms 1880 KB Output is correct
46 Correct 79 ms 1628 KB Output is correct
47 Correct 80 ms 1880 KB Output is correct
48 Correct 1186 ms 15924 KB Output is correct
49 Correct 1163 ms 15956 KB Output is correct
50 Correct 1155 ms 16108 KB Output is correct
51 Correct 1072 ms 15480 KB Output is correct
52 Correct 994 ms 13912 KB Output is correct
53 Correct 992 ms 14932 KB Output is correct
54 Correct 994 ms 13908 KB Output is correct
55 Correct 970 ms 13660 KB Output is correct
56 Correct 950 ms 13444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 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 0 ms 460 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 344 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 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 38 ms 1628 KB Output is correct
28 Correct 29 ms 1372 KB Output is correct
29 Correct 36 ms 1680 KB Output is correct
30 Correct 30 ms 1628 KB Output is correct
31 Correct 37 ms 1628 KB Output is correct
32 Correct 28 ms 1624 KB Output is correct
33 Correct 37 ms 1628 KB Output is correct
34 Correct 30 ms 1624 KB Output is correct
35 Correct 91 ms 1628 KB Output is correct
36 Correct 90 ms 1796 KB Output is correct
37 Correct 104 ms 1780 KB Output is correct
38 Correct 91 ms 1628 KB Output is correct
39 Correct 90 ms 1792 KB Output is correct
40 Correct 78 ms 1880 KB Output is correct
41 Correct 87 ms 2136 KB Output is correct
42 Correct 82 ms 1836 KB Output is correct
43 Correct 74 ms 1868 KB Output is correct
44 Correct 81 ms 1880 KB Output is correct
45 Correct 82 ms 1880 KB Output is correct
46 Correct 79 ms 1628 KB Output is correct
47 Correct 80 ms 1880 KB Output is correct
48 Correct 1186 ms 15924 KB Output is correct
49 Correct 1163 ms 15956 KB Output is correct
50 Correct 1155 ms 16108 KB Output is correct
51 Correct 1072 ms 15480 KB Output is correct
52 Correct 994 ms 13912 KB Output is correct
53 Correct 992 ms 14932 KB Output is correct
54 Correct 994 ms 13908 KB Output is correct
55 Correct 970 ms 13660 KB Output is correct
56 Correct 950 ms 13444 KB Output is correct
57 Execution timed out 2571 ms 121716 KB Time limit exceeded
58 Halted 0 ms 0 KB -