# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1109755 | Kirill22 | Longest beautiful sequence (IZhO17_subsequence) | C++17 | 10 ms | 49744 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
//long long C(int n, int k) {
// long long res = 1;
// for (int i = 1; i <= n; i++) {
// res *= (n - i + 1);
// res /= i;
// }
// return res;
//}
const int N = 1 << 20, M = 1 << 10;
int cnt[N], dp[N], pr[N], mx[N], a[N], k[N], n;
//set<pair<int, int>> life[21];
int mitm[M][M][11];
void solve() {
for (int i = 1; i < N; i++) {
cnt[i] = cnt[i / 2] + (i % 2);
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
fill(mitm[i][j], mitm[i][j] + 11, -1);
}
}
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> k[i];
}
for (int i = 0; i < n; i++) {
int x = a[i];
dp[i] = 1, pr[i] = i;
for (int msk = 0; msk < (1 << 10); msk++) {
int need = k[i] - cnt[msk & (x >> 10)];
if (need < 0 || need > 10) {
continue;
}
int& j = mitm[msk][x % 1024][need];
if (j != -1 && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
pr[i] = j;
}
}
for (int msk = 0; msk < (1 << 10); msk++) {
int need = cnt[msk & (x % 1024)];
int& j = mitm[x >> 10][msk][need];
if (j == -1 || dp[j] < dp[i]) {
j = i;
}
}
}
pair<int, int> res = {1, 0};
for (int i = 0; i < n; i++) {
res = max(res, {dp[i], i});
}
cout << res.first << '\n';
vector<int> ans = {res.second};
while (pr[res.second] != res.second) {
res.second = pr[res.second];
ans.push_back(res.second);
}
std::reverse(ans.begin(), ans.end());
for (auto& i : ans) {
cout << i + 1 << " ";
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
freopen("subsequence.in", "r", stdin);
freopen("subsequence.out", "w", stdout);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |