# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1127241 | Gray | Longest beautiful sequence (IZhO17_subsequence) | C++20 | 291 ms | 327680 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define ff first
#define ss second
#define ln "\n"
#define mp make_pair
#define popcnt __builtin_popcount
void solve(){
ll n; cin >> n;
vector<ll> a(n), k(n);
for (ll i=0; i<n; i++){
cin >> a[i];
}
for (ll i=0; i<n; i++){
cin >> k[i];
}
vector<vector<vector<pair<ll, ll>>>> past((1<<10), vector<vector<pair<ll, ll>>>(1<<10, vector<pair<ll, ll>>(11, {0, -1})));
vector<ll> ldp(n, 1), dep(n, -1);
for (ll i=0; i<n; i++){
ll l = a[i]>>10, r=a[i]&((1<<10)-1);
for (ll j=0; j<(1<<10); j++){
if (k[i]>=popcnt(j&l) and k[i]-popcnt(j&l)<=10 and ldp[i]<past[j][r][k[i]-popcnt(j&l)].ff+1){
ldp[i]=past[j][r][k[i]-popcnt(j&l)].ff+1;
dep[i]=past[j][r][k[i]-popcnt(j&l)].ss;
}
}
for (ll j=0; j<(1<<10); j++){
past[l][j][popcnt(r&j)]=max(past[l][j][popcnt(r&j)], {ldp[i], i});
}
}
ll mx = *max_element(ldp.begin(), ldp.end());
cout << mx << ln;
for (ll i=0; i<n; i++){
if (ldp[i]==mx){
ll cur = i;
vector<ll> res;
while (cur!=-1){
res.push_back(cur+1);
cur=dep[cur];
}
reverse(res.begin(), res.end());
for (auto ch:res) cout << ch << " ";
cout << ln; break;
}
}
}
int main(){
#ifdef LOCAL
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#else
freopen("subsequence.in", "r", stdin);
freopen("subsequence.out", "w", stdout);
#endif
auto start = chrono::high_resolution_clock::now();
ll t=1;
// cin >> t;
while (t--) solve();
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << setprecision(0) << fixed << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
}
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... |