제출 #1364126

#제출 시각아이디문제언어결과실행 시간메모리
1364126AvianshCOVID tests (CEOI24_covid)C++20
90 / 100
7084 ms16532 KiB
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

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

    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 calcsize(double prob){
    double ans = (1.0)/prob;
    ans/=1.8;
    if(rand()%2){
        return floor(ans);
    }
    return ceil(ans);
}

vector<int>ans;

bool test(int l, int r){
    vector<bool>quer(N);
    for(int i = l;i<=r;i++){
        auto it = lower_bound(ans.begin(),ans.end(),i);
        if(it!=ans.end()){
            if((*it)==i)
                continue;
        }
        quer[i]=1;
    }
    return test_students(quer);
}

const int mxn = 1005;

double pos[mxn];

double prob0(int len){
    return pos[len];
}

pair<double,int> expect[mxn][mxn];
//stores: {expected num queries, preftoquery}

double finbest(int pref, int suf){
    if(expect[pref][suf]!=make_pair(1e18,-1)){
        return expect[pref][suf].first;
    }
    int len = pref+suf;
    if(pref==0){
        //find best
        if(suf==0){
            //base case
            expect[pref][suf]={0,0};
        }
        for(int qpref = 1;qpref<=suf;qpref++){
            //try querying this one
            double p0 = prob0(qpref);
            double curr = p0*finbest(0,suf-qpref);
            curr+=(1-p0)*finbest(qpref,suf-qpref);
            expect[pref][suf]=min(expect[pref][suf],{curr+1,qpref});
        }
    }
    else if(pref==1){
        //guaranteed
        double curr = finbest(0,suf);
        expect[pref][suf] = {curr,0};
    }
    else{
        //only consider shorter
        for(int qpref = 1;qpref<pref;qpref++){
            //try querying with this one
            double p0 = (prob0(qpref)-prob0(pref))/(1-prob0(pref));
            double curr = p0*finbest(pref-qpref,suf);
            curr+=(1-p0)*finbest(qpref,len-qpref);
            expect[pref][suf]=min(expect[pref][suf],{curr+1,qpref});
        }
    }
    return expect[pref][suf].first;
}

void pre(){
    pos[0]=1;
    for(int i = 1;i<mxn;i++){
        pos[i]=pos[i-1]*(1-P);
    }
    for(int i = 0;i<mxn;i++){
        for(int j = 0;j<mxn;j++){
            expect[i][j]={1e18,-1};
        }
    }
    for(int i = 0;i<mxn;i++){
        for(int j = 0;j<mxn;j++){
            finbest(i,j);
        }
    }
}

void solve(int l, int pref){
    //assume there is atleast one here
    if(pref==0&&l==N){
        //over
        return;
    }
    if(pref==1){
        ans.push_back(l);
        l++;
        pref--;
        solve(l,pref);
        return;
    }
    pair<double,int>nx = expect[pref][N-l-pref];
    assert(nx.second);
    vector<bool>quer(N);
    fill(quer.begin()+l,quer.begin()+l+nx.second,1);
    if(test_students(quer)){
        solve(l,nx.second);
    }
    else{
        solve(l+nx.second,pref ? (pref-nx.second): 0);
    }
}

std::vector<bool> find_positive() {
    std::vector<bool> answer(N);
    if(P==0){
        return answer;
    }
    ans.clear();
    solve(0,0);
    for(int i : ans){
        answer[i]=1;
    }
    return answer;
}

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

    // You may perform any extra initialization here.

    pre();

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

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp: In function 'bool test_students(std::vector<bool>)':
Main.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     scanf(" %c", &answer);
      |     ~~~~~^~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:172:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |     scanf("%d %lf %d", &N, &P, &T);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:190:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  190 |         scanf(" %c", &verdict);
      |         ~~~~~^~~~~~~~~~~~~~~~~
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…