Farmer John is lining up his NN cows (2≤N≤1032≤N≤103), numbered 1…N1…N, for a photoshoot. FJ initially planned for the ii-th cow from the left to be the cow numbered ai,ai, and wrote down the permutation a1,a2,…,aNa1,a2,…,aN on a sheet of paper. Unfortunately, that paper was recently stolen by Farmer Nhoj!
Luckily, it might still be possible for FJ to recover the permutation that he originally wrote down. Before the sheet was stolen, Bessie recorded the sequence b1,b2,…,bN−1b1,b2,…,bN−1 that satisfies bi=ai+ai+1bi=ai+ai+1 for each 1≤i<N.1≤i<N.
Based on Bessie's information, help FJ restore the "lexicographically minimum" permutation aa that could have produced bb. A permutation xx is lexicographically smaller than a permutation yy if for some jj, xi=yixi=yi for all i<ji<j and xj<yjxj<yj (in other words, the two permutations are identical up to a certain point, at which xx is smaller than yy). It is guaranteed that at least one such aa exists.
The first line of input contains a single integer N.N.The second line contains N−1N−1 space-separated integers b1,b2,…,bN−1.b1,b2,…,bN−1.
A single line with NN space-separated integers a1,a2,…,aN.a1,a2,…,aN.
5 4 6 7 6
3 1 5 2 4
aa produces bb because 3+1=43+1=4, 1+5=61+5=6, 5+2=75+2=7, and 2+4=6.2+4=6.
Problem credits: Benjamin Qi and Chris Zhang
[/hide]
(Analysis by Benjamin Qi)
For each ii from 11 to N,N, try setting a1=i.a1=i. Then we can determine the rest of the elements of aa by setting ai=bi−1−ai−1ai=bi−1−ai−1 for each 2≤i≤N.2≤i≤N. If this indeed produces a valid permutation (all elements of aa are in [1,N][1,N] and none repeat), then return the result. This runs in O(N2)O(N2) time.
Dhruv Rohatgi's code:
#include <iostream>
#include <algorithm>
using namespace std;
int N;
int b[100000], d[100000], ans[100000];
bool used[100000];
int main() {
freopen("photo.in","r",stdin);
freopen("photo.out","w",stdout);
cin >> N;
for(int i=0;i<N-1;i++)
cin >> b[i];
for(int i=2;i<N;i++)
d[i] = b[i-1]-b[i-2];
for(int a=1;a<=N;a++)
{
ans[0] = a, ans[1] = b[0] - a;
for(int i=2;i<N;i++)
ans[i] = ans[i-2] + d[i];
for(int i=1;i<=N;i++)
used[i] = 0;
bool bad = 0;
for(int i=0;i<N;i++)
{
if(used[ans[i]] || ans[i] <= 0 || ans[i] > N)
{
bad = 1;
break;
}
used[ans[i]] = 1;
}
if(!bad)
{
for(int i=0;i<N;i++)
{
cout << ans[i];
if(i<N-1) cout << ' ';
}
cout << '\n';
return 0;
}
}
}
Bonus: Solve the problem in O(N).
[/hide]
以上就是关于【USACO 2020 January Contest, Bronze Problem 2. Photoshoot】的解答,如需了解学校/赛事/课程动态,可至翰林教育官网获取更多信息。
往期文章阅读推荐:
5金3银!2026 IOAI国际人工智能奥赛收官!中国队取得历史性突破!
AI奥赛2026国家队名单公布! 新赛季翰林助力直通IOAI全球总决赛!

© 2026. All Rights Reserved. 沪ICP备2023009024号-1