UVA 11423 - Cache Simulator (樹狀數組)

來源:互聯網
上載者:User

標籤:ber   pop   back   empty   margin   mil   sso   沒有   數組   

UVA 11423 - Cache Simulator (樹狀數組)

option=com_onlinejudge&Itemid=8&category=523&page=show_problem&problem=2418" style="">題目連結

題目大意:模仿磁碟緩衝區的工作機制,給你n個不同size的(遞增的)磁碟緩衝區。給你要訪問的資料,依據LRU原則,問每一個size的磁碟分別有多少次miss(資料沒有在緩衝中就是miss)。

解題思路:由於資料最多有10^7,所以資料訪問的序列最長也就是10^7。

樹狀數組的每一個位置代表的是訪問序列的位置有沒有數,由於假設之前的數在後面有訪問到的話,那麼這個數就應該在後面了,這樣前面的那個數就應該不存在。

做法:先將要訪問的資料序列處理出來,放在隊列中,而且找到每一個數之前出現過的離它近期的那個位置。查詢當前的位置和之前那個出現的位置之間有多少個數(假設dis個);小於dis的cache的miss++,然後要記得刪除樹狀數組之前的那個位置上的值。

假設是沒有出現過的數,那麼miss肯定是要+1的。

注意:每次state都要又一次計算miss。

代碼:

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <map>using namespace std;const int N = 35;const int maxn = 1e7 + 5;#define lowbit(x) ((x)&(-x))int n;int Miss[N], Cache[N];int C[maxn];char str[N];void add (int x, int v) {    while (x < maxn) {        C[x] += v;        x += lowbit(x);    }}int sum (int x) {    int ret = 0;    while (x > 0) {        ret += C[x];        x -= lowbit(x);    }    return ret;}struct Num {    int value, pre;    Num (int value , int pre) {        this->value = value;        this->pre = pre;    }};queue<Num> Q;map<int, int> vis;void init () {    int b, 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 (int i = 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));        }    }}void solve () {    init();    memset (C, 0, sizeof (C));    int cnt = 0;    while (!Q.empty()) {        if (Q.front().value >= 0) {            add(cnt + 1, 1);            if (Q.front().pre > 0) {                int dis = sum(cnt + 1) - sum(Q.front().pre);    //                printf ("%d %d %d %d\n", Q.front().value, cnt + 1, Q.front().pre, dis);                for (int i = 0; i < n; i++) {                    if (Cache[i] < dis)                        Miss[i]++;                    else                        break;                }                    add(Q.front().pre, -1);            } else {                for (int i = 0; i < n; i++)                    Miss[i]++;            }        } else {            for (int i = 0; i < n - 1; i++)                printf ("%d ", Miss[i]);            printf ("%d\n", Miss[n - 1]);            memset (Miss, 0, sizeof (Miss));        }        Q.pop();        cnt++;    }}int main () {    scanf ("%d", &n);    for (int i = 0; i < n; i++)         scanf("%d", &Cache[i]);        solve();    return 0;};

UVA 11423 - Cache Simulator (樹狀數組)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.