标签:== name sub ati i++ rest || contest its
给n-1个数,
qi=pi+1−pi
p为1-n的排列序列
q为给的序列。
根据q求出p, 求不出时为-1。
另p数组首个为0.求出p数组,再得到p数组中最小的值,将p中所有数减掉最小值再加1得到原p。
同时记录原p中每个数出现的次数。超过一次,说明不能还原 。
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int MAXN = 2e5 + 10; LL a[MAXN]; map<LL, int> b; int main() { int n, v; cin >> n; a[0] = 0; LL sum = 0, mmin = 0; for (int i = 2;i <= n;i++) { cin >> v; a[i] = a[i - 1] + v; sum += a[i]; mmin = min(mmin, a[i]); } for (int i = 1;i <= n;i++) { a[i] = a[i] - mmin + 1; if (b[a[i]] == 1 || a[i] > n) { cout << -1 << endl; return 0; } b[a[i]] = 1; } for (int i = 1;i <= n;i++) cout << a[i] << ‘ ‘ ; cout << endl; return 0; }
C. Polycarp Restores Permutation
标签:== name sub ati i++ rest || contest its
原文地址:https://www.cnblogs.com/YDDDD/p/10570961.html