AT_agc007_b [AGC007B] Construct Sequences 题解
题意简述:给定一个 \(1,2,\dots,n\) 的排列 \(P\),需要构造两个序列 \(A,B\),满足:
- \(A\) 单调递增;
- \(B\) 单调递减;
- \(A_{P_1}+B_{P_1}<A_{P_2}+B_{P_2}<\dots<A_{P_n}+B_{P_n}\);
- \(1\le A_i,B_i\le 10^9\)。
\(1\le n\le 2\times 10^4\)。
思路:显然第三个条件最为严格,因此考虑在优先满足第三个条件的情况下尽量满足前两个条件。不妨先将所有的 \(A_i+B_i\) 统一,再在原本的基础上进行微扰调整。有一个构造方式:\(A_i=n\cdot i\),\(B_i=n\cdot(n-i+1)\),这样就同时满足了前两个条件。此时我们需要依次满足 \(P_1,P_2,\dots,P_n\) 的条件,显然只需要对 \(A/B\) 其中一方进行调整即可,后面对 \(A\) 调整。由于我们需要保持前两个条件,因此考虑有什么数加上后一定不会影响到数列的单调性。不难发现 \(A\) 原来的公差为 \(n\),只要每次微扰的范围在 \(n\) 以内,就一定不会破坏单调性。因此只需要对 \(i=1,2,\dots,n\),令 \(A_{P_i}+i\to A_{P_i}\) 即可。
AC CODE:
#include<bits/stdc++.h>
#define y0 y_0
#define y1 y_1
#define fi first
#define se second
#define endl '\n'
#define vec vector
#define em emplace
#define pb push_back
#define eb emplace_back
#define lc(x) x << 1
#define rc(x) x << 1 | 1
#define lowbit(x) ((x) & (-(x)))
#define popcount(x) __builtin_popcount(x)
#define ckmin(x, y) x = (x) <= (y) ? (x) : (y)
#define ckmax(x, y) x = (x) >= (y) ? (x) : (y)
#define debug(x) cerr << #x << " = " << x << endl
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
// #define MultiTest
const double EPS = 1e-8;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e4 + 5;
int n, p[N], a[N], b[N];
void prework() {
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) cin >> p[i];
for(int i = 1; i <= n; i++) a[i] = N * i, b[i] = N * (n - i + 1);
for(int i = 1; i <= n; i++) {
a[p[i]] += i;
}
for(int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
for(int i = 1; i <= n; i++) cout << b[i] << " ";
}
int main() {
IOS;
prework();
int T = 1;
#ifdef MultiTest
cin >> T;
#endif
while(T--)
solve();
return 0;
}

浙公网安备 33010602011771号