CodeForces 746G. New Roads

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 nt and k (2 ≤ n ≤ 2·1051 ≤ 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
题意: 构造一棵n个节点的树 其中深度为i的有a[i]个节点 共有k个叶节点 求一个方案

首先我们钦定同一深度的点都指向同一个父亲 这样叶子节点最多

然后从下到上进行调整 用两个指针 如果一个指针指向了尽头就跳到上一层

然后输出即可

另外C和E都FST了好不爽啊 C的边界条件挂了好气啊

#include <bits/stdc++.h>

using namespace std;

const int maxn = 200020;

int cur, cnt, l[maxn], r[maxn], f[maxn], n, t, k, a[maxn];

int now, L[maxn];

int main()
{
	scanf( "%d%d%d", &n, &t, &k );
	l[ 0 ] = r[ 0 ] = 1;
	L[ 0 ] = 2;
	for( int i = 1 ; i <= t ; i++ )
	{
		scanf( "%d",&a[ i ] );
		l[ i ] = r[ i - 1 ] + 1;
		L[ i ] = l[ i ] + 1;
		r[ i ] = l[ i ] + a[ i ] - 1;
		for( int j = l[ i ] ; j <= r[ i ] ; j++ )
		{
			f[ j ] = l[ i - 1 ];
			if( j != l[ i ] || i == t ) cnt++ ;
		}
	}
	if( cnt < k ) return puts("-1"), 0;
	int ret = t, now = l[ ret ] + 1;
	while( cnt > k )
	{
		if( !ret ) return puts("-1"),0;
		if( L[ ret - 1 ] == r[ ret - 1 ] + 1 ) { now = l[ --ret ] + 1; continue; }
		if( now == r[ ret ] + 1 ) { now = l[ --ret ] + 1; continue; }
		f[ now ] = L[ ret - 1 ];
		cnt--;
		now++;
		L[ ret - 1 ]++;
	}
	printf("%d\n",n);
	for( int i = 2 ; i <= n ; i++ )
		printf( "%d %d\n",i,f[ i ] );
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值