QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#164152 | #7119. Longest Trip | QAQAutoMaton | 5 | 16ms | 4140kb | C++14 | 3.8kb | 2023-09-04 20:24:11 | 2023-09-04 20:24:11 |
Judging History
answer
#include "longesttrip.h"
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
std::mt19937 rnd(123456789);
int g[305][305];
vector<int> ans;
void build2(vector<int> &node){
int x=0,y=0;
for(auto i:node)for(auto j:node)if(i!=j && !g[i][j]){
x=i;y=j;
break;
}
if(!x){
for(auto i:node)ans.emplace_back(i);
return;
}
for(int i=node.size()-1;~i;--i)if(node[i]==x || node[i]==y){
swap(node[i],node.back());
node.pop_back();
}
if(node.size()<=2){
if(node.size()==1){
ans.emplace_back(x);ans.emplace_back(node[0]);ans.emplace_back(y);
}
else{
ans.emplace_back(x);ans.emplace_back(node[0]);ans.emplace_back(y);ans.emplace_back(node[1]);
}
}
else{
build2(node);
ans.emplace(ans.begin(),x);
ans.emplace_back(y);
}
}
vector<int> c1,c2;
bool G(int x,int y){
if(g[x][y]!=-1)return g[x][y];
else return g[x][y]=g[y][x]=are_connected({x-1},{y-1});
}
void shift(vector<int> &a,int x){
vector<int> b(0);
// let x be the 1st element of a
int oc=0;
for(auto i:a){
if(i==x)oc=1;
if(oc)b.emplace_back(i);
}
for(auto i:a){
if(i==x)break;
b.emplace_back(i);
}
a.swap(b);
}
bool find_any(vector<int> a,vector<int> b,int &ax,int &ay){
for(auto &i:a)--i;
for(auto &i:b)--i;
if(!are_connected(a,b)){ax=ay=0;return 0;}
int l=0,r=b.size()-1;
while(l<r){
int mid=(l+r)>>1;
if(are_connected(a,vector<int>(b.begin()+l,b.begin()+mid+1)))r=mid;
else l=mid+1;
}
int x=b[l];
l=0,r=a.size()-1;
while(l<r){
int mid=(l+r)>>1;
if(are_connected(vector<int>(a.begin()+l,a.begin()+mid+1),{x}))r=mid;
else l=mid+1;
}
ax=a[l]+1;ay=x+1;
return 1;
}
std::vector<int> vec;
std::vector<int> v3({0,1,2});
std::vector<int> longest_trip(int n, int d)
{
ans.clear();
if(d==3){
for(int i=0;i<n;++i)ans.emplace_back(i);
return ans;
}
for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)g[i][j]=-1;
c1.clear();c2.clear();
vec.clear();
for(int i=2;i<=n;++i)vec.emplace_back(i);
shuffle(vec.begin(),vec.end(),rnd);
c1.emplace_back(vec.back());
vec.pop_back();
c2.emplace_back(vec.back());
vec.pop_back();
for(auto i:vec){
shuffle(v3.begin(),v3.end(),rnd);
for(int j=0;j<3;++j){
bool chk=0;
if(j!=2){
if(v3[j]==0)chk=G(c1.back(),i);
else if(v3[j]==1)chk=G(c2.back(),i);
else chk=G(c1.back(),c2.back());
}
else chk=1;
if(chk){
if(v3[j]==0)c1.emplace_back(i);
else if(v3[j]==1)c2.emplace_back(i);
else{
while(!c2.empty()){c1.emplace_back(c2.back());c2.pop_back();}
c2.emplace_back(i);
}
}
}
}
int v11=G(1,c1[0]),v12=G(1,c1.back());
int v21=G(1,c2[0]),v22=G(1,c2.back());
if((v11||v12) && (v21||v22)){
if(v11){
reverse(c1.begin(),c1.end());
}
if(v22){
reverse(c2.begin(),c2.end());
}
for(auto i:c1)ans.emplace_back(i);
ans.emplace_back(1);
for(auto i:c2)ans.emplace_back(i);
}
else{
if(!(v11+v12+v21+v22)){
for(auto i:c2)c1.emplace_back(i);
int ax=0,ay=0;
find_any({c1},{1},ax,ay);
if(!ax)ans=c1;
else{
shift(c1,ax);
c1.emplace(c1.begin(),1);
ans=c1;
}
}
else{
if(v11+v12){
swap(c1,c2);
swap(v11,v21);swap(v12,v22);
}
if(v22){
reverse(c2.begin(),c2.end());
swap(v21,v22);
// 能联通的放到begin
}
if(!v22){
ans.emplace_back(1);
for(auto i:c2)ans.emplace_back(i);
for(auto i:c1)ans.emplace_back(i);
goto END;
}
c2.emplace_back(1);
int ax=0,ay=0;
find_any(c2,c1,ax,ay);
if(!ax){
if(c1.size()>c2.size()){
ans=c1;
}
else ans=c2;
}
else{
shift(c1,ay);
shift(c2,ax);
reverse(c1.begin(),c1.end());
for(auto i:c1)ans.emplace_back(i);
for(auto i:c2)ans.emplace_back(i);
}
}
}
END:;
for(auto &i:ans){i-=1;}
return ans;
}
詳細信息
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 8ms
memory: 4140kb
input:
341 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 3 1 3 ...
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 0 1 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 0 1 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 0 1 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 0 1 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 0 1 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 0 1 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 0 1 2...
result:
ok
Test #2:
score: 0
Accepted
time: 2ms
memory: 3904kb
input:
103 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10 3 1 10...
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 10 0 1 2 3 4 5 6 7 8 9 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 10 0 1 2 3 4 5 6 7 8 9 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 10 0 1 2 3 4 5 6 7 8 9 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 10 0 1 2 3 4 5 6 7 8 9 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 10 0 1 2 3 4 5 6 7 8 9 3kC2Ia2048...
result:
ok
Test #3:
score: 0
Accepted
time: 1ms
memory: 3860kb
input:
22 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 50 3 1 12 3 1 12 3 1
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3...
result:
ok
Test #4:
score: 0
Accepted
time: 1ms
memory: 3848kb
input:
8 128 3 1 128 3 1 128 3 1 128 3 1 128 3 1 128 3 1 128 3 1 128 3 1
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 128 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 9...
result:
ok
Test #5:
score: 0
Accepted
time: 0ms
memory: 4132kb
input:
4 256 3 1 256 3 1 256 3 1 256 3 1
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 256 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 9...
result:
ok
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 10
Accepted
time: 2ms
memory: 3836kb
input:
341 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 1 1 3 2 1 ...
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 2 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 2 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2...
result:
ok
Test #7:
score: -10
Wrong Answer
time: 1ms
memory: 3856kb
input:
103 10 2 1
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 8 3 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 9 9
result:
wrong answer non-disjoint arrays
Subtask #3:
score: 0
Wrong Answer
Test #19:
score: 25
Accepted
time: 1ms
memory: 3844kb
input:
341 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 ...
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 2 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 2 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2...
result:
ok
Test #20:
score: -25
Wrong Answer
time: 1ms
memory: 3932kb
input:
103 10 1 1
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 8 3 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 9 9
result:
wrong answer non-disjoint arrays
Subtask #4:
score: 0
Wrong Answer
Test #83:
score: 60
Accepted
time: 16ms
memory: 3840kb
input:
341 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 ...
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 2 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 1 3 2 0 1 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 0 2...
result:
ok
Test #84:
score: 0
Wrong Answer
time: 1ms
memory: 3808kb
input:
103 10 1 1
output:
3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 8 3 3kC2Ia2048BfyJVGojMUKKtilctlZKcB 0 1 1 9 9
result:
wrong answer non-disjoint arrays