답안 #686162

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
686162 2023-01-25T06:11:44 Z jamezzz Flight to the Ford (BOI22_communication) C++17
100 / 100
3382 ms 2100 KB
#include "communication.h"
#include <bits/stdc++.h>
using namespace std;
//
// --- Sample implementation for the task communication ---
//
// To compile this program with the sample grader, place:
//     communication.h communication_sample.cpp sample_grader.cpp
// in a single folder, then open the terminal in this directory (right-click onto an empty spot in the directory,
// left click on "Open in terminal") and enter e.g.:
//     g++ -std=c++17 communication_sample.cpp sample_grader.cpp
// in this folder. This will create a file a.out in the current directory which you can execute from the terminal
// as ./a.out
// See task statement or sample_grader.cpp for the input specification
//

typedef pair<int,int> ii;
typedef vector<ii> vii;
#define pf printf
#define pb push_back
#define sz(x) (int)x.size()

int getSize(vii &v){
	int sum=0;
	for(auto[a,b]:v)sum+=b-a+1;
	return sum;
}

int inside(vii &v,int X,int up){
	int half=(getSize(v)+up)/2;
	for(int i=0;i<sz(v);++i){
		if(half==0)break;
		auto[l,r]=v[i];
		if(r-l+1<=half){
			if(l<=X&&X<=r)return 1;
			half-=r-l+1;
		}
		else{
			if(l<=X&&X<=l+half-1)return 1;
			half=0;
		}
	}
	return 0;
}

void split(vii &v,vii &vi,vii &ve,int up){
	int half=(getSize(v)+up)/2;
	for(int i=0;i<sz(v);++i){
		auto[l,r]=v[i];
		if(r-l+1<=half)vi.pb({l,r}),half-=r-l+1;
		else if(half==0)ve.pb({l,r});
		else vi.pb({l,l+half-1}),ve.pb({l+half,r}),half=0;
	}
}

void encode(int N,int X){
    vii corr,wrong;
    corr.pb({1,N});
    int num=0;
    while(getSize(corr)+getSize(wrong)>3){
		//pf("corr: ");for(auto[l,r]:corr)pf("(%d %d) ",l,r);pf("\n");
		//pf("wrong: ");for(auto[l,r]:wrong)pf("(%d %d) ",l,r);pf("\n");
		int res=send(inside(corr,X,1)||inside(wrong,X,0));
		vector<ii> corrI,corrE,wrongI,wrongE;
		split(corr,corrI,corrE,1);
		split(wrong,wrongI,wrongE,0);
		corr.clear();
		wrong.clear();
		if(res==1){
			for(ii x:corrI)corr.pb(x);
			for(ii x:wrongI)corr.pb(x);
			for(ii x:corrE)wrong.pb(x);
		}
		else{
			for(ii x:corrE)corr.pb(x);
			for(ii x:wrongE)corr.pb(x);
			for(ii x:corrI)wrong.pb(x);
		}
	}
	vector<int> rem;
	for(auto [l,r]:corr){
		for(int i=l;i<=r;++i)rem.pb(i);
	}
	for(auto [l,r]:wrong){
		for(int i=l;i<=r;++i)rem.pb(i);
	}
	//pf("rem: ");for(int i:rem)pf("%d ",i);pf("\n");
	if(X==rem[0])send(0),send(0),send(0),send(0);
	if(sz(rem)>=2&&X==rem[1])send(0),send(1),send(1),send(0);
	if(sz(rem)>=3&&X==rem[2])send(1),send(1),send(1),send(1);
}

ii decode(int N){
    vii corr,wrong;
    corr.pb({1,N});
    while(getSize(corr)+getSize(wrong)>3){
		//pf("corr: ");for(auto[l,r]:corr)pf("(%d %d) ",l,r);pf("\n");
		//pf("wrong: ");for(auto[l,r]:wrong)pf("(%d %d) ",l,r);pf("\n");
		int res=receive();
		vector<ii> corrI,corrE,wrongI,wrongE;
		split(corr,corrI,corrE,1);
		split(wrong,wrongI,wrongE,0);
		corr.clear();
		wrong.clear();
		if(res==1){
			for(ii x:corrI)corr.pb(x);
			for(ii x:wrongI)corr.pb(x);
			for(ii x:corrE)wrong.pb(x);
		}
		else{
			for(ii x:corrE)corr.pb(x);
			for(ii x:wrongE)corr.pb(x);
			for(ii x:corrI)wrong.pb(x);
		}
	}
	vector<int> rem;
	for(auto [l,r]:corr){
		for(int i=l;i<=r;++i)rem.pb(i);
	}
	for(auto [l,r]:wrong){
		for(int i=l;i<=r;++i)rem.pb(i);
	}
	//pf("rem: ");for(int i:rem)pf("%d ",i);pf("\n");
	int msk=0;
	for(int i=0;i<4;++i){
		int x=receive();
		msk+=x<<i;
	}
	vector<int> msks={0,6,15},pos;
	for(int i=0;i<3;++i){
		int diff=msks[i]^msk;
		if(__builtin_popcount(diff)<2||diff==5||diff==9||diff==10)pos.pb(rem[i]);
	}
	pos.pb(1);
	return {pos[0],pos[1]};
}

Compilation message

communication.cpp: In function 'void encode(int, int)':
communication.cpp:59:9: warning: unused variable 'num' [-Wunused-variable]
   59 |     int num=0;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1792 KB Output is correct
2 Correct 15 ms 1684 KB Output is correct
3 Correct 17 ms 1856 KB Output is correct
4 Correct 15 ms 1976 KB Output is correct
5 Correct 15 ms 1688 KB Output is correct
6 Correct 28 ms 1836 KB Output is correct
7 Correct 57 ms 1856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 757 ms 1836 KB Output is correct
2 Correct 371 ms 1856 KB Output is correct
3 Correct 466 ms 1748 KB Output is correct
4 Correct 629 ms 1768 KB Output is correct
5 Correct 729 ms 1660 KB Output is correct
6 Correct 505 ms 1660 KB Output is correct
7 Correct 1958 ms 1812 KB Output is correct
8 Correct 3382 ms 1892 KB Output is correct
9 Correct 2783 ms 1940 KB Output is correct
10 Correct 2869 ms 1976 KB Output is correct
11 Correct 3139 ms 1816 KB Output is correct
12 Correct 3041 ms 1948 KB Output is correct
13 Correct 3290 ms 2060 KB Output is correct
14 Correct 2707 ms 2100 KB Output is correct
15 Correct 1772 ms 1740 KB Output is correct
16 Correct 3327 ms 1816 KB Output is correct
17 Correct 882 ms 1764 KB Output is correct
18 Correct 783 ms 1804 KB Output is correct
19 Correct 662 ms 1784 KB Output is correct
20 Correct 766 ms 1736 KB Output is correct
21 Correct 877 ms 1912 KB Output is correct
22 Correct 717 ms 1812 KB Output is correct
23 Correct 1224 ms 1748 KB Output is correct
24 Correct 10 ms 1712 KB Output is correct
25 Correct 15 ms 1668 KB Output is correct
26 Correct 18 ms 1688 KB Output is correct
27 Correct 11 ms 1764 KB Output is correct
28 Correct 16 ms 1728 KB Output is correct
29 Correct 20 ms 1872 KB Output is correct
30 Correct 36 ms 1696 KB Output is correct