This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N;
double P;
bool test_students(vector<bool> mask);
bool estPasse[1001][1001];
double dp[1001][1001];
double powers[1001];
double esp(int prefix, int total) {
if(estPasse[prefix][total]) return dp[prefix][total];
estPasse[prefix][total] = true;
if(total == 0) return dp[prefix][total] = 0;
if(prefix == 0) {
double min_esp = INFINITY;
for(int sz = 1;sz <= total;sz++) {
double P_vide = powers[sz];
min_esp = min(min_esp,
1
+ esp(0, total - sz) * P_vide
+ esp(sz, total) * (1 - P_vide)
);
}
return dp[prefix][total] = min_esp;
} else if(prefix == 1) {
return dp[prefix][total] = esp(0, total - 1);
} else {
double min_esp = INFINITY;
for(int sz = 1;sz < prefix;sz++) {
double P_vide = powers[sz] * (1 - powers[prefix - sz]) / (1 - powers[prefix]);
min_esp = min(min_esp,
1
+ esp(prefix - sz, total - sz) * P_vide
+ esp(sz, total) * (1 - P_vide)
);
}
return dp[prefix][total] = min_esp;
}
}
bool ask_inter(int deb, int fin) {
vector<bool> mask(N, false);
fill(mask.begin() + deb, mask.begin() + fin, true);
return test_students(mask);
}
vector<bool> find_positive() {
vector<bool> answer(N, false);
int prefix = 0, total = N;
while(total != 0) {
if(prefix == 0) {
double min_esp = INFINITY;
int opt_sz = -1;
for(int sz = 1;sz <= total;sz++) {
double P_vide = powers[sz];
double E =
1
+ esp(0, total - sz) * P_vide
+ esp(sz, total) * (1 - P_vide);
if(E < min_esp) {
min_esp = E;
opt_sz = sz;
}
}
bool ans = ask_inter(N - total, N - total + opt_sz);
if(ans) {
prefix = opt_sz;
} else total -= opt_sz;
} else if(prefix == 1) {
answer[N - total] = true;
prefix = 0;
total -= 1;
} else {
double min_esp = INFINITY;
int opt_sz = -1;
for(int sz = 1;sz < prefix;sz++) {
double P_vide = powers[sz] * (1 - powers[prefix - sz]) / (1 - powers[prefix]);
double E =
1
+ esp(prefix - sz, total - sz) * P_vide
+ esp(sz, total) * (1 - P_vide);
if(E < min_esp) {
min_esp = E;
opt_sz = sz;
}
}
bool ans = ask_inter(N - total, N - total + opt_sz);
if(ans) {
prefix = opt_sz;
} else {
prefix -= opt_sz;
total -= opt_sz;
}
}
}
return answer;
}
void init() {
powers[0] = 1.;
for(int i = 1;i <= 1000;i++) {
powers[i] = (1 - P) * powers[i - 1];
}
}
/*
int __nbTests = 0;
vector<bool> __covid;
int main() {
N = 1000;
P = 0.2;
int T = 10;
init();
cerr << "ESP: " << esp(0, N) << endl;
for(int i = 0;i < T;i++) {
__nbTests = 0;
__covid.clear();
for(int i = 0;i < N;i++) {
__covid.push_back(rand() <= P * RAND_MAX);
}
vector<bool> answer = find_positive();
assert(answer == __covid);
cerr << i << ": " << __nbTests << endl;
}
}
bool test_students(vector<bool> mask) {
__nbTests++;
bool ok = false;
for(int i = 0;i < N;i++) {
if(mask[i] && __covid[i]) ok = true;
}
return ok;
}
/*/
int main() {
int T;
scanf("%d %lf %d", &N, &P, &T);
init();
for (int i = 0; i < T; i++) {
vector<bool> answer = find_positive();
assert(answer.size() == (size_t)N);
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;
}
bool test_students(vector<bool> mask) {
assert(mask.size() == (size_t)N);
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';
}
//*/
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:160:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
160 | scanf("%d %lf %d", &N, &P, &T);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:176:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
176 | scanf(" %c", &verdict);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp: In function 'bool test_students(std::vector<bool>)':
Main.cpp:195:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
195 | scanf(" %c", &answer);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |