Skip to content

Commit fe072a8

Browse files
committed
Tutorial: Algortihmic Grand Prix Added except Last
1 parent 96e9ae5 commit fe072a8

File tree

64 files changed

+753
-184
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+753
-184
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "bits/stdc++.h"
2+
3+
using i64 = long long;
4+
5+
void solve()
6+
{
7+
int a, b;
8+
std::cin >> a >> b;
9+
10+
int c = std::min(a % b, b - a % b);
11+
int k1 = (c - a) / b;
12+
int k2 = (-c - a) / b;
13+
14+
i64 A = a + 1LL * k1 * b;
15+
i64 B = a + 1LL * k2 * b;
16+
17+
if (A != B and std::abs(A) == std::abs(B))
18+
std::cout << "YES\n";
19+
else
20+
std::cout << "NO\n";
21+
}
22+
23+
int main()
24+
{
25+
int t;
26+
std::cin >> t;
27+
28+
while (t--)
29+
solve();
30+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "bits/stdc++.h"
2+
3+
using i64 = long long;
4+
5+
void solve()
6+
{
7+
int n, k;
8+
std::cin >> n >> k;
9+
10+
std::vector<int> h(n);
11+
std::vector a(n, std::array<int, 2>());
12+
for (int i = 0; i < n; i++)
13+
std::cin >> h[i];
14+
15+
for (int i = 0; i < n; i++)
16+
{
17+
std::cin >> a[i][1];
18+
a[i][0] = h[i];
19+
}
20+
21+
std::sort(std::begin(a), std::end(a));
22+
std::multiset<std::pair<int, int>> m;
23+
24+
std::vector<int> ans;
25+
int cnt = 0;
26+
for (int i = 0, id = 0; i < n; i++)
27+
{
28+
while (id < n and a[id][0] <= k)
29+
{
30+
m.emplace(-a[id][1], a[id][0]);
31+
id++;
32+
}
33+
34+
if (m.empty())
35+
break;
36+
37+
cnt++;
38+
auto [energy_gain, height] = *std::begin(m);
39+
m.erase(std::begin(m));
40+
41+
k += -energy_gain;
42+
ans.emplace_back(k);
43+
}
44+
45+
std::cout << cnt << "\n";
46+
for (int i = 0; i < cnt; i++)
47+
std::cout << ans[i] << " ";
48+
std::cout << "\n";
49+
}
50+
51+
int main()
52+
{
53+
int t;
54+
std::cin >> t;
55+
56+
while (t--)
57+
solve();
58+
59+
return 0;
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "bits/stdc++.h"
2+
3+
int main()
4+
{
5+
int n, k;
6+
std::cin >> n >> k;
7+
8+
std::vector<int> a(n);
9+
for (int i = 0; i < n; i++)
10+
std::cin >> a[i];
11+
12+
std::sort(std::begin(a), std::end(a));
13+
14+
std::vector qry(k, std::array<int, 2>());
15+
for (auto &[c, s] : qry)
16+
std::cin >> c >> s;
17+
18+
std::sort(std::begin(qry), std::end(qry), [&](const auto &x, const auto &y)
19+
{
20+
return x[1] > y[1];
21+
});
22+
23+
for (int i = 0, id = 0; i < n and id < k; id++)
24+
{
25+
auto [c, s] = qry[id];
26+
while (i < n and c > 0)
27+
{
28+
a[i] = std::max(a[i], s);
29+
i++;
30+
c--;
31+
}
32+
}
33+
34+
std::cout << std::accumulate(std::begin(a), std::end(a), 0LL);
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "bits/stdc++.h"
2+
3+
using i64 = long long;
4+
5+
void solve()
6+
{
7+
int n;
8+
std::cin >> n;
9+
10+
std::vector<int> a(n);
11+
for (int i = 0; i < n; i++)
12+
std::cin >> a[i];
13+
14+
std::sort(std::begin(a), std::end(a));
15+
int z = 0;
16+
for (int i = 0; i < n - 1; i++)
17+
z = std::gcd(z, a[n - 1] - a[i]);
18+
19+
i64 y = 0;
20+
for (int i = 0; i < n; i++)
21+
y += (a.back() - a[i]) / z;
22+
23+
std::cout << y << " " << z << "\n";
24+
}
25+
26+
int main()
27+
{
28+
int t;
29+
std::cin >> t;
30+
31+
while (t--)
32+
solve();
33+
34+
return 0;
35+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include "bits/stdc++.h"
2+
3+
using i64 = long long;
4+
5+
void solve()
6+
{
7+
int n;
8+
std::cin >> n;
9+
10+
std::vector<int> a(n);
11+
for (int i = 0; i < n; i++)
12+
std::cin >> a[i];
13+
14+
if (n <= 3)
15+
{
16+
std::cout << "1\n" << a[0] << "\n";
17+
18+
std::cout << n - 1 << "\n";
19+
for (int i = 1; i < n; i++)
20+
std::cout << a[i] << " ";
21+
std::cout << "\n";
22+
return;
23+
}
24+
25+
std::vector<int> b, c;
26+
std::sort(std::begin(a), std::end(a));
27+
auto work = [&](int id1, int id2)
28+
{
29+
b.emplace_back(a[id1]);
30+
b.emplace_back(a[id2]);
31+
int d = b[1] - b[0];
32+
33+
for (int i = 0; i < n; i++)
34+
{
35+
if (i == id1 || i == id2)
36+
continue;
37+
38+
if (a[i] - b.back() == d)
39+
b.emplace_back(a[i]);
40+
else
41+
c.emplace_back(a[i]);
42+
}
43+
44+
if (std::size(c) <= 2)
45+
return true;
46+
47+
d = 0;
48+
for (int i = 1; i < std::size(c); i++)
49+
d = std::gcd(d, c[i] - c[i - 1]);
50+
51+
std::multiset<int> need;
52+
for (int i = 1; i < std::size(c); i++)
53+
{
54+
if (c[i] - c[i - 1] != d)
55+
{
56+
int x = c[i - 1];
57+
while (x + d != c[i])
58+
{
59+
if (std::size(need) > std::size(b))
60+
return false;
61+
62+
x += d;
63+
need.emplace(x);
64+
}
65+
}
66+
}
67+
68+
while (!need.empty())
69+
{
70+
int min = *std::begin(need);
71+
auto it = std::find(std::begin(b), std::end(b), min);
72+
73+
if (it == std::end(b))
74+
return false;
75+
76+
for (auto p = it; p != std::end(b); p++)
77+
{
78+
auto del = need.find(*p);
79+
if (del != std::end(need))
80+
need.erase(del);
81+
}
82+
83+
c.insert(std::end(c), it, std::end(b));
84+
b.erase(it, std::end(b));
85+
}
86+
std::sort(std::begin(c), std::end(c));
87+
for (int i = 2; i < std::size(c); i++)
88+
{
89+
if (c[i] - c[i - 1] != c[1] - c[0])
90+
return false;
91+
}
92+
93+
return true;
94+
};
95+
96+
for (int i = 0; i < 3; i++)
97+
{
98+
int id1 = i, id2 = (i + 1) % 3;
99+
if (id1 > id2)
100+
std::swap(id1, id2);
101+
102+
auto possible = work(id1, id2);
103+
if (possible)
104+
{
105+
std::cout << std::size(b) << "\n";
106+
for (int x : b)
107+
std::cout << x << " ";
108+
std::cout << "\n";
109+
110+
std::cout << std::size(c) << "\n";
111+
for (int x : c)
112+
std::cout << x << " ";
113+
std::cout << "\n";
114+
return;
115+
}
116+
117+
b.clear();
118+
c.clear();
119+
}
120+
121+
std::cout << "-1\n";
122+
}
123+
124+
int main()
125+
{
126+
int t;
127+
std::cin >> t;
128+
129+
while (t--)
130+
solve();
131+
132+
return 0;
133+
}

0 commit comments

Comments
 (0)