Submission #1044908

#TimeUsernameProblemLanguageResultExecution timeMemory
1044908SamAndCOVID tests (CEOI24_covid)C++17
60.29 / 100
1693 ms344 KiB
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <random>
using namespace std;
mt19937 rnf(1234);

/// You may use:

// The number of students
int N;

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

vector<bool> trueanswer;

// This function performs a test on a subset of samples.
// Its argument is a vector of Booleans of length 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(std::vector<bool> mask) {
    /*assert(mask.size() == (size_t)N);
    for (int i = 0; i < N; ++i)
    {
        if (mask[i] && trueanswer[i])
            return true;
    }
    return false;*/

    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:

double dp[1003], dp1[1003];
int u[1003], u1[1003];

void rec(int l, int r, vector<bool>& answer, bool z)
{
    if (z == false)
    {
        if (u[r - l + 1] == 0)
        {
            vector<bool> v(N, false);
            for (int i = l; i <= r; ++i)
            {
                v[i] = true;
                if (test_students(v))
                    answer[i] = true;
                v[i] = false;
            }
            return;
        }
        if (u[r - l + 1] == r - l + 1)
        {
            vector<bool> v(N, false);
            for (int i = l; i <= r; ++i)
                v[i] = true;
            if (!test_students(v))
                return;
            rec(l, r, answer, true);
            return;
        }
        int m = l + u[r - l + 1] - 1;
        rec(l, m, answer, false);
        rec(m + 1, r, answer, false);
    }
    else
    {
        if (u1[r - l + 1] == 0)
        {
            vector<bool> v(N, false);
            for (int i = l; i <= r; ++i)
            {
                v[i] = true;
                if (test_students(v))
                    answer[i] = true;
                v[i] = false;
            }
            return;
        }
        int m = l + u1[r - l + 1] - 1;
        vector<bool> v(N, false);
        for (int i = l; i <= m; ++i)
            v[i] = true;
        if (!test_students(v))
            rec(m + 1, r, answer, true);
        else
        {
            rec(l, m, answer, true);
            rec(m + 1, r, answer, false);
        }
    }
}

// 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.
std::vector<bool> find_positive() {
    std::vector<bool> answer(N, false);
    rec(0, N - 1, answer, false);
    return answer;
}

double ast[1003];

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

    trueanswer.assign(N, false);
    for (int i = 0; i < N; ++i)
    {
        if (rnf() % 1000000000 < P * 1000000000)
            trueanswer[i] = true;
    }

    ast[0] = 1;
    for (int i = 1; i <= N; ++i)
        ast[i] = (ast[i - 1] * (1 - P));

    for (int n = 1; n <= N; ++n)
    {
        dp1[n] = n;
        u1[n] = 0;
        for (int i = 1; i < n; ++i)
        {
            double sh0 = ast[i] * (1 - ast[n - i]) / (1 - ast[n]);
            double ydp = sh0 * (1 + dp1[n - i]) + (1 - sh0) * (1 + dp1[i] + dp[n - i]);
            if (ydp < dp1[n])
            {
                dp1[n] = ydp;
                u1[n] = i;
            }
        }

        dp[n] = n;
        u[n] = 0;
        if (ast[n] * 1 + (1 - ast[n]) * (1 + dp1[n]) < dp[n])
        {
            dp[n] = ast[n] * 1 + (1 - ast[n]) * (1 + dp1[n]);
            u[n] = n;
        }
        for (int i = 1; i < n; ++i)
        {
            if (dp[i] + dp[n - i] < dp[n])
            {
                dp[n] = dp[i] + dp[n - i];
                u[n] = i;
            }
        }
    }

    // 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);
    }

    return 0;
}

// 1000 0.1 1
// 1 0.001 1
// 1000 0.001 300
// 1000 0.005256 300
// 1000 0.011546 300
// 1000 0.028545 300
// 1000 0.039856 300
// 1000 0.068648 300
// 1000 0.104571 300
// 1000 0.158765 300
// 1000 0.2 300

Compilation message (stderr)

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