-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path744.cpp
More file actions
125 lines (118 loc) · 1.96 KB
/
Copy path744.cpp
File metadata and controls
125 lines (118 loc) · 1.96 KB
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
#include<iostream>
#include<stack>
using namespace std;
int* arr;
int* sample;
int* idx;
bool check(int n) {
for (int i = 0; i < n; i++) {
int base = sample[i];
int come = base;
for (int j = i + 1; j < n; j++) {
if (sample[j] > base) {
if (sample[j] < come) {
return false;
}
else {
come = sample[j];
}
}
}
}
return true;
}
void print(int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << idx[i] << " ";
}
cout << endl;
}
int search(int x, int s, int t) {
if (s == t) {
if (arr[s] == x) {
return s;
}
}
else {
int mid = (s + t) / 2;
if (arr[mid] == x) {
return mid;
}
else if (arr[mid] > x) {
return search(x, s, mid);
}
else {
return search(x, mid + 1, t);
}
}
}
void merge(int s, int t) {
if (s < t) {
int mid = (s + t) / 2;
merge(s, mid);
merge(mid + 1, t);
int* tmp = new int[t - s + 1];
int* ti = new int[t - s + 1];
for (int i = 0; i < t - s + 1; i++) {
tmp[i] = arr[i + s];
ti[i] = idx[i + s];
}
int pos = s;
int i = 0;
int j = mid - s + 1;
while (pos <= t) {
if (j > t - s || (i <= mid - s && tmp[i] <= tmp[j])) {
arr[pos] = tmp[i];
idx[pos] = ti[i];
pos++;
i++;
}
else if (i > mid - s || (j <= t - s && tmp[j] < tmp[i])) {
arr[pos] = tmp[j];
idx[pos] = ti[j];
pos++;
j++;
}
}
delete[] tmp;
delete[] ti;
}
}
int main() {
int caseNum;
cin >> caseNum;
for (int c = 0; c < caseNum; c++) {
int n;
cin >> n;
arr = new int[n];
idx = new int[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
idx[i] = i;
}
merge(0, n - 1);
int m;
cin >> m;
for (int ct = 0; ct < m; ct++) {
sample = new int[n];
for (int i = 0; i < n; i++) {
int t;
cin >> t;
sample[i] = idx[search(t, 0, n-1)];
}
if (check(n)) {
cout << "Aye\n";
}
else {
cout << "Impossible\n";
}
delete[] sample;
}
delete[] arr;
}
return 0;
}