| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1308251 | afric | Cave (IOI13_cave) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#include "cave.h"
using namespace std;
/*void answer(vector<int> i, vector<int> j)
{
cout << "ANSWER with key Indexes = " << endl;
for (int item : i)
{
cout << item << " ";
}
cout << endl << "key States = " << endl;
for (int item : j)
{
cout << item << " ";
}
cout << endl;
}
//TESTING
int tryCombination(vector<int> q)
{
for (int item : q)
{
cout << item << " ";
}
cout << endl;
int a;
cin >> a;
return a;
}*/
void binarySearch(int k, int n, vector<int> &keyIndex, vector<int> &keyState)
{
// logN binary search to find key
vector<int> ans = keyState;
for (int i = 0; i < n; i++)
{
if (ans[i] == -1)
{
ans[i]=0;
}
}
int pos = 1;
if (tryCombination(ans)>k)
{
// correct position for switch is down
pos = 0;
}
// binary search
int start = 0;
int end = n;
while ((end-start)>1)
{
int mid = (start+end)/2;
vector<int> q;
for (int i = 0; i < n; i++)
{
if (keyState[i]!=-1)
{
q.push_back(keyState[i]);
}
// first we query range(start..mid)
else if (i >= start && i < mid)
{
// we want to query this i
q.push_back(pos);
}
else{
// not in query --> use off position.
q.push_back(abs(pos-1));
}
}
int a = tryCombination(q);
if (a > k)
{
// first closed door after k so k must be open.
end = mid;
}
else{
start = mid;
}
}
// binary search is over so start==end (covers 1)
keyIndex[start] = k;
keyState[start] = pos;
}
void exploreCave(int N)
{
vector<int> keyIndex(N,-1);
vector<int> keyState(N, -1);
for (int i = 0; i < N; i++)
{
binarySearch(i,N,keyIndex,keyState);
}
answer(keyState,keyIndex);
}
/*// TESTING
int main()
{
int N;
cin >> N;
exploreCave(N);
}*/
