restore list

 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
1 3 4 6 14 23 34 44 45 47 48 49
1 3 49 4
49 48 1 3
vector<int> solve(listGenerator r){
vector<int> st;
bool hasConflict = false;
int a[3];
while ( r.hasNext() ) {
int v = r.getNext();
if ( !hasConflict )
{
if (st.size() < 2) {
st.push_back(v);
} else {
int n = st.size();
int el1 = st[n-2];
int el2 = st[n-1];
if ( (el2-el1)*(v - el2) > 0) {
st.push(v)
} else {
hasConflict = true;
if ( e2 > e1) {
for(int i=0; i<n-1; i++) printf("%d ",st[i]);
st.clear();
a[0] = el1;
a[1] = v;
a[2] = el2;
} else {
st.pop_back();
st.pop_back();
a[0] = el2;
a[1] = v;
a[2] = el1;
}
}
}
} else {
if (v < a[1]) {
st.push_back(a[2]);
a[2] = a[1];
a[1] = v;
} else {
printf("%d ",a[0]);
a[0] = a[1];
a[1] = v
}
}
}
printf("%d %d %d ",a[0],a[1],a[2]);
for(int i=n-1; i>0; i--) printf("%d ",st[i]);
}