답안 #1051821

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1051821 2024-08-10T10:00:07 Z SamAnd Rope (JOI17_rope) C++17
80 / 100
2500 ms 128284 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006;

int nn;
int n, m;
int c[N];

pair<int, int> a[N];
int ans[N];
vector<int> v[N];

int q[N];
set<pair<int, int> > s;
void ubd(int i, int y)
{
    if (s.find(m_p(q[a[i].fi], a[i].fi)) != s.end())
        s.erase(m_p(q[a[i].fi], a[i].fi));
    if (s.find(m_p(q[a[i].se], a[i].se)) != s.end())
        s.erase(m_p(q[a[i].se], a[i].se));
    q[a[i].fi] += y;
    q[a[i].se] += y;
    if (a[i].fi)
        s.insert(m_p(q[a[i].fi], a[i].fi));
    if (a[i].se)
        s.insert(m_p(q[a[i].se], a[i].se));
}

void solvv()
{
    for (int x = 1; x <= m; ++x)
    {
        v[x].clear();
        q[x] = 0;
    }
    s.clear();

    for (int i = 1; i <= n; ++i)
    {
        v[a[i].fi].push_back(i);
        if (a[i].se != a[i].fi)
            v[a[i].se].push_back(i);
    }

    for (int x = 1; x <= m; ++x)
        s.insert(m_p(q[x], x));
    for (int i = 1; i <= n; ++i)
        ubd(i, 1);

    for (int x = 1; x <= m; ++x)
    {
        int yans = 0;
        for (int i = 0; i < sz(v[x]); ++i)
        {
            int j = v[x][i];
            ubd(j, -1);
            if (a[j].fi == x)
                ++yans;
            if (a[j].se == x)
                ++yans;
        }
        yans += (--s.end())->fi;
        ans[x] = min(ans[x], nn - yans);
        for (int i = 0; i < sz(v[x]); ++i)
        {
            int j = v[x][i];
            ubd(j, 1);
        }
    }
}

void solv()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> c[i];
    nn = n;

    for (int i = 1; i <= m; ++i)
        ans[i] = n;

    if (n % 2 == 0)
    {
        for (int i = 1; i <= n; i += 2)
        {
            a[i / 2 + 1] = m_p(c[i], c[i + 1]);
        }
        n = n / 2;
        solvv();
        n *= 2;
        a[1] = m_p(c[1], 0);
        a[2] = m_p(c[n], 0);
        for (int i = 2; i < n; i += 2)
        {
            a[i / 2 + 2] = m_p(c[i], c[i + 1]);
        }
        n = 2 + (n - 2) / 2;
        solvv();
    }
    else
    {
        a[1] = m_p(c[1], 0);
        for (int i = 2; i <= n; i += 2)
            a[i / 2 + 1] = m_p(c[i], c[i + 1]);
        n = (n - 1) / 2 + 1;
        solvv();
        n = (n - 1) * 2 + 1;
        a[1] = m_p(c[n], 0);
        for (int i = 1; i < n; i += 2)
            a[i / 2 + 2] = m_p(c[i], c[i + 1]);
        solvv();
    }

    for (int i = 1; i <= m; ++i)
        cout << ans[i] << "\n";
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    ios_base::sync_with_stdio(false), cin.tie(0);

    int tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solv();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 31064 KB Output is correct
