最佳缓存


14

您将获得一系列的内存请求和一个缓存大小。在任何高速缓存替换策略下,您必须返回尽可能少的高速缓存未命中数。

最佳策略是Belady的算法,您可以根据需要使用它。


缓存系统的工作方式如下:缓存开始为空。内存请求进来。如果请求在高速缓存中请求一条数据,一切都很好。否则,您将导致缓存未命中。此时,您可以将请求的数据插入缓存中以备将来使用。如果缓存已满,并且您想插入新数据,则必须逐出缓存中先前的数据。您可能永远不会插入不仅在缓存中的数据。

您的目标是针对给定的内存请求序列和缓存大小,找到最少可能的缓存未命中数。


系统会为您提供缓存大小,正整数和内存请求序列(这是令牌列表)。这些令牌可以是您喜欢的任何类型的令牌,只要可以使用至少256个不同的令牌(字节就可以,布尔不是)。例如,整数,字符串,列表都可以。如果需要,请进行澄清。


测试用例:

3
[5, 0, 1, 2, 0, 3, 1, 2, 5, 2]

6

请参阅维基百科,以获取实现此目的的替代政策。

2
[0, 1, 2, 0, 1, 0, 1]

3

只需避免添加2到缓存中。

3
[0, 1, 2, 1, 4, 3, 1, 0, 2, 3, 4, 5, 0, 2, 3, 4]

9

实现此目的的一种方法是永远不要逐出02,并且1在最后一次使用后尽快将其逐出。


计分:这是代码高尔夫球。最少的字节数获胜。


我们是否可以假定该列表至少包含2个令牌?
Arnauld

@Arnauld我要说不,尽管如果只有1个解决方案,答案当然总是
1。– isaacg

Answers:


4

JavaScript(ES6),128个字节

将输入作为(size)(list)

s=>a=>a.map((x,i)=>c.includes(x)?0:c[e++,[x,...c].map(m=(x,j)=>(k=[...a,x].indexOf(x,i+1))<m||(p=j,m=k)),i<s?i:p-1]=x,e=c=[])&&e

在线尝试!

已评论

这是Belady算法的实现。

s => a =>                      // s = cache size; a[] = token list
  a.map((x, i) =>              // for each token x at position i in a[]:
    c.includes(x) ?            //   if x is currently stored in the cache:
      0                        //     do nothing
    :                          //   else:
      c[                       //     update the cache:
        e++,                   //       increment the number of errors (cache misses)
        [x, ...c]              //       we want to find which value among x and all current
                               //       cache values will be needed for the longest time in
                               //       the future (or not needed anymore at all)
        .map(m =               //       initialize m to a non-numeric value
                 (x, j) =>     //       for each x at position j in this array:
          ( k = [...a, x]      //         k = position of x in the array made of all values
            .indexOf(x, i + 1) //         of a[] followed by x, starting at i + 1
          ) < m                //         if it's greater than or equal to m, or m is
          || (p = j, m = k)    //         still non-numeric: set p to j and m to k
        ),                     //       end of inner map()
        i < s ?                //       if i is less than the cache size:
          i                    //         just fill the cache by using the next cache slot
        :                      //       else:
          p - 1                //         use the slot that was found above
                               //         special case: if p = 0, x was the best candidate
                               //         and we're going to store it at c[-1], which is
                               //         simply ignored (it will not trigger c.includes(x))
      ] = x,                   //     store x at this position
      e = c = []               //     start with e = [] (coerced to 0) and c = []
  ) && e                       // end of outer map; return e

4

Perl 5,193个字节

sub g{
  my($i,$m,$s,@a,%c)=(-1,0,@_);
  for(@a){
    $i++;
    next if $c{$_}++ || ++$m && keys%c <= $s;
    my($x,$d);
    for $k (sort keys %c){  #find which to delete, the one furtherst away
      my $n=0;
      ++$n && /^$k$/ && last for @a[$i+1..$#a];
      ($x,$d)=($n,$k) if $n>$x
    }
    delete $c{$d}
  }
  $m
}

在线尝试!

print g(3,  5, 0, 1, 2, 0, 3, 1, 2, 5, 2),"\n";                     # 6
print g(2,  0, 1, 2, 0, 1, 0, 1),"\n";                              # 3
print g(3,  0, 1, 2, 1, 4, 3, 1, 0, 2, 3, 4, 5, 0, 2, 3, 4),"\n";   # 9

193个字节,没有缩进,换行符,空格和注释:

sub g{my($i,$m,$s,@a,%c)=(-1,0,@_);for(@a){$i++;next if$c{$_}++||++$m&&keys%c<=$s;my($x,$d);for$k(sort keys%c){my$n=0;++$n&&/^$k$/&&last for@a[$i+1..$#a];($x,$d)=($n,$k)if$n>$x}delete$c{$d}}$m}


1

Haskell,82个字节

f n|let(d:t)#c=1-sum[1|elem d c]+minimum[t#take n e|e<-scanr(:)(d:c)c];_#_=0=(#[])

在线尝试!

说明

蛮力运作:尝试所有缓存策略并返回最佳结果。

f n            Define a function f on argument n (cache size) and a list (implicit).
 |let(d:t)#c=  Define binary helper function #.
               Arguments are list with head d (current data) and tail t (remaining data), and list c (cache).
 1-            It returns 1 minus
 sum[1|        1 if
 elem d c]+    d is in the cache, plus
 minimum[      minimum of
 t#            recursive calls to # with list t
 take n e|     and cache being the first n values of e, where
 e<-           e is drawn from
 scanr(:)  c]  the prefixes of c
 (d:c)         with d and c tacked to the end.
 ;_#_=0        If the first list is empty, return 0.
 =(#[])        f then calls # with the list argument and empty cache.

0

Perl 6、146字节

->\a,\b {$_=set();$!=0;for b.kv ->\i,\v {$_{v}&&next;++$!;vb[i^..*]||next;$_∖=.keys.max({(grep $_,:k,b[i^..*])[0]//Inf})if $_>=a;$_∪=v};$!}

在线尝试!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.