Submission #1363909

#TimeUsernameProblemLanguageResultExecution timeMemory
1363909AvianshCOVID tests (CEOI24_covid)C++20
46.65 / 100
725 ms496 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){
    if(prob<=0){
        return 1e9;
    }
    if(prob>=1){
        return 1;
    }
    return ceil((1/prob)/2);
}

vector<int>solve(vector<int>pos, double prob){
    random_shuffle(pos.begin(),pos.end());
    int len = min(((int)pos.size()+1)/2,calcsize(prob));
    vector<vector<int>>sets;
    for(int i = 0;i<pos.size();i+=len){
        int j = i+len-1;
        j=min(j,(int)pos.size()-1);
        //check i to j as one set
        vector<bool>quer(N,0);
        for(int z = i;z<=j;z++){
            quer[pos[z]]=1;
        }
        if(test_students(quer)){
            vector<int>cur;
            for(int z = i;z<=j;z++){
                cur.push_back(pos[z]);
            }
            sets.push_back(cur);
        }
    }
    if(len==1){
        //i have the answer now
        vector<int>ans;
        for(vector<int>v:sets){
            //assert(v.size()==1);
            ans.push_back(v[0]);
        }
        return ans;
    }
    vector<int>nx;
    for(vector<int>v:sets){
        for(int i : v){
            nx.push_back(i);
        }
    }
    if(nx.size()==0){
        return {};
    }
    prob*=pos.size();
    prob/=nx.size();
    if(pos.size()==nx.size()){
        //increase prob a little
        prob*=5;
    }
    return solve(nx,prob);
}

std::vector<bool> find_positive() {
    std::vector<bool> answer(N);
    vector<int>curr(N);
    vector<bool>temp(N,1);
    if(!test_students(temp)){
        return answer;
    }
    iota(curr.begin(),curr.end(),0);
    vector<int>inds = solve(curr,P);
    for(int i : inds){
        answer[i]=1;
    }
    return answer;
}

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

    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'std::vector<int> solve(std::vector<int>, double)':
Main.cpp:54:19: warning: 'void std::random_shuffle(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<int*, vector<int> >]' is deprecated: use 'std::shuffle' instead [-Wdeprecated-declarations]
   54 |     random_shuffle(pos.begin(),pos.end());
      |     ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:51,
                 from Main.cpp:5:
/usr/include/c++/13/bits/stl_algo.h:4581:5: note: declared here
 4581 |     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~~~~~~~~~~~
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:117:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |     scanf("%d %lf %d", &N, &P, &T);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:133:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         scanf(" %c", &verdict);
      |         ~~~~~^~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...