UVA 11423-cache Simulator (tree-like array)
Topic links
The main topic: imitate disk buffer working mechanism, give you n different size (increment) disk buffer, give you to access 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 idea: Because the data is 10^7, the data access sequence is the longest, which is 10^7. Each position of the tree array represents the location of the access sequence, because if the previous number is accessed later, then the number should be at the back so that the preceding number should not exist. Procedure: The sequence of data to be accessed is processed, placed in the queue, and the nearest location to which it appears before each number is found. 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. If the number is not present, then miss must be +1. Note: Each state has to recalculate the miss.
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)