IOI2016Day2. Messy

Topic link: http://uoj.ac/problem/239


Topic meaning:

 

This is an interactive question. The interactive library maintains a data structure that can store n as a binary string. At the beginning, you can insert several binary strings into the empty data structure.


This data structure then changes the binary string stored in it.


The method of change is to generate a permutation pi of 0 to n-1, changing the original binary string a0a1a2..an-1 into ap0ap1..apn-1.


Then you can query, each time a string is in the data structure, asking you to find the permutation p in no more than w insertions and r queries

 

Analysis: After reading the question, look at the data range, w=r=896=128*7=nlog^2(n), prompting us to use the divide and conquer algorithm


 

Addend:


We need to add all the numbers at once at the beginning.


Which numbers are considered to be added? Combined with divide and conquer, we divide l, r into (l, mid) (mid+1, r) If we add the number on the left to the library, when we divide and conquer, if we find a number that appears


It means that he is in the range of (l, mid), otherwise in (mid+1, r), the division and conquer can be completed.


Let's talk about this operation specifically: according to the above, we only operate on (l, mid), enumerating i (l<=i<=mid), we make (l,i-1)(i+1, mid) is 0, other positions


All of them are 1, and they are added to the library, so that the number of 1s in each layer is different, so all the numbers will not affect the final divide and conquer after adding them.


 

Find :


We divide and conquer (0, n-1), and each time we can determine which interval the number at a position is in, and recursively to the bottom layer to get the final answer.


 

Attach the interactive code  :

 

 

#include<bits/stdc++.h>
#include "messy.h"
using namespace std;
const int N=350;
int ans[N],used[N],len;
inline void add(int l,int r)
{
    if(l>=r) return ;
    string str="";
    for(int i=0;i<len;i++) str+='0';
    for(int i=0;i<l;i++) str[i]='1';
    for(int i=r+1;i<len;i++) str[i]='1';
    
    int mid=(l+r)>>1;
    for(int i=l;i<=mid;i++) 
    {
        str [i] = ' 1 ' ;
        add_element(str);
        str [i] = ' 0 ' ;
    }
    add(l,mid);add(mid+1,r);
}

int temp[N];

inline void solve(int l,int r)
{
    if(l>=r) return ;
    string str="";
    for(int i=0;i<len;i++) str+='0';
    for(int i=0;i<l;i++) str[ans[i]]='1';
    for(int i=r+1;i<len;i++) str[ans[i]]='1';
    int cnt1=0,cnt2=0,mid=(l+r)>>1;
    for(int i=l;i<=r;i++) 
    {
        int o=ans[i];
        str[o]='1';
        if(check_element(str))
        {
            cnt1++;
            temp[l+cnt1-1]=ans[i];
        }
        else 
        {
            cnt2 ++ ;
            temp[mid+cnt2]=ans[i];
        }
        str [o] = ' 0 ' ;
    }
    for(int i=l;i<=r;i++) ans[i]=temp[i];
    solve(l,mid);solve(mid+1,r);
}


vector<int> cnt;
vector<int> restore_permutation(int n, int w, int r)
{
    len = n;
    
    memset(used,0,sizeof(used));
    add ( 0 , len- 1 );
    compile_set();
    for(int i=0;i<len;i++) ans[i]=i;
    
    solve(0,len-1);
    for(int i=0;i<len;i++) temp[ans[i]]=i;
    for(int i=0;i<len;i++) cnt.push_back(temp[i]);
    return cnt;
}
View Code

 


Reprint please declare! ! !


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324924836&siteId=291194637