ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Photoshoot

2020-02-05 15:39:44  阅读:253  来源: 互联网

标签:int vis maxn permutation FJ Photoshoot line


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.
输入
The first line of input contains a single integer N.
The second line contains N−1 space-separated integers b1,b2,…,bN−1.
输出
A single line with N space-separated integers a1,a2,…,aN.
样例输入

5
4 6 7 6

样例输出

3 1 5 2 4

提示
a produces b because 3+1=4, 1+5=6, 5+2=7, and 2+4=6.

解题思路
暴力枚举第一位数

AC代码

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int b[maxn];
int vis[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=2;i<=n;++i)
    {
        scanf("%d",&b[i]);
    }
    for(int i=1;i<=n;++i)
    {
        a[1]=i;
        int f=0;
        for(int j=2;j<=n;++j)
        {
            int temp=b[j]-a[j-1];
            if(temp<=n&&temp>=1&&vis[temp]!=i)
            {
                a[j]=temp;
                vis[temp]=i;
            }
            else
            {
                f=1;
                break;
            }
        }
        if(f) continue;
        else break;
    }
    for(int i=1;i<=n;++i) printf("%d%c",a[i],i==n?'\n':' ');
    return 0;
}

umbrellalla 发布了6 篇原创文章 · 获赞 0 · 访问量 233 私信 关注

标签:int,vis,maxn,permutation,FJ,Photoshoot,line
来源: https://blog.csdn.net/qq_44096896/article/details/104170804

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有