本文主要是介绍Photoshoot//枚举//排位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.
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.
题意
用1到n排成序列,给出的数为按顺序相邻相加的n-1个和,求符合的序列
思路
暴力枚举第一个数,然后检验是否符合即可
代码
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#define pi 3.1415926
using namespace std;
typedef long long ll;
const ll inf=100000000000;
int b[2000],a[2000];
bool vis[2000];
int main()
{int n;cin>>n;for(int i=1;i<n;i++)cin>>b[i];for(int i=1;i<=n;i++){memset(vis,false,sizeof(vis));vis[i]=true;a[0]=i;int judge=1;for(int j=1;j<n;j++){if(b[j]-a[j-1]>0&&!vis[b[j]-a[j-1]]){vis[b[j]-a[j-1]]=true;a[j]=b[j]-a[j-1];}else{judge=0;break;}}if(judge==1) break;}A:for(int i=0;i<n-1;i++)cout<<a[i]<<" ";cout<<a[n-1]<<endl;return 0;
}
注意
这篇关于Photoshoot//枚举//排位3的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!