2019牛客暑期多校训练营(第三场)J LRU management(模拟)

链接:https://ac.nowcoder.com/acm/contest/883/J
来源:牛客网
 

题目描述

ZYB has finished his computer course recently. He is very interested in the LRU algorithm for cache management.

To simplify the problem, assume that a block contains a name (which is a string) and a set of data (which is a number). ZYB wants to implement the LRU algorithm with an array. 
 

His array can hold at most  M\ M M elements at any time. In the beginning, the array is empty. In each operation, the CPU may access a block  s\ s s. ZYB will search for it in his array by brute force. If this block is present in his array (which means he can find a block with the same name), he takes it out of the array and puts it back at the end of the array. Otherwise, he simply adds it to the end of the array. If at any time, the size of the array exceeds the capacity, he will remove the first block in his array (the block at the front of the array).

 


Seems boring? Well, sometimes ZYB may ask the data of a block in, before or after a certain block. Could you help him to write a program to achieve his goal?

输入描述:

There are multiple cases. The first line of the input contains a single positive integer  T\ T T, indicating the number of cases. 

For each case, the first line of the input contains two integers  Q\ Q Q and M (1≤Q, M≤500000)M\ (1 \leq Q, \ M \leq 500000)M (1≤Q, M≤500000), denoting the number of operations and the capacity of the array. The following  Q\ Q Q lines each contain an integer opt (0≤opt≤1)opt \ (0 \le opt \le 1)opt (0≤opt≤1), a string s (1≤∣s∣≤10)s \ (1 \leq |s| \leq 10)s (1≤∣s∣≤10) and an integer v\ v v, separated by single spaces, describing an operation. 

 

If  opt=0\ opt = 0 opt=0, then  ∣v∣<10\ |v| < 10 ∣v∣<10, and the operation is that the CPU wants to access a block. If this access misses (which means you can't find a block in the array with the name  s\ s s), add a block at the end of the array with the name  s\ s s and the data  v\ v v, and the result of the operation is  v\ v v. If this access hits, the result of the operation is the data of the block you found (ignore  v\ v v in this case). Don't forget to move that block to the end of the array.

 

If  opt=1\ opt = 1 opt=1,  then  v\ v v will be  −1,0\ -1,0 −1,0 or  1\ 1 1 , and the operation is that you should answer ZYB's question. Let  k\ k k be the index of the block with the name  s\ s s in the array. Then the result of this operation is the data of the block with the index  k+v\ k+v k+v in the array. If such block doesn't exist, please output ``Invalid'' (without quotation marks) instead.

Note that ZYB's questions (operations of type  1\ 1 1) don't count as access and don't cause the array to be updated. 

It is guaranteed that the sum of  Q\ Q Q  over all cases does not exceed  1000000\ 1000000 1000000, and that  s\ s s only contains digits (i.e. `0' - `9'). 

输出描述:

For each case, print the result of each operation in a line as defined in the input section of the statement.

示例1

输入

复制

1
8 3
0 0101010 1
0 0101011 2
1 0101010 1
0 1100000 3
0 0101011 -1
0 1111111 4
1 0101011 -1
1 0101010 0

输出

复制

1
2
2
3
2
4
3
Invalid

        用一个list来表示现在的队列,用一个map来表示某个给出的字符串指向的list中的地址来模拟就好了~~

        不过我之前傻傻的在list中用了个map<string, list<fuck>::iterator>::iterator来指向map中某个字符串的地址,但是不情况具体状况就是比用string指回map要慢而导致超时,这种情况下用unordered_map也能过。

#include <bits/stdc++.h>
using namespace std;
int n, m;
struct fuck {
    int v;
    string m;
};
list<fuck>Q;
map<string, list<fuck>::iterator >G;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int te; cin >> te;
    while (te--){
        Q.clear(); G.clear();
        cin >> n >> m; int sz = 0;
        for (int i = 0; i < n; i++) {
            int a, b; string c;
            fuck it;
            cin >> a >> c >> b;
            if (a == 0) {
                if (G.count(c)) {
                    it = *G[c];
                    Q.erase(G[c]);
                    Q.push_back(it);
                    G[c] = --Q.end();
                    cout << it.v << "\n";
                }
                else {
                    Q.push_back(fuck{ b,c });
                    G[c] = --Q.end();sz++;
                    cout << b << "\n";
                }
                if (sz > m) {
                    G.erase(Q.begin()->m);
                    Q.pop_front();
                    sz--;
                }
            }
            else {
                if (!G.count(c)) {
                    cout << "Invalid\n";
                    continue;
                }
                list<fuck>::iterator now = G[c];
                if(b==0){
                    cout << now->v << "\n";
                }
                else if (b == 1) {
                    if (now != --Q.end()) {
                        cout << (++now)->v << "\n";
                    }
                    else {
                        cout << "Invalid\n";
                    }
                }
                else {
                    if (now != Q.begin()) {
                        cout << (--now)->v << "\n";
                    }
                    else {
                        cout << "Invalid\n";
                    }
                }
            }
        }
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct fuck {
    int v;
    unordered_map<string, list<fuck>::iterator>::iterator sp;
};
list<fuck>Q;
unordered_map<string, list<fuck>::iterator> G;
int main() {
    ios::sync_with_stdio(0);
    int te; cin >> te;
    cin.tie(0); cout.tie(0);
    while (te--) {
        Q.clear(); G.clear();
        cin >> n >> m; int sz = 0;
        for (int i = 0; i < n; i++) {
            int a, b; string c;
            list<fuck>::iterator it;
            cin >> a >> c >> b;
            if (a == 0) {
                if (G.count(c)) {
                    int v = G[c]->v;
                    Q.erase(G[c]);
                    Q.push_back(fuck{ v,G.find(c) });
                    G[c] = --Q.end();
                    cout << v << "\n";
                }
                else {
                    Q.push_back(fuck{ b,G.find(c) });
                    G[c] = --Q.end(); sz++;
                    Q.back().sp = G.find(c);
                    cout << b << "\n";
                }
                if (sz > m) {
                    G.erase(Q.front().sp);
                    Q.pop_front();
                    sz--;
                }
            }
            else {
                if (!G.count(c)) {
                    cout << "Invalid\n";
                    continue;
                }
                it = G[c];
                if (b == 0) {
                    cout << it->v << "\n";
                }
                else if (b == 1) {
                    if (it != --Q.end()) {
                        cout << (++it)->v << "\n";
                    }
                    else {
                        cout << "Invalid\n";
                    }
                }
                else {
                    if (it != Q.begin()) {
                        cout << (--it)->v << "\n";
                    }
                    else {
                        cout << "Invalid\n";
                    }
                }
            }
        }
    }
    return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值