来源:codeforces 2020 GDUT Rating Contest III (Div. 2) CF链接
题目:
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<N.
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<j and xj<yj (in other words, the two permutations are identical up to a certain point, at which x is smaller than y). It is guaranteed that at least one such a exists.
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.
Example
inputCopy
5
4 6 7 6
outputCopy
3 1 5 2 4
Note
a produces b because 3+1=4, 1+5=6, 5+2=7, and 2+4=6.
题意:给一个n-1长度的序列a,拆分出一个长度为n、包含1-n的正整数的序列b,满足b[i]+b[i+1]=a[i]
思路:第一个数组确定了后面所有的数字都确定了,故暴力枚举第一个数字即可。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
int n,a[1510],b[1510],c[1510],flag=1;
int main()
{
cin>>n;
for (int i=1;i<n;i++)
scanf("%d",&b[i]);
for (int k=1;k<b[1];k++)
{
flag=0;
memset(c,0,sizeof(c));
a[1]=k;
c[a[1]]++;
for (int i=2;i<=n;i++)
{
a[i]=b[i-1]-a[i-1];
if (a[i]<=0 || a[i]>n || c[a[i]]) flag=1;
if (flag) break;
c[a[i]]++;
}
if (flag) continue;
else break;
}
for (int i=1;i<=n;i++)
printf("%d ",a[i]);
return 0;
}