链接: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
#include
using namespace std;
int n, m;
struct fuck {
int v;
string m;
};
listQ;
map::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::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
using namespace std;
int n, m;
struct fuck {
int v;
unordered_map::iterator>::iterator sp;
};
listQ;
unordered_map::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::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;
}