CodeForces - 231B Magic, Wizardry and Wonders
2017-11-26 19:26
603 查看
B. Magic, Wizardry and Wonders
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
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.
Examples
input
output
input
output
input
output
题意:本来有n张牌,每个牌上有一个数字,范围是1~l,用倒数第二张减倒数第一张,然后去掉刚刚的两张,再把这个数放在最后,不断重复这个操作最终剩下数字d(其实就是把所有奇数位上的数相加减去偶数位上的数)。
给出上述的数字,求最开始n张牌可能的序列,如果不存在可能的序列,输出-1。
思路:分d>0和d<=0两种情况。
当d>0时,因为d=奇数位和-偶数位和,奇数位全取l,偶数位全取1为d最大的情况,若给定的d比这个值大则输出-1。
偶数位和是固定的,用关系求出奇数位和,先平均分到每个奇数位,然后把多出来的再分开即可。
d<=0时相似。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define max_ 100010
using namespace std;
int n,d,l;
int num[110];
int main()
{
while(cin>>n>>d>>l)
{
int od=(n+1)/2;
int even=n/2;
if(d<=0&&od*1-even*l>d)
{
printf("-1\n");
continue;
}
if(d>0&&od*l-even*1<d)
{
printf("-1\n");
continue;
}
int a,b;
if(d>0)
{
b=even*1;
a=d+b;
int c=a%od;
printf("%d",a/od);
for(int i=2;i<=n;i++)
{
if(i&1)
{
if(c)
printf(" %d",a/od+1),c--;
else
printf(" %d",a/od);
}
else
{
printf(" 1");
}
}
}
else
{
a=od*1;
b=a-d;
int c=b%even;
printf("1 %d",b/even);
for(int i=3;i<=n;i++)
{
if(i&1)
{
printf(" 1");
}
else
{
if(c)
printf(" %d",b/even+1),c--;
else
printf(" %d",b/even);
}
}
}
printf("\n");
}
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
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.
Examples
input
3 3 2
output
2 1 2
input
5 -4 3
output
-1
input
5 -4 4
output
2 4 1 4 1
题意:本来有n张牌,每个牌上有一个数字,范围是1~l,用倒数第二张减倒数第一张,然后去掉刚刚的两张,再把这个数放在最后,不断重复这个操作最终剩下数字d(其实就是把所有奇数位上的数相加减去偶数位上的数)。
给出上述的数字,求最开始n张牌可能的序列,如果不存在可能的序列,输出-1。
思路:分d>0和d<=0两种情况。
当d>0时,因为d=奇数位和-偶数位和,奇数位全取l,偶数位全取1为d最大的情况,若给定的d比这个值大则输出-1。
偶数位和是固定的,用关系求出奇数位和,先平均分到每个奇数位,然后把多出来的再分开即可。
d<=0时相似。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define max_ 100010
using namespace std;
int n,d,l;
int num[110];
int main()
{
while(cin>>n>>d>>l)
{
int od=(n+1)/2;
int even=n/2;
if(d<=0&&od*1-even*l>d)
{
printf("-1\n");
continue;
}
if(d>0&&od*l-even*1<d)
{
printf("-1\n");
continue;
}
int a,b;
if(d>0)
{
b=even*1;
a=d+b;
int c=a%od;
printf("%d",a/od);
for(int i=2;i<=n;i++)
{
if(i&1)
{
if(c)
printf(" %d",a/od+1),c--;
else
printf(" %d",a/od);
}
else
{
printf(" 1");
}
}
}
else
{
a=od*1;
b=a-d;
int c=b%even;
printf("1 %d",b/even);
for(int i=3;i<=n;i++)
{
if(i&1)
{
printf(" 1");
}
else
{
if(c)
printf(" %d",b/even+1),c--;
else
printf(" %d",b/even);
}
}
}
printf("\n");
}
}
相关文章推荐
- CodeForces 231B Magic, Wizardry and Wonders
- Codeforces Round #143 (Div. 2) B. Magic, Wizardry and Wonders
- codeforces 231B Magic, Wizardry and Wonders(超强技巧+思维+观察力)
- Codeforces Round #143 (Div. 2)-B. Magic, Wizardry and Wonders
- codeforce-231B--- Magic, Wizardry and Wonders (思维+构造)
- CodeForces Round #143(231B) - Magic, Wizardry and Wonders
- 【codeforces】Bear and Three Balls(排序,去重)
- 【codeforces】-#621-AWet Shark and Odd and Even(n个数最大偶数和)
- Codeforces-703B Mishka and trip
- codeforces 678C. Joty and Chocolate(容斥)
- Codeforces 842C - Ilya And The Tree 【树上DFS】
- Codeforces-689 D Friends and Subsequences(RMQ+二分)
- 【CodeForces】703C - Chris and Road(思维,好题)
- 4000 codeforces 296B - Yaroslav and Two Strings (DP+容斥)
- 【codeforces】#621B-Wet Shark and Bishops
- codeforces 453B B. Little Pony and Harmony Chest(dp+数论)
- Codeforces_714E:Sonya_and_Problem_Wihtout_a_Legend(DP+想法题)
- Codeforces 366D Dima and Trap Graph 【并查集】
- Codeforces 514 D R2D2 and Droid Army(RMQ+二分)
- Codeforces Good Bye 2017 908F - New Year and Rainbow Roads 贪心+模拟