AtCoder Grand Contest 005 C - Tree Restoring
题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c
题目大意:
给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节点的树,满足树上到第\(i\)个点的距离为\(A_i\),问能否构造
我真要吐槽一下……\(N\leqslant 100\),2s时限,256MB内存……我一直以为是个\(O(n^3\log n)\)级别的题,然后……\(O(n)\)?烟雾弹???
唉,其实还是因为自己太菜了……
首先知道一些性质(记\(Min=\min\limits_{i=1}^n\{A_i\},Max=\max\limits_{i=1}^n\{A_i\}\)):
- \(Min=\lceil\dfrac{Max}{2}\rceil\)
- 如果\(Max\)为奇数,\(Min\)可以出现两次;否则只能出现一次
- \((Min,Max]\)之间的出现次数至少为二
照着这些性质判断即可
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1; char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1; char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=1e2;
int v[N+10];
int main(){
int n=read(),Max=-inf,Min=inf;
for (int i=1;i<=n;i++){
int x=read();
Max=max(Max,x);
Min=min(Min,x);
v[x]++;
}
if (Min!=(Max+1)/2){
printf("Impossible\n");
return 0;
}
if (v[Min]!=(Max&1)+1){
printf("Impossible\n");
return 0;
}
for (int i=Min+1;i<=Max;i++){
if (v[i]<2){
printf("Impossible\n");
return 0;
}
}
printf("Possible\n");
return 0;
}