इष्टतम कैशिंग


14

आपको मेमोरी अनुरोध और कैश आकार का एक क्रम दिया जाएगा। आपको किसी भी कैश रिप्लेसमेंट रणनीति के तहत कैश की कम से कम संभावित संख्या को वापस करना होगा।

एक इष्टतम रणनीति बेलड़ी का एल्गोरिदम है , जिसे आप चाहें तो उपयोग कर सकते हैं।


एक कैशिंग सिस्टम निम्नानुसार काम करता है: कैश खाली होना शुरू होता है। मेमोरी अनुरोध अंदर आते हैं। यदि अनुरोध कैश में डेटा का एक टुकड़ा मांगता है, तो सब ठीक है। यदि नहीं, तो आप कैची मिस करते हैं। इस बिंदु पर आप उस डेटा को सम्मिलित कर सकते हैं जो भविष्य में उपयोग के लिए कैश में अनुरोध किया गया था। यदि कैश भरा हुआ था और आप नया डेटा सम्मिलित करना चाहते हैं, तो आपको वह डेटा निकालना होगा जो पहले कैश में था। आप वह डेटा कभी नहीं डाल सकते जो सिर्फ कैश में नहीं था।

आपका लक्ष्य किसी दिए गए मेमोरी रिक्वेस्ट सीक्वेंस और कैश साइज के लिए कैश मिसेज की न्यूनतम संभावित संख्या का पता लगाना है।


आपको कैश आकार, एक सकारात्मक पूर्णांक, और मेमोरी अनुरोध अनुक्रम दिया जाएगा, जो टोकन की एक सूची है। ये टोकन आप जिस भी प्रकार के टोकन को पसंद कर सकते हैं, जब तक कम से कम 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

एक तरह से इस लक्ष्य को हासिल करने के लिए कभी नहीं हटाना उतना कर रहा है 0और 2, और हटाना उतना 1अपने पिछले उपयोग के बाद जितनी जल्दी हो सके।


स्कोरिंग: यह कोड गोल्फ है। सबसे कम बाइट्स जीतता है।


क्या हम मान सकते हैं कि सूची में कम से कम 2 टोकन हैं?
अरनुलद

@Arnauld मैं नहीं कहने जा रहा हूं, हालांकि अगर केवल 1 समाधान है, तो इसका उत्तर हमेशा 1 होता है
isaacg

जवाबों:


4

जावास्क्रिप्ट (ईएस 6), 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

इसे ऑनलाइन आज़माएं!

टिप्पणी की गई

यह बेलड़ी के एल्गोरिदम का कार्यान्वयन है।

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

पर्ल 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

हास्केल , 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.

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.