이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "advisor.h"
#include <vector>
void ComputeAdvice(int *C, int N, int K, int M) {
std::vector<int> reuse(K+N,false);
std::vector<int> next(N);
std::vector<int> first(N,N);
for(int i=N-1;i>=0;i--){
next[i]=first[C[i]];
first[C[i]]=i;
}
next.insert(next.begin(),first.begin(),first.begin()+K);
std::vector<int> colors;
for(int i=0;i<K;i++){
colors.push_back(i);
}
colors.insert(colors.end(),C,C+N);
//time entered: [0,K) for init, [K,N+K) for added
//time color will be reused: next[cache[i]]
std::vector<int> cache(K);
std::vector<int> where(N,-1);//index in cache for this color, or -1 if not present
for(int i=0;i<K;i++){
cache[i]=i;
where[i]=i;
}
for(int i=0;i<N;i++){
int late=0;
for(int k=0;k<K;k++){
if(next[cache[k]]>next[cache[late]]){
late=k;
}
}
if(where[C[i]]!=-1){
late=where[C[i]];
reuse[cache[late]]=true;
}
//evict cache[late]
where[colors[cache[late]]]=-1;
cache[late]=i+K;
where[colors[cache[late]]]=late;
}
for(int i=0;i<K+N;i++){
WriteAdvice(reuse[i]);
}
}
#include "assistant.h"
#include <vector>
void Assist(unsigned char *A, int N, int K, int R) {
std::vector<int> advice(A,A+R);
std::vector<std::pair<int,int> > cache(K);//(color,reused)
std::vector<int> where(N,-1);//where in cache a color is, or -1 if not present
for(int i=0;i<K;i++){
cache[i]={i,advice[i]};
where[i]=i;
}
for(int i=0;i<N;i++){
int color=GetRequest();
int evict=0;
for(int k=0;k<K;k++){
if(!cache[k].second){
evict=k;
}
}
if(where[color]!=-1){
evict=where[color];
}
where[cache[evict].first]=-1;
if(cache[evict].first!=color){
PutBack(cache[evict].first);
}
cache[evict].first=color;
cache[evict].second=advice[i+K];
where[cache[evict].first]=evict;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |