답안 #840752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840752 2023-08-31T16:43:08 Z de_sousa 가장 긴 여행 (IOI23_longesttrip) C++17
5 / 100
16 ms 332 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;
	    }
	    reverse(S[con[i]].begin(),S[con[i]].end());
	    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:138:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  138 |  while(tmp1.size()!=target){
      |        ~~~~~~~~~~~^~~~~~~~
longesttrip.cpp:151:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  151 |  while(tmp1.size()!=target){
      |        ~~~~~~~~~~~^~~~~~~~
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[0].size();i++){
      |                 ~^~~~~~~~~~~~
longesttrip.cpp:166:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |     for(int i=0;i<S[1].size();i++){
      |                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 208 KB Output is correct
2 Incorrect 3 ms 208 KB Incorrect
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 208 KB Output is correct
2 Correct 8 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 6 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 208 KB Output is correct
2 Correct 8 ms 208 KB Output is correct
3 Correct 9 ms 208 KB Output is correct
4 Correct 9 ms 208 KB Output is correct
5 Correct 6 ms 208 KB Output is correct
6 Correct 16 ms 208 KB Output is correct
7 Incorrect 1 ms 208 KB Incorrect
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 332 KB Output is correct
2 Correct 9 ms 208 KB Output is correct
3 Correct 8 ms 208 KB Output is correct
4 Correct 10 ms 208 KB Output is correct
5 Correct 7 ms 208 KB Output is correct
6 Correct 12 ms 208 KB Output is correct
7 Incorrect 0 ms 208 KB Incorrect
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 208 KB Output is correct
2 Correct 7 ms 208 KB Output is correct
3 Correct 7 ms 208 KB Output is correct
4 Correct 9 ms 208 KB Output is correct
5 Correct 12 ms 208 KB Output is correct
6 Correct 15 ms 208 KB Output is correct
7 Incorrect 1 ms 208 KB Incorrect
8 Halted 0 ms 0 KB -