#include "advisor.h"
#include <vector>
#include <set>
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: [0,K) for init, [K,N+K) for added
std::set<std::pair<int,int> > pq;//(next time, previous time)
std::vector<int> in(N,-1);//time of last appearance of color if in cache, -1 otherwise
for(int i=0;i<K;i++){
in[i]=i;
pq.insert({next[i],i});
}
for(int i=0;i<N;i++){
int evict=pq.rbegin()->second;
if(in[C[i]]!=-1){
evict=in[C[i]];
reuse[in[C[i]]]=true;
}
//evict 'evict'
pq.erase({next[evict],evict});
in[colors[evict]]=-1;
in[colors[i+K]]=i+K;
pq.insert({next[i+K],i+K});
}
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 |
1 |
Correct |
4 ms |
872 KB |
Output is correct |
2 |
Correct |
4 ms |
760 KB |
Output is correct |
3 |
Correct |
6 ms |
872 KB |
Output is correct |
4 |
Correct |
7 ms |
976 KB |
Output is correct |
5 |
Correct |
7 ms |
1008 KB |
Output is correct |
6 |
Correct |
9 ms |
1052 KB |
Output is correct |
7 |
Correct |
10 ms |
1028 KB |
Output is correct |
8 |
Correct |
12 ms |
1080 KB |
Output is correct |
9 |
Correct |
10 ms |
1044 KB |
Output is correct |
10 |
Correct |
15 ms |
1172 KB |
Output is correct |
11 |
Correct |
12 ms |
1008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
1340 KB |
Output is correct |
2 |
Correct |
178 ms |
3680 KB |
Output is correct |
3 |
Correct |
2446 ms |
9696 KB |
Output is correct |
4 |
Correct |
79 ms |
6624 KB |
Output is correct |
5 |
Correct |
114 ms |
6624 KB |
Output is correct |
6 |
Correct |
483 ms |
7392 KB |
Output is correct |
7 |
Correct |
1396 ms |
8928 KB |
Output is correct |
8 |
Correct |
1452 ms |
8536 KB |
Output is correct |
9 |
Correct |
72 ms |
6640 KB |
Output is correct |
10 |
Correct |
2137 ms |
10152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1104 ms |
7032 KB |
Output is correct |
2 |
Correct |
1718 ms |
9696 KB |
Output is correct |
3 |
Correct |
1781 ms |
9696 KB |
Output is correct |
4 |
Correct |
1790 ms |
9696 KB |
Output is correct |
5 |
Correct |
1451 ms |
8936 KB |
Output is correct |
6 |
Correct |
1782 ms |
9696 KB |
Output is correct |
7 |
Correct |
1802 ms |
9696 KB |
Output is correct |
8 |
Correct |
1790 ms |
9696 KB |
Output is correct |
9 |
Correct |
1778 ms |
9696 KB |
Output is correct |
10 |
Correct |
1781 ms |
9696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1088 KB |
Output is correct |
2 |
Correct |
12 ms |
1052 KB |
Output is correct |
3 |
Correct |
7 ms |
1132 KB |
Output is correct |
4 |
Correct |
7 ms |
1132 KB |
Output is correct |
5 |
Correct |
7 ms |
1160 KB |
Output is correct |
6 |
Correct |
7 ms |
1128 KB |
Output is correct |
7 |
Correct |
10 ms |
1008 KB |
Output is correct |
8 |
Correct |
11 ms |
1052 KB |
Output is correct |
9 |
Correct |
12 ms |
1052 KB |
Output is correct |
10 |
Correct |
22 ms |
1320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1439 ms |
7904 KB |
Output is correct - 120000 bits used |
2 |
Correct |
1591 ms |
8160 KB |
Output is correct - 122000 bits used |
3 |
Correct |
1801 ms |
8672 KB |
Output is correct - 125000 bits used |
4 |
Correct |
1798 ms |
8672 KB |
Output is correct - 125000 bits used |
5 |
Correct |
1798 ms |
8416 KB |
Output is correct - 125000 bits used |
6 |
Correct |
1785 ms |
8672 KB |
Output is correct - 125000 bits used |
7 |
Correct |
1818 ms |
8416 KB |
Output is correct - 124828 bits used |
8 |
Correct |
1777 ms |
8416 KB |
Output is correct - 124910 bits used |
9 |
Correct |
1788 ms |
8416 KB |
Output is correct - 125000 bits used |
10 |
Correct |
1777 ms |
8672 KB |
Output is correct - 125000 bits used |