#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <set>
#include <vector>
#include <algorithm>
//#define DEBUG
//#define PRINTQ
#ifndef DEBUG
#include "grader.h"
#endif // DEBUG
const int MAXN = 256;
std::set<int> candidates[MAXN + 1];
std::set<int> available, cavailable;
#ifdef DEBUG
int p[MAXN + 1] = {13, 142, 68, 65, 151, 196, 76, 16, 167, 12, 114, 197, 69, 171, 50, 24, 27, 20, 28, 166, 116, 7, 85, 200, 2, 8, 43, 149, 179, 128, 33, 80, 134, 84, 99, 6, 5, 133, 115, 55, 108, 126, 164, 3, 82, 14, 38, 205, 29, 109, 127, 78, 88, 39, 54, 112, 103, 123, 131, 158, 107, 130, 120, 42, 156, 34, 121, 21, 48, 182, 136, 194, 58, 187, 22, 192, 138, 40, 143, 19, 170, 37, 75, 163, 15, 177, 30, 97, 111, 35, 189, 89, 144, 91, 36, 180, 64, 168, 124, 31, 1, 47, 152, 113, 165, 181, 106, 193, 92, 11, 125, 135, 159, 148, 81, 67, 95, 157, 178, 201, 79, 77, 83, 206, 169, 150, 98, 25, 72, 198, 129, 175, 204, 61, 118, 104, 62, 191, 186, 162, 173, 208, 183, 202, 90, 59, 52, 4, 49, 74, 174, 110, 132, 71, 161, 44, 153, 184, 119, 86, 199, 188, 46, 139, 137, 87, 70, 56, 53, 160, 57, 141, 66, 185, 100, 122, 203, 176, 102, 105, 207, 45, 155, 190, 101, 51, 145, 17, 172, 18, 96, 140, 117, 10, 94, 23, 63, 60, 195, 41, 154, 32, 9, 73, 93, 146, 147, 26};
int n = 208;
int nq = 0;
int st1 = 1;
std::vector<int> ans;
#endif // DEBUG
int order_of_key(std::set<int> st, int poz){
auto it = st.begin();
for( int i = 1; i <= poz; i++ )
it++;
return *it;
}
#ifdef DEBUG
int isPermutation(std::vector<int> q){
std::sort(q.begin(), q.end());
int i = 1;
for(auto x : q){
if(x != i)
return 0;
i++;
}
return 1;
}
#endif // DEBUG
#ifdef DEBUG
int query(std::vector<int> q){
#ifdef PRINTQ
for(auto x : q)
printf("%d ", x);
printf(" perm : %d", isPermutation(q));
st1 = std::min(st1, (int)isPermutation(q));
printf("\n");
#endif // PRINTQ
nq++;
int x = 0;
for( int i = 0; i < n; i++ ){
if(q[i] == p[i])
x++;
}
return x;
}
#endif // DEBUG
void solve(int n){
srand(time(0));
int i, j, x;
for( i = 1; i <= n; i++ ){
available.insert(i);
for( j = 1; j <= n; j++ )
candidates[i].insert(j);
}
std::vector<int> q(n);
for( i = 1; i <= n; i++ ){
while(candidates[i].size() > 1){
q[i - 1] = order_of_key(candidates[i], rand() % candidates[i].size());
cavailable = available;
available.erase(q[i - 1]);
for( j = i + 1; j <= n; j++ ){
q[j - 1] = order_of_key(available, rand() % available.size());
available.erase(q[j - 1]);
}
available = cavailable;
#ifdef PRINTQ
printf("%d available : ", i);
for( auto x : available )
printf("%d ", x);
printf("\n");
#endif // DEBUG
if( (x = query(q)) == i - 1 ){
for( j = i; j <= n; j++ ){
if(candidates[j].find(q[j - 1]) != candidates[j].end())
candidates[j].erase(q[j - 1]);
}
}
else if(x == n)
return;
}
q[i - 1] = *candidates[i].begin();
for( j = i + 1; j <= n; j++ )
if(candidates[j].find(q[i - 1]) != candidates[j].end())
candidates[j].erase(q[i - 1]);
available.erase(*candidates[i].begin());
}
ans = q;
query(q);
}
#ifdef DEBUG
int main(){
solve(n);
int i = 0;
int st = 1;
for( auto x : ans ){
if(x != p[i])
st = 0;
i++;
}
printf("Correct Permutations : %d\n", st1);
printf("Good Last Permutation : %d\n", st);
printf("Number Of Queries : %d\n", nq);
return 0;
}
#endif // DEBUG
Compilation message
mouse.cpp: In function 'void solve(int)':
mouse.cpp:105:3: error: 'ans' was not declared in this scope; did you mean 'abs'?
105 | ans = q;
| ^~~
| abs