Submission #1351787

#TimeUsernameProblemLanguageResultExecution timeMemory
1351787dex111222333444555Longest beautiful sequence (IZhO17_subsequence)C++20
100 / 100
1615 ms91896 KiB
#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
#define ii pair<int, int>
#define fi first
#define se second
#define siz(v) (int)(v).size()
#define lli pair<long long, int>
#define dbg(x) "[" #x " = " << x << "]"
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))

using namespace std;

bool M1;
const long long inf = 1e18 + 123;
const int mod = 1e9 + 7, infINT = 1e9 + 1321, LOG = 10;

template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}

#define cntb(x) (__builtin_popcount(x))
#define left(x) (x & (MASK(LOG) - 1))
#define right(x) (x >> (LOG))

const int MAXN = 1e5 + 5;

int numVal, val[MAXN], req[MAXN];
int f[MASK(LOG)][MASK(LOG)][LOG + 1], pos[MASK(LOG)][MASK(LOG)][LOG + 1], trace[MAXN];

void input(){
    cin >> numVal;
    for(int i = 1; i <= numVal; i++) cin >> val[i];
    for(int i = 1; i <= numVal; i++) cin >> req[i];
}

string binary(const int &mask){
    string s = "";
    for(int i = 0; i < LOG; i++) s += (BIT(mask, i)) + '0';
    return s;
}

void solve(){
    int res = 0, cur = -1;

    for(int i = 1; i <= numVal; i++){

        int L = left(val[i]);
        int R = right(val[i]);
        int best = 1;

        for(int l = 0; l < MASK(LOG); l++){
            int x = req[i] - cntb(l & L);
            if (x < 0 || x > LOG) continue;
            if (maximize(best, f[l][R][x] + 1)){
                trace[i] = pos[l][R][x];
            }
        }

        for(int r = 0; r < MASK(LOG); r++){
            if (maximize(f[L][r][cntb(r & R)], best)){
                pos[L][r][cntb(r & R)] = i;
            }
        }

        if (maximize(res, best)) cur = i;
    }

    cout << res << '\n';

    vector<int > ans;

    while(cur > 0){
        ans.push_back(cur);
        cur = trace[cur];
    }

    reverse(all(ans));
    for(int x: ans) cout << x << ' '; cout << '\n';
}

bool M2;
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    input();
    solve();
    cerr << (&M2 - &M1) / 1048576 << " mb\n";
    cerr << (1.0 * clock()) / CLOCKS_PER_SEC << ".s\n";
}

Compilation message (stderr)

subsequence.cpp: In function 'int main()':
subsequence.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
subsequence.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...