답안 #854412

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
854412 2023-09-27T12:04:44 Z Wansur 가장 긴 여행 (IOI23_longesttrip) C++17
100 / 100
13 ms 1208 KB
#include <vector>
#include<bits/stdc++.h>
using namespace std;

bool are_connected(std::vector<int> A, std::vector<int> B);

bool ask(vector<int> a,vector<int> b){
	return are_connected(a,b);
}

bool ask(int a,int b){
	return are_connected({a},{b});
}

std::vector<int> longest_trip(int n, int D){
	vector<int> p1,p2;
	p1.push_back(0);
	int t=1;
	if(n%2==0){
		t++;
		if(ask({0},{1})){
			p1.push_back(1);
		}
		else{
			p2.push_back(1);
		}
	}
	for(int i=t;i<n;i+=2){
		if(!p2.size()){
			if(ask(i,i+1)){
				if(ask(i,p1.back())){
					p1.push_back(i);
					p1.push_back(i+1);
				}
				else if(ask(i+1,p1.back())){
					p1.push_back(i+1);
					p1.push_back(i);
				}
				else{
					p2.push_back(i);
					p2.push_back(i+1);
				}
			}
			else{
				if(ask(i,p1.back())){
					p1.push_back(i);
					p2.push_back(i+1);
				}
				else{
					p1.push_back(i+1);
					p2.push_back(i);
				}
			}
		}
		else{
			if(ask(i,i+1)){
				if(ask(i,p1.back())){
					p1.push_back(i);
					p1.push_back(i+1);
					if(ask(i+1,p2.back())){
						reverse(p2.begin(),p2.end());
						for(int x:p2){
							p1.push_back(x);
						}
						p2.clear();
					}
				}
				else{
					p2.push_back(i);
					p2.push_back(i+1);
					if(ask(i+1,p1.back())){
						reverse(p2.begin(),p2.end());
						for(int x:p2){
							p1.push_back(x);
						}
						p2.clear();
					}
				}
			}
			else{
				int x=ask(i,p1.back());
				if(x){
					if(ask(i+1,p2.back())){
						p1.push_back(i);
						p2.push_back(i+1);
					}
					else{
						p2.push_back(i);
						p1.push_back(i+1);
					}
				}
				else{
					p2.push_back(i);
					p1.push_back(i+1);
				}
			}
		}
	}
	if(p2.size()==0){
		return p1;
	}
	if(!ask(p1,p2)){
		if(p1.size()>p2.size()){
			return p1;
		}
		return p2;
	}
	if((p1.size()==1 || ask(p1[0],p1.back())) && (p2.size()==1 || ask(p2[0],p2.back()))){
		int pos1=0,pos2=0;
		for(int l=0,r=p1.size()-1;l<=r;){
			int mid=l+r>>1;
			vector<int> t;
			for(int i=0;i<=mid;i++){
				t.push_back(p1[i]);
			}
			if(ask(t,p2)){
				pos1=mid;
				r=mid-1;
			}
			else{
				l=mid+1;
			}
		}
		for(int l=0,r=p2.size()-1;l<=r;){
			int mid=l+r>>1;
			vector<int> t;
			for(int i=0;i<=mid;i++){
				t.push_back(p2[i]);
			}
			if(ask(t,{p1[pos1]})){
				pos2=mid;
				r=mid-1;
			}
			else{
				l=mid+1;
			}
		}
		vector<int> a,b;
		for(int i=pos1;i<p1.size();i++){
			a.push_back(p1[i]);
		}
		for(int i=0;i<pos1;i++){
			a.push_back(p1[i]);
		}
		for(int i=pos2;i<p2.size();i++){
			b.push_back(p2[i]);
		}
		for(int i=0;i<pos2;i++){
			b.push_back(p2[i]);
		}
		reverse(a.begin(),a.end());
		for(int x:b){
			a.push_back(x);
		}
		return a;
	}
	if(ask(p1.back(),p2[0])){
		for(int x:p2){
			p1.push_back(x);
		}
		return p1;
	}
	if(ask(p1.back(),p2.back())){
		reverse(p2.begin(),p2.end());
		for(int x:p2){
			p1.push_back(x);
		}
		return p1;
	}
	if(ask(p1[0],p2[0])){
		reverse(p1.begin(),p1.end());
		for(int x:p2){
			p1.push_back(x);
		}
		return p1;
	}
	for(int x:p1){
		p2.push_back(x);
	}
	return p2;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:111:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  111 |    int mid=l+r>>1;
      |            ~^~
longesttrip.cpp:125:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  125 |    int mid=l+r>>1;
      |            ~^~
longesttrip.cpp:139:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |   for(int i=pos1;i<p1.size();i++){
      |                  ~^~~~~~~~~~
longesttrip.cpp:145:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |   for(int i=pos2;i<p2.size();i++){
      |                  ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 572 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 4 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 8 ms 500 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 432 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 5 ms 608 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 5 ms 596 KB Output is correct
11 Correct 4 ms 600 KB Output is correct
12 Correct 4 ms 344 KB Output is correct
13 Correct 4 ms 600 KB Output is correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 10 ms 344 KB Output is correct
17 Correct 5 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 5 ms 600 KB Output is correct
20 Correct 5 ms 440 KB Output is correct
21 Correct 5 ms 344 KB Output is correct
22 Correct 5 ms 600 KB Output is correct
23 Correct 6 ms 344 KB Output is correct
24 Correct 5 ms 692 KB Output is correct
25 Correct 7 ms 344 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 9 ms 344 KB Output is correct
29 Correct 8 ms 344 KB Output is correct
30 Correct 5 ms 344 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 6 ms 344 KB Output is correct
33 Correct 5 ms 436 KB Output is correct
34 Correct 6 ms 432 KB Output is correct
35 Correct 6 ms 344 KB Output is correct
36 Correct 5 ms 344 KB Output is correct
37 Correct 6 ms 344 KB Output is correct
38 Correct 6 ms 344 KB Output is correct
39 Correct 6 ms 344 KB Output is correct
40 Correct 6 ms 600 KB Output is correct
41 Correct 6 ms 344 KB Output is correct
42 Correct 6 ms 344 KB Output is correct
43 Correct 6 ms 344 KB Output is correct
44 Correct 6 ms 344 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 13 ms 596 KB Output is correct
47 Correct 10 ms 344 KB Output is correct
48 Correct 11 ms 344 KB Output is correct
49 Correct 7 ms 344 KB Output is correct
50 Correct 6 ms 436 KB Output is correct
51 Correct 7 ms 440 KB Output is correct
52 Correct 7 ms 432 KB Output is correct
53 Correct 6 ms 600 KB Output is correct
54 Correct 7 ms 600 KB Output is correct
55 Correct 6 ms 600 KB Output is correct
56 Correct 5 ms 1208 KB Output is correct
57 Correct 6 ms 1116 KB Output is correct
58 Correct 7 ms 700 KB Output is correct
59 Correct 8 ms 864 KB Output is correct
60 Correct 7 ms 612 KB Output is correct
61 Correct 7 ms 612 KB Output is correct
62 Correct 6 ms 608 KB Output is correct
63 Correct 8 ms 608 KB Output is correct
64 Correct 7 ms 700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 5 ms 344 KB Output is correct
11 Correct 4 ms 344 KB Output is correct
12 Correct 5 ms 344 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 6 ms 344 KB Output is correct
17 Correct 5 ms 344 KB Output is correct
18 Correct 6 ms 344 KB Output is correct
19 Correct 6 ms 344 KB Output is correct
20 Correct 5 ms 344 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 8 ms 344 KB Output is correct
23 Correct 8 ms 344 KB Output is correct
24 Correct 7 ms 596 KB Output is correct
25 Correct 6 ms 344 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 6 ms 344 KB Output is correct
29 Correct 6 ms 436 KB Output is correct
30 Correct 6 ms 596 KB Output is correct
31 Correct 6 ms 344 KB Output is correct
32 Correct 11 ms 344 KB Output is correct
33 Correct 9 ms 344 KB Output is correct
34 Correct 8 ms 344 KB Output is correct
35 Correct 7 ms 344 KB Output is correct
36 Correct 9 ms 344 KB Output is correct
37 Correct 6 ms 432 KB Output is correct
38 Correct 7 ms 432 KB Output is correct
39 Correct 7 ms 436 KB Output is correct
40 Correct 6 ms 856 KB Output is correct
41 Correct 6 ms 600 KB Output is correct
42 Correct 7 ms 600 KB Output is correct
43 Correct 6 ms 344 KB Output is correct
44 Correct 5 ms 600 KB Output is correct
45 Correct 6 ms 344 KB Output is correct
46 Correct 5 ms 444 KB Output is correct
47 Correct 6 ms 344 KB Output is correct
48 Correct 6 ms 344 KB Output is correct
49 Correct 7 ms 340 KB Output is correct
50 Correct 6 ms 344 KB Output is correct
51 Correct 6 ms 344 KB Output is correct
52 Correct 6 ms 344 KB Output is correct
53 Correct 6 ms 436 KB Output is correct
54 Correct 6 ms 344 KB Output is correct
55 Correct 5 ms 344 KB Output is correct
56 Correct 6 ms 344 KB Output is correct
57 Correct 7 ms 344 KB Output is correct
58 Correct 6 ms 344 KB Output is correct
59 Correct 6 ms 344 KB Output is correct
60 Correct 6 ms 344 KB Output is correct
61 Correct 7 ms 600 KB Output is correct
62 Correct 6 ms 1204 KB Output is correct
63 Correct 7 ms 608 KB Output is correct
64 Correct 7 ms 708 KB Output is correct
65 Correct 7 ms 864 KB Output is correct
66 Correct 7 ms 696 KB Output is correct
67 Correct 6 ms 440 KB Output is correct
68 Correct 6 ms 700 KB Output is correct
69 Correct 6 ms 608 KB Output is correct
70 Correct 7 ms 444 KB Output is correct
71 Correct 7 ms 600 KB Output is correct
72 Correct 7 ms 452 KB Output is correct
73 Correct 7 ms 444 KB Output is correct
74 Correct 8 ms 868 KB Output is correct
75 Correct 7 ms 1112 KB Output is correct
76 Correct 7 ms 696 KB Output is correct
77 Correct 7 ms 868 KB Output is correct
78 Correct 7 ms 952 KB Output is correct
79 Correct 6 ms 608 KB Output is correct
80 Correct 6 ms 860 KB Output is correct
81 Correct 8 ms 696 KB Output is correct
82 Correct 7 ms 600 KB Output is correct