Submission #840764

#TimeUsernameProblemLanguageResultExecution timeMemory
840764de_sousaLongest Trip (IOI23_longesttrip)C++17
85 / 100
20 ms444 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...