问题 M: Construct Sequences----------------------------------------思维(构造+套路题)

题目描述
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值