Submission #92852

#TimeUsernameProblemLanguageResultExecution timeMemory
92852SamAndLongest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
322 ms2936 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;

int bb = 20;
int bc(int x)
{
    int ans = 0;
    for (int i = 0; i < bb; ++i)
    {
        if ((x & (1 << i)))
            ++ans;
    }
    return ans;
}

int n;
int a[N], b[N];

int dp[N];
int p[N];
void solv1()
{
    for (int i = 1; i <= n; ++i)
    {
        dp[i] = 1;
        p[i] = 0;
        for (int j = 1; j < i; ++j)
        {
            int x = a[i] & a[j];
            if (bc(x) == b[i])
            {
                if (dp[j] + 1 > dp[i])
                {
                    dp[i] = dp[j] + 1;
                    p[i] = j;
                }
            }
        }
    }
    int maxi = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (dp[i] > dp[maxi])
        {
            maxi = i;
        }
    }
    vector<int> ans;
    for (int i = maxi; i; i = p[i])
        ans.push_back(i);
    reverse(ans.begin(), ans.end());
    printf("%d\n", ans.size());
    for (int i = 0; i < ans.size(); ++i)
        printf("%d ", ans[i]);
}

int maxp[1 << 9];
void solv2()
{
    for (int i = 1; i <= n; ++i)
    {
        dp[i] = 1;
        p[i] = 0;
        for (int y = 0; y < (1 << bb); ++y)
        {
            int x = a[i] & y;
            if (bc(x) == b[i])
            {
                if (maxp[y])
                {
                    if (dp[maxp[y]] + 1 > dp[i])
                    {
                        dp[i] = dp[maxp[y]] + 1;
                        p[i] = maxp[y];
                    }
                }
            }
        }
        if (dp[i] > dp[maxp[a[i]]])
            maxp[a[i]] = i;
    }
    int maxi = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (dp[i] > dp[maxi])
        {
            maxi = i;
        }
    }
    vector<int> ans;
    for (int i = maxi; i; i = p[i])
        ans.push_back(i);
    reverse(ans.begin(), ans.end());
    printf("%d\n", ans.size());
    for (int i = 0; i < ans.size(); ++i)
        printf("%d ", ans[i]);
}

int main()
{
    //freopen("input2.txt", "r", stdin);
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &b[i]);
    if (n <= 5000)
        solv1();
    else
    {
        bb = 8;
        solv2();
    }
    return 0;
}

Compilation message (stderr)

subsequence.cpp: In function 'void solv1()':
subsequence.cpp:53:30: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type {aka long unsigned int}' [-Wformat=]
     printf("%d\n", ans.size());
                    ~~~~~~~~~~^
subsequence.cpp:54:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < ans.size(); ++i)
                     ~~^~~~~~~~~~~~
subsequence.cpp: In function 'void solv2()':
subsequence.cpp:95:30: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<int>::size_type {aka long unsigned int}' [-Wformat=]
     printf("%d\n", ans.size());
                    ~~~~~~~~~~^
subsequence.cpp:96:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < ans.size(); ++i)
                     ~~^~~~~~~~~~~~
subsequence.cpp: In function 'int main()':
subsequence.cpp:103:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
subsequence.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a[i]);
         ~~~~~^~~~~~~~~~~~~
subsequence.cpp:107:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &b[i]);
         ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...