Submission #889869

# Submission time Handle Problem Language Result Execution time Memory
889869 2023-12-20T08:41:12 Z boris_mihov Council (JOI23_council) C++17
0 / 100
864 ms 1048576 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstring>
#include <vector>
#include <bitset>

typedef long long llong;
const int MAXN = 300000 + 10;
const int MAXB = (1 << 20);
const int MAXM = 18;

int n, m;
int a[MAXN];
int cntMask[MAXB];
int comb[MAXM][MAXM];
int up[MAXB][MAXM][MAXM];
llong down[MAXB][MAXM];
llong cntDown[MAXB];
int cnt[MAXB];

int zero, one, two;

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

    // std::cout << "zero: " << std::bitset <12> (zero).to_string() << '\n';
    // std::cout << "one : " << std::bitset <12> (one).to_string() << '\n';
    // std::cout << "two : " << std::bitset <12> (two).to_string() << '\n';

    for (int i = 0 ; i <= m ; ++i)
    {   
        comb[i][0] = 1; 
    }

    for (int i = 1 ; i <= m ; ++i)
    {
        for (int j = 1 ; j <= i ; ++j)
        {
            comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        // std::cout << "increase: " << std::bitset <12> (((1 << m) - 1) ^ a[i]).to_string() << '\n';
        cntDown[((1 << m) - 1) ^ a[i]]++;
    }

    for (int mask = (1 << m) - 1 ; mask >= 0 ; --mask)
    {
        down[mask][m] = cntDown[mask];
        for (int bit = m - 1 ; bit >= 0 ; --bit)
        {
            if (mask & (1 << bit))
            {
                down[mask][bit] = down[mask][bit + 1];
            } else
            {
                down[mask][bit] = down[mask][bit + 1];
                down[mask][bit] += down[mask | (1 << bit)][bit + 1];
            }
        }
    }

    for (int mask = 0 ; mask < (1 << m) ; ++mask)
    {
        int myPop = __builtin_popcount(mask);
        up[mask][0][m] = down[mask][0];
        
        // if (std::bitset <12> (mask).to_string() == "010001101110") std::cout << "count is: " << std::bitset <12> (mask).to_string() << " (" << myPop << ") = " << down[mask][0] << '\n';
        for (int pop = 0 ; pop <= myPop ; ++pop)
        {
            for (int bit = m - 1 ; bit >= 0 ; --bit)
            {
                if (mask & (1 << bit))
                {
                    if (pop > 0) up[mask][pop][bit] += up[mask][pop - 1][bit + 1];
                    up[mask][pop][bit] += up[mask ^ (1 << bit)][pop][bit + 1];
                } else
                {
                    up[mask][pop][bit] = up[mask][pop][bit + 1];
                }
            }
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        int answer = __builtin_popcount(two | (one & (~a[i])));
        int mask = (one & a[i]) | (zero & (~a[i]));
        int myAnd = __builtin_popcount(mask & (~a[i]));
        // std::cout << "answer: " << answer << '\n';
        // std::cout << "mask: " << std::bitset <12> (mask).to_string()  << '\n';
        // std::cout << "curr: " << std::bitset <12> (~a[i]).to_string()  << '\n';
        // std::cout << "and&: " << std::bitset <12> ((~a[i]) & mask).to_string()  << '\n';

        for (int bit = m - 1 ; bit >= 0 ; --bit)
        {
            // std::cout << "add?: " << bit << ": " << up[mask][bit][0] << ' ' << comb[bit] << '\n';
            if (up[mask][bit][0] > comb[myAnd][bit])
            {
                answer += bit;
                break;
            }
        }

        std::cout << answer << '\n';
    }
}

void input()
{   
    std::cin >> n >> m;
    for (int i = 1 ; i <= n ; ++i)
    {
        for (int bit = m - 1 ; bit >= 0 ; --bit)
        {
            int curr;
            std::cin >> curr;

            a[i] <<= 1;
            a[i] |= curr;
            cnt[bit] += curr;
        }
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 1 ms 8792 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Runtime error 864 ms 1048576 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 1 ms 8792 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Runtime error 864 ms 1048576 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 47 ms 10396 KB Output is correct
3 Correct 37 ms 10320 KB Output is correct
4 Correct 28 ms 10328 KB Output is correct
5 Correct 41 ms 10360 KB Output is correct
6 Incorrect 29 ms 10332 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 47 ms 10396 KB Output is correct
3 Correct 37 ms 10320 KB Output is correct
4 Correct 28 ms 10328 KB Output is correct
5 Correct 41 ms 10360 KB Output is correct
6 Incorrect 29 ms 10332 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 47 ms 10396 KB Output is correct
3 Correct 37 ms 10320 KB Output is correct
4 Correct 28 ms 10328 KB Output is correct
5 Correct 41 ms 10360 KB Output is correct
6 Incorrect 29 ms 10332 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 47 ms 10396 KB Output is correct
3 Correct 37 ms 10320 KB Output is correct
4 Correct 28 ms 10328 KB Output is correct
5 Correct 41 ms 10360 KB Output is correct
6 Incorrect 29 ms 10332 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 1 ms 8792 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Runtime error 864 ms 1048576 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -