답안 #50115

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
50115 2018-06-07T15:15:48 Z SpaimaCarpatilor Rope (JOI17_rope) C++17
100 / 100
1884 ms 102840 KB
#include<bits/stdc++.h>

using namespace std;

int N, M, ans[1000009], g[1000009], h[1000009], a[1000009], id[1000009];
vector < int > v[1000009];

bool cmp (int i, int j)
{
    return (g[i] > g[j]);
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d %d", &N, &M);
for (int i=1; i<=M; i++)
    ans[i] = N, id[i] = i;
for (int i=1; i<=N; i++)
    scanf ("%d", &a[i]), ans[a[i]] --;
for (int shift = 0; shift < 2; shift ++)
{
    int all = 0;
    for (int i=shift + 1; i<N; i+=2)
    {
        all ++;
        g[a[i]] ++, g[a[i + 1]] ++;
        if (a[i] != a[i + 1])
            v[a[i]].push_back (a[i + 1]),
            v[a[i + 1]].push_back (a[i]);
    }
    sort (id + 1, id + M + 1, cmp);
    for (int i=1; i<=M; i++)
    {
        for (auto j : v[i])
            h[j] ++;
        int extra = 0;
        if (shift == 1 && a[1] != i) h[a[1]] --, extra ++;
        if (shift != N % 2 && a[N] != i) h[a[N]] --, extra ++;

        ///min 2 * all + extra - g[i] - (g[j] - h[j]) = 2 * all + extra - g[i] - max (g[j] - h[j])
        int p = 1, curr = ans[i];
        while (p <= M && (h[id[p]] != 0 || id[p] == i)) p ++;
        if (p <= M)
            curr = min (curr, 2 * all + extra - g[i] - g[id[p]]);
        vector < int > currJ = v[i];
        if (shift == 1 && a[1] != i) currJ.push_back (a[1]);
        if (shift != N % 2 && a[N] != i) currJ.push_back (a[N]);
        for (auto j : currJ)
            curr = min (curr, 2 * all + extra - g[i] - (g[j] - h[j]));
        if (curr < ans[i])
            ans[i] = curr;
        ///
        for (auto j : v[i])
            h[j] --;
        if (shift == 1 && a[1] != i) h[a[1]] ++;
        if (shift != N % 2 && a[N] != i) h[a[N]] ++;
        /*for (int j=i + 1; j<=M; j++)
        {
            int curr = 2 * all - g[i] - g[j] + h[i][j];
            if (shift == 1) curr += (a[1] != i && a[1] != j);
            if (N % 2 != shift) curr += (a[N] != i && a[N] != j);
            if (curr < ans[i]) ans[i] = curr;
            if (curr < ans[j]) ans[j] = curr;
        }*/
    }
    for (int i=1; i<=M; i++)
        g[i] = h[i] = 0, v[i].clear ();
}
for (int i=1; i<=M; i++)
    printf ("%d\n", ans[i]);
return 0;
}

Compilation message

rope.cpp: In function 'int main()':
rope.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d %d", &N, &M);
 ~~~~~~^~~~~~~~~~~~~~~~~
rope.cpp:22:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d", &a[i]), ans[a[i]] --;
     ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23908 KB Output is correct
