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;
}
AtCoder Grand Contest 005 C - Tree Restoring的更多相关文章
- AtCoder Grand Contest 005
AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...
- AtCoder Grand Contest 010 F - Tree Game
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...
- AtCoder Grand Contest 018 D - Tree and Hamilton Path
题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d 题目大意: 给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点 ...
- AtCoder Grand Contest 005【A栈模拟,B单调栈】
挖草,AtCoder实在是太吊了~ %%%,目前只A了两题: A题: 就是利用栈模拟一下就好了:S进栈,T的话有S就出栈,然后len减一下就好了: #include <bits/stdc++.h ...
- Atcoder Grand Contest 005 E - Sugigma: The Showdown(思维题)
洛谷题面传送门 & Atcoder 题面传送门 记先手移动棋子的树为红树,后手移动棋子的树为蓝树. 首先考虑一个性质,就是如果与当前红色棋子所在的点相连的边中存在一条边,满足这条边的两个端点在 ...
- AtCoder Grand Contest 005题解
传送门 \(A\) 咕咕 const int N=5e5+5; char s[N];int res,n,sum; int main(){ scanf("%s",s+1),res=n ...
- AtCoder Grand Contest 010
AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...
- AtCoder Grand Contest 014
AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中 ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
随机推荐
- Service Mesh vs SideCar
Istio = 微服务框架 + 服务治理 Istio 大幅降低微服务架构下应用程序的开发难度,势必极大的推动微服务的普及.个人乐观估计,随着isito的成熟,微服务开发领域将迎来一次颠覆性的变革.后面 ...
- Ajax_HTTP请求以及响应
什么是HTTP请求? 就是从用户的浏览器端向服务器端发送请求 一个HTTP请求一般由四个部分组成 1.HTTP请求的方法或者动作,比如GET或者POST请求 2.请求的URL,也就是请求的地址 3.请 ...
- sbt is a build tool for Scala, Java, and more
http://www.scala-sbt.org/0.13/docs/index.html sbt is a build tool for Scala, Java, and more. It requ ...
- a column-oriented DBMS
https://clickhouse.yandex/docs/en/introduction/what_is_clickhouse.html
- js对table操作(添加删除交换上下TR)
<table width="100%" border="0" cellpadding="2" cellspacing="1& ...
- iOS中区分照片的来源
原理就是通过枚举出每个assets group,然后取得group property,group property是个整数,对应头文件中的一些枚举值.用这个可以判断照片是从哪来的(相机胶卷.照片流.相 ...
- Spring boot 使用Junt
//@RunWith:启动器,SpringJUnit4ClassRunner:Spring整合JUnit4 //@SpringBootTest获取启动类,相当于@Contextconfiguartio ...
- UVAlive 6611 Alice's Print Service 二分
Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using h ...
- React 编码
https://github.com/Minwe/style-guide/blob/master/React.js.md https://github.com/planningcenter/react ...
- css3中animation的应用
1.css3 的相关属性: 相关代码: div { animation-name: myfirst; //动画的名称 animation-duration: 5s; //动画一个周期需要5秒 anim ...