Skip to content

Commit a396216

Browse files
authored
added code
1 parent 5ec8bc2 commit a396216

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "bits/stdc++.h"
2+
3+
using i64 = long long;
4+
5+
int main()
6+
{
7+
std::ios::sync_with_stdio(false);
8+
std::cin.tie(nullptr);
9+
10+
int n;
11+
std::cin >> n;
12+
13+
std::vector<int> a(n);
14+
std::map<int, int> m;
15+
for (int i = 0; i < n; i++)
16+
{
17+
std::cin >> a[i];
18+
if (i > 0 and a[i] != a[i - 1])
19+
m[a[i] - a[i - 1]]++;
20+
}
21+
22+
int q;
23+
std::cin >> q;
24+
25+
std::vector<std::pair<i64, int>> weight;
26+
for (auto [num, f] : m)
27+
{
28+
for (int i = 1, j = 1; ; i *= 2, j++)
29+
{
30+
if (f - i > 0)
31+
{
32+
weight.emplace_back(1LL * i * num, i);
33+
f -= i;
34+
}
35+
else
36+
{
37+
weight.emplace_back(1LL * f * num, f);
38+
break;
39+
}
40+
}
41+
}
42+
43+
constexpr int N = 3e5 + 2;
44+
constexpr int inf = 1e9;
45+
std::vector<int> dp(N, inf);
46+
dp[0] = 0;
47+
48+
for (int i = 0; i < std::size(weight); i++)
49+
{
50+
std::vector<int> ndp(N, inf);
51+
ndp[0] = 0;
52+
for (int j = 1; j < N; j++)
53+
{
54+
ndp[j] = dp[j];
55+
if (j - weight[i].first >= 0)
56+
ndp[j] = std::min(ndp[j], dp[j - weight[i].first] + weight[i].second);
57+
}
58+
59+
std::swap(dp, ndp);
60+
}
61+
62+
while (q--)
63+
{
64+
int k;
65+
std::cin >> k;
66+
67+
std::cout << (dp[k] == inf ? -1 : dp[k]) << " \n"[q == 0];
68+
}
69+
70+
return 0;

0 commit comments

Comments
 (0)