답안 #1044915

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1044915 2024-08-05T14:33:27 Z SamAnd COVID tests (CEOI24_covid) C++17
70.45 / 100
1471 ms 596 KB
#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] == -1)
        {
            assert(l == r);
            answer[l] = true;
            return;
        }
        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;
            }
        }
        if (n == 1)
        {
            dp1[n] = 0;
            u1[n] = -1;
        }

        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;
            }
        }
    }
    //printf("%f\n", dp[N]);
    //return 0;

    // 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.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

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:127:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |     scanf("%d %lf %d", &N, &P, &T);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:193:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  193 |         scanf(" %c", &verdict);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 6 ms 344 KB Output is correct
4 Correct 6 ms 444 KB Output is correct
5 Correct 6 ms 596 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 7 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 7 ms 344 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 6 ms 344 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 7 ms 344 KB Output is correct
16 Correct 6 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 344 KB Output is correct (P=0.001, F=15.1, Q=13.2) -> 90.00 points
2 Correct 107 ms 344 KB Output is correct (P=0.005256, F=51.1, Q=55.9) -> 65.42 points
3 Correct 209 ms 344 KB Output is correct (P=0.011546, F=94.9, Q=106.5) -> 60.45 points
4 Correct 382 ms 344 KB Output is correct (P=0.028545, F=191.5, Q=213.8) -> 61.40 points
5 Correct 567 ms 344 KB Output is correct (P=0.039856, F=246.3, Q=273.3) -> 62.57 points
6 Correct 852 ms 344 KB Output is correct (P=0.068648, F=366.2, Q=398.8) -> 66.37 points
7 Correct 1087 ms 344 KB Output is correct (P=0.104571, F=490.3, Q=523.0) -> 71.05 points
8 Correct 1329 ms 344 KB Output is correct (P=0.158765, F=639.1, Q=664.1) -> 77.82 points
9 Correct 1471 ms 344 KB Output is correct (P=0.2, F=731.4, Q=750.5) -> 81.49 points