Topic background
Xiao Chen has installed a machine translation software on his computer. He often uses this software to translate English articles.
Title Description
The principle of this translation software is very simple. It just replaces each English word with the corresponding Chinese meaning from beginning to end. For each English word, the software will first find the Chinese meaning of the word in the memory. If there is one in the memory, the software will use it for translation; If there is no in the memory, the software will search in the dictionary in the external memory, find out the Chinese meaning of the word, then translate it, and put the word and translated meaning into the memory for subsequent search and translation.
Suppose there are in memory MM Each unit can store one word and translation meaning. Whenever the software stores a new word in memory, if the number of words stored in the current memory does not exceed M-1M − 1, the software will store new words in an unused memory unit; If stored in memory MM A word, the software will empty the word that entered the memory first, vacate the unit and store the new word.
Suppose the length of an English article is NN A word. Given this article to be translated, how many times does the translation software need to look up the dictionary in external memory? Suppose there are no words in memory before the translation starts.
Input format
common twenty-two that 's ok. Two numbers in each line are separated by a space.
The first line is two positive integers M. Nm, N, represents the memory capacity and the length of the article.
Second behavior NN Non negative integers, in the order of the article, each number (size no more than) 10001000) stands for an English word. Two words in the article are the same word if and only if their corresponding non negative integers are the same.
Output format
An integer that is the number of times the software needs to look up the dictionary.
Input and output samples
Enter #1 copy
3 7 1 2 1 5 4 4 1Output #1 copy
5Description / tips
Example explanation
The whole dictionary lookup process is as follows: each line represents the translation of a word, and the memory status after this translation is before the colon:
- 1: Find word 1 and call it into memory.
- 1 2: find word 2 and call it into memory.
- 1 2: find word 1 in memory.
- 1 2 5: find word 5 and call it into memory.
- 2 5 4: find word 4 and call in memory to replace word 1.
- 2 5 4: find word 4 in memory.
- 5 4 1: find word 1 and call in memory to replace word 2.
Totally fifty-five Second dictionary.
Data range
For 40 \% 40% data, n ≤ 1000N ≤ 1000;
For 100 \% 100% data, there are N, M ≤ 100000N,M ≤ 100000.
After all the students are lined up according to the above method, the teacher wants to know the numbers of all the students from left to right.
0.
#Include < bits / STDC + +. H > / / universal title using namespace std; int main(){ int m,n,t,ans=0; cin>>m>>n; vector<int>v; while(cin>>t){ if(find(v.begin(),v.end(),t)==v.end()){ v.push_back(t); ++ans; } if(v.size()>m) v.erase(v.begin()); } cout<<ans<<endl; }
Title Description
In a school, the teacher should line up NN students in the class. The students are numbered 1\sim N1 ∼ n. he adopts the following methods:
-
First, arrange classmate 11 into the queue. At this time, he is the only one in the queue;
-
Students No. 2-N2 − N are listed in turn. Students No. I are listed in the following way: the teacher designates students No. I to stand on the left or right of a student No. 1\sim (i -1)1 ∼ (I − 1) (i.e. students who have been listed before);
-
M (m < n) m (m < n) students were removed from the queue, and the position order of other students remained unchanged.
After all the students are lined up according to the above method, the teacher wants to know the numbers of all the students from left to right.
Input format
The 11th line is a positive integer NN, indicating that there are NN students.
Line 2-N2 − N, line ii contains two integers k,pk,p, where kk is a positive integer less than ii and pp is 00 or 11. If pp is 00, it means inserting classmate ii to the left of classmate kk, and if pp is 11, it means inserting to the right.
The N+1N+1 line is a positive integer MM, indicating the number of students removed.
In the next MM line, each line has a positive integer xx, which means that classmate xx will be removed from the queue. If classmate xx is no longer in the queue, this instruction will be ignored.
Output format
11 lines, containing positive integers separated by up to NN spaces, representing the numbers of all students in the queue from left to right. The end of the line is wrapped without spaces.
Input and output samples
Enter #1 copy
4 1 0 2 1 1 0 2 3 3Output #1 copy
2 4 1Description / tips
Example explanation:
Insert classmate 22 to the left of classmate 11. At this time, the queue is:
2 121
Insert classmate 33 to the right of classmate 22. At this time, the queue is:
2 3 1231
Insert classmate 44 to the left of classmate 11. At this time, the queue is:
2 3 4 12341
Remove classmate 33 from the queue. At this time, the queue is:
2 4 1241
Classmate 33 is no longer in the queue. Ignore the last instruction
Final queue:
2 4 1241
Data range
For 20 \% 20% data, n ≤ 10N ≤ 10;
For 40 \% 40% data, n ≤ 1000N ≤ 1000;
For 100 \% 100% data, there are N, M ≤ 100000N,M ≤ 100000.
//The notes are very detailed #include <iostream> #include <cstdio> struct node { //Array implements a two-way linked list. Here, a node is defined. l represents the left and r represents the other side int l, r; }l[100005]; void InitList(int n) { //Initialize the linked list to - 1 for(int i = 1; i <= n; i++) l[i].l = l[i].r = -1; l[0].r = 1; //There was student No. 1 at the beginning l[1].l = 0; //Subscript 0 is the beginning of the two-way linked list return ; } void addLeft(int x, int pos) { l[x].r = pos; //The right side of node x is set to pos l[x].l = l[pos].l;//The left side of node x is the left side of the pos position node l[l[pos].l].r = x;//The left node and the right node of pos should be x l[pos].l = x; pos On the left is x return ; } void addRight(int x, int pos) { l[x].l = pos; //Adding a right node is the same as adding a left node l[x].r = l[pos].r; l[l[pos].r].l = x; l[pos].r = x; return ; } void DeleteNode(int x) { //So how to delete it? Very simply, we set both sides of the node x to be deleted to - 1, and then add it to the original nodes on both sides, if(l[x].l != -1) { //The left and right skip nodes x directly link together. l[l[x].l].r = l[x].r; l[l[x].r].l = l[x].l; l[x].l = -1; l[x].r = -1; } return ; } void printList() { int x = l[0].r; printf("%d ",x); while(l[x].r != -1) { x = l[x].r; printf("%d ", x); } return ; } int main(){ int n; scanf("%d",&n); InitList(n); for(int i = 2; i <= n; i++) { int pos, dir; scanf("%d %d",&pos, &dir); if(dir) addRight(i, pos); else addLeft(i, pos); } int m; scanf("%d",&m); for(int i = 1; i <= m; i++) { int x; scanf("%d",&x); DeleteNode(x); } printList(); return 0; }