Submission #840756

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

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