2529: 【普及-】【P1540】机器翻译

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:12 解决:7

题目描述

这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换。对于每个英文单词,软件会先在内存中查找这个单词的中文含义,如果内存中有,软件就会用它进行翻译;如果内存中没有,软件就会在外存中的词典内查找,查出单词的中文含义然后翻译,并将这个单词和译义放入内存,以备后续的查找和翻译。

假设内存中有 lns="http://www.w3.org/1998/Math/MathML"> 个单元,每单元能存放一个单词和译义。每当软件将一个新单词存入内存前,如果当前内存中已存入的单词数不超过 lns="http://www.w3.org/1998/Math/MathML">1,软件会将新单词存入一个未使用的内存单元;若内存中已存入 lns="http://www.w3.org/1998/Math/MathML"> 个单词,软件会清空最早进入内存的那个单词,腾出单元来,存放新单词。

假设一篇英语文章的长度为 lns="http://www.w3.org/1998/Math/MathML"> 个单词。给定这篇待译文章,翻译软件需要去外存查找多少次词典?假设在翻译开始前,内存中没有任何单词。

输入

共 lns="http://www.w3.org/1998/Math/MathML">2 行。每行中两个数之间用一个空格隔开。

第一行为两个正整数 lns="http://www.w3.org/1998/Math/MathML">,,代表内存容量和文章的长度。

第二行为 lns="http://www.w3.org/1998/Math/MathML"> 个非负整数,按照文章的顺序,每个数(大小不超过 lns="http://www.w3.org/1998/Math/MathML">1000)代表一个英文单词。文章中两个单词是同一个单词,当且仅当它们对应的非负整数相同。

输出

一个整数,为软件需要查词典的次数。

样例输入 复制

3 7
1 2 1 5 4 4 1

样例输出 复制

5

提示

样例解释

整个查字典过程如下:每行表示一个单词的翻译,冒号前为本次翻译后的内存状况:

  1. 1:查找单词 1 并调入内存。
  2. 1 2:查找单词 2 并调入内存。
  3. 1 2:在内存中找到单词 1。
  4. 1 2 5:查找单词 5 并调入内存。
  5. 2 5 4:查找单词 4 并调入内存替代单词 1。
  6. 2 5 4:在内存中找到单词 4。
  7. 5 4 1:查找单词 1 并调入内存替代单词 2。

共计查了 lns="http://www.w3.org/1998/Math/MathML">5 次词典。

数据范围

  • 对于 lns="http://www.w3.org/1998/Math/MathML">10% 的数据有 lns="http://www.w3.org/1998/Math/MathML">=1lns="http://www.w3.org/1998/Math/MathML">5
  • 对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据有 lns="http://www.w3.org/1998/Math/MathML">1100lns="http://www.w3.org/1998/Math/MathML">11000


#include<bits/stdc++.h>
using namespace std;
int n,m,mark[1005] = {0}, ans = 0,t;
queue<int> q;
int main(){
    cin >> m >> n;
    for(int i = 0; i < n; i++) {
    	cin >> t;
    	if(mark[t] == 1) continue;
    	if(q.size() < m) {
    		q.push(t);
    		mark[t] = 1;
    		ans++;
		}else{
			mark[q.front()] = 0;
			q.pop();
			q.push(t);
			mark[t] = 1;
			ans++;
		}
	}
	cout << ans;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int m,n,t,ans;
queue<int> q;
set<int> ds;
int main(){
	cin>>m>>n;
	while (n--) {
		cin>>t;
		if (ds.find(t)==ds.end()) {
			ds.insert(t);
			q.push(t);
			if (q.size()>m) {
				ds.erase(q.front());
				q.pop();
			}
			ans+=1;
		}
	}
	cout<<ans<<endl;
	return 0;
}