3 Correct 23 ms 24112 KB Output is correct
4 Correct 24 ms 24112 KB Output is correct
5 Correct 24 ms 24112 KB Output is correct
6 Correct 23 ms 24112 KB Output is correct
7 Correct 24 ms 24112 KB Output is correct
8 Correct 23 ms 24112 KB Output is correct
9 Correct 27 ms 24168 KB Output is correct
10 Correct 22 ms 24168 KB Output is correct
11 Correct 23 ms 24168 KB Output is correct
12 Correct 25 ms 24168 KB Output is correct
13 Correct 24 ms 24168 KB Output is correct
14 Correct 23 ms 24168 KB Output is correct
15 Correct 23 ms 24168 KB Output is correct
16 Correct 23 ms 24168 KB Output is correct
17 Correct 23 ms 24168 KB Output is correct
18 Correct 24 ms 24168 KB Output is correct
19 Correct 24 ms 24168 KB Output is correct
20 Correct 24 ms 24168 KB Output is correct
21 Correct 26 ms 24168 KB Output is correct
22 Correct 27 ms 24168 KB Output is correct
23 Correct 23 ms 24168 KB Output is correct
24 Correct 25 ms 24168 KB Output is correct
25 Correct 29 ms 24168 KB Output is correct
26 Correct 25 ms 24168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23908 KB Output is correct
3 Correct 23 ms 24112 KB Output is correct
4 Correct 24 ms 24112 KB Output is correct
5 Correct 24 ms 24112 KB Output is correct
6 Correct 23 ms 24112 KB Output is correct
7 Correct 24 ms 24112 KB Output is correct
8 Correct 23 ms 24112 KB Output is correct
9 Correct 27 ms 24168 KB Output is correct
10 Correct 22 ms 24168 KB Output is correct
11 Correct 23 ms 24168 KB Output is correct
12 Correct 25 ms 24168 KB Output is correct
13 Correct 24 ms 24168 KB Output is correct
14 Correct 23 ms 24168 KB Output is correct
15 Correct 23 ms 24168 KB Output is correct
16 Correct 23 ms 24168 KB Output is correct
17 Correct 23 ms 24168 KB Output is correct
18 Correct 24 ms 24168 KB Output is correct
19 Correct 24 ms 24168 KB Output is correct
20 Correct 24 ms 24168 KB Output is correct
21 Correct 26 ms 24168 KB Output is correct
22 Correct 27 ms 24168 KB Output is correct
23 Correct 23 ms 24168 KB Output is correct
24 Correct 25 ms 24168 KB Output is correct
25 Correct 29 ms 24168 KB Output is correct
26 Correct 25 ms 24168 KB Output is correct
27 Correct 36 ms 25068 KB Output is correct
28 Correct 46 ms 25068 KB Output is correct
29 Correct 37 ms 25068 KB Output is correct
30 Correct 35 ms 25068 KB Output is correct
31 Correct 35 ms 25068 KB Output is correct
32 Correct 34 ms 25068 KB Output is correct
33 Correct 38 ms 25068 KB Output is correct
34 Correct 39 ms 25068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23908 KB Output is correct
3 Correct 23 ms 24112 KB Output is correct
4 Correct 24 ms 24112 KB Output is correct
5 Correct 24 ms 24112 KB Output is correct
6 Correct 23 ms 24112 KB Output is correct
7 Correct 24 ms 24112 KB Output is correct
8 Correct 23 ms 24112 KB Output is correct
9 Correct 27 ms 24168 KB Output is correct
10 Correct 22 ms 24168 KB Output is correct
11 Correct 23 ms 24168 KB Output is correct
12 Correct 25 ms 24168 KB Output is correct
13 Correct 24 ms 24168 KB Output is correct
14 Correct 23 ms 24168 KB Output is correct
15 Correct 23 ms 24168 KB Output is correct
16 Correct 23 ms 24168 KB Output is correct
17 Correct 23 ms 24168 KB Output is correct
18 Correct 24 ms 24168 KB Output is correct
19 Correct 24 ms 24168 KB Output is correct
20 Correct 24 ms 24168 KB Output is correct
21 Correct 26 ms 24168 KB Output is correct
22 Correct 27 ms 24168 KB Output is correct
23 Correct 23 ms 24168 KB Output is correct
24 Correct 25 ms 24168 KB Output is correct
25 Correct 29 ms 24168 KB Output is correct
26 Correct 25 ms 24168 KB Output is correct
27 Correct 36 ms 25068 KB Output is correct
28 Correct 46 ms 25068 KB Output is correct
29 Correct 37 ms 25068 KB Output is correct
30 Correct 35 ms 25068 KB Output is correct
31 Correct 35 ms 25068 KB Output is correct
32 Correct 34 ms 25068 KB Output is correct
33 Correct 38 ms 25068 KB Output is correct
34 Correct 39 ms 25068 KB Output is correct
35 Correct 52 ms 25068 KB Output is correct
36 Correct 52 ms 25068 KB Output is correct
37 Correct 42 ms 25076 KB Output is correct
38 Correct 40 ms 25076 KB Output is correct
39 Correct 52 ms 25076 KB Output is correct
40 Correct 45 ms 25076 KB Output is correct
41 Correct 51 ms 25076 KB Output is correct
42 Correct 38 ms 25076 KB Output is correct
43 Correct 41 ms 25196 KB Output is correct
44 Correct 39 ms 25196 KB Output is correct
45 Correct 37 ms 25196 KB Output is correct
46 Correct 37 ms 25196 KB Output is correct
47 Correct 37 ms 25196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23908 KB Output is correct
3 Correct 23 ms 24112 KB Output is correct
4 Correct 24 ms 24112 KB Output is correct
5 Correct 24 ms 24112 KB Output is correct
6 Correct 23 ms 24112 KB Output is correct
7 Correct 24 ms 24112 KB Output is correct
8 Correct 23 ms 24112 KB Output is correct
9 Correct 27 ms 24168 KB Output is correct
10 Correct 22 ms 24168 KB Output is correct
11 Correct 23 ms 24168 KB Output is correct
12 Correct 25 ms 24168 KB Output is correct
13 Correct 24 ms 24168 KB Output is correct
14 Correct 23 ms 24168 KB Output is correct
15 Correct 23 ms 24168 KB Output is correct
16 Correct 23 ms 24168 KB Output is correct
17 Correct 23 ms 24168 KB Output is correct
18 Correct 24 ms 24168 KB Output is correct
19 Correct 24 ms 24168 KB Output is correct
20 Correct 24 ms 24168 KB Output is correct
21 Correct 26 ms 24168 KB Output is correct
22 Correct 27 ms 24168 KB Output is correct
23 Correct 23 ms 24168 KB Output is correct
24 Correct 25 ms 24168 KB Output is correct
25 Correct 29 ms 24168 KB Output is correct
26 Correct 25 ms 24168 KB Output is correct
27 Correct 36 ms 25068 KB Output is correct
28 Correct 46 ms 25068 KB Output is correct
29 Correct 37 ms 25068 KB Output is correct
30 Correct 35 ms 25068 KB Output is correct
31 Correct 35 ms 25068 KB Output is correct
32 Correct 34 ms 25068 KB Output is correct
33 Correct 38 ms 25068 KB Output is correct
34 Correct 39 ms 25068 KB Output is correct
35 Correct 52 ms 25068 KB Output is correct
36 Correct 52 ms 25068 KB Output is correct
37 Correct 42 ms 25076 KB Output is correct
38 Correct 40 ms 25076 KB Output is correct
39 Correct 52 ms 25076 KB Output is correct
40 Correct 45 ms 25076 KB Output is correct
41 Correct 51 ms 25076 KB Output is correct
42 Correct 38 ms 25076 KB Output is correct
43 Correct 41 ms 25196 KB Output is correct
44 Correct 39 ms 25196 KB Output is correct
45 Correct 37 ms 25196 KB Output is correct
46 Correct 37 ms 25196 KB Output is correct
47 Correct 37 ms 25196 KB Output is correct
48 Correct 222 ms 34672 KB Output is correct
49 Correct 220 ms 34812 KB Output is correct
50 Correct 290 ms 34812 KB Output is correct
51 Correct 226 ms 34812 KB Output is correct
52 Correct 205 ms 34812 KB Output is correct
53 Correct 207 ms 34812 KB Output is correct
54 Correct 212 ms 34812 KB Output is correct
55 Correct 194 ms 34812 KB Output is correct
56 Correct 193 ms 34812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23908 KB Output is correct
3 Correct 23 ms 24112 KB Output is correct
4 Correct 24 ms 24112 KB Output is correct
5 Correct 24 ms 24112 KB Output is correct
6 Correct 23 ms 24112 KB Output is correct
7 Correct 24 ms 24112 KB Output is correct
8 Correct 23 ms 24112 KB Output is correct
9 Correct 27 ms 24168 KB Output is correct
10 Correct 22 ms 24168 KB Output is correct
11 Correct 23 ms 24168 KB Output is correct
12 Correct 25 ms 24168 KB Output is correct
13 Correct 24 ms 24168 KB Output is correct
14 Correct 23 ms 24168 KB Output is correct
15 Correct 23 ms 24168 KB Output is correct
16 Correct 23 ms 24168 KB Output is correct
17 Correct 23 ms 24168 KB Output is correct
18 Correct 24 ms 24168 KB Output is correct
19 Correct 24 ms 24168 KB Output is correct
20 Correct 24 ms 24168 KB Output is correct
21 Correct 26 ms 24168 KB Output is correct
22 Correct 27 ms 24168 KB Output is correct
23 Correct 23 ms 24168 KB Output is correct
24 Correct 25 ms 24168 KB Output is correct
25 Correct 29 ms 24168 KB Output is correct
26 Correct 25 ms 24168 KB Output is correct
27 Correct 36 ms 25068 KB Output is correct
28 Correct 46 ms 25068 KB Output is correct
29 Correct 37 ms 25068 KB Output is correct
30 Correct 35 ms 25068 KB Output is correct
31 Correct 35 ms 25068 KB Output is correct
32 Correct 34 ms 25068 KB Output is correct
33 Correct 38 ms 25068 KB Output is correct
34 Correct 39 ms 25068 KB Output is correct
35 Correct 52 ms 25068 KB Output is correct
36 Correct 52 ms 25068 KB Output is correct
37 Correct 42 ms 25076 KB Output is correct
38 Correct 40 ms 25076 KB Output is correct
39 Correct 52 ms 25076 KB Output is correct
40 Correct 45 ms 25076 KB Output is correct
41 Correct 51 ms 25076 KB Output is correct
42 Correct 38 ms 25076 KB Output is correct
43 Correct 41 ms 25196 KB Output is correct
44 Correct 39 ms 25196 KB Output is correct
45 Correct 37 ms 25196 KB Output is correct
46 Correct 37 ms 25196 KB Output is correct
47 Correct 37 ms 25196 KB Output is correct
48 Correct 222 ms 34672 KB Output is correct
49 Correct 220 ms 34812 KB Output is correct
50 Correct 290 ms 34812 KB Output is correct
51 Correct 226 ms 34812 KB Output is correct
52 Correct 205 ms 34812 KB Output is correct
53 Correct 207 ms 34812 KB Output is correct
54 Correct 212 ms 34812 KB Output is correct
55 Correct 194 ms 34812 KB Output is correct
56 Correct 193 ms 34812 KB Output is correct
57 Correct 1884 ms 88620 KB Output is correct
58 Correct 1645 ms 88620 KB Output is correct
59 Correct 1561 ms 88620 KB Output is correct
60 Correct 1673 ms 92764 KB Output is correct
61 Correct 1670 ms 99376 KB Output is correct
62 Correct 636 ms 99376 KB Output is correct
63 Correct 1012 ms 99916 KB Output is correct
64 Correct 772 ms 102840 KB Output is correct
65 Correct 393 ms 102840 KB Output is correct