Description
给出一个数组
a
。
要求构造一颗树,使节点
Solution
这颗树的直径
d=maxni=1ai
。
可以通过对
d
<script type="math/tex" id="MathJax-Element-7">d</script>奇偶性的分类讨论,把在直径上节点的值计算掉。
然后其他的点的值一定大于等于在直径上的点的值的最小值。
等号可不可取也是要分奇偶性讨论的。
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
inline void read(int &x) {
static char c; x = 0;
for (c = get(); c < '0' || c > '9'; c = get());
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
int n, d;
int a[N], buc[N];
inline void Godie(void) {
puts("Impossible");
exit(0);
}
int main(void) {
freopen("1.in", "r", stdin);
read(n);
for (int i = 1; i <= n; i++) {
read(a[i]); ++buc[a[i]];
}
d = *max_element(a + 1, a + n + 1);
if (d & 1) {
for (int i = d - d / 2; i <= d; i++) {
buc[i] -= 2; if (buc[i] < 0) Godie();
}
for (int i = d - d / 2; i; i--)
if (buc[i]) Godie();
} else {
if (!buc[d / 2]) Godie(); --buc[d / 2];
for (int i = d / 2 + 1; i <= d; i++) {
buc[i] -= 2; if (buc[i] < 0) Godie();
}
for (int i = d / 2; i; i--)
if (buc[i]) Godie();
}
puts("Possible");
return 0;
}