Submission #842737

# Submission time Handle Problem Language Result Execution time Memory
842737 2023-09-03T10:27:33 Z errorgorn Longest Trip (IOI23_longesttrip) C++17
85 / 100
13 ms 1224 KB
#include "longesttrip.h"
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define fi first
#define se second
#define endl '\n'

#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound

#define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

#define G are_connected

int n;
vector<vector<signed> > v;

vector<signed> rev(vector<signed> v){
	reverse(all(v));
	return v;
}

vector<signed> concat(vector<signed> a,vector<signed> b){
	a.insert(a.end(),all(b));
	return a;
}

vector<signed> longest_trip(signed N, signed D){
    n=N;
    v.clear();
    
    rep(x,0,n) v.pub({x});
    
    while (sz(v)>=3){
		swap(v.back(),v[rng()%sz(v)]); vector<signed> a=v.back(); v.pob();
		swap(v.back(),v[rng()%sz(v)]); vector<signed> b=v.back(); v.pob();
		swap(v.back(),v[rng()%sz(v)]); vector<signed> c=v.back(); v.pob();
		
		if (rng()%2==0) swap(b,a);
		if (rng()%3==0) swap(c,a);
		else if (rng()%2==0) swap(c,b);
		
		if (G({a.back()},{b.back()})){
			swap(a,c);
			v.pub(a);
			v.pub(concat(b,rev(c)));
			continue;
		}
		
		if (G({a.back()},{c.back()})){
			swap(a,b);
		}
		
		vector<signed> d;
		if (!v.empty()){
			swap(v.back(),v[rng()%sz(v)]); d=v.back();
		}
		
		if (!d.empty() && G({a.back()},{d.back()})){
			v.pob();
			v.pub(concat(a,rev(d)));
			v.pub(concat(b,rev(c)));
		}
		else{
			v.pub(a);
			v.pub(concat(b,rev(c)));
		}
	}
	
	//cout<<"2 cyc"<<endl;
	//for (auto it:v[0]) cout<<it<<" "; cout<<endl;
	//for (auto it:v[1]) cout<<it<<" "; cout<<endl;
	
	if ((sz(v[0])==1 || G({v[0].front()},{v[0].back()})) && (sz(v[1])==1 || G({v[1].front()},{v[1].back()}))){
		vector<signed> l=v[0],r=v[1];
		
		if (!G(v[0],v[1])) return (sz(v[0])>sz(v[1])?v[0]:v[1]);
		
		while (sz(l)>1){
			vector<signed> a,b;
			rep(x,0,sz(l)/2) a.pub(l[x]);
			rep(x,sz(l)/2,sz(l)) b.pub(l[x]);
			
			if (G(a,r)) l=a;
			else l=b;
		}
		while (sz(r)>1){
			vector<signed> a,b;
			rep(x,0,sz(r)/2) a.pub(r[x]);
			rep(x,sz(r)/2,sz(r)) b.pub(r[x]);
			
			if (G(l,a)) r=a;
			else r=b;
		}
		
		int idxl,idxr;
		rep(x,0,sz(v[0])) if (v[0][x]==l[0]) idxl=x;
		rep(x,0,sz(v[1])) if (v[1][x]==r[0]) idxr=x;
		
		vector<signed> res;
		rep(x,0,sz(v[0])) res.pub(v[0][(idxl+1+x)%sz(v[0])]);
		rep(x,0,sz(v[1])) res.pub(v[1][(idxr+x)%sz(v[1])]);
		return res;
	}
	else{
		if (G({v[0].front()},{v[1].front()})) return concat(rev(v[0]),v[1]);
		if (G({v[0].front()},{v[1].back()})) return concat(rev(v[0]),rev(v[1]));
		if (G({v[0].back()},{v[1].front()})) return concat(v[0],v[1]);
		if (G({v[0].back()},{v[1].back()})) return concat(v[0],rev(v[1]));
		
		return (sz(v[0])>sz(v[1])?v[0]:v[1]);
	}
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:44:23: warning: narrowing conversion of 'x' from 'long long int' to 'int' [-Wnarrowing]
   44 |     rep(x,0,n) v.pub({x});
      |                       ^
longesttrip.cpp:44:23: warning: narrowing conversion of 'x' from 'long long int' to 'int' [-Wnarrowing]
longesttrip.cpp:114:39: warning: 'idxr' may be used uninitialized in this function [-Wmaybe-uninitialized]
  114 |   rep(x,0,sz(v[1])) res.pub(v[1][(idxr+x)%sz(v[1])]);
      |                                  ~~~~~^~~
longesttrip.cpp:113:39: warning: 'idxl' may be used uninitialized in this function [-Wmaybe-uninitialized]
  113 |   rep(x,0,sz(v[0])) res.pub(v[0][(idxl+1+x)%sz(v[0])]);
      |                                   ~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 344 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 6 ms 600 KB Output is correct
4 Correct 6 ms 712 KB Output is correct
5 Correct 6 ms 480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 6 ms 596 KB Output is correct
4 Correct 5 ms 720 KB Output is correct
5 Correct 6 ms 980 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 9 ms 344 KB Output is correct
8 Correct 6 ms 604 KB Output is correct
9 Correct 6 ms 960 KB Output is correct
10 Correct 5 ms 984 KB Output is correct
11 Correct 6 ms 736 KB Output is correct
12 Correct 6 ms 732 KB Output is correct
13 Correct 7 ms 984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 340 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 6 ms 856 KB Output is correct
4 Correct 5 ms 456 KB Output is correct
5 Correct 6 ms 868 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 8 ms 344 KB Output is correct
8 Correct 6 ms 600 KB Output is correct
9 Correct 5 ms 600 KB Output is correct
10 Correct 6 ms 728 KB Output is correct
11 Correct 6 ms 724 KB Output is correct
12 Correct 5 ms 1132 KB Output is correct
13 Correct 5 ms 736 KB Output is correct
14 Correct 11 ms 344 KB Output is correct
15 Correct 13 ms 596 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 6 ms 344 KB Output is correct
18 Correct 6 ms 444 KB Output is correct
19 Correct 6 ms 856 KB Output is correct
20 Correct 6 ms 464 KB Output is correct
21 Correct 6 ms 984 KB Output is correct
22 Correct 6 ms 716 KB Output is correct
23 Correct 7 ms 1124 KB Output is correct
24 Correct 6 ms 724 KB Output is correct
25 Correct 10 ms 344 KB Output is correct
26 Correct 9 ms 344 KB Output is correct
27 Correct 10 ms 344 KB Output is correct
28 Correct 8 ms 344 KB Output is correct
29 Correct 8 ms 344 KB Output is correct
30 Correct 5 ms 448 KB Output is correct
31 Correct 7 ms 448 KB Output is correct
32 Correct 7 ms 448 KB Output is correct
33 Correct 6 ms 856 KB Output is correct
34 Correct 7 ms 856 KB Output is correct
35 Correct 7 ms 852 KB Output is correct
36 Correct 5 ms 864 KB Output is correct
37 Correct 6 ms 856 KB Output is correct
38 Correct 6 ms 468 KB Output is correct
39 Correct 7 ms 960 KB Output is correct
40 Correct 10 ms 968 KB Output is correct
41 Correct 7 ms 1124 KB Output is correct
42 Correct 10 ms 720 KB Output is correct
43 Correct 8 ms 868 KB Output is correct
44 Correct 8 ms 964 KB Output is correct
45 Correct 10 ms 344 KB Output is correct
46 Correct 12 ms 592 KB Output is correct
47 Correct 10 ms 344 KB Output is correct
48 Correct 9 ms 344 KB Output is correct
49 Correct 9 ms 344 KB Output is correct
50 Correct 6 ms 600 KB Output is correct
51 Correct 9 ms 692 KB Output is correct
52 Correct 8 ms 444 KB Output is correct
53 Correct 6 ms 456 KB Output is correct
54 Correct 7 ms 856 KB Output is correct
55 Correct 6 ms 856 KB Output is correct
56 Correct 6 ms 868 KB Output is correct
57 Correct 7 ms 480 KB Output is correct
58 Correct 8 ms 984 KB Output is correct
59 Correct 9 ms 1116 KB Output is correct
60 Correct 9 ms 724 KB Output is correct
61 Correct 8 ms 600 KB Output is correct
62 Correct 8 ms 872 KB Output is correct
63 Correct 9 ms 604 KB Output is correct
64 Correct 11 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 344 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 7 ms 600 KB Output is correct
4 Correct 8 ms 344 KB Output is correct
5 Correct 6 ms 472 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 8 ms 344 KB Output is correct
8 Correct 6 ms 600 KB Output is correct
9 Correct 6 ms 1112 KB Output is correct
10 Correct 6 ms 732 KB Output is correct
11 Correct 6 ms 724 KB Output is correct
12 Correct 7 ms 980 KB Output is correct
13 Correct 6 ms 968 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 11 ms 344 KB Output is correct
17 Correct 8 ms 344 KB Output is correct
18 Correct 8 ms 600 KB Output is correct
19 Correct 6 ms 600 KB Output is correct
20 Correct 5 ms 600 KB Output is correct
21 Correct 8 ms 344 KB Output is correct
22 Correct 10 ms 344 KB Output is correct
23 Correct 9 ms 596 KB Output is correct
24 Correct 9 ms 344 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 7 ms 600 KB Output is correct
27 Correct 8 ms 448 KB Output is correct
28 Correct 7 ms 600 KB Output is correct
29 Correct 5 ms 856 KB Output is correct
30 Correct 5 ms 600 KB Output is correct
31 Correct 6 ms 608 KB Output is correct
32 Correct 11 ms 544 KB Output is correct
33 Correct 11 ms 344 KB Output is correct
34 Correct 10 ms 344 KB Output is correct
35 Correct 9 ms 344 KB Output is correct
36 Correct 10 ms 344 KB Output is correct
37 Correct 7 ms 456 KB Output is correct
38 Correct 9 ms 696 KB Output is correct
39 Correct 8 ms 600 KB Output is correct
40 Correct 7 ms 972 KB Output is correct
41 Correct 7 ms 704 KB Output is correct
42 Correct 8 ms 716 KB Output is correct
43 Correct 6 ms 988 KB Output is correct
44 Correct 6 ms 716 KB Output is correct
45 Correct 6 ms 1116 KB Output is correct
46 Correct 6 ms 976 KB Output is correct
47 Correct 6 ms 724 KB Output is correct
48 Correct 7 ms 728 KB Output is correct
49 Partially correct 8 ms 712 KB Output is partially correct
50 Partially correct 9 ms 600 KB Output is partially correct
51 Partially correct 7 ms 852 KB Output is partially correct
52 Partially correct 8 ms 616 KB Output is partially correct
53 Partially correct 7 ms 728 KB Output is partially correct
54 Partially correct 9 ms 708 KB Output is partially correct
55 Partially correct 8 ms 708 KB Output is partially correct
56 Correct 7 ms 728 KB Output is correct
57 Correct 8 ms 732 KB Output is correct
58 Partially correct 7 ms 460 KB Output is partially correct
59 Partially correct 8 ms 728 KB Output is partially correct
60 Partially correct 9 ms 472 KB Output is partially correct
61 Partially correct 8 ms 612 KB Output is partially correct
62 Correct 6 ms 976 KB Output is correct
63 Correct 7 ms 612 KB Output is correct
64 Partially correct 10 ms 712 KB Output is partially correct
65 Partially correct 9 ms 724 KB Output is partially correct
66 Partially correct 10 ms 612 KB Output is partially correct
67 Partially correct 9 ms 712 KB Output is partially correct
68 Partially correct 9 ms 856 KB Output is partially correct
69 Partially correct 10 ms 712 KB Output is partially correct
70 Partially correct 9 ms 480 KB Output is partially correct
71 Correct 8 ms 612 KB Output is correct
72 Partially correct 9 ms 1224 KB Output is partially correct
73 Partially correct 8 ms 716 KB Output is partially correct
74 Partially correct 8 ms 724 KB Output is partially correct
75 Partially correct 10 ms 720 KB Output is partially correct
76 Partially correct 9 ms 468 KB Output is partially correct
77 Correct 7 ms 732 KB Output is correct
78 Partially correct 8 ms 856 KB Output is partially correct
79 Partially correct 10 ms 980 KB Output is partially correct
80 Partially correct 8 ms 868 KB Output is partially correct
81 Partially correct 8 ms 616 KB Output is partially correct
82 Partially correct 9 ms 864 KB Output is partially correct