题解:P17233 [Algo Beat Contest 017 B] 线性筛
本题考察的是一维动态规划。
定义
- 若
i 是完全立方数,则dp[i]=1 。 - 若
i 不是完全立方数:首先我们可以知道x 之前有\sqrt[3]{x} 个立方数,则dp[i]=dp[i-\lfloor\sqrt[3]{x}\rfloor]+1 即可。
代码
/*
* @Author: byk_ALEX
* @Date: 2026-08-10 14:05:06
* @Algorithm:
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
int n;
int cnt, now;
int a[maxn];
int dp[maxn];//定义dp[i]为a[i]在第几轮被删掉
vector<int> ans[maxn];
bool check(int x){
int k = round(cbrt(x));
if(k * k * k == x){
return true;
}
return false;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){
if(check(i)){
dp[i] = 1;
}
else{
int now = floor(cbrt(i));
dp[i] = dp[i - now] + 1;
}
ans[dp[i]].push_back(a[i]);
cnt = max(cnt, dp[i]);
}
cout << cnt << '\n';
for(int i = 1; i <= cnt; i++){
int len = ans[i].size();
for(int j = 0; j < len; j++){
cout << ans[i][j] << " ";
}
cout << '\n';
}
return 0;
}
本题解使用了 deepseek-v4-pro 进行润色以保证格式的正确。