Submission #1285127

#TimeUsernameProblemLanguageResultExecution timeMemory
1285127weebyeahCOVID tests (CEOI24_covid)C++20
100 / 100
779 ms400 KiB
#include <bits/stdc++.h>
using namespace std;

#define Umazing ios::sync_with_stdio(false); cin.tie(nullptr);
#define ll long long

#define pii pair<int, int>
#define pll pair<ll, ll>

/// You may use:

// The number of students
int N;

// The probability any given student is positive
double P;

// This function performs a test on a subset of samples.
// Its argument is a vector of Booleans of igth N,
// where the i-th element is true if the i-th sample should be added to the mix.
// It returns true if (and only if) at least one of the samples in the mix is positive.
bool test_students(vector<bool> mask)
{
    assert(mask.size() == (size_t)N);

    std::string mask_str(N, ' ');
    for (int i = 0; i < N; i++)
        mask_str[i] = mask[i] ? '1' : '0';

    printf("Q %s\n", mask_str.c_str());
    fflush(stdout);

    char answer;
    scanf(" %c", &answer);
    return answer == 'P';
}

/// You should implement:

// This function will be called once for each test instance.
// It should use test_students to determine which samples are positive.
// It must return a vector of Booleans of length N,
// where the i-th element is true if and only if the i-th sample is positive.

int bn = -1;
double bp = -1.0;
vector<double> a, be, po;
vector<int> xo, yo;

vector<bool> find_positive()
{
    if (bn != N || bp != P)
    {
        bn = N;
        bp = P;

        a.assign(N + 1, 0.0);
        be.assign(N + 1, 0.0);
        po.assign(N + 1, 1.0);

        xo.assign(N + 1, 0);
        yo.assign(N + 1, 0);

        double r = 1.0 - P;
        for (int i = 1; i <= N; i++)
        {
            po[i] = po[i - 1] * r;
        }

        be[1] = 0.0;
        for (int i = 2; i <= N; i++)
        {
            double cb = 1e9;
            int by = 1;
            double d = 1.0 - po[i];
            for (int j = 1; j <= i - 1; j++)
            {
                double py = (1.0 - po[j]) / d;
                double ny = (po[j] - po[i]) / d;
                double curr = 1.0 + py * be[j] + ny * be[i - j];

                if (curr < cb)
                {
                    cb = curr;
                    by = j;
                }
            }

            be[i] = cb;
            yo[i] = by;
        }

        a[0] = 0.0;
        for (int i = 1; i <= N; i++)
        {
            double cb = 1e9;
            int cbx = 1;
            double rn = 0.0;
            for (int j = 1; j <= i; j++)
            {
                rn += po[j - 1] * a[i - j];
                double curr = 1.0 + po[j] * a[i - j] + (1.0 - po[j]) * be[j] + P * rn;

                if (curr < cb)
                {
                    cb = curr;
                    cbx = j;
                }
            }

            a[i] = cb;
            xo[i] = cbx;
        }
    }

    vector<int> sta(N);

    int st = 0;
    int rem = N;
    while (rem > 0)
    {
        int x = xo[rem];

        vector<bool> m(N, false);
        for (int i = 0; i < x; i++)
        {
            m[st + i] = true;
        }

        bool pb = test_students(m);

        if (!pb)
        {
            for (int i = 0; i < x; i++)
            {
                sta[st + i] = 1;
            }

            st += x;
            rem -= x;

            continue;
        }

        int pos = st;
        int l = x;

        while (l > 1)
        {
            int y = yo[l];

            vector<bool> mp(N, false);
            for (int i = 0; i < y; i++)
            {
                mp[pos + i] = true;
            }

            bool lp = test_students(mp);

            if (lp)
                l = y;
            else
            {
                for (int i = 0; i < y; i++)
                {
                    sta[pos + i] = 1;
                }

                pos += y;
                l -= y;
            }
        }

        sta[pos] = 2;

        int cons = (pos - st) + 1;
        st = pos + 1;
        rem -= cons;
    }

    vector<bool> res(N, false);
    for (int i = 0; i < N; i++)
    {
        res[i] = (sta[i] == 2);
    }

    return res;
}


int main() 
{
    int T;
    scanf("%d %lf %d", &N, &P, &T);

    // You may perform any extra initialization here.

    for (int i = 0; i < T; i++) {
        std::vector<bool> answer = find_positive();
        assert(answer.size() == (size_t)N);

        std::string answer_str(N, ' ');
        for (int j = 0; j < N; j++)
            answer_str[j] = answer[j] ? '1' : '0';

        printf("A %s\n", answer_str.c_str());
        fflush(stdout);

        char verdict;
        scanf(" %c", &verdict);
        if (verdict == 'W')
            exit(0);
    }
}

Compilation message (stderr)

Main.cpp: In function 'bool test_students(std::vector<bool>)':
Main.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |     scanf(" %c", &answer);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:194:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  194 |     scanf("%d %lf %d", &N, &P, &T);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:210:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  210 |         scanf(" %c", &verdict);
      |         ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...