Submission #768884

# Submission time Handle Problem Language Result Execution time Memory
768884 2023-06-28T20:08:34 Z danikoynov Council (JOI23_council) C++14
33 / 100
4000 ms 345208 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 3e5 + 10, maxm = 21;
int n, m, imp[maxm], cnt[maxm];
bool a[maxn][maxm];
pair < int, int > dp[maxm][(1 << maxm)][2];
int val_mask[maxn], rev_mask[maxn];
void solve()
{

    cin >> n >> m;
    for (int i = 1; i <= n; i ++)
        for (int j = 0; j < m; j ++)
            cin >> a[i][j], cnt[j] += a[i][j];

    for (int mask = 0; mask < (1 << m); mask ++)
        for (int i = 0; i < m; i ++)
            dp[i][mask][0] = dp[i][mask][1] = {-1, -1};

    for (int i = 1; i <= n; i ++)
    {
        for (int j = 0; j < m; j ++)
        {
            val_mask[i] = (val_mask[i] + (1 << j) * a[i][j]);
            rev_mask[i] = (rev_mask[i] + (1 << j) * (1 - a[i][j]));

            ///cout << "mask " << rev_mask[i] << endl;

        }
        for (int mask = 0; mask < (1 << m); mask ++)
        {
            if ((mask & rev_mask[i]) == mask)
            {

                int val = __builtin_popcount(mask);
                if (dp[0][mask][0].second == i)
                {
                    dp[0][mask][0].first = max(dp[0][mask][0].first, val);
                }
                else if (dp[0][mask][1].second == i)
                {
                    dp[0][mask][1].first = max(dp[0][mask][1].first, val);
                    if (dp[0][mask][0].first < dp[0][mask][1].first)
                        swap(dp[0][mask][0], dp[0][mask][1]);
                }
                else if (val > dp[0][mask][0].first)
                {
                    dp[0][mask][1] = dp[0][mask][0];
                    dp[0][mask][0] = {val, i};
                }
                else if (val > dp[0][mask][1].first)
                {
                    dp[0][mask][1] = {val, i};
                }
            }
        }
        /**int val = __builtin_popcount(rev_mask[i]);
        if (dp[0][rev_mask[i]][0].second == i)
        {
            dp[0][rev_mask[i]][0].first = max(dp[0][rev_mask[i]][0].first, val);
        }
        else if (dp[0][rev_mask[i]][1].second == i)
        {
            dp[0][rev_mask[i]][1].first = max(dp[0][rev_mask[i]][1].first, val);
            if (dp[0][rev_mask[i]][0].first < dp[0][rev_mask[i]][1].first)
                swap(dp[0][rev_mask[i]][0], dp[0][rev_mask[i]][1]);
        }
        else if (val > dp[0][rev_mask[i]][0].first)
        {
            dp[0][rev_mask[i]][1] = dp[0][rev_mask[i]][0];
            dp[0][rev_mask[i]][0] = {val, i};
        }
        else if (val > dp[0][rev_mask[i]][1].first)
        {
            dp[0][rev_mask[i]][1] = {val, i};
        }*/
    }


    for (int i = 1; i <= m; i ++)
        for (int mask = 0; mask < (1 << m); mask ++)
        {
            dp[i][mask][0] = dp[i - 1][mask][0];
            dp[i][mask][1] = dp[i - 1][mask][1];
            if ((mask & (1 << (i - 1))) > 0)
            {
                int new_mask = (mask ^ (1 << (i - 1)));
                int val = dp[i - 1][new_mask][0].first, idx = dp[i - 1][new_mask][0].second;
                if (dp[i][mask][0].second == idx)
                {
                    if (val > dp[i][mask][0].first)
                        dp[i][mask][0].first = val;
                }
                else if (dp[i][mask][1].second == idx)
                {
                    if (val > dp[i][mask][1].first)
                        dp[i][mask][1].first = val;
                    if (dp[i][mask][0].first < dp[i][mask][1].first)
                        swap(dp[i][mask][0], dp[i][mask][1]);
                }
                else if (val > dp[i][mask][0].first)
                {
                    dp[i][mask][1] = dp[i][mask][0];
                    dp[i][mask][0] = {val, idx};
                }
                else if (val > dp[i][mask][1].first)
                {
                    dp[i][mask][1] = {val, idx};
                }

                val = dp[i - 1][new_mask][1].first;
                idx = dp[i - 1][new_mask][1].second;
                if (dp[i][mask][0].second == idx)
                {
                    if (val > dp[i][mask][0].first)
                        dp[i][mask][0].first = val;
                }
                else if (dp[i][mask][1].second == idx)
                {
                    if (val > dp[i][mask][1].first)
                        dp[i][mask][1].first = val;
                    if (dp[i][mask][0].first < dp[i][mask][1].first)
                        swap(dp[i][mask][0], dp[i][mask][1]);
                }
                else if (val > dp[i][mask][0].first)
                {
                    dp[i][mask][1] = dp[i][mask][0];
                    dp[i][mask][0] = {val, idx};
                }
                else if (val > dp[i][mask][1].first)
                {
                    dp[i][mask][1] = {val, idx};
                }

            }
           /// cout << "state " << i << " " << mask << " " << dp[i][mask][0] << " " << dp[i][mask][1] << endl;
        }

    for (int i = 1; i <= n; i ++)
    {
        int two = 0, one = 0, zero = 0;
        for (int j = 0; j < m; j ++)
        {
            if (cnt[j] >= n / 2 + 2)
                two |= (1 << j);
            else if (cnt[j] >= n / 2 + 1)
                one |= (1 << j);
            else if (cnt[j] >= n / 2)
                zero |= (1 << j);
        }

        int mask = 0, rev = 0;
        for (int j = m - 1; j >= 0; j --)
        {
            mask = mask * 2 + (a[i][j]);
            rev = rev * 2 + (1 - a[i][j]);
        }

        int ONE = two | (one & (rev));
        int ZERO = (one & (mask)) | (zero & (rev));

        /**cout << i << " : " << ONE << " : " << ZERO << endl;
        for (int bit = 0; bit < m; bit ++)
            if ((ZERO & (1 << bit)) > 0)
            cout << 1 << " ";
            else
                cout << 0 << " ";
            cout << endl;*/
        int best = dp[m][ZERO][0].first;
        int val = __builtin_popcount(ZERO & rev_mask[i]);
        if (val == best)
            best = dp[m][ZERO][1].first;
        ///cout << i << " : " << val << " " << best << endl;
        cout << __builtin_popcount(ONE) + best << endl;
        continue;
        int ans = 0;
        for (int p = 1; p <= n; p ++)
        {
            if (p == i)
                continue;
            ///cout << "here " << p << " " << ((rev_mask[p])) << endl;
            int cur = __builtin_popcount((rev_mask[p]) & ZERO);
            /**cout << "step " << p << endl;
                    for (int bit = 0; bit < m; bit ++)
            if ((rev_mask[p] & (1 << bit)) > 0)
            cout << 1 << " ";
            else
            cout << 0 << " ";
            cout << endl;*/
            if (cur > __builtin_popcount(ans))
                ans = ((rev_mask[p]) & ZERO);
            ///ans = max(ans, __builtin_popcount((~val_mask[p]) & zero));
        }

        ans |= ONE;
        cout << __builtin_popcount(ans) << endl;



    }

}

