UVA 11423-cache Simulator (tree-like array)

Source: Internet
Author: User

UVA 11423-cache Simulator (tree-like array)

option=com_onlinejudge&itemid=8&category=523&page=show_problem&problem=2418 "style=" "> Topic Links

The main idea: mimic the working mechanism of disk buffers, give you n a different size (incremental) disk buffer. Give you access to the data, according to the LRU principle, ask each size of the disk how many times miss (data is not in the cache is Miss).

Problem-solving ideas: Because the data has 10^7, so the data access to ask the longest sequence is 10^7.

Each position of the tree array represents the location of the access sequence and there are no numbers, because assuming the previous number is at the back of the interview, then this number should be in the back, so that the preceding number should not exist.

Practice: The data sequence to be accessed first is processed, placed in the queue, and find each number before the occurrence of the near its recent location. The number of queries between the current position and the previous location (assuming dis), the miss++ of the cache that is less than the dis, and then the value of the location before the tree array is deleted.

Assuming that there are no occurrences of the number, then miss must be +1.

Note: Each state has to compute miss again.

Code:

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <map>using namespace STD;Const intN = *;Const intMAXN =1e7+5;#Define lowbit (x) ((x) & ( ×))intNintMiss[n], cache[n];intC[MAXN];CharStr[n];voidAdd (intXintV) { while(x < MAXN)        {C[x] + = V;    x + = Lowbit (x); }}intSUM (intx) {intRET =0; while(X >0) {ret + = c[x];    X-= Lowbit (x); }returnRET;}structNum {intvalue, pre; Num (intValue,intPre) { This->value = value; This->pre = pre; }}; queue<Num>Q; map<int, int>VisvoidInit () {intb, Y, K;memset(Miss,0,sizeof(Miss)); Vis.clear (); while(scanf('%s ', str) && str[0] !=' E ') {if(str[0] ==' R ') {scanf("%d%d%d", &b, &y, &k); for(inti =0; I < K;                i++) {Q.push (Num (b + y * I, vis[b + y * i]));            Vis[b + y * i] = q.size (); }        }Else if(str[0] ==' A ') {scanf("%d", &b);            Q.push (Num (b, vis[b]));        VIS[B] = Q.size (); }Else{Q.push (Num (-1,0)); }    }}voidSolve () {init ();memsetC0,sizeof(C));intCNT =0; while(! Q.empty ()) {if(Q.front (). Value >=0) {Add (cnt +1,1);if(Q.front (). Pre >0) {intdis = SUM (cnt +1)-SUM (Q.front (). Pre);//printf ("%d%d%d%d\n", Q.front (). Value, CNT + 1, Q.front (). Pre, dis);                 for(inti =0; I < n; i++) {if(Cache[i] < dis) miss[i]++;Else                         Break; } Add (Q.front (). Pre,-1); }Else{ for(inti =0; I < n;            i++) miss[i]++; }        }Else{ for(inti =0; I < n-1; i++)printf("%d", Miss[i]);printf("%d\n", Miss[n-1]);memset(Miss,0,sizeof(Miss));        } q.pop ();    cnt++; }}intMain () {scanf("%d", &n); for(inti =0; I < n; i++)scanf("%d", &cache[i]); Solve ();return 0;};

UVA 11423-cache Simulator (tree-like array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.