forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
45 lines (42 loc) · 958 Bytes
/
C.cpp
File metadata and controls
45 lines (42 loc) · 958 Bytes
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
#include <bits/stdc++.h>
using namespace std;
const int Z = 100000;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int Q, s, t, n, a[222], dist[2][200200];
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
memset(dist, -1, sizeof dist);
dist[0][Z] = 0;
queue < pair<int,int> > q;
q.push({0, 0});
while (!q.empty())
{
int x = q.front().second, u = q.front().first;
q.pop();
for (int i = 0; i < n; i++)
{
int y = 2 * a[i] - x;
if (y >= -Z && y <= Z && dist[u ^ 1][y + Z] < 0)
{
dist[u ^ 1][y + Z] = dist[u][x + Z] + 1;
q.push({u ^ 1, y});
}
}
}
cin >> Q;
while (Q--)
{
cin >> s >> t;
int ans = -1;
if (dist[0][t - s + Z] >= 0)
ans = dist[0][t - s + Z];
if (dist[1][t + s + Z] >= 0)
if (ans < 0 || ans > dist[1][t + s + Z])
ans = dist[1][t + s + Z];
cout << ans << '\n';
}
}