-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1148.cc
130 lines (113 loc) · 3.07 KB
/
1148.cc
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdlib>
using namespace std;
typedef set<int>::iterator Iterator;
struct Vector {
int x, y;
Vector() {}
Vector(int x, int y) : x(x), y(y) {}
Vector& operator += (const Vector &other) {
x += other.x;
y += other.y;
return *this;
}
};
bool check_x(int x, int dim) {
return ((dim == 1 || dim == 4) && x > 0) || ((dim == 2 || dim == 3) && x < 0);
}
bool check_y(int y, int dim) {
return ((dim == 1 || dim == 2) && y > 0) || ((dim == 3 || dim == 4) && y < 0);
}
int x_select(const Vector &cur, set<int> &v, int dim) {
int dx = 0;
if((dim == 1 || dim == 4) && cur.x <= 0) {
Iterator it = v.lower_bound(-cur.x);
if(it == v.end()) return false;
dx = *it;
}
else if((dim == 2 || dim == 3) && cur.x >= 0) {
Iterator it = v.lower_bound(cur.x);
if(it == v.end()) return false;
dx = -*it;
}
else {
dx = *v.begin();
if(!check_x(cur.x+dx, dim) || (check_x(cur.x-dx, dim) && abs(cur.x-dx) < abs(cur.x+dx))) dx = -dx;
}
v.erase(abs(dx));
return dx;
}
int y_select(const Vector &cur, set<int> &v, int dim) {
int dy = 0;
if((dim == 1 || dim == 2) && cur.y <= 0) {
Iterator it = v.lower_bound(-cur.y);
if(it == v.end()) return false;
dy = *it;
}
else if((dim == 3 || dim == 4) && cur.y >= 0) {
Iterator it = v.lower_bound(cur.y);
if(it == v.end()) return false;
dy = -*it;
}
else {
dy = *v.begin();
if(!check_y(cur.y+dy, dim) || (check_y(cur.y-dy, dim) && abs(cur.y-dy) < abs(cur.y+dy))) dy = -dy;
}
v.erase(abs(dy));
return dy;
}
bool dfs(const Vector &cur, set<int> &v, vector<Vector> &history, const vector<int> &us, int phase) {
if(phase == us.size()) return true;
int dim = us[phase];
for(int i = 0; i < 2; ++i) {
int dx, dy;
if(i == 0) {
dx = x_select(cur, v, dim);
dy = y_select(cur, v, dim);
}
else {
dy = y_select(cur, v, dim);
dx = x_select(cur, v, dim);
}
Vector ncur = cur;
ncur += Vector(dx, dy);
history.push_back(Vector(dx, dy));
if(dfs(cur, v, history, us, phase+1)) return true;
else {
v.insert(abs(dx));
v.insert(abs(dy));
history.pop_back();
}
}
return false;
}
int main() {
ios::sync_with_stdio(0);
int N;
cin >> N;
set<int> v;
vector<int> us(N);
for(int i = 0; i < 2*N; ++i) {
int n;
cin >> n;
v.insert(n);
}
for(int i = 0; i < N; ++i) cin >> us[i];
Vector cur(0, 0);
vector<Vector> history;
if(dfs(cur, v, history, us, 0)) {
for(int i = 0; i < history.size(); ++i) {
if(history[i].x > 0) cout << '+';
cout << history[i].x << ' ';
if(history[i].y > 0) cout << '+';
cout << history[i].y << endl;
}
}
else {
cout << 0 << endl;
}
return 0;
}