#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
vector<int>longest_trip_3(int N){
vector<int>ans;
for(int i=0;i<N;i++)ans.push_back(i);
return ans;
}
vector<int>longest_trip_2(int N){
vector<int>ans;
vector<int>tans;
tans.push_back(0);
for(int i=1;i<N;i++){
if(are_connected({i-1},{i}))tans.push_back(i);
else if(i!=N-1)tans.push_back(i+1),tans.push_back(i),i++;
else ans.push_back(i);
}
for(auto x:tans)ans.push_back(x);
return ans;
}
int vis[256];
int fnext(int x,int sz){
if(sz==0)return -1;
//cerr<<"fnext:"<<x<<" "<<sz<<"\n";
int st=1,en=sz,ans=0;
int id=-1;
vector<int>ask;
while(st<=en){
int m=(st+en)/2;
ask.clear();
int cur=0;
while(ask.size()<m){
if(!vis[cur])ask.push_back(cur);
cur++;
}
//cerr<<m<<" ask:";
//for(auto x:ask)cerr<<x<<" ";
//cerr<<"\n";
if(are_connected({x},ask)){
en=m-1;
ans=m;
id=cur-1;
}else{
st=m+1;
}
}
return id;
}
pair<int,int> fconnection(vector<int>comp1,vector<int>comp2){
int sz=comp1.size();
//cerr<<"fnext:"<<x<<" "<<sz<<"\n";
int st=0,en=sz-1;
int ans1=-1,ans2=-1;
int id1=-1;
vector<int>ask;
while(st<=en){
int m=(st+en)/2;
ask.clear();
for(int i=0;i<=m;i++)ask.push_back(comp1[i]);
if(are_connected(ask,comp2)){
en=m-1;
ans1=comp1[m];
id1=m;
}else{
st=m+1;
}
}
vector<int>temp;
for(int i=0;i<=id1;i++)temp.push_back(comp1[i]);
st=0,en=comp2.size()-1;
while(st<=en){
int m=(st+en)/2;
ask.clear();
for(int i=0;i<=m;i++)ask.push_back(comp2[i]);
if(are_connected(ask,temp)){
en=m-1;
ans2=comp2[m];
}else{
st=m+1;
}
}
return {ans1,ans2};
}
int vis2[256];
vector<int>longest_trip_1(int N){
for(int i=0;i<256;i++)vis[i]=0,vis2[i]=0;
vector<int>ans;
vector<int>rans;
vector<int>ians;
ans.push_back(0);
vis[0]=1;
int cur=0,left=N-1;
while(1){
//cerr<<cur<<"\n";
int x=fnext(cur,left);
if(x!=-1){
cur=x;
left--;
ans.push_back(cur);
vis[cur]++;
}else{
break;
}
}
for(int i=0;i<N;i++)if(!vis[i])ians.push_back(i);
if(ans.size()==N){
//assert(0);
return ans;
}
int outcom=-1;
for(int i=0;i<N;i++){
if(!vis[i]){
outcom=i;
break;
}
}
//auto x=fincom(outcom,ans);
int check=are_connected(ans,ians);
if(!check){
//assert(0);
return ans.size()>ians.size()?ans:ians;
}
vector<int>comp1,comp2;
for(int i=0;i<N;i++){
if(i!=outcom&&!are_connected({i},{outcom}))comp1.push_back(i);
else comp2.push_back(i);
}
auto connection=fconnection(comp1,comp2);
for(auto x:comp1)if(x!=connection.first)rans.push_back(x);
rans.push_back(connection.first);
rans.push_back(connection.second);
for(auto x:comp2)if(x!=connection.second)rans.push_back(x);
//for(int i=1;i<rans.size();i++)assert(are_connected({rans[i]},{rans[i-1]}));
return rans;
}
vector<int> longest_trip(int N, int D)
{
if(D==1)return longest_trip_1(N);
else if(D==2)return longest_trip_2(N);
else return longest_trip_3(N);
//cerr<<"end of query\n";
return {};
}
Compilation message
longesttrip.cpp: In function 'int fnext(int, int)':
longesttrip.cpp:32:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
32 | while(ask.size()<m){
| ~~~~~~~~~~^~
longesttrip.cpp:25:20: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
25 | int st=1,en=sz,ans=0;
| ^~~
longesttrip.cpp: In function 'std::vector<int> longest_trip_1(int)':
longesttrip.cpp:106:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
106 | if(ans.size()==N){
| ~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
11 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
6 ms |
344 KB |
Output is correct |
3 |
Correct |
5 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
6 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
344 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
7 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
5 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
344 KB |
Output is correct |
2 |
Correct |
14 ms |
344 KB |
Output is correct |
3 |
Correct |
19 ms |
344 KB |
Output is correct |
4 |
Correct |
28 ms |
344 KB |
Output is correct |
5 |
Correct |
40 ms |
704 KB |
Output is correct |
6 |
Correct |
10 ms |
344 KB |
Output is correct |
7 |
Incorrect |
2 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
344 KB |
Output is correct |
2 |
Correct |
10 ms |
344 KB |
Output is correct |
3 |
Correct |
20 ms |
344 KB |
Output is correct |
4 |
Partially correct |
32 ms |
440 KB |
Output is partially correct |
5 |
Partially correct |
51 ms |
700 KB |
Output is partially correct |
6 |
Correct |
12 ms |
344 KB |
Output is correct |
7 |
Incorrect |
2 ms |
344 KB |
Incorrect |
8 |
Halted |
0 ms |
0 KB |
- |