답안 #840677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840677 2023-08-31T15:37:58 Z de_sousa 가장 긴 여행 (IOI23_longesttrip) C++17
85 / 100
18 ms 424 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

std::vector<int> longest_trip(int N, int D)
{
    vector<int> S1;
    vector<int> S2;
    S1.push_back(0);
    S2.push_back(1);
    for(int i=2;i<N;i++){
	if(rand()%2)swap(S1,S2);
	if(are_connected({S1.back()},{i})){
	    S1.push_back(i);
	}
	else if(are_connected({S2.back()},{i})){
	    S2.push_back(i);
	}
	else{
	    while(S2.size()){
		S1.push_back(S2.back());
		S2.pop_back();
	    }
	    S2.push_back(i);
	}
    }
    
    if(S1.size()<S2.size())swap(S1,S2);
    
    if(!are_connected(S1,S2))
	return S1;

    if(!are_connected({S1[0]},{S1.back()})){
	if(!are_connected({S1.back()},{S2.back()}))reverse(S1.begin(),S1.end());
	while(S2.size()){
	    S1.push_back(S2.back());
	    S2.pop_back();
	}
	return S1;
    }

    if(S2.size()!=1&&!are_connected({S2[0]},{S2.back()})){
	if(!are_connected({S2.back()},{S1.back()}))reverse(S2.begin(),S2.end());
	while(S1.size()){
	    S2.push_back(S1.back());
	    S1.pop_back();
	}
	return S2;
    }


    vector<int> cand1=S1;
    vector<int> cand2=S2;

    while(cand1.size()!=1){
	vector<int> tmp1=cand1;
	vector<int> tmp2;
	int target=tmp1.size()/2;
	while(tmp1.size()!=target){
	    tmp2.push_back(tmp1.back());
	    tmp1.pop_back();
	}
	if(are_connected(tmp1,cand2))cand1=tmp1;
	else cand1=tmp2;	
    }
    swap(cand1,cand2);
    
    while(cand1.size()!=1){
	vector<int> tmp1=cand1;
	vector<int> tmp2;
	int target=tmp1.size()/2;
	while(tmp1.size()!=target){
	    tmp2.push_back(tmp1.back());
	    tmp1.pop_back();
	}
	if(are_connected(tmp1,cand2))cand1=tmp1;
	else cand1=tmp2;	
    }
    swap(cand1,cand2);

    int pos1=0;
    for(int i=0;i<S1.size();i++){
	if(S1[i]==cand1[0])pos1=i;
    }

    int pos2=0;
    for(int i=0;i<S2.size();i++){
	if(S2[i]==cand2[0])pos2=i;
    }

    vector<int> S3;
    S3.push_back(S1[pos1]);
    for(int i=(pos1+1)%S1.size();i!=pos1;i=(i+1)%S1.size())S3.push_back(S1[i]);
    reverse(S3.begin(),S3.end());
    S3.push_back(S2[pos2]);
    for(int i=(pos2+1)%S2.size();i!=pos2;i=(i+1)%S2.size())S3.push_back(S2[i]);
    return S3;
    
    
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:59:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   59 |  while(tmp1.size()!=target){
      |        ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:72:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   72 |  while(tmp1.size()!=target){
      |        ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:82:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for(int i=0;i<S1.size();i++){
      |                 ~^~~~~~~~~~
longesttrip.cpp:87:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     for(int i=0;i<S2.size();i++){
      |                 ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 2 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 208 KB Output is correct
2 Correct 11 ms 208 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 9 ms 296 KB Output is correct
5 Correct 8 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 260 KB Output is correct
2 Correct 10 ms 208 KB Output is correct
3 Correct 5 ms 292 KB Output is correct
4 Correct 14 ms 256 KB Output is correct
5 Correct 10 ms 208 KB Output is correct
6 Correct 12 ms 208 KB Output is correct
7 Correct 7 ms 208 KB Output is correct
8 Correct 6 ms 208 KB Output is correct
9 Correct 11 ms 208 KB Output is correct
10 Correct 6 ms 208 KB Output is correct
11 Correct 8 ms 304 KB Output is correct
12 Correct 6 ms 300 KB Output is correct
13 Correct 11 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 208 KB Output is correct
2 Correct 11 ms 208 KB Output is correct
3 Correct 13 ms 336 KB Output is correct
4 Correct 8 ms 300 KB Output is correct
5 Correct 7 ms 308 KB Output is correct
6 Correct 15 ms 208 KB Output is correct
7 Correct 14 ms 208 KB Output is correct
8 Correct 9 ms 208 KB Output is correct
9 Correct 10 ms 208 KB Output is correct
10 Correct 5 ms 308 KB Output is correct
11 Correct 9 ms 208 KB Output is correct
12 Correct 10 ms 308 KB Output is correct
13 Correct 8 ms 208 KB Output is correct
14 Correct 7 ms 208 KB Output is correct
15 Correct 13 ms 208 KB Output is correct
16 Correct 15 ms 208 KB Output is correct
17 Correct 9 ms 208 KB Output is correct
18 Correct 12 ms 208 KB Output is correct
19 Correct 11 ms 208 KB Output is correct
20 Correct 12 ms 208 KB Output is correct
21 Correct 11 ms 208 KB Output is correct
22 Correct 10 ms 300 KB Output is correct
23 Correct 6 ms 304 KB Output is correct
24 Correct 8 ms 208 KB Output is correct
25 Correct 11 ms 208 KB Output is correct
26 Correct 8 ms 208 KB Output is correct
27 Correct 7 ms 208 KB Output is correct
28 Correct 12 ms 208 KB Output is correct
29 Correct 11 ms 256 KB Output is correct
30 Correct 10 ms 208 KB Output is correct
31 Correct 14 ms 208 KB Output is correct
32 Correct 11 ms 208 KB Output is correct
33 Correct 13 ms 296 KB Output is correct
34 Correct 12 ms 208 KB Output is correct
35 Correct 14 ms 260 KB Output is correct
36 Correct 10 ms 208 KB Output is correct
37 Correct 13 ms 208 KB Output is correct
38 Correct 10 ms 208 KB Output is correct
39 Correct 10 ms 208 KB Output is correct
40 Correct 12 ms 208 KB Output is correct
41 Correct 13 ms 332 KB Output is correct
42 Correct 8 ms 208 KB Output is correct
43 Correct 15 ms 208 KB Output is correct
44 Correct 13 ms 208 KB Output is correct
45 Correct 13 ms 208 KB Output is correct
46 Correct 17 ms 208 KB Output is correct
47 Correct 15 ms 208 KB Output is correct
48 Correct 9 ms 208 KB Output is correct
49 Correct 14 ms 208 KB Output is correct
50 Correct 12 ms 208 KB Output is correct
51 Correct 11 ms 208 KB Output is correct
52 Correct 12 ms 208 KB Output is correct
53 Correct 12 ms 208 KB Output is correct
54 Correct 8 ms 332 KB Output is correct
55 Correct 13 ms 208 KB Output is correct
56 Correct 7 ms 208 KB Output is correct
57 Correct 14 ms 300 KB Output is correct
58 Correct 14 ms 208 KB Output is correct
59 Correct 13 ms 208 KB Output is correct
60 Correct 9 ms 208 KB Output is correct
61 Correct 11 ms 308 KB Output is correct
62 Correct 15 ms 304 KB Output is correct
63 Correct 8 ms 208 KB Output is correct
64 Correct 12 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 208 KB Output is correct
2 Correct 11 ms 260 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 9 ms 424 KB Output is correct
5 Correct 8 ms 336 KB Output is correct
6 Correct 15 ms 208 KB Output is correct
7 Correct 15 ms 208 KB Output is correct
8 Correct 8 ms 208 KB Output is correct
9 Correct 10 ms 208 KB Output is correct
10 Correct 10 ms 208 KB Output is correct
11 Correct 5 ms 304 KB Output is correct
12 Correct 7 ms 304 KB Output is correct
13 Correct 10 ms 208 KB Output is correct
14 Correct 9 ms 208 KB Output is correct
15 Correct 10 ms 208 KB Output is correct
16 Correct 13 ms 208 KB Output is correct
17 Correct 10 ms 208 KB Output is correct
18 Correct 10 ms 208 KB Output is correct
19 Correct 13 ms 300 KB Output is correct
20 Correct 10 ms 300 KB Output is correct
21 Correct 8 ms 208 KB Output is correct
22 Correct 12 ms 208 KB Output is correct
23 Correct 11 ms 208 KB Output is correct
24 Correct 11 ms 208 KB Output is correct
25 Correct 11 ms 208 KB Output is correct
26 Correct 12 ms 208 KB Output is correct
27 Correct 14 ms 208 KB Output is correct
28 Correct 11 ms 208 KB Output is correct
29 Correct 12 ms 208 KB Output is correct
30 Correct 12 ms 208 KB Output is correct
31 Correct 9 ms 208 KB Output is correct
32 Correct 13 ms 208 KB Output is correct
33 Correct 18 ms 208 KB Output is correct
34 Correct 9 ms 208 KB Output is correct
35 Correct 10 ms 208 KB Output is correct
36 Correct 15 ms 208 KB Output is correct
37 Correct 8 ms 208 KB Output is correct
38 Correct 16 ms 208 KB Output is correct
39 Correct 8 ms 208 KB Output is correct
40 Correct 11 ms 208 KB Output is correct
41 Correct 14 ms 208 KB Output is correct
42 Correct 14 ms 208 KB Output is correct
43 Correct 7 ms 208 KB Output is correct
44 Correct 10 ms 208 KB Output is correct
45 Correct 9 ms 304 KB Output is correct
46 Correct 11 ms 208 KB Output is correct
47 Correct 12 ms 332 KB Output is correct
48 Correct 7 ms 208 KB Output is correct
49 Correct 6 ms 332 KB Output is correct
50 Correct 14 ms 208 KB Output is correct
51 Correct 12 ms 208 KB Output is correct
52 Correct 8 ms 208 KB Output is correct
53 Correct 11 ms 208 KB Output is correct
54 Correct 7 ms 336 KB Output is correct
55 Correct 14 ms 208 KB Output is correct
56 Correct 13 ms 208 KB Output is correct
57 Correct 8 ms 208 KB Output is correct
58 Correct 11 ms 208 KB Output is correct
59 Correct 10 ms 300 KB Output is correct
60 Correct 8 ms 292 KB Output is correct
61 Correct 13 ms 208 KB Output is correct
62 Correct 10 ms 208 KB Output is correct
63 Partially correct 14 ms 208 KB Output is partially correct
64 Partially correct 11 ms 208 KB Output is partially correct
65 Partially correct 14 ms 208 KB Output is partially correct
66 Partially correct 14 ms 308 KB Output is partially correct
67 Partially correct 10 ms 208 KB Output is partially correct
68 Partially correct 11 ms 208 KB Output is partially correct
69 Correct 13 ms 208 KB Output is correct
70 Partially correct 14 ms 308 KB Output is partially correct
71 Partially correct 12 ms 208 KB Output is partially correct
72 Partially correct 15 ms 208 KB Output is partially correct
73 Correct 13 ms 208 KB Output is correct
74 Partially correct 11 ms 300 KB Output is partially correct
75 Correct 9 ms 208 KB Output is correct
76 Partially correct 15 ms 208 KB Output is partially correct
77 Correct 13 ms 208 KB Output is correct
78 Partially correct 10 ms 208 KB Output is partially correct
79 Partially correct 14 ms 296 KB Output is partially correct
80 Partially correct 13 ms 304 KB Output is partially correct
81 Partially correct 11 ms 208 KB Output is partially correct
82 Partially correct 11 ms 208 KB Output is partially correct