[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 ...
随机推荐
- 基本的Ceph性能测试工具和方法
测试环境 1. 测试准备 1.1 磁盘读写性能 1.1.1 单个 OSD 磁盘写性能,大概 165MB/s. root@ceph1:~# echo 3 > /proc/sys/vm/drop_c ...
- redis基本操作和在springboot中的使用
本文介绍redis的使用 redis启动步骤 说明 redis自增自减相关操作 redis string set操作 get操作 其他操作 redis hash set操作 get操作 其他操作 re ...
- Eclipse maven工程生命周期clean、compile、test、package
1.清理. 编译 工程右键->Run As->Maven build... 清理.编译操作控制台信息输出: 2.测试 同清理.编译,goals改成test 3.打包 同清理.编译,go ...
- 谈谈contentWindow 和contentDocument以及 iframe
1,contentWindow:是用来获取子窗口的window对象的,它兼容各大浏览器,用法如下 document.getElementById("iframeId").conte ...
- android 侧滑菜单
就是用手一滑才出现,占手机半个多屏幕的菜单.为了美观和页面转跳,很多时候要用到. 实现的话就是使用官方的DrawerLayout,注意这个布局一定要是最顶层的布局. 在DrawerLayout里面直接 ...
- leetcode216
public class Solution { public IList<IList<int>> CombinationSum3(int k, int n) { , , , , ...
- 从cocos2d-x-2.x到cocos2d-x-3.x: lua项目配置
cocos2dx-x3.0的正式版出来也有一段时间了,现在最新的版本是到了3.2alpha,和2.x系列相比,能够找到的相关资料除了官网上的wiki,其他的也不见得多,遇到的一些和2.x的差异和问题在 ...
- solrCloud选举leader的逻辑分析
First call *setup(ElectionContext) to ensure the election process is in it'd. Next calljoinElecti ...
- XHProf安装使用笔记
编译安装 获取源代码包root@sourcjoy>wget http://pecl.php.net/get/xhprof-0.9.2.tgz解压root@sourcjoy>tar zxf ...
- zend studio在线安装svn的插件
这个,其实真的很简单 ,但是让我纠结了很久. 安装步骤: 1.确保电脑能够上网: 2.打开zend studio,然后选择help->welcome会弹出一个欢迎页面; 3.等待几秒后可以看到右 ...