[AGC074C] PORALIS
Genius_Star · · 题解
或许更好的阅读体验。
根本不是人的构造,感觉完全想不到。
思路:
说一下官方验题人的构造吧。
考虑增量构造。
假设现在有一个长度为
验证一下:
-
当
i = 1 时,p' \operatorname{or} (2^{x + 1} - 1) = ((2^{x + 1} - 1), \cdots, (2^{x + 1} - 1)) ,显然 LIS 是1 。 -
否则
i > 1 时,p' \operatorname{or} a_{i - 1} = (p_1 \operatorname{or} a_{i - 1}, \cdots, p_k \operatorname{or} a_{i - 1}, 2^x + a_{i - 1}) ,显然因为k 合法,所以p_j \operatorname{or} a_{i - 1} 的 LIS 是i - 1 ,然后后面2^x + a_{i - 1} 比前面都大,一定可以拼上,所以 LIS 是i 。
这样每次增量会使得值域扩大两倍,显然不能一直这样到
考虑倍增构造,即知道长度为
验证一下:
-
对于
i = 1 ,显然p' \operatorname{or} (2^{x + 2} - 1) 都是2^{x + 2} - 1 ,LIS 是1 合法。 -
对于
i = 2t ,即p' \operatorname{or} (a_t + 2^{x + 1}) = (p_1 \operatorname{or} a_t + 2^{x + 1}, \cdots, p_k \operatorname{or} a_t + 2^{x + 1}, p_1 \operatorname{or} a_t + 2^x + 2^{x + 1}, \cdots, p_k \operatorname{or} a_t + 2^x + 2^{x + 1}, 2^{x + 1} + a_t) ,分为三段考虑,显然第二段严格全部大于第一轮,由于p_j \operatorname{or} a_t 的 LIS 是t ,所以前面两段 LIS 是2t ,最后因为p_j \operatorname{or} a_t \ge a_t ,所以它不能拼到后面,于是总 LIS 就是2t 。 -
对于
i = 2t + 1 ,即p' \operatorname{or} a_t = (p_1 \operatorname{or} a_t, \cdots, p_k \operatorname{or} a_t, p_1 \operatorname{or}a_t + 2^x, \cdots, p_k \operatorname{or} a_t + 2^x, 2^{x + 1} + a_t) ,一样分为三段考虑,每段都严格比上一段最大值大,所以 LIS 是每段 LIS 之和,所以是2t + 1 。
于是得证;这样会使得值域最多增大
那么构造
-
当
n 是奇数,递归下去到\frac{n - 1}{2} 构造然后增量过来。 -
否则
n 是偶数,递归下去到n - 1 ,然后增量过来。
显然值域是
完整代码:
#include<bits/stdc++.h>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define popcnt(x) __builtin_popcount(x)
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
using namespace std;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 1e9;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
int T, n;
inline pair<vector<int>, vector<int>> dfs(int n){
if(n == 1)
return {{0}, {0}};
if(n == 2)
return {{0, 1}, {1, 0}};
if(n & 1){
auto t = dfs((n - 1) >> 1);
auto p = t.fi, a = t.se;
int m = (n - 1) >> 1;
int X = 0;
for(int i = 0; i < m; ++i){
int x = __lg(p[i]);
if((1 << x) <= p[i])
++x;
X = max(X, x);
x = __lg(a[i]);
if((1 << x) <= a[i])
++x;
X = max(X, x);
}
for(int i = 0; i < m; ++i)
p.push_back(p[i] + (1 << X));
p.push_back(1 << (X + 1));
vector<int> A;
A.push_back((1 << (X + 2)) - 1);
for(int i = 0; i < m; ++i){
A.push_back(a[i] + (1 << (X + 1)));
A.push_back(a[i]);
}
return {p, A};
}
else{
auto t = dfs(n - 1);
auto p = t.fi, a = t.se;
int X = 0;
for(int i = 0; i < n - 1; ++i){
int x = __lg(p[i]);
if((1 << x) <= p[i])
++x;
X = max(X, x);
x = __lg(a[i]);
if((1 << x) <= a[i])
++x;
X = max(X, x);
}
p.push_back(1 << X);
a.insert(a.begin(), (1 << (X + 1)) - 1);
return {p, a};
}
}
inline void solve(){
n = read();
auto t = dfs(n);
auto p = t.fi, a = t.se;
for(auto v : p){
write(v);
putchar(' ');
}
putchar('\n');
for(auto v : a){
write(v);
putchar(' ');
}
putchar('\n');
}
int main(){
T = read();
while(T--)
solve();
return 0;
}