Codeforces Round #143 (Div. 2)-B. Magic, Wizardry and Wonders


B. Magic, Wizardry and Wonders
time limit per test
2 seconds
memory limit per test
256 megabytes
standard input
standard output

Vasya the Great Magician and Conjurer loves all kinds of miracles and wizardry. In one wave of a magic wand he can turn an object into something else. But, as you all know, there is no better magic in the Universe than the magic of numbers. That's why Vasya adores math and spends a lot of time turning some numbers into some other ones.

This morning he has n cards with integers lined up in front of him. Each integer is not less than 1, but not greater than l. When Vasya waves his magic wand, two rightmost cards vanish from the line and a new card magically appears in their place. It contains the difference between the left and the right numbers on the two vanished cards. Vasya was very interested to know what would happen next, and so he waved with his magic wand on and on, until the table had a single card left.

Suppose that Vasya originally had the following cards: 4, 1, 1, 3 (listed from left to right). Then after the first wave the line would be: 4, 1, -2, and after the second one: 4, 3, and after the third one the table would have a single card with number 1.

Please note that in spite of the fact that initially all the numbers on the cards were not less than 1 and not greater than l, the numbers on the appearing cards can be anything, no restrictions are imposed on them.

It is now evening. Vasya is very tired and wants to return everything back, but does not remember which cards he had in the morning. He only remembers that there were n cards, they contained integers from 1 to l, and after all magical actions he was left with a single card containing number d.

Help Vasya to recover the initial set of cards with numbers.


The single line contains three space-separated integers: n (2 ≤ n ≤ 100) — the initial number of cards on the table, d (|d| ≤ 104) — the number on the card that was left on the table after all the magical actions, and l (1 ≤ l ≤ 100) — the limits for the initial integers.


If Vasya is mistaken, that is, if there doesn't exist a set that meets the requirements given in the statement, then print a single number -1, otherwise print the sought set containing n integers from 1 to l. Separate the integers by spaces. Print the integers in the order, in which they were written on the cards from left to right. If there are several suitable sets of numbers, you can print any of them.

3 3 2
2 1 2 
5 -4 3
5 -4 4
2 4 1 4 1 

  用maxs[i], mins[i]表示只剩i张卡片时第i张卡片上数字的最大值和最小值,对于题中给出的n, L.可以确定maxs[n] = L,

 mins[n] = 1, 则可以推出maxs[n-1] = L - mins[n], mins[n-1] = 1 - maxs[n]以此类推,求出maxs和mins.

题中给出的d是桌上只剩一张卡片时的卡片上的数字,若d < mins[1] || d > maxs[1]则无解。

否则,a = 1 - d, b = L - d.则[a, b]和[mins[2], maxs[2]]两个区间必定有交集.

1.mins[2] <= a <= maxs[2]那么最初第一张卡牌的数字就可以确定为1,那么现在桌上有两张卡片,第二张卡片数字为1 - d.

2. mins[2] <= b <= maxs[2]那么最初第一张卡牌的数字就可以确定为l,那么现在桌上有两张卡片,第二张卡片数字为L- d.

上述两步只要执行满足条件的一步就行,不断重复上述步骤,从而求出第2, 3, 4, ..n张卡牌原来上的数字.

#include <bits/stdc++.h>
#define maxn 105

using namespace std;
typedef long long ll;

int maxs[maxn], mins[maxn], ans[maxn];
int main(){
	int n, d, l;
	scanf("%d%d%d", &n, &d, &l);
	mins[0] = 1;
	maxs[0] = l;
	for(int i = 1; i < n; i++){
		maxs[i] = l - mins[i-1];
		mins[i] = 1 - maxs[i-1];
	if(d > maxs[n-1] || d < mins[n-1]){
		return 0;
	for(int i = n-1; i >= 1; i--){
		int k1 = l - d;
		int k2 = 1 - d;
		if(k1 >= mins[i-1] && k1 <= maxs[i-1]){
			ans[i] = l;
			d = k1;
			ans[i] = 1;
			d = k2;
		if(i == 1)
		 ans[i-1] = d;
	printf("%d", ans[n-1]);
	for(int i = n-2; i >= 0; i--)
	 printf(" %d", ans[i]);
	return 0;

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26122039/article/details/52346555
个人分类: 贪心
想对作者说点什么? 我来说一句



Codeforces Round #143 (Div. 2)-B. Magic, Wizardry and Wonders