제출 #989233

#제출 시각아이디문제언어결과실행 시간메모리
989233tch1cherinLongest beautiful sequence (IZhO17_subsequence)C++17
100 / 100
1480 ms92092 KiB
#pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,popcnt,bmi2,fma") #include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; const int P = 10; int dp[1 << P][1 << P][P + 1] = {}, prv[1 << P][1 << P][P + 1]; int path[MAX_N]; int A[MAX_N], K[MAX_N]; int main() { cin.tie(nullptr)->sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } for (int i = 0; i < n; i++) { cin >> K[i]; } pair<int, int> best = {-1, -1}; for (int i = 0; i < n; i++) { int answer = 1; path[i] = -1; int left = A[i] >> P, right = A[i] & ((1 << P) - 1); for (int lhs = 0; lhs < (1 << P); lhs++) { int bits = K[i] - __builtin_popcount(lhs & left); if (bits >= 0 && bits <= P && answer < dp[lhs][right][bits] + 1) { answer = dp[lhs][right][bits] + 1; path[i] = prv[lhs][right][bits]; } } for (int rhs = 0; rhs < (1 << P); rhs++) { int bits = __builtin_popcount(rhs & right); if (dp[left][rhs][bits] < answer) { dp[left][rhs][bits] = answer; prv[left][rhs][bits] = i; } } best = max(best, {answer, i}); } vector<int> answer; for (int i = best.second; i >= 0; answer.push_back(i), i = path[i]); reverse(answer.begin(), answer.end()); int k = (int)answer.size(); cout << k << "\n"; for (int i = 0; i < k; i++) { cout << 1 + answer[i] << " \n"[i + 1 == k]; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...