排位赛3-Photoshoot
题目
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.
题意
有n个数,a1…an,现在由他们得出一个新的数组,b1…bn,bi=ai+a(i+1),现在给出b数组,求出合法的且字典序最小的a数组。
解法
因为n只有1000,所以我们可以直接模拟a1,然后求出整个a数组,再判断是否合法就可以了。签到题。
代码:
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
int a[1010],b[1010],c[1010];
int n;
bool flag;
int main()
{
scanf("%d",&n);
for (int i=1;i<n;i++) scanf("%d",&b[i]);
for (int i=1;i<=n;i++)
{
a[1]=i;
for (int i=1;i<=n;i++) c[i]=0;
if (a[1]>0 && a[1]<=n) c[a[1]]++;
for (int j=2;j<=n;j++)
{
a[j]=b[j-1]-a[j-1];
if (a[j]>0 && a[j]<=n) c[a[j]]++;
}
flag=true;
for (int i=1;i<=n;i++) if (c[i]!=1) flag=false;
if (flag) break;
}
for (int i=1;i<=n;i++) printf("%d ",a[i]);
return 0;
}