2 Correct 4 ms 31068 KB Output is correct
3 Correct 4 ms 31256 KB Output is correct
4 Correct 3 ms 31068 KB Output is correct
5 Correct 3 ms 31068 KB Output is correct
6 Correct 3 ms 31068 KB Output is correct
7 Correct 4 ms 31164 KB Output is correct
8 Correct 3 ms 31068 KB Output is correct
9 Correct 5 ms 31068 KB Output is correct
10 Correct 4 ms 31248 KB Output is correct
11 Correct 3 ms 31068 KB Output is correct
12 Correct 3 ms 31068 KB Output is correct
13 Correct 5 ms 31068 KB Output is correct
14 Correct 4 ms 31068 KB Output is correct
15 Correct 4 ms 31068 KB Output is correct
16 Correct 5 ms 31068 KB Output is correct
17 Correct 4 ms 31068 KB Output is correct
18 Correct 4 ms 31068 KB Output is correct
19 Correct 4 ms 31068 KB Output is correct
20 Correct 3 ms 31068 KB Output is correct
21 Correct 4 ms 31068 KB Output is correct
22 Correct 4 ms 31068 KB Output is correct
23 Correct 4 ms 31068 KB Output is correct
24 Correct 4 ms 31068 KB Output is correct
25 Correct 3 ms 31068 KB Output is correct
26 Correct 4 ms 31068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 31064 KB Output is correct
2 Correct 4 ms 31068 KB Output is correct
3 Correct 4 ms 31256 KB Output is correct
4 Correct 3 ms 31068 KB Output is correct
5 Correct 3 ms 31068 KB Output is correct
6 Correct 3 ms 31068 KB Output is correct
7 Correct 4 ms 31164 KB Output is correct
8 Correct 3 ms 31068 KB Output is correct
9 Correct 5 ms 31068 KB Output is correct
10 Correct 4 ms 31248 KB Output is correct
11 Correct 3 ms 31068 KB Output is correct
12 Correct 3 ms 31068 KB Output is correct
13 Correct 5 ms 31068 KB Output is correct
14 Correct 4 ms 31068 KB Output is correct
15 Correct 4 ms 31068 KB Output is correct
16 Correct 5 ms 31068 KB Output is correct
17 Correct 4 ms 31068 KB Output is correct
18 Correct 4 ms 31068 KB Output is correct
19 Correct 4 ms 31068 KB Output is correct
20 Correct 3 ms 31068 KB Output is correct
21 Correct 4 ms 31068 KB Output is correct
22 Correct 4 ms 31068 KB Output is correct
23 Correct 4 ms 31068 KB Output is correct
24 Correct 4 ms 31068 KB Output is correct
25 Correct 3 ms 31068 KB Output is correct
26 Correct 4 ms 31068 KB Output is correct
27 Correct 65 ms 33928 KB Output is correct
28 Correct 47 ms 33824 KB Output is correct
29 Correct 73 ms 34128 KB Output is correct
30 Correct 49 ms 34088 KB Output is correct
31 Correct 64 ms 33920 KB Output is correct
32 Correct 48 ms 33884 KB Output is correct
33 Correct 62 ms 34252 KB Output is correct
34 Correct 48 ms 34040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 31064 KB Output is correct
2 Correct 4 ms 31068 KB Output is correct
3 Correct 4 ms 31256 KB Output is correct
4 Correct 3 ms 31068 KB Output is correct
5 Correct 3 ms 31068 KB Output is correct
6 Correct 3 ms 31068 KB Output is correct
7 Correct 4 ms 31164 KB Output is correct
8 Correct 3 ms 31068 KB Output is correct
9 Correct 5 ms 31068 KB Output is correct
10 Correct 4 ms 31248 KB Output is correct
11 Correct 3 ms 31068 KB Output is correct
12 Correct 3 ms 31068 KB Output is correct
13 Correct 5 ms 31068 KB Output is correct
14 Correct 4 ms 31068 KB Output is correct
15 Correct 4 ms 31068 KB Output is correct
16 Correct 5 ms 31068 KB Output is correct
17 Correct 4 ms 31068 KB Output is correct
18 Correct 4 ms 31068 KB Output is correct
19 Correct 4 ms 31068 KB Output is correct
20 Correct 3 ms 31068 KB Output is correct
21 Correct 4 ms 31068 KB Output is correct
22 Correct 4 ms 31068 KB Output is correct
23 Correct 4 ms 31068 KB Output is correct
24 Correct 4 ms 31068 KB Output is correct
25 Correct 3 ms 31068 KB Output is correct
26 Correct 4 ms 31068 KB Output is correct
27 Correct 65 ms 33928 KB Output is correct
28 Correct 47 ms 33824 KB Output is correct
29 Correct 73 ms 34128 KB Output is correct
30 Correct 49 ms 34088 KB Output is correct
31 Correct 64 ms 33920 KB Output is correct
32 Correct 48 ms 33884 KB Output is correct
33 Correct 62 ms 34252 KB Output is correct
34 Correct 48 ms 34040 KB Output is correct
35 Correct 164 ms 33884 KB Output is correct
36 Correct 164 ms 34232 KB Output is correct
37 Correct 163 ms 34512 KB Output is correct
38 Correct 166 ms 34492 KB Output is correct
39 Correct 170 ms 34648 KB Output is correct
40 Correct 141 ms 34140 KB Output is correct
41 Correct 143 ms 34140 KB Output is correct
42 Correct 136 ms 34136 KB Output is correct
43 Correct 136 ms 34136 KB Output is correct
44 Correct 145 ms 34308 KB Output is correct
45 Correct 147 ms 34316 KB Output is correct
46 Correct 139 ms 34136 KB Output is correct
47 Correct 141 ms 34140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 31064 KB Output is correct
2 Correct 4 ms 31068 KB Output is correct
3 Correct 4 ms 31256 KB Output is correct
4 Correct 3 ms 31068 KB Output is correct
5 Correct 3 ms 31068 KB Output is correct
6 Correct 3 ms 31068 KB Output is correct
7 Correct 4 ms 31164 KB Output is correct
8 Correct 3 ms 31068 KB Output is correct
9 Correct 5 ms 31068 KB Output is correct
10 Correct 4 ms 31248 KB Output is correct
11 Correct 3 ms 31068 KB Output is correct
12 Correct 3 ms 31068 KB Output is correct
13 Correct 5 ms 31068 KB Output is correct
14 Correct 4 ms 31068 KB Output is correct
15 Correct 4 ms 31068 KB Output is correct
16 Correct 5 ms 31068 KB Output is correct
17 Correct 4 ms 31068 KB Output is correct
18 Correct 4 ms 31068 KB Output is correct
19 Correct 4 ms 31068 KB Output is correct
20 Correct 3 ms 31068 KB Output is correct
21 Correct 4 ms 31068 KB Output is correct
22 Correct 4 ms 31068 KB Output is correct
23 Correct 4 ms 31068 KB Output is correct
24 Correct 4 ms 31068 KB Output is correct
25 Correct 3 ms 31068 KB Output is correct
26 Correct 4 ms 31068 KB Output is correct
27 Correct 65 ms 33928 KB Output is correct
28 Correct 47 ms 33824 KB Output is correct
29 Correct 73 ms 34128 KB Output is correct
30 Correct 49 ms 34088 KB Output is correct
31 Correct 64 ms 33920 KB Output is correct
32 Correct 48 ms 33884 KB Output is correct
33 Correct 62 ms 34252 KB Output is correct
34 Correct 48 ms 34040 KB Output is correct
35 Correct 164 ms 33884 KB Output is correct
36 Correct 164 ms 34232 KB Output is correct
37 Correct 163 ms 34512 KB Output is correct
38 Correct 166 ms 34492 KB Output is correct
39 Correct 170 ms 34648 KB Output is correct
40 Correct 141 ms 34140 KB Output is correct
41 Correct 143 ms 34140 KB Output is correct
42 Correct 136 ms 34136 KB Output is correct
43 Correct 136 ms 34136 KB Output is correct
44 Correct 145 ms 34308 KB Output is correct
45 Correct 147 ms 34316 KB Output is correct
46 Correct 139 ms 34136 KB Output is correct
47 Correct 141 ms 34140 KB Output is correct
48 Correct 2056 ms 50452 KB Output is correct
49 Correct 2066 ms 50260 KB Output is correct
50 Correct 2115 ms 52592 KB Output is correct
51 Correct 1852 ms 52424 KB Output is correct
52 Correct 1738 ms 50916 KB Output is correct
53 Correct 1714 ms 49620 KB Output is correct
54 Correct 1552 ms 48596 KB Output is correct
55 Correct 1599 ms 48436 KB Output is correct
56 Correct 1495 ms 48212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 31064 KB Output is correct
2 Correct 4 ms 31068 KB Output is correct
3 Correct 4 ms 31256 KB Output is correct
4 Correct 3 ms 31068 KB Output is correct
5 Correct 3 ms 31068 KB Output is correct
6 Correct 3 ms 31068 KB Output is correct
7 Correct 4 ms 31164 KB Output is correct
8 Correct 3 ms 31068 KB Output is correct
9 Correct 5 ms 31068 KB Output is correct
10 Correct 4 ms 31248 KB Output is correct
11 Correct 3 ms 31068 KB Output is correct
12 Correct 3 ms 31068 KB Output is correct
13 Correct 5 ms 31068 KB Output is correct
14 Correct 4 ms 31068 KB Output is correct
15 Correct 4 ms 31068 KB Output is correct
16 Correct 5 ms 31068 KB Output is correct
17 Correct 4 ms 31068 KB Output is correct
18 Correct 4 ms 31068 KB Output is correct
19 Correct 4 ms 31068 KB Output is correct
20 Correct 3 ms 31068 KB Output is correct
21 Correct 4 ms 31068 KB Output is correct
22 Correct 4 ms 31068 KB Output is correct
23 Correct 4 ms 31068 KB Output is correct
24 Correct 4 ms 31068 KB Output is correct
25 Correct 3 ms 31068 KB Output is correct
26 Correct 4 ms 31068 KB Output is correct
27 Correct 65 ms 33928 KB Output is correct
28 Correct 47 ms 33824 KB Output is correct
29 Correct 73 ms 34128 KB Output is correct
30 Correct 49 ms 34088 KB Output is correct
31 Correct 64 ms 33920 KB Output is correct
32 Correct 48 ms 33884 KB Output is correct
33 Correct 62 ms 34252 KB Output is correct
34 Correct 48 ms 34040 KB Output is correct
35 Correct 164 ms 33884 KB Output is correct
36 Correct 164 ms 34232 KB Output is correct
37 Correct 163 ms 34512 KB Output is correct
38 Correct 166 ms 34492 KB Output is correct
39 Correct 170 ms 34648 KB Output is correct
40 Correct 141 ms 34140 KB Output is correct
41 Correct 143 ms 34140 KB Output is correct
42 Correct 136 ms 34136 KB Output is correct
43 Correct 136 ms 34136 KB Output is correct
44 Correct 145 ms 34308 KB Output is correct
45 Correct 147 ms 34316 KB Output is correct
46 Correct 139 ms 34136 KB Output is correct
47 Correct 141 ms 34140 KB Output is correct
48 Correct 2056 ms 50452 KB Output is correct
49 Correct 2066 ms 50260 KB Output is correct
50 Correct 2115 ms 52592 KB Output is correct
51 Correct 1852 ms 52424 KB Output is correct
52 Correct 1738 ms 50916 KB Output is correct
53 Correct 1714 ms 49620 KB Output is correct
54 Correct 1552 ms 48596 KB Output is correct
55 Correct 1599 ms 48436 KB Output is correct
56 Correct 1495 ms 48212 KB Output is correct
57 Execution timed out 2506 ms 128284 KB Time limit exceeded
58 Halted 0 ms 0 KB -