[AGC005C]Tree Restoring 构造
Description
给出一个数组a,要求构造一颗树,使节点x距离最远的点的距离为\(a_x\)。
Input
第一行一个正整数NN(2≤N≤1002≤N≤100)
接下来一行,有NN个正整数,描述序列a1,a2,...,aNa1,a2,...,aN(1≤ai≤N−11≤ai≤N−1)
Output
如果对于输入的序列存在这样的树,则输出"Possible",否则输出"Impossible"。二者皆不含引号。
Sample Input
#Sample Input 1
5
3 2 2 3 3
#Sample Input 2
3
1 1 2
#Sample Input 3
10
1 2 2 2 2 2 2 2 2 2
#Sample Input 4
10
1 1 2 2 2 2 2 2 2 2
#Sample Input 5
6
1 1 1 1 1 5
#Sample Input 6
5
4 3 2 3 4
Sample Output
#Sample Output 1
Possible
#Sample Output 2
Impossible
#Sample Output 3
Possible
#Sample Output 4
Impossible
#Sample Output 5
Impossible
#Sample Output 6
Possible
HINT
对于第一组样例,有如下美妙树:
黑边表示原树边,而红边表示的是距离每一个点最远的点是谁。
Sol
显然只要能把直径构造出来,那么剩下的点无论如何也能构造出来,首先判断最小的\(a_i\)能否达到\(直径长度\lceil\frac{直径长度}{2}\rceil\),然后我们枚举直径长度到\(直径长度\lceil\frac{直径长度}{2}\rceil\)的所有长度,如果不到两个那么就不可行,否则可行。注意如果直径长度是偶数的话\(直径长度\lceil\frac{直径长度}{2}\rceil\)有一个就可以了。
Code
#include <bits/stdc++.h>
using namespace std;
int a[105],b[105],n,mid,g;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[a[i]]++;
sort(a+1,a+n+1,greater<int>());mid=(a[1]+1)>>1,g=(a[1]&1)+1;
if(a[n]<mid||b[mid]!=g) return puts("Impossible"),0;
for(int i=a[1];i>mid;i--) if(b[i]<2) return puts("Impossible"),0;
puts("Possible");
}
[AGC005C]Tree Restoring 构造的更多相关文章
- Tree Restoring
Tree Restoring Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Aoki lo ...
- ZOJ 3965 Binary Tree Restoring
Binary Tree Restoring 思路: 递归 比较a序列和b序列中表示同一个子树的一段区间,不断递归 代码: #include<bits/stdc++.h> using nam ...
- zoj 3965 Binary Tree Restoring(搜索)
Binary Tree Restoring Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Given two ...
- C#结合Jquery LigerUI Tree插件构造树
Jquery LigerUI Tree是Jquery LigerUI()的插件之一,使用它可以快速的构建树形菜单.呵呵 废话不说了,直入正题,下面介绍C#结合ligerui 构造树形菜单的两种方法 1 ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造
C. Bear and Forgotten Tree 3 题目连接: http://www.codeforces.com/contest/658/problem/C Description A tre ...
- 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...
- AtCoder - 2061 Tree Restoring
Problem Statement Aoki loves numerical sequences and trees. One day, Takahashi gave him an integer s ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- Huffman Tree 简单构造
//函数:构造Huffman树HT[2*n-1] #define MAXVALUE 9999//假设权值不超过9999 #define MAXLEAF 30 #define MAXNODE MAXLE ...
随机推荐
- 用python40行代码编写的计算器
效果图 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...
- oracle——存储过程参数
oracle 存储过程类型: 1.in:输入类型,即由应用程序将数据传入oracle存储过程中:这种参数在存储过程中是只读参数,在存储过程中无法对该类型的参数进行修改: 2.out:输出参数,是在存储 ...
- C#使用 SharpAVI进行 屏幕录制
再 nuget 中 搜索 shapAvi 并添加引用 github 地址:https://github.com/baSSiLL/SharpAvi using SharpAvi; using Sharp ...
- python数据字典的操作
一.什么是字典? 字典是Python语言中唯一的映射类型. 映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的的关系,通常被认为是可变的哈希表. 字典对象是可变的,它是一个容器 ...
- 敏捷软件开发Note
[敏捷原则] 1.我们最优先要做的是通过尽早的.持续的交付有价值的软件为使客户满意. 初期交付的系统中所包含的功能越少,最终交付的系统的质量就越高.交付的越频繁,最终的产品质量就越高.敏捷实践会说早地 ...
- 模仿慕课网一步步发布一个开源库到 JCenter
H:\common\-common-25.2.2\upload.gradle // Bintray /* Properties properties = new Properties() proper ...
- docker问题:docker端口映射错误
1 docker端口映射错误 1.1 问题描述 利用docker启动nginx容器的时候报错: 1.2 解决办法 一次执行下面的命令就可以解决 pkill docker iptables -t nat ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- 在Ubuntu16.04上使用rz上传文件,XXX was skipped
原本想把hadoop-2.8.5.tar.gz上传到/usr/local/src文件夹下,报错,was skipped 如下图: 换个文件夹位置,更换到本用户文件夹下,可以上传,说明是对文件夹操作权限 ...
- 面试题:cook和session
1.首先,Cookie与Session存在的目的是什么? 答:二者都是为了保持客户端访问用户与后台服务器的交互状态,之所以为了保持这种状态,一是为了方便一些业务的实现,另一方面就是为了简化后台服务端的 ...