#include "advisor.h"
#include <queue>
using namespace std;
#define MAXN 100005
typedef pair<int,int> pii;
static int C[MAXN];
static int in_scaffold[MAXN],recent[MAXN],next_[MAXN],first_active[MAXN],request_active[MAXN],request_num[MAXN];
static priority_queue<pii> que;
void ComputeAdvice(int *arr, int N, int K, int M)
{
int i,k;
for (i=0;i<N;i++) C[i+1] = arr[i]+1;
for (i=1;i<=N;i++) next_[i] = 1e9;
for (i=N;i;i--){
recent[i] = next_[C[i]];
next_[C[i]] = i;
}
for (i=1;i<=K;i++) in_scaffold[i] = 1, que.push(pii(next_[i],i));
for (i=1;i<=K;i++) first_active[i] = 0;
for (i=1;i<=N;i++) request_active[i] = 0;
for (i=1;i<=N;i++){
if (in_scaffold[C[i]]){
if (request_num[C[i]]) request_active[request_num[C[i]]] = 1;
else first_active[C[i]] = 1;
request_num[C[i]] = i;
que.push(pii(recent[i],C[i]));
continue;
}
k = que.top().second; que.pop();
in_scaffold[C[i]] = 1; in_scaffold[k] = 0;
request_num[C[i]] = i;
que.push(pii(recent[i],C[i]));
}
for (i=1;i<=K;i++) WriteAdvice(first_active[i]);
for (i=1;i<=N;i++) WriteAdvice(request_active[i]);
}
I2luY2x1ZGUgImFkdmlzb3IuaCIKI2luY2x1ZGUgPHF1ZXVlPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBNQVhOIDEwMDAwNQp0eXBlZGVmIHBhaXI8aW50LGludD4gcGlpOwoKc3RhdGljIGludCBDW01BWE5dOwpzdGF0aWMgaW50IGluX3NjYWZmb2xkW01BWE5dLHJlY2VudFtNQVhOXSxuZXh0X1tNQVhOXSxmaXJzdF9hY3RpdmVbTUFYTl0scmVxdWVzdF9hY3RpdmVbTUFYTl0scmVxdWVzdF9udW1bTUFYTl07CnN0YXRpYyBwcmlvcml0eV9xdWV1ZTxwaWk+IHF1ZTsKCnZvaWQgQ29tcHV0ZUFkdmljZShpbnQgKmFyciwgaW50IE4sIGludCBLLCBpbnQgTSkKewogICAgaW50I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