Submission #840764

# Submission time Handle Problem Language Result Execution time Memory
840764 2023-08-31T16:48:25 Z de_sousa Longest Trip (IOI23_longesttrip) C++17
85 / 100
20 ms 444 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

std::vector<int> longest_trip(int N, int D)
{
    std::random_device rd;
    std::mt19937 g(rd());
    
    vector<vector<int>> S;
    {
	
	vector<int> R;
	for(int i=0;i<N;i++)R.push_back(i);
	shuffle(R.begin(),R.end(),g);
    
	vector<int> C;
	for(int i=1;i<N;i++){
	    C.push_back(are_connected({R[i-1]},{R[i]}));
	}
	
	vector<int> wS(N,-1);
	S.push_back({R[0]});
	wS[R[0]]=0;
	if(C[0]){
	    wS[R[1]]=0;
	    S[0].push_back(R[1]);
	}
	else{
	    wS[R[1]]=1;
	    S.push_back({R[1]});
	}
	for(int i=2;i<N;i++){
	    if(C[i-1]){
		wS[R[i]]=wS[R[i-1]];
		S[wS[R[i]]].push_back(R[i]);
	    }
	    else if(!C[i-2]){
		wS[R[i]]=wS[R[i-2]];
		S[wS[R[i]]].push_back(R[i]);
	    }
	    else{
		wS[R[i]]=S.size();
		S.push_back({R[i]});
	    }
	}
    }
    
    
    while(S.size()>2){

	shuffle(S.begin(),S.end(),g);
	
	vector<int> R;
	for(int i=0;i<S.size();i++){
	    R.push_back(S[i].back());
	}
	
	vector<int> C;
	for(int i=1;i<R.size();i++){
	    C.push_back(are_connected({R[i-1]},{R[i]}));
	}

	vector<int> con(R.size(),-1);
	if(C[0]){
	    con[0]=1;
	    con[1]=-2;
	}
	for(int i=2;i<R.size();i++){
	    if(C[i-1]){
		if(con[i-1]==-1){
		    con[i-1]=i;
		    con[i]=-2;
		}
	    }
	    else if(!C[i-2]){
		if(con[i-2]==-1){
		    con[i-2]=i;
		    con[i]=-2;
		}
	    }
	}
	
	vector<vector<int>> nS;
	for(int i=0;i<S.size();i++){
	    if(con[i]==-2)continue;
	    if(con[i]==-1){
		nS.push_back(S[i]);
		continue;
	    }
	    while(S[con[i]].size()){
		S[i].push_back(S[con[i]].back());
		S[con[i]].pop_back();
	    }
	    nS.push_back(S[i]);
	}
	S=nS;
    }
    
    if(S.size()==1)return S[0];

    if(S[0].size()<S[1].size())swap(S[0],S[1]);
    
    if(!are_connected(S[0],S[1]))
	return S[0];

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

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


    vector<int> cand1=S[0];
    vector<int> cand2=S[1];

    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<S[0].size();i++){
	if(S[0][i]==cand1[0])pos1=i;
    }

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

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

    
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:55:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |  for(int i=0;i<S.size();i++){
      |              ~^~~~~~~~~
longesttrip.cpp:60:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |  for(int i=1;i<R.size();i++){
      |              ~^~~~~~~~~
longesttrip.cpp:69:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |  for(int i=2;i<R.size();i++){
      |              ~^~~~~~~~~
longesttrip.cpp:85:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |  for(int i=0;i<S.size();i++){
      |              ~^~~~~~~~~
longesttrip.cpp:133:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  133 |  while(tmp1.size()!=target){
      |        ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:146:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  146 |  while(tmp1.size()!=target){
      |        ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:156:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |     for(int i=0;i<S[0].size();i++){
      |                 ~^~~~~~~~~~~~
longesttrip.cpp:161:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  161 |     for(int i=0;i<S[1].size();i++){
      |                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 2 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 208 KB Output is correct
2 Correct 8 ms 208 KB Output is correct
3 Correct 6 ms 208 KB Output is correct
4 Correct 9 ms 312 KB Output is correct
5 Correct 4 ms 236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 208 KB Output is correct
2 Correct 7 ms 208 KB Output is correct
3 Correct 10 ms 208 KB Output is correct
4 Correct 8 ms 304 KB Output is correct
5 Correct 9 ms 208 KB Output is correct
6 Correct 13 ms 208 KB Output is correct
7 Correct 11 ms 208 KB Output is correct
8 Correct 10 ms 228 KB Output is correct
9 Correct 13 ms 208 KB Output is correct
10 Correct 6 ms 312 KB Output is correct
11 Correct 7 ms 312 KB Output is correct
12 Correct 10 ms 308 KB Output is correct
13 Correct 10 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 9 ms 208 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 8 ms 208 KB Output is correct
5 Correct 8 ms 208 KB Output is correct
6 Correct 17 ms 208 KB Output is correct
7 Correct 10 ms 208 KB Output is correct
8 Correct 11 ms 208 KB Output is correct
9 Correct 11 ms 340 KB Output is correct
10 Correct 6 ms 316 KB Output is correct
11 Correct 6 ms 312 KB Output is correct
12 Correct 11 ms 316 KB Output is correct
13 Correct 9 ms 316 KB Output is correct
14 Correct 18 ms 208 KB Output is correct
15 Correct 12 ms 208 KB Output is correct
16 Correct 8 ms 208 KB Output is correct
17 Correct 15 ms 208 KB Output is correct
18 Correct 7 ms 208 KB Output is correct
19 Correct 9 ms 316 KB Output is correct
20 Correct 10 ms 320 KB Output is correct
21 Correct 12 ms 324 KB Output is correct
22 Correct 12 ms 340 KB Output is correct
23 Correct 12 ms 324 KB Output is correct
24 Correct 10 ms 324 KB Output is correct
25 Correct 13 ms 208 KB Output is correct
26 Correct 17 ms 208 KB Output is correct
27 Correct 8 ms 240 KB Output is correct
28 Correct 10 ms 208 KB Output is correct
29 Correct 8 ms 208 KB Output is correct
30 Correct 11 ms 208 KB Output is correct
31 Correct 6 ms 308 KB Output is correct
32 Correct 14 ms 208 KB Output is correct
33 Correct 10 ms 208 KB Output is correct
34 Correct 5 ms 208 KB Output is correct
35 Correct 11 ms 208 KB Output is correct
36 Correct 7 ms 208 KB Output is correct
37 Correct 8 ms 312 KB Output is correct
38 Correct 13 ms 336 KB Output is correct
39 Correct 9 ms 328 KB Output is correct
40 Correct 15 ms 316 KB Output is correct
41 Correct 11 ms 328 KB Output is correct
42 Correct 14 ms 208 KB Output is correct
43 Correct 14 ms 336 KB Output is correct
44 Correct 13 ms 328 KB Output is correct
45 Correct 13 ms 208 KB Output is correct
46 Correct 20 ms 208 KB Output is correct
47 Correct 10 ms 208 KB Output is correct
48 Correct 14 ms 208 KB Output is correct
49 Correct 14 ms 208 KB Output is correct
50 Correct 11 ms 208 KB Output is correct
51 Correct 15 ms 208 KB Output is correct
52 Correct 16 ms 208 KB Output is correct
53 Correct 7 ms 228 KB Output is correct
54 Correct 10 ms 208 KB Output is correct
55 Correct 11 ms 312 KB Output is correct
56 Correct 7 ms 208 KB Output is correct
57 Correct 9 ms 208 KB Output is correct
58 Correct 12 ms 316 KB Output is correct
59 Correct 11 ms 324 KB Output is correct
60 Correct 10 ms 444 KB Output is correct
61 Correct 15 ms 312 KB Output is correct
62 Correct 17 ms 328 KB Output is correct
63 Correct 12 ms 308 KB Output is correct
64 Correct 11 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 208 KB Output is correct
2 Correct 10 ms 208 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 6 ms 304 KB Output is correct
5 Correct 10 ms 208 KB Output is correct
6 Correct 16 ms 208 KB Output is correct
7 Correct 12 ms 208 KB Output is correct
8 Correct 8 ms 208 KB Output is correct
9 Correct 8 ms 208 KB Output is correct
10 Correct 7 ms 316 KB Output is correct
11 Correct 10 ms 332 KB Output is correct
12 Correct 5 ms 320 KB Output is correct
13 Correct 5 ms 316 KB Output is correct
14 Correct 11 ms 208 KB Output is correct
15 Correct 15 ms 208 KB Output is correct
16 Correct 11 ms 308 KB Output is correct
17 Correct 16 ms 304 KB Output is correct
18 Correct 13 ms 208 KB Output is correct
19 Correct 13 ms 208 KB Output is correct
20 Correct 11 ms 320 KB Output is correct
21 Correct 15 ms 208 KB Output is correct
22 Correct 16 ms 208 KB Output is correct
23 Correct 9 ms 220 KB Output is correct
24 Correct 7 ms 208 KB Output is correct
25 Correct 6 ms 208 KB Output is correct
26 Correct 8 ms 208 KB Output is correct
27 Correct 14 ms 312 KB Output is correct
28 Correct 7 ms 208 KB Output is correct
29 Correct 9 ms 208 KB Output is correct
30 Correct 6 ms 208 KB Output is correct
31 Correct 7 ms 308 KB Output is correct
32 Correct 12 ms 208 KB Output is correct
33 Correct 11 ms 208 KB Output is correct
34 Correct 14 ms 208 KB Output is correct
35 Correct 14 ms 208 KB Output is correct
36 Correct 13 ms 208 KB Output is correct
37 Correct 10 ms 208 KB Output is correct
38 Correct 8 ms 316 KB Output is correct
39 Correct 15 ms 208 KB Output is correct
40 Correct 7 ms 208 KB Output is correct
41 Correct 8 ms 208 KB Output is correct
42 Correct 10 ms 208 KB Output is correct
43 Correct 9 ms 316 KB Output is correct
44 Correct 12 ms 308 KB Output is correct
45 Correct 11 ms 328 KB Output is correct
46 Correct 12 ms 332 KB Output is correct
47 Correct 10 ms 208 KB Output is correct
48 Correct 7 ms 312 KB Output is correct
49 Correct 5 ms 348 KB Output is correct
50 Correct 13 ms 312 KB Output is correct
51 Partially correct 13 ms 316 KB Output is partially correct
52 Partially correct 15 ms 332 KB Output is partially correct
53 Partially correct 13 ms 312 KB Output is partially correct
54 Partially correct 17 ms 320 KB Output is partially correct
55 Partially correct 16 ms 324 KB Output is partially correct
56 Correct 10 ms 320 KB Output is correct
57 Correct 13 ms 208 KB Output is correct
58 Correct 12 ms 332 KB Output is correct
59 Partially correct 10 ms 312 KB Output is partially correct
60 Partially correct 11 ms 312 KB Output is partially correct
61 Partially correct 12 ms 332 KB Output is partially correct
62 Correct 11 ms 208 KB Output is correct
63 Correct 8 ms 208 KB Output is correct
64 Correct 12 ms 332 KB Output is correct
65 Partially correct 10 ms 332 KB Output is partially correct
66 Partially correct 9 ms 328 KB Output is partially correct
67 Partially correct 15 ms 208 KB Output is partially correct
68 Partially correct 11 ms 336 KB Output is partially correct
69 Partially correct 19 ms 336 KB Output is partially correct
70 Partially correct 11 ms 328 KB Output is partially correct
71 Correct 8 ms 208 KB Output is correct
72 Correct 12 ms 324 KB Output is correct
73 Partially correct 13 ms 324 KB Output is partially correct
74 Partially correct 13 ms 328 KB Output is partially correct
75 Partially correct 10 ms 208 KB Output is partially correct
76 Partially correct 13 ms 316 KB Output is partially correct
77 Correct 7 ms 308 KB Output is correct
78 Correct 11 ms 208 KB Output is correct
79 Partially correct 15 ms 312 KB Output is partially correct
80 Partially correct 15 ms 320 KB Output is partially correct
81 Partially correct 15 ms 356 KB Output is partially correct
82 Partially correct 15 ms 208 KB Output is partially correct