[刷题]Codeforces 746G - New Roads
Description
There are n cities in Berland, each of them has a unique id — an integer from 1 to n, the capital is the one with id 1. Now there is a serious problem in Berland with roads — there are no roads.
That is why there was a decision to build n - 1 roads so that there will be exactly one simple path between each pair of cities.
In the construction plan t integers a1, a2, …, at were stated, where t equals to the distance from the capital to the most distant city, concerning new roads. ai equals the number of cities which should be at the distance i from the capital. The distance between two cities is the number of roads one has to pass on the way from one city to another.
Also, it was decided that among all the cities except the capital there should be exactly k cities with exactly one road going from each of them. Such cities are dead-ends and can’t be economically attractive. In calculation of these cities the capital is not taken into consideration regardless of the number of roads from it.
Your task is to offer a plan of road’s construction which satisfies all the described conditions or to inform that it is impossible.
Input
The first line contains three positive numbers n, t and k (2 ≤ n ≤ 2·105, 1 ≤ t, k < n) — the distance to the most distant city from the capital and the number of cities which should be dead-ends (the capital in this number is not taken into consideration).
The second line contains a sequence of t integers a1, a2, …, at (1 ≤ ai < n), the i-th number is the number of cities which should be at the distance i from the capital. It is guaranteed that the sum of all the values ai equals n - 1.
Output
If it is impossible to built roads which satisfy all conditions, print -1.
Otherwise, in the first line print one integer n — the number of cities in Berland. In the each of the next n - 1 line print two integers — the ids of cities that are connected by a road. Each road should be printed exactly once. You can print the roads and the cities connected by a road in any order.
If there are multiple answers, print any of them. Remember that the capital has id 1.
Examples
input
7 3 3
2 3 1
output
7
1 3
2 1
2 6
2 4
7 4
3 5
input
14 5 6
4 4 2 2 1
output
14
3 1
1 4
11 6
1 2
10 13
6 10
10 12
14 12
8 4
5 1
3 7
2 6
5 9
input
3 1 1
2
output
-1
Key
题意:给一棵一共有n个结点(包括根节点)的树,一共有t+1层。除第一层只有一个根节点外,给出了其他每层的节点数。已知有k个结点没有子节点,要求用n−1个树枝连接所有结点,给出一种可能的连法。
可能的连法(可能)有很多,只要输出随便其中一个即可。
思路:先遍历一遍所有层,求出可行的最大最小的maxk、mink。如果题目给的k不在此范围内,则说明不能组成符合要求的树。这是唯二输出“-1”的情况。
求能产生的最少的无儿子结点:
求能产生的最多的无儿子结点:
令needk=k−mink,则除去一定有的无儿子结点,还有needk个无儿子结点需要手动产生。
然后遍历每一层,自己做出needk个无儿子结点即可。无需建立树,边遍历边输出。
第一次写出了最后一题,开心的一匹(ง •̀▿•́)ง。虽然写了就知道并不难。。。
Code
#include<cstdio>
int n, t, k;
int arr[200010];
int main()
{
scanf("%d%d%d", &n, &t, &k);
arr[0] = 1;
for (int i = 1;i <= t;++i) {
scanf("%d", arr + i);
}
arr[t + 1] = 0;
int mink = 0;
int maxk = 0;
for (int i = 0;i <= t;++i) {
maxk += arr[i] - 1;
if (arr[i] > arr[i + 1])
mink += arr[i] - arr[i + 1];
}
++maxk;
if (mink > k || maxk < k) {
printf("-1");
return 0;
}
printf("%d\n", n);
int needk = k - mink; // dead-ends that needed to created by myself
int nown = 1;
for (int i = 0;i != t;++i) {
int nextn = nown + arr[i];
if (!needk) {
if (arr[i + 1] >= arr[i]) {
int err = arr[i + 1] - arr[i] + 1;
int rem = arr[i] - 1;
for (int j = 0;j != err;++j) {
printf("%d %d\n", nown, nextn++);
}
++nown;
for (int j = 0;j != rem;++j) {
printf("%d %d\n", nown++, nextn++);
}
}
else { // arr[i + 1] < arr[i]
for (int j = 0;j != arr[i + 1];++j) {
printf("%d %d\n", nown++, nextn++);
}
nown += arr[i] - arr[i + 1];
}
}
else {
if (arr[i + 1] >= arr[i]) {
int nextnown = nextn;
int nextnextn = nextn + arr[i + 1];
int err = arr[i + 1] - arr[i] + 1;
for (int j = 0;j != err;++j) {
printf("%d %d\n", nown, nextn++);
}
while (nextn != nextnextn) {
if (!needk) break;
--needk;
printf("%d %d\n", nown, nextn++);
}
++nown;
while (nextn != nextnextn) {
printf("%d %d\n", nown++, nextn++);
}
nown = nextnown;
}
else { // arr[i + 1] < arr[i]
int nextnown = nextn;
int nextnextn = nextn + arr[i + 1];
printf("%d %d\n", nown, nextn++);
while (nextn != nextnextn) {
if (!needk) break;
--needk;
printf("%d %d\n", nown, nextn++);
}
++nown;
while (nextn != nextnextn) {
printf("%d %d\n", nown++, nextn++);
}
nown = nextnown;
}
}
}
return 0;
}
[刷题]Codeforces 746G - New Roads的更多相关文章
- [刷题]Codeforces 794C - Naming Company
http://codeforces.com/contest/794/problem/C Description Oleg the client and Igor the analyst are goo ...
- [刷题codeforces]650A.637A
650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...
- [刷题codeforces]651B/651A
651B Beautiful Paintings 651A Joysticks 点击可查看原题 651B是一个排序题,只不过多了一步去重然后记录个数.每次筛一层,直到全为0.从这个题里学到一个正确姿势 ...
- [刷题]Codeforces 786A - Berzerk
http://codeforces.com/problemset/problem/786/A Description Rick and Morty are playing their own vers ...
- CF刷题-Codeforces Round #481-G. Petya's Exams
题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...
- CF刷题-Codeforces Round #481-F. Mentors
题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...
- CF刷题-Codeforces Round #481-D. Almost Arithmetic Progression
题目链接:https://codeforces.com/contest/978/problem/D 题解: 题目的大意就是:这组序列能否组成等差数列?一旦构成等差数列,等差数列的公差必定确定,而且,对 ...
- Codeforces 746G New Roads (构造)
G. New Roads ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
随机推荐
- java类的equals()函数和hashCode()函数用法
以前总觉得java类对象很简单,但是今天的一个同事的点播,让我对java的对象有了不一样的理解,下面我来介绍一下equals()和hashCode()的用法: 先粘一段代码: public class ...
- Unity3D Layer要点
简介 Layer可以用于光照的分层和物理碰撞的分层,这样可以很好地进行性能优化 数据结构 Layer在Unity中有3中呈现方式:1.string名字,2.int层索引 ...
- 20155304 2016-2017-2 《Java程序设计》第六周学习总结
20155304 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 第十章 串流设计的概念 无论来源和目的地实体形式是什么,只要取得InputStream和Ou ...
- c++内存优化:二级间接索引模式内存池
/********************************************************* 在一些不确定内存总占用量的情形下,频繁的使用new申请内存,再通过链表 进行索引似 ...
- 老李分享: 并行计算基础&编程模型与工具
在当前计算机应用中,对高速并行计算的需求是广泛的,归纳起来,主要有三种类型的应用需求: 计算密集(Computer-Intensive)型应用,如大型科学工程计算与数值模拟: 数据密集(Data-In ...
- windows下安装zabbix_agent
Server端在linux系统上,server端版本为2.2.6,是以前就装好的已经跑了很久的稳定版.目前的需求是要将新业务的服务器添加到该监控队列.而这些服务器是windows系统. 第一次下载了最 ...
- 和我一步步部署 kubernetes 集群
和我一步步部署 kubernetes 集群 本系列文档介绍使用二进制部署最新 kubernetes v1.6.1 集群的所有步骤,而不是使用 kubeadm 等自动化方式来部署集群: 在部署的过程中, ...
- druid 搭建集群环境
下载druid 下载地址 http://static.druid.io/artifacts/releases/druid-services-0.6.145-bin.tar.gz 解压 tar -zxv ...
- 浅谈MVC页面之间参数传递
关于MVC页面之间的传值,有多种方式,下面,我们就Html.RenderAction 方式 和 Html.RenderPartial 方式 来给大家分享一下有什么不同. 一.Html.RenderAc ...
- 除去ubuntu的grub引导
除去ubuntu的grub引导 step如下>> 进入ubuntu终端 sudo gedit /etc/defauli/grub 将 #GRUB_HIDDEN_TIMEOUT=0 最前面的 ...