답안 #268657

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
268657 2020-08-16T16:22:11 Z DavidDamian 도서관 (JOI18_library) C++11
100 / 100
368 ms 512 KB
#include<bits/stdc++.h>
#include "library.h"
using namespace std;
///Binary Search with gaps
set<int> S; //Set with non fixed numbers
vector<int> Q;
vector<int> Q2;
vector<int> disc;
bool inRange(int L,int R)
{
    for(int i=L;i<=R;i++){
        Q[i-1]=1; //Fills the whole range
        if(disc[i-1]==0) Q2[i-1]=1; //Fills the whole range if and only if the number is still in S (not discovered)
    }
    int A=Query(Q); //Query with the numbers in the range + previous discovered numbers
    int B=Query(Q2); //Query only with new numbers
    for(int i=L;i<=R;i++){ //Reset the arrays
        Q2[i-1]=0;
        if(!disc[i-1]) Q[i-1]=0;
    }
    return A==B; //If the answers are same, then there is one number adjacent to the last discovered, in Range is true
}
int binarySearch(int N)
{
    int L=1;
    int R=N;
    int x;
    while(L<R){
        int mid=(L+R)/2;
        auto it=S.lower_bound(L);
        int minimum=(*it);
        if(minimum<=mid && inRange(L,mid))
            R=mid;
        else
            L=mid+1;
    }
    if(L==R) x=L;
    return x;
}
void Solve(int N)
{
	vector<int> ANS(N);
	if(N==1){
        ANS[0]=1;
        Answer(ANS);
        return;
	}
	Q.resize(N);
	Q2.resize(N);
	disc.resize(N);
	for(int i=1;i<=N;i++){
        S.insert(i);
        Q[i-1]=1;
	}
	for(int i=1;i<=N;i++){
        Q[i-1]=0;
        if(Query(Q)==1){
            ANS[0]=i;
            S.erase(i);
            disc[i-1]=1;
            break;
        }
        Q[i-1]=1;
	}
	for(int i=1;i<=N;i++){
        Q[i-1]=0;
	}
	Q[ ANS[0]-1 ]=1;
	int i=1;
	while(S.size()){
        int nxt=binarySearch(N);
        ANS[i++]=nxt;
        S.erase(nxt);
        disc[nxt-1]=1;
        Q[nxt-1]=1;
	}
	Answer(ANS);
}

Compilation message

library.cpp: In function 'int binarySearch(int)':
library.cpp:38:12: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |     return x;
      |            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 384 KB # of queries: 2749
2 Correct 49 ms 384 KB # of queries: 2787
3 Correct 44 ms 384 KB # of queries: 3004
4 Correct 30 ms 256 KB # of queries: 2921
5 Correct 42 ms 384 KB # of queries: 2868
6 Correct 39 ms 384 KB # of queries: 2919
7 Correct 29 ms 256 KB # of queries: 2908
8 Correct 38 ms 384 KB # of queries: 2742
9 Correct 40 ms 256 KB # of queries: 2880
10 Correct 24 ms 256 KB # of queries: 1628
11 Correct 0 ms 256 KB # of queries: 0
12 Correct 0 ms 256 KB # of queries: 1
13 Correct 0 ms 256 KB # of queries: 6
14 Correct 0 ms 256 KB # of queries: 7
15 Correct 2 ms 256 KB # of queries: 91
16 Correct 4 ms 256 KB # of queries: 231
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 384 KB # of queries: 2749
2 Correct 49 ms 384 KB # of queries: 2787
3 Correct 44 ms 384 KB # of queries: 3004
4 Correct 30 ms 256 KB # of queries: 2921
5 Correct 42 ms 384 KB # of queries: 2868
6 Correct 39 ms 384 KB # of queries: 2919
7 Correct 29 ms 256 KB # of queries: 2908
8 Correct 38 ms 384 KB # of queries: 2742
9 Correct 40 ms 256 KB # of queries: 2880
10 Correct 24 ms 256 KB # of queries: 1628
11 Correct 0 ms 256 KB # of queries: 0
12 Correct 0 ms 256 KB # of queries: 1
13 Correct 0 ms 256 KB # of queries: 6
14 Correct 0 ms 256 KB # of queries: 7
15 Correct 2 ms 256 KB # of queries: 91
16 Correct 4 ms 256 KB # of queries: 231
17 Correct 368 ms 384 KB # of queries: 19548
18 Correct 331 ms 384 KB # of queries: 18781
19 Correct 337 ms 384 KB # of queries: 18931
20 Correct 323 ms 384 KB # of queries: 17933
21 Correct 293 ms 384 KB # of queries: 16904
22 Correct 274 ms 384 KB # of queries: 19157
23 Correct 340 ms 384 KB # of queries: 18738
24 Correct 179 ms 384 KB # of queries: 8615
25 Correct 347 ms 384 KB # of queries: 18634
26 Correct 330 ms 384 KB # of queries: 17475
27 Correct 142 ms 384 KB # of queries: 8856
28 Correct 158 ms 512 KB # of queries: 10069
29 Correct 160 ms 384 KB # of queries: 10063
30 Correct 180 ms 384 KB # of queries: 10069