题目描述
You are given a permutation p of the set {1,2,…,N}. Please construct two sequences of positive integers a1, a2, …, aN and b1, b2, …, bN satisfying the following conditions:
·1≤ai,bi≤109 for all i
·a1<a2<…<aN
·b1>b2>…>bN
·ap1+bp1<ap2+bp2<…<apN+bpN
Constraints
·2≤N≤20,000
·p is a permutation of the set {1,2,…,N}
输入
The input is given from Standard Input in the following format:
N
p1 p2 … pN
输出
The output consists of two lines. The first line contains a1, a2, …, aN seperated by a space. The second line contains b1, b2, …, bN seperated by a space.
It can be shown that there always exists a solution for any input satisfying the constraints.
样例输入 Copy
【样例1】
2
1 2
【样例2】
3
3 2 1
【样例3】
3
2 3 1
样例输出 Copy
【样例1】
1 4
5 4
【样例2】
1 2 3
5 3 1
【样例3】
5 10 100
100 10 1
提示
样例1解释
a1+b1=6 and a2+b2=8. So this output satisfies all conditions.
解析:
这种题有套路
一般先让该式ap1+bp1<ap2+bp2<…<apN+bpN 转换为等式
ap1+bp1=ap2+bp2=…=apN+bpN
那么我让A数组等于i*n
让B数组等于(n-i+1)*n
那么就可以构成等式了
最后我们再对pi 加加减减就行了
#include<bits/stdc++.h>
using namespace std;
const int N=2e4+100;
int n;
int p[N];
int a[N],b[N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&p[i]);
for(int i=1;i<=n;i++) a[i]=i*n;
for(int i=1;i<=n;i++) b[i]=(n-i+1)*n;
for(int i=1;i<=n;i++)
{
b[p[i]]+=i-1;
}
for(int i=1;i<=n;i++) cout<<a[i]<<" ";
cout<<endl;
for(int i=1;i<=n;i++) cout<<b[i]<<" ";
cout<<endl;
}