int main()
{
    ///speed();
    solve();
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 307 ms 345112 KB Output is correct
6 Correct 408 ms 345168 KB Output is correct
7 Correct 232 ms 345208 KB Output is correct
8 Correct 245 ms 345160 KB Output is correct
9 Correct 438 ms 345172 KB Output is correct
10 Correct 231 ms 345140 KB Output is correct
11 Correct 249 ms 345168 KB Output is correct
12 Correct 434 ms 345164 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 3 ms 4180 KB Output is correct
42 Correct 3 ms 4248 KB Output is correct
43 Correct 3 ms 4180 KB Output is correct
44 Correct 3 ms 4180 KB Output is correct
45 Correct 3 ms 4180 KB Output is correct
46 Correct 31 ms 37336 KB Output is correct
47 Correct 28 ms 37312 KB Output is correct
48 Correct 33 ms 37284 KB Output is correct
49 Correct 28 ms 37396 KB Output is correct
50 Correct 32 ms 37332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 307 ms 345112 KB Output is correct
6 Correct 408 ms 345168 KB Output is correct
7 Correct 232 ms 345208 KB Output is correct
8 Correct 245 ms 345160 KB Output is correct
9 Correct 438 ms 345172 KB Output is correct
10 Correct 231 ms 345140 KB Output is correct
11 Correct 249 ms 345168 KB Output is correct
12 Correct 434 ms 345164 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 3 ms 4180 KB Output is correct
42 Correct 3 ms 4248 KB Output is correct
43 Correct 3 ms 4180 KB Output is correct
44 Correct 3 ms 4180 KB Output is correct
45 Correct 3 ms 4180 KB Output is correct
46 Correct 31 ms 37336 KB Output is correct
47 Correct 28 ms 37312 KB Output is correct
48 Correct 33 ms 37284 KB Output is correct
49 Correct 28 ms 37396 KB Output is correct
50 Correct 32 ms 37332 KB Output is correct
51 Correct 1802 ms 345172 KB Output is correct
52 Execution timed out 4046 ms 328844 KB Time limit exceeded
53 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 93 ms 9312 KB Output is correct
3 Correct 99 ms 9548 KB Output is correct
4 Correct 59 ms 9452 KB Output is correct
5 Correct 102 ms 9452 KB Output is correct
6 Correct 61 ms 9504 KB Output is correct
7 Correct 105 ms 9456 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 93 ms 9312 KB Output is correct
3 Correct 99 ms 9548 KB Output is correct
4 Correct 59 ms 9452 KB Output is correct
5 Correct 102 ms 9452 KB Output is correct
6 Correct 61 ms 9504 KB Output is correct
7 Correct 105 ms 9456 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 440 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 578 ms 9716 KB Output is correct
21 Correct 453 ms 9708 KB Output is correct
22 Correct 455 ms 9720 KB Output is correct
23 Correct 519 ms 10036 KB Output is correct
24 Correct 1477 ms 9820 KB Output is correct
25 Correct 606 ms 9760 KB Output is correct
26 Correct 599 ms 9804 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 312 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 93 ms 9312 KB Output is correct
3 Correct 99 ms 9548 KB Output is correct
4 Correct 59 ms 9452 KB Output is correct
5 Correct 102 ms 9452 KB Output is correct
6 Correct 61 ms 9504 KB Output is correct
7 Correct 105 ms 9456 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 440 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 578 ms 9716 KB Output is correct
21 Correct 453 ms 9708 KB Output is correct
22 Correct 455 ms 9720 KB Output is correct
23 Correct 519 ms 10036 KB Output is correct
24 Correct 1477 ms 9820 KB Output is correct
25 Correct 606 ms 9760 KB Output is correct
26 Correct 599 ms 9804 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 312 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 564 KB Output is correct
46 Correct 1 ms 1236 KB Output is correct
47 Correct 3185 ms 13460 KB Output is correct
48 Correct 1843 ms 17676 KB Output is correct
49 Correct 1837 ms 18112 KB Output is correct
50 Correct 2973 ms 20232 KB Output is correct
51 Execution timed out 4058 ms 17492 KB Time limit exceeded
52 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 93 ms 9312 KB Output is correct
3 Correct 99 ms 9548 KB Output is correct
4 Correct 59 ms 9452 KB Output is correct
5 Correct 102 ms 9452 KB Output is correct
6 Correct 61 ms 9504 KB Output is correct
7 Correct 105 ms 9456 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 440 KB Output is correct
18 Correct 1 ms 304 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 578 ms 9716 KB Output is correct
21 Correct 453 ms 9708 KB Output is correct
22 Correct 455 ms 9720 KB Output is correct
23 Correct 519 ms 10036 KB Output is correct
24 Correct 1477 ms 9820 KB Output is correct
25 Correct 606 ms 9760 KB Output is correct
26 Correct 599 ms 9804 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 312 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 564 KB Output is correct
46 Correct 1 ms 1236 KB Output is correct
47 Correct 3185 ms 13460 KB Output is correct
48 Correct 1843 ms 17676 KB Output is correct
49 Correct 1837 ms 18112 KB Output is correct
50 Correct 2973 ms 20232 KB Output is correct
51 Execution timed out 4058 ms 17492 KB Time limit exceeded
52 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 1236 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 307 ms 345112 KB Output is correct
6 Correct 408 ms 345168 KB Output is correct
7 Correct 232 ms 345208 KB Output is correct
8 Correct 245 ms 345160 KB Output is correct
9 Correct 438 ms 345172 KB Output is correct
10 Correct 231 ms 345140 KB Output is correct
11 Correct 249 ms 345168 KB Output is correct
12 Correct 434 ms 345164 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 3 ms 4180 KB Output is correct
42 Correct 3 ms 4248 KB Output is correct
43 Correct 3 ms 4180 KB Output is correct
44 Correct 3 ms 4180 KB Output is correct
45 Correct 3 ms 4180 KB Output is correct
46 Correct 31 ms 37336 KB Output is correct
47 Correct 28 ms 37312 KB Output is correct
48 Correct 33 ms 37284 KB Output is correct
49 Correct 28 ms 37396 KB Output is correct
50 Correct 32 ms 37332 KB Output is correct
51 Correct 1802 ms 345172 KB Output is correct
52 Execution timed out 4046 ms 328844 KB Time limit exceeded
53 Halted 0 ms 0 KB -