30分求调,逻辑感觉没问题,但过不了
查看原帖
30分求调,逻辑感觉没问题,但过不了
1427826
Bocchi_楼主2024/12/5 21:46
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e3 + 10;
int mem[N],n,m,a[N],hh = 0,tt = -1,cnt;
int main()
{
	scanf("%d %d",&m,&n);
	for (int i = 1;i<=n;i++)scanf("%d", &a[i]);
	
	for (int i = 1;i<=n;i++)//遍历每一个数
	{
		bool t = false;//默认是没有存过,即默认需要查字典
		for (int j = hh; j <= tt; j++)//查看是否存过了
		{
			if (a[i] == mem[j])
			{
				t = true;//找到曾经存过的,进行标记
			}
		}

		if (tt - hh + 1<=m-1)//如果内存足够
		{
			t == true ? cnt += 0: cnt++;//依据是否需要查字典进行统计查字典次数
			mem[++tt] = a[i];//队尾插入队列
		}
		else
		{
			hh++;//弹出队首
			t == true ? cnt += 0: cnt++;//依据是否需要查字典进行统计查字典次数
			mem[++tt] = a[i];//插入队列
		}
	}
	printf("%d",cnt);//遍历完全部的数之后输出统计的查字典的次数
	return 0;
}
2024/12/5 21:46
加载中...