POI2017 简要题解
Stage I
Task 1 Flappy Bird
题意
有一个二维平面,初始时一只鸟在
题解
显然对于每个横坐标,可行的纵坐标是一段区间内所有的奇数或偶数。而要求点击次数最小,只需要最后的纵坐标最小即可。
时间复杂度:
代码
#include <iostream>
#include <vector>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int x = 0, y1 = 0, y2 = 0;
int n, l;
std::cin >> n >> l;
for (int i = 0; i < n; ++i) {
int a, b, c;
std::cin >> a >> b >> c;
y1 -= a - x;
y2 += a - x;
x = a;
y1 = std::max(y1, b + 1);
y2 = std::min(y2, c - 1);
if ((y1 ^ x) & 1)
++y1;
if ((y2 ^ x) & 1)
--y2;
if (y1 > y2) {
std::cout << "NIE" << "\n";
return 0;
}
}
y1 -= l - x;
if ((y1 ^ l) & 1)
++y1;
std::cout << (l + y1) / 2 << "\n";
return 0;
}
Task 2 Podzielność
题意
有一些
题解
显然
时间复杂度:
代码
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
std::vector<long long> a(n);
long long sum = 0;
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
sum += i * a[i];
}
if (sum % (n - 1) != 0)
--a[sum % (n - 1)];
for (int i = 1; i < n; ++i)
a[i] += a[i - 1];
for (int i = 0; i < q; ++i) {
long long k;
std::cin >> k;
if (k >= a[n - 1]) {
std::cout << -1 << "\n";
} else {
std::cout << std::upper_bound(a.begin(), a.end(), k) - a.begin() << "\n";
}
}
return 0;
}
Task 3 Reprezentacje różnicowe
题意
给定数列
题解
由于
代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
constexpr int A[] = {0, 1, 2, 4, 8, 16, 21, 42, 51, 102, 112, 224, 235, 470, 486, 972, 990, 1980, 2002, 4004, 4027, 8054, 8078, 16156, 16181, 32362, 32389, 64778, 64806, 129612, 129641, 259282, 259313, 518626, 518658, 1037316, 1037349, 2074698, 2074734, 4149468, 4149505, 8299010, 8299049, 16598098, 16598140, 33196280, 33196324, 66392648, 66392693, 132785386, 132785432, 265570864, 265570912, 531141824, 531141876, 1062283752, 1062283805};
std::vector<std::tuple<int, int, int>> ans;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
for (int i = 1; i <= 56; ++i)
for (int j = 1; j < i; ++j)
if (A[i] - A[j] <= 1e9)
ans.emplace_back(A[i] - A[j], i, j);
std::sort(ans.begin(), ans.end());
int t;
std::cin >> t;
while (t--) {
int x;
std::cin >> x;
int k = std::lower_bound(ans.begin(), ans.end(), std::make_tuple(x + 1, 0, 0)) - ans.begin() - 1;
auto [y, p, q] = ans[k];
if (x != y) {
p = 2 * (x - k + 27);
q = p - 1;
}
std::cout << p << " " << q << "\n";
}
return 0;
}
Task 4 Sabotaż
题意
一个公司有
题解
显然最坏情况初始叛徒是叶子,因此最后所有的叛徒是一棵子树。简单 dp 出使得每棵子树全部成为叛徒的最大的
时间复杂度:
代码
#include <iostream>
#include <vector>
#include <iomanip>
int n, k;
std::vector<double> dp;
std::vector<int> sz;
std::vector<std::vector<int>> e;
void dfs(int u) {
if (e[u].empty()) {
dp[u] = 1;
sz[u] = 1;
return;
}
for (int v : e[u]) {
dfs(v);
sz[u] += sz[v];
}
for (int v : e[u])
dp[u] = std::max(dp[u], std::min(dp[v], 1.0 * sz[v] / sz[u]));
++sz[u];
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> k;
e.resize(n);
sz.resize(n);
dp.resize(n);
for (int i = 1; i < n; ++i) {
int p;
std::cin >> p;
--p;
e[p].push_back(i);
}
dfs(0);
double ans = 0;
for (int i = 0; i < n; ++i)
if (sz[i] > k)
ans = std::max(ans, dp[i]);
std::cout << std::fixed << std::setprecision(10) << ans << "\n";
return 0;
}
Task 5 Turysta
题意
给定一张
题解
众所周知
时间复杂度:
代码
#include <iostream>
#include <vector>
#include <algorithm>
int n;
std::vector<std::vector<bool>> g;
int cntScc, dfsClock;
std::vector<int> dfn, low, belong;
std::vector<int> stk;
std::vector<std::vector<int>> ans;
std::vector<std::vector<int>> h;
void tarjan(int u) {
dfn[u] = low[u] = dfsClock++;
stk.push_back(u);
for (int v = 0; v < n; ++v) {
if (g[u][v]) {
if (dfn[v] == -1) {
tarjan(v);
low[u] = std::min(low[u], low[v]);
} else if (belong[v] == -1) {
low[u] = std::min(low[u], dfn[v]);
}
}
}
if (dfn[u] == low[u]) {
int v;
do {
v = stk.back();
belong[v] = cntScc;
stk.pop_back();
} while (v != u);
++cntScc;
}
}
auto solve(std::vector<int> v) {
if (int(v.size()) == 1)
return v;
std::vector<int> h;
for (auto u : v)
h.insert(std::find_if(h.begin(), h.end(), [&](int v) {return g[u][v];}), u);
auto it = std::find_if(h.begin(), h.end(), [&](int v) {return g[v][h[0]];}) + 1;
std::vector<int> c(h.begin(), it);
while (it != h.end()) {
auto r = it;
while (std::find_if(c.begin(), c.end(), [&](int v) {return g[*r][v];}) == c.end())
++r;
auto p = c.begin();
while (p + 1 != c.end() && !(g[*p][*it] && g[*r][*(p + 1)]))
++p;
c.insert(p == c.end() ? c.begin() : p + 1, it, r + 1);
it = r + 1;
}
return c;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n;
g.assign(n, std::vector<bool>(n));
dfn.assign(n, -1);
low.resize(n);
belong.assign(n, -1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
int x;
std::cin >> x;
g[i][j] = x ^ 1;
g[j][i] = x;
}
}
for (int i = 0; i < n; ++i)
if (dfn[i] == -1)
tarjan(i);
h.resize(cntScc);
ans.resize(n);
for (int i = 0; i < n; ++i)
h[belong[i]].push_back(i);
for (int i = 0; i < cntScc; ++i) {
h[i] = solve(h[i]);
for (auto it = h[i].begin(); it != h[i].end(); ++it) {
int u = *it;
ans[u].insert(ans[u].end(), it, h[i].end());
ans[u].insert(ans[u].end(), h[i].begin(), it);
for (int j = i - 1; j >= 0; --j)
ans[u].insert(ans[u].end(), h[j].begin(), h[j].end());
}
}
for (int i = 0; i < n; ++i) {
std::cout << ans[i].size();
for (int u : ans[i])
std::cout << " " << u + 1;
std::cout << "\n";
}
return 0;
}