H. Photoshoot 枚举

GDUT 2020寒假训练 排位赛三 H

原题链接

  • H. Photoshoot

题目

H. Photoshoot 枚举_第1张图片
outputstandard output
Farmer John is lining up his N cows (2≤N≤103), numbered 1…N, for a photoshoot. FJ initially planned for the i-th cow from the left to be the cow numbered ai, and wrote down the permutation a1,a2,…,aN on a sheet of paper. Unfortunately, that paper was recently stolen by Farmer Nhoj!

Luckily, it might still possible for FJ to recover the permutation that he originally wrote down. Before the sheet was stolen, Bessie recorded the sequence b1,b2,…,bN−1 that satisfies bi=ai+ai+1 for each 1≤i Based on Bessie’s information, help FJ restore the “lexicographically minimum” permutation a that could have produced b. A permutation x is lexicographically smaller than a permutation y if for some j, xi=yi for all i

Input
The first line of input contains a single integer N.
The second line contains N−1 space-separated integers b1,b2,…,bN−1.
Output
A single line with N space-separated integers a1,a2,…,aN.

样例

input

5
4 6 7 6

output
3 1 5 2 4

题目大意

有一串长度为n的数组a,和长度为n-1的数组b,两数组满足 b i = a i + a i + 1 ,   1 ≤   i < n b_i=a_i+a_{i+1},\ 1\leq\ ibi=ai+ai+1, 1 i<n.现在给出长度n和数组b,求出满足条件字典序最小的a数组

思路

枚举
从1到n枚举a的第一位数字,然后通过 a [ j + 1 ] = b [ j ] − a [ j ] a[j+1]=b[j]-a[j] a[j+1]=b[j]a[j]推出a的下一位数,然后检查当前数字是否被用过,最后满足条件的一定是字典序最小的。

代码

#include
#include
#include
#include
using namespace std;
bool check[1009];
int a[1009];
int b[1009];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<n;i++)
	{
		cin>>b[i];
	}
	for(int i=1;i<=n;i++)
	{
		a[1]=i;
		bool flag=true;
		memset(check,true,sizeof check);
		check[i]=false;
		for(int j=1;j<n;j++)
		{
			a[j+1]=b[j]-a[j];
			if(a[j+1]<=0||(check[a[j+1]]==false))
			{
				flag=false;
				break;
			}
			check[a[j+1]]=false;
		}
		if(flag)
		{
			for(int j=1;j<=n;j++)
			{
				cout<<a[j]<<" ";
			
			}
			return 0;
		}
	}
	
	return 0;
}

你可能感兴趣的:(排位赛)