Submission #983296

# Submission time Handle Problem Language Result Execution time Memory
983296 2024-05-15T10:00:06 Z Faisal_Saqib Permutation (APIO22_perm) C++17
97 / 100
777 ms 936 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define vll vector<ll>
vector<int> cp(ll k)// O(lg n)
{
    vector<int> ans;
    ll n_req=0;
    ll hb=-1;
    for(int j=59;j>=0;j--)
    {
        ll pw=(1ll<<j);
        if(k&pw)
        {
            if(hb==-1)
            {
                n_req+=j;
                hb=j;
            }
            else
                n_req++;
        }
    }
    ll last=n_req-1;
    int first=0;
    // cout<<last<<endl;
    for(int l=0;l<60 and l<hb;l++)
    {
        if(k&(1ll<<l))
        {
            ans.push_back(first);
            first++;
        }
        if(first<=last)
        {
            ans.push_back(last);
            last--;
        }
    }
    reverse(begin(ans),end(ans));
    return ans;
}
ll task(ll n) // O(lg n)
{
    ll cp=0;
    for(int bit=59;bit>=0;bit--)
    {
        if(n&(1ll<<bit))
        {
            cp+=bit;
            n^=(1ll<<bit);
            break;
        }
    }
    while(n)
    {
        cp++;
        n-=(n&-n);
    }
    return cp;
}
vector<int> construct_permutation(ll k)
{
    vector<int> ans=cp(k);
    ll mx=ans.size();
    if(mx<90)
    {
        return ans;
    }
    ll SQ=4e5;
    SQ=min(SQ,k);
    ll spq=sqrtl(k)+2;
    for(ll d=1;d<=SQ;spq--,d++)
    {
        if(k%d==0)
        {
            ll f=task(d);
            ll s=task(k/d);
            if((f+s)<mx)
            {
                mx=f+s;
                vector<int> f_=cp(d);
                vector<int> s_=cp(k/d);
                ll nm=s_.size();
                for(int i=0;i<f_.size();i++)
                    f_[i]+=nm;
                ans.clear();
                for(auto i:s_)
                    ans.pb(i);
                for(auto i:f_)
                    ans.pb(i);
            }
        }
        if(spq>0)
        {
            
            swap(d,spq);
            if(k%d==0)
            {
                ll f=task(d);
                ll s=task(k/d);
                if((f+s)<mx)
                {
                    mx=f+s;
                    vector<int> f_=cp(d);
                    vector<int> s_=cp(k/d);
                    ll nm=s_.size();
                    for(int i=0;i<f_.size();i++)
                        f_[i]+=nm;
                    ans.clear();
                    for(auto i:s_)
                        ans.pb(i);
                    for(auto i:f_)
                        ans.pb(i);
                }
            }
            swap(d,spq);
        }
    }
    return ans;
}
// int main()
// {
//     int n;
//     cin>>n;
//    auto tp= construct_permutation(n);
//    for(auto k:tp)
//    {
//     cout<<k<<' ';
//    }
//    cout<<endl;
// }

Compilation message

perm.cpp: In function 'std::vector<int> construct_permutation(long long int)':
perm.cpp:87:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |                 for(int i=0;i<f_.size();i++)
      |                             ~^~~~~~~~~~
perm.cpp:110:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |                     for(int i=0;i<f_.size();i++)
      |                                 ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Partially correct 146 ms 428 KB Partially correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Partially correct 211 ms 936 KB Partially correct
9 Correct 1 ms 600 KB Output is correct
10 Partially correct 777 ms 464 KB Partially correct
11 Partially correct 165 ms 460 KB Partially correct
12 Correct 120 ms 424 KB Output is correct
13 Correct 321 ms 708 KB Output